Repository navigation
Loop contracts: add nested loops' generated locals to an explicit loop_modifies - #5209
Open
feliperodri wants to merge 3 commits into
Open
feliperodri wants to merge 3 commits into
feliperodri wants to merge 3 commits into
Conversation
…p_modifies With an explicit `#[kani::loop_modifies]`, Kani adds to the clause the locals that the loop contract transformation generates for the loop, since the user cannot name them (model-checking#4947). The locals that it generates for the loops nested in that loop were not added, although the outer loop writes them when it enters an inner loop: the loop invariant closure of the inner loop, the references it is made of, the temporaries of the inner loop's own modifies clause, and the index and length of the iterator of an inner `for` loop. CBMC only accepted these writes because it treated such variables as loop locals. Since diffblue/cbmc#9185, a variable declared before a loop (Kani declares all MIR locals at function entry) whose address is taken is never a loop local, so the outer loop fails with `Check that var_N is assignable`. Add these locals for each nested loop: those whose name contains the nested loop's id, the arguments of its register function call and its loop modifies binding, and, transitively, the temporaries these are computed from outside the nested loop. Variables of the user are never added, so an incomplete user clause still fails. With diffblue/cbmc#9524, `tests/expected/loop-contract` passes 92/92 (it also does with CBMC 6.11.0). Signed-off-by: Felipe Monteiro <[email protected]>
6 of 7 tasks
rajath-mk
reviewed
Nov 24, 2026
A user variable whose name starts with kani_ matched the prefix check, so when an inner loop's clause mentioned it, Kani added it to the outer loop's write set, and an outer clause that left it out verified. Match the names that #[kani::loop_invariant] generates without the loop id exactly instead: kani_loop_modifies, kani_loop_decreases and __kani_empty_loop_clause. Every other generated name contains the loop id. Add the kani_-named case to loop_modifies_nested_fail.rs, and nested-loop cases that must verify with an outer clause listing only user variables: an inner for loop over a range and over an array, an inner invariant without a clause, and three levels of nesting. Signed-off-by: Felipe R. Monteiro <[email protected]>
Member
Author
|
The two compile-timer failures are noise, not a regression from this PR. The only code this PR changes is the loop-contract transformation ( The numbers fit runner variance:
The other required checks pass. |
This branch has not been deployed
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.
With an explicit
#[kani::loop_modifies], Kani adds to the clause the locals it generates for that loop, since the user can't name them (#4947). It didn't do the same for the locals it generates for loops nested inside, even though the outer loop writes them each time it enters the inner loop. Those are the inner loop's invariant closure and the references it holds, the temporaries for the inner loop's own modifies clause, and the index and iterator length of an innerforloop.CBMC 6.11.0 accepts these writes because it treats such variables as loop locals. Since diffblue/cbmc#9185, and still with the fix in diffblue/cbmc#9524, a variable that is declared before a loop and whose address is taken is never a loop local. Kani declares every MIR local at function entry, so
loop_modifies_nested.rs,loop_modifies_own_loop.rsandfor_loop_modifies.rsfail against CBMC develop withCheck that var_N is assignable.For each nested loop, Kani now also adds the locals whose name contains that loop's id, the arguments of the loop's register function call, its
loop_modifiesbinding, and the temporaries those are computed from outside the nested loop. User variables are never added, so an incomplete user clause still fails, whichloop_modifies_nested_fail.rschecks.This is one of the two Kani-side pieces of getting the nightly CBMC Latest job green again; the other is #5198. The job has failed since 2026-11-06 because of diffblue/cbmc#9185.
Testing
tests/expected/loop-contractpasses 92 of 92 with CBMC 6.11.0 and with CBMC develop plus diffblue/cbmc#9524 (it has 20 failures with CBMC develop alone). With CBMC develop plus diffblue/cbmc#9524, the fullexpectedsuite passes 562 of 562, and thekanisuite's only failures are two quantifier tests that also fail onmainin the same environment.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.