You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Loop contracts can't verify code whose memory safety rests on a type Invariant (e.g. slice::Iter): havoc drops it with no user-side way to restore it #4993
Requested feature: a way for a loop contract to re-establish the havoc'd state's type Invariant (core::ub_checks::Invariant), so a loop whose body is memory-safe only by that invariant can be verified.
Use case: proving provided-method loops over std cursors such as core::slice::Iter (and str::pattern::Searcher, which loops over one). Iter::next is memory-safe by its ptr/end invariant, not a local bounds check. Under a loop contract the havoc resets the cursor, so next() dereferences an invalid pointer. The invariant lives in private fields, so no loop_invariant expression can restore it after havoc. There is no user-side workaround.
Test case:
#![feature(stmt_expr_attributes, proc_macro_hygiene)]structPtrCur{ptr:*constu8,end:*constu8}implPtrCur{unsafefnnext(&mutself) -> Option<u8>{ifself.ptr == self.end{returnNone;}let b = *self.ptr;// safe only while the ptr/end invariant holdsself.ptr = self.ptr.add(1);Some(b)}}#[cfg(kani)]mod verify {usesuper::*;#[kani::proof]fncheck_bug(){let a:[u8;4] = kani::any();let p = a.as_ptr();letmut cur = PtrCur{ptr: p,end:unsafe{ p.add(4)}};#[kani::loop_invariant(true)]loop{matchunsafe{ cur.next()}{Some(0) => continue,
_ => break,}}}}
Result: VERIFICATION:- FAILED, all failing checks at let b = *self.ptr (null pointer dereference, pointer invalid, dead object, deallocated, outside object bounds). The loop is contracted, not unwound: loop_invariant_base, loop_assigns, and loop_invariant_step pass, and CBMC reports it at iteration 1. So this is not an unwinding-bound issue. The havoc drops the cursor's invariant, and #[kani::loop_invariant(true)] cannot restore it. The same failure arises contracting the Searcher provided-method loop in verify-rust-std.
A possible direction: loop-contract havoc re-assumes the havoc'd state's type Invariant, as
autoharness already does for nondet inputs (assume_safe). Open design questions:
Opt-in or automatic? A flag, or automatic when the type implements Invariant?
Loop-free predicate.slice::Iter::is_safe itself loops internally (can_dereference / same_allocation), so re-assuming it inside a contracted loop doesn't compose. Loop-free lowering, or
special-case it in the instrumentation?
Library vs. tool split.ptr <= end + same_allocation(ptr, end) doesn't capture "cursor within
the original allocation," so this needs a stronger relational library Invariant plus the tool
mechanism. Where's the split?
I've looked at this enough to be fairly confident a naive re-assume of the existing is_safe() isn't
sufficient on its own (the three points above). If the overall direction looks reasonable, I'd be happy
to be assigned and implement it.
Activity
kasimte commentedon Oct 7, 2026
A possible direction: loop-contract havoc re-assumes the havoc'd state's type
Invariant, asautoharness already does for nondet inputs (
assume_safe). Open design questions:Invariant?slice::Iter::is_safeitself loops internally (can_dereference/same_allocation), so re-assuming it inside a contracted loop doesn't compose. Loop-free lowering, orspecial-case it in the instrumentation?
ptr <= end+same_allocation(ptr, end)doesn't capture "cursor withinthe original allocation," so this needs a stronger relational library
Invariantplus the toolmechanism. Where's the split?
I've looked at this enough to be fairly confident a naive re-assume of the existing
is_safe()isn'tsufficient on its own (the three points above). If the overall direction looks reasonable, I'd be happy
to be assigned and implement it.