Repository navigation
DFCC: only variables declared inside a loop are loop locals - #9185
Merged
tautschnig merged 1 commit intoNov 6, 2026
Merged
Conversation
tautschnig
requested review from
feliperodri,
kroening,
peterschrammel and
remi-delmas-3000
as code owners
October 14, 2026 07:06
There was a problem hiding this comment.
🟢 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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
feliperodri
approved these changes
Oct 14, 2026
tautschnig
force-pushed
the
kiro/9175-dfcc-loop-locals
branch
from
November 5, 2026 21:48
c6b933e to
5023bfc
Compare
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
force-pushed
the
kiro/9175-dfcc-loop-locals
branch
from
November 5, 2026 21:48
5023bfc to
80ccd6e
Compare
This was referenced Nov 18, 2026
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
gen_loop_locals_settreated 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 omitstnow 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