Skip to content

DFCC: only variables declared inside a loop are loop locals - #9185

Merged
tautschnig merged 1 commit into
diffblue:developfrom
tautschnig:kiro/9175-dfcc-loop-locals
Nov 6, 2026
Merged

tautschnig merged 1 commit into
diffblue:developfrom
tautschnig:kiro/9175-dfcc-loop-locals

Conversation

@tautschnig

Copy link
Copy Markdown
Collaborator

gen_loop_locals_set treated a variable declared before a loop as a loop local when nothing outside the loop reads or writes it. A declaration without an initializer is not a write, so such a variable was neither havoced nor checked against the loop's assigns clause, and its base-case value leaked into the step case. With this change only variables declared inside the loop are loop locals.

Both the example from the issue (inferred loop assigns) and a variant with an explicit __CPROVER_assigns(i) that omits t now fail as expected; both are added as regression tests. The contracts, contracts-dfcc and contracts-non-dfcc suites pass, and seven mlkem-native proofs whose loops have explicit assigns clauses still verify.

Fixes #9175

  • Each commit message has a non-empty body, explaining why the change was made.
  • Methods or procedures I have added are documented, following the guidelines provided in CODING_STANDARD.md.
  • n/a The feature or user visible behaviour I have added or modified has been documented in the User Guide in doc/cprover-manual/
  • Regression or unit tests are included, or existing tests cover the modified code (in this case I have detailed which ones those are in the commit message).
  • n/a My commit message includes data points confirming performance improvements (if claimed).
  • My PR is restricted to a single feature or bugfix.
  • White-space or formatting changes outside the feature-related changed lines are in commits of their own.

Copilot AI balanced review requested due to automatic review settings October 14, 2026 07:06

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The implementation directly addresses the unsound classification and includes focused regression coverage for both affected paths.

0 open findings

What changed in this PR

Fixes unsound DFCC loop-local classification by requiring variables to be declared inside the loop.

Changes:

  • Restricts loop locals to declarations within the loop body.
  • Adds regression coverage for inferred and explicit assigns clauses.
File Description
src/​goto-instrument/​contracts/​dynamic-frames/​dfcc_infer_loop_assigns.cpp Corrects loop-local detection.
regression/​contracts-dfcc/​loop_locals_decl_outside_no_init/​main.c Tests inferred assigns behavior.
regression/​contracts-dfcc/​loop_locals_decl_outside_no_init/​test.desc Defines expected assertion failure.
regression/​contracts-dfcc/​loop_locals_decl_outside_explicit_assigns/​main.c Tests explicit assigns enforcement.
regression/​contracts-dfcc/​loop_locals_decl_outside_explicit_assigns/​test.desc Defines expected assigns failure.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@codecov

codecov Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.92%. Comparing base (d35cbee) to head (80ccd6e).
⚠️ Report is 4 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #9185      +/-   ##
===========================================
- Coverage    80.94%   80.92%   -0.02%     
===========================================
  Files         1720     1720              
  Lines       190658   190631      -27     
  Branches        73       73              
===========================================
- Hits        154323   154270      -53     
- Misses       36335    36361      +26     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

gen_loop_locals_set previously classified a variable as a loop local if
it was declared outside the loop but had no reads or writes outside the
loop.  This is unsound: such a variable's storage persists across
iterations (unlike a variable declared inside the loop body, which gets a
fresh DECL each iteration), so it must be havoced at the loop head to
model arbitrary inter-iteration values in the step case.

The fix restricts the loop-local classification to variables whose DECL
instruction is inside the loop body.  This is a conservative but sound
criterion.  The removed #include <analyses/goto_rw.h> is no longer
needed because the read/write range analysis was only used in the removed
heuristic, and so are the function identifier, message handler, and
namespace parameters of gen_loop_locals_set.

Fixes diffblue#9175

Co-authored-by: Kiro <[email protected]>
@tautschnig
tautschnig force-pushed the kiro/9175-dfcc-loop-locals branch from 5023bfc to 80ccd6e Compare November 5, 2026 21:48
@tautschnig
tautschnig merged commit 328d8e5 into diffblue:develop Nov 6, 2026
38 of 43 checks passed
@tautschnig
tautschnig deleted the kiro/9175-dfcc-loop-locals branch November 6, 2026 05:59
feliperodri added a commit to feliperodri/cbmc that referenced this pull request Nov 19, 2026
The reads through pointers that decide whether an address-taken variable
is a loop local missed two cases, letting a variable that holds the
previous iteration's value at the loop head be treated as a loop local
(and hence not havoced):

1. Pointer chains: after `p = &t; pp = &p;`, a read of `**pp`, or passing
   `pp` to a function, reaches `t`. The reachability walk now follows the
   objects that a reached pointer variable may point to, including
   pointer variables stored in local aggregates.
2. Pointers of unknown origin: the alias analysis is field-insensitive, so
   after `s.q = &t; p = s.q;` it reports an unknown origin for `p`. Such a
   pointer previously only reached the escaped variables. It now reaches
   all address-taken candidates, both when it is dereferenced and when it
   escapes (e.g. is passed to a function).

The new regression tests cover both cases; CBMC before diffblue#9185 reports
VERIFICATION SUCCESSFUL on all four. The contracts suites and Kani's loop
contract regression tests (92 of 92) still pass.
feliperodri added a commit to feliperodri/cbmc that referenced this pull request Nov 19, 2026
The reads through pointers that decide whether an address-taken variable
is a loop local missed two cases, letting a variable that holds the
previous iteration's value at the loop head be treated as a loop local
(and hence not havoced):

1. Pointer chains: after `p = &t; pp = &p;`, a read of `**pp`, or passing
   `pp` to a function, reaches `t`. The reachability walk now follows the
   objects that a reached pointer variable may point to, including
   pointer variables stored in local aggregates.
2. Pointers of unknown origin: the alias analysis is field-insensitive, so
   after `s.q = &t; p = s.q;` it reports an unknown origin for `p`. Such a
   pointer previously only reached the escaped variables. It now reaches
   all address-taken candidates, both when it is dereferenced and when it
   escapes (e.g. is passed to a function).

The new regression tests cover both cases; CBMC before diffblue#9185 reports
VERIFICATION SUCCESSFUL on all four. The contracts suites and Kani's loop
contract regression tests (92 of 92) still pass.
feliperodri added a commit to feliperodri/cbmc that referenced this pull request Nov 19, 2026
Two more cases in which an address-taken variable holding the previous
iteration's value at the loop head could be treated as a loop local (and
hence not havoced):

1. Pointers that the alias analysis does not track, e.g. a pointer stored
   in a field of a global struct: `g.q = &t; ... *g.q`. Only pointer
   symbols were queried, so `g.q` never fell back to an unknown origin.
   All pointer-valued fields, elements and dereferences are now queried,
   and an unknown origin may reach any address-taken candidate.
2. Stores into holders whose own address escapes: after `save(&p)`, a
   callee can reach `t` through the saved pointer once `p = &t`. Escapes
   are now computed for all variables, to a fixpoint, and a store into a
   local pointer or aggregate whose address escapes escapes the stored
   addresses.

New regression tests cover both cases and their struct variants; CBMC
before diffblue#9185 reports VERIFICATION SUCCESSFUL on all four. The contracts
suites and Kani's loop contract regression tests (92 of 92) still pass.
feliperodri added a commit to feliperodri/cbmc that referenced this pull request Nov 19, 2026
Since diffblue#9185, only variables declared inside a loop are loop locals. This
is sound, but front-ends that declare all locals at the start of a
function, such as Kani for Rust MIR, lose all loop locals: every
temporary that a loop writes must then be havoced and be in the loop's
assigns clause, which the user cannot name, so loops with an explicit
assigns clause fail with `Check that var_N is assignable`.

A variable declared outside a loop is now also a loop local if:
- its address is not taken (it is not dirty), so no pointer can reach it,
- no instruction outside the loop uses it, other than its DECL and DEAD
  and the assignments to CPROVER instrumentation variables (the
  `__CPROVER_dead_object` updates that model the lifetime of variables),
- it does not occur in the contract clauses of any loop of the function,
- and it is dead at the loop head: every path from the head overwrites
  the whole variable before reading it (writes to a part of it do not
  count). Liveness is computed by a small backward analysis.
Then the loop never reads a value it had before an iteration, so it need
not be havoced, and nothing after the loop reads the values the loop
assigns to it.

Requiring that the address of the variable is not taken keeps the
criterion syntactic: any reasoning about pointers that may reach the
variable (pointer chains, pointers loaded from memory or returned by
calls, integer-to-pointer casts, memory builtins, addresses that escape)
is unnecessary. Such a loop local is not tracked in the loop's write set,
since it has no DECL in the loop.

Assigns clause inference uses the loop locals of the original function,
computed before inlining, so that the inferred clauses and the
instrumentation classify variables the same way.

The removed heuristic of diffblue#8489 (no reads or writes outside the loop)
accepted unsound cases, which the new regression tests cover: CBMC before
diffblue#9185 reports VERIFICATION SUCCESSFUL on 15 of them, which have failing
assertions.
feliperodri added a commit to tautschnig/kani that referenced this pull request Nov 20, 2026
…l-checking#5198)

The nightly **CBMC Latest** workflow has failed every night since
2026-10-22. This PR fixes the first of its two failures.

`json-handler/schema-validation` checks that `tools.cbmc` in the
`--export-json` output matches `\d+\.\d+\.\d+ \(cbmc-` (added in model-checking#4941).
That only matches release builds (`6.11.0 (cbmc-6.11.0)`). The nightly
job builds CBMC from a shallow `actions/checkout` with no tags, so `cbmc
--version` reports the bare git revision (`6.11.0 (8fcda333)`), and the
check fails. Kani records the `--version` output verbatim; only the test
assumed the format. The check now also accepts a hex revision
(optionally `-dirty`).

The second failure is unrelated to this PR: since 2026-11-06, ~20
`expected/loop-contract` tests fail because of diffblue/cbmc#9185 ("only
variables declared inside a loop are loop locals"). Kani declares every
MIR local at function entry, so loop-body temporaries in loops with an
explicit `loop_modifies` clause are now checked against the clause
(`Check that var_N is assignable`). That needs a Kani change and will
come in a separate PR. Because `kani-regression.sh` stops at the first
failing suite, the loop-contract failure has hidden this one since
2026-11-06.

### Testing
Ran `tests/json-handler/schema-validation/test.sh`:
- with a `cbmc` wrapper that reports `6.11.0 (8fcda333)` (the CI
format): fails before this change, passes after;
- with release CBMC 6.11.0: passes.

By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 and MIT licenses.

---------

Signed-off-by: Felipe Monteiro <[email protected]>
Signed-off-by: Felipe R. Monteiro <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants