Skip to content

Loop contracts: add nested loops' generated locals to an explicit loop_modifies - #5209

Open
feliperodri wants to merge 3 commits into
model-checking:mainfrom
feliperodri:loop-modifies-nested-generated-locals
Open

feliperodri wants to merge 3 commits into
model-checking:mainfrom
feliperodri:loop-modifies-nested-generated-locals

Conversation

@feliperodri

Copy link
Copy Markdown
Member

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 inner for loop.

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.rs and for_loop_modifies.rs fail against CBMC develop with Check 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_modifies binding, and the temporaries those are computed from outside the nested loop. User variables are never added, so an incomplete user clause still fails, which loop_modifies_nested_fail.rs checks.

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-contract passes 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 full expected suite passes 562 of 562, and the kani suite's only failures are two quantifier tests that also fail on main in 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.

…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]>
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]>
@feliperodri

Copy link
Copy Markdown
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 (LoopContractPass), and that pass is enabled only with -Z loop-contracts. No benchmark in tests/perf uses that flag or a loop contract, so the benchmarked compilations never run the changed code.

The numbers fit runner variance:

  • compile-timer-short: every crate is 4–10% slower by a similar amount, including crates with no loops at all. That points to the machine, not the code.
  • compile-timer-long: 70.2 s on main vs 72.0 s here (+2.5%). s2n-quic-core is slightly faster (28.88 s to 28.76 s), and s2n-codec is 0.95 s slower.

The other required checks pass.

This branch has not been deployed

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

Labels

Z-CompilerBenchCI Tag a PR to run benchmark CI Z-EndToEndBenchCI Tag a PR to run benchmark CI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants