Skip to content

DFCC loop contracts: a variable declared before a loop without an initializer is not havoced, so a false assertion verifies #9175

Closed

Description

@Talha-Dmr

With --dfcc --apply-loop-contracts, a variable that is declared before a loop without an initializer, and read and written only inside the loop, keeps the value it got in the base-case iteration when the step case runs. A false assertion then verifies.

#include <assert.h>

void main()
{
  unsigned n;
  __CPROVER_assume(n > 3 && n < 100);
  unsigned i = 0;
  unsigned t;
  while(i < n)
    __CPROVER_loop_invariant(i <= n)
  {
    if(i > 0)
      assert(t == 0); // false at i == 2 in a real run (t == 1)
    t = i;
    i++;
  }
}
goto-cc looplocal.c -o a.goto
goto-instrument --dfcc main --apply-loop-contracts a.goto b.goto
cbmc b.goto

Expected: VERIFICATION FAILED, as with cbmc looplocal.c --unwind 5.

Actual: VERIFICATION SUCCESSFUL.

With an initializer (unsigned t = 0;), the contract check fails as expected.

As far as I can tell, the cause is gen_loop_locals_set in src/goto-instrument/contracts/dynamic-frames/dfcc_infer_loop_assigns.cpp. It treats a variable as a loop local if "there is no write or read of it outside the loop". t is only declared outside the loop, so it is classified as a loop local and is not havoced. Unlike a variable declared inside the loop, it is not declared again in each iteration, so its value flows from one iteration into the next.

Found with CBMC 6.11.0. The same rule is on develop (4b26409).

Activity

  1. added 2 commits that reference this issue on Nov 5, 2026
    5023bfc
    80ccd6e
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