Skip to content

Quantifier whose body is a statement expression: a short-circuit path that skips the final expression is dropped, so false __CPROVER_exists/__CPROVER_forall assertions verify #9189

Open

Description

@Talha-Dmr

When the body of __CPROVER_exists or __CPROVER_forall is a statement expression whose final expression short-circuits around a nested statement expression, CBMC 6.11.0 drops the short-circuit path. The body a && b is encoded as a ⇒ b, so false assertions verify:

#include <assert.h>

int main(void)
{
  // Both statements are false (no q satisfies the first body, and q = 4
  // violates the second), so both assertions must fail.
  assert(__CPROVER_exists { unsigned long q; ({ q < 4 && ({ int r = q > 10; r; }); }) });
  assert(__CPROVER_forall { unsigned long q; ({ q < 4 && ({ int r = q < 10; r; }); }) });

  // The same bodies without the inner statement expression fail as expected.
  assert(__CPROVER_exists { unsigned long q; q < 4 && q > 10 });
  assert(__CPROVER_forall { unsigned long q; q < 4 && q < 10 });
  return 0;
}
$ cbmc quantifier_stmt_expr.c --z3
[main.assertion.1] line 7 assertion __CPROVER_exists { unsigned long q; ({ q < 4 && ({ int r = q > 10; r; }); }) }: SUCCESS
[main.assertion.2] line 8 assertion __CPROVER_forall { unsigned long q; ({ q < 4 && ({ int r = q < 10; r; }); }) }: SUCCESS
[main.assertion.3] line 11 assertion __CPROVER_exists { unsigned long q; q < 4 && q > 10 }: FAILURE
[main.assertion.4] line 12 assertion __CPROVER_forall { unsigned long q; q < 4 && q < 10 }: FAILURE
VERIFICATION FAILED

The default SAT back end gives the same four results. It also prints warning: ignoring exists, which comes from the third assertion (the exists without a statement expression), not from the first.

--show-goto-functions shows the converted bodies:

ASSERT ∃ main::1::2::1::q : unsignedbv[64] . true ∧ ((true ∧ ¬(¬(main::1::2::1::q < cast(4, unsignedbv[64])))) ⇒ cast((cast(main::1::2::1::q > cast(10, unsignedbv[64]), signedbv[32]) ≠ 0 ? true : false), 𝔹))
ASSERT ∀ main::1::4::1::q : unsignedbv[64] . true ∧ ((true ∧ ¬(¬(main::1::4::1::q < cast(4, unsignedbv[64])))) ⇒ cast((cast(main::1::4::1::q < cast(10, unsignedbv[64]), signedbv[32]) ≠ 0 ? true : false), 𝔹))

There is no conjunct for the path where q < 4 is false.

Cause

convert_statement_expression in src/ansi-c/goto-conversion/goto_clean_expr.cpp converts the body with goto_convert and then walks the paths of the resulting program, adding path_condition ⇒ value for each path that reaches the final EXPRESSION instruction (last). A path that ends without reaching it adds nothing, so it is treated as true.

The body is converted as an expression statement, whose value is unused. For a && b with a side effect in b, that conversion jumps over the final expression when a is false. The same statement expression used as an expression statement in ordinary code:

int main(void)
{
  unsigned long q;
  ({ q < 4 && ({ int r = q > 10; r; }); });
  return 0;
}

becomes

IF ¬(main::1::q < cast(4, unsignedbv[64])) THEN GOTO 1
DECL main::$tmp::tmp_statement_expression : signedbv[32]
DECL main::1::1::1::r : signedbv[32]
ASSIGN main::1::1::1::r := cast(main::1::q > cast(10, unsignedbv[64]), signedbv[32])
ASSIGN main::$tmp::tmp_statement_expression := main::1::1::1::r
DEAD main::1::1::1::r
EXPRESSION cast((main::$tmp::tmp_statement_expression ≠ 0 ? true : false), empty)
1: DEAD main::$tmp::tmp_statement_expression

so the ¬(q < 4) path never reaches the EXPRESSION. When the skipped value would be false, as in a && b, the result is wrong for both exists and forall (shown above). When it would be true, dropping it does not change the result: with || under __CPROVER_forall, ({ q >= 4 || ({ int r = q < 10; r; }); }) verifies and the same body with q < 2 fails, as expected. I have not checked ?:.

convert_statement_expression was added in #8605 and changed in #8616, #8618 and #8649 (and mechanically in #8675).

Impact

Kani builds exists bodies as in_range && predicate. When the predicate calls a function, Kani inlines it as a statement expression, so such a kani::exists! can hold although no value in the range satisfies it: model-checking/kani#5004.

Environment

CBMC 6.11.0 (release binary), x86_64 Linux. goto_clean_expr.cpp is unchanged between cbmc-6.11.0 and develop (32441ee); I did not run develop.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions