Skip to content

Loop contracts: KaniIter for Range::len underflows on a symbolic range with start > end #4955

Closed

Description

@kasimte

I tried this code:

// kani-flags: -Z loop-contracts
#![feature(proc_macro_hygiene)]
#![feature(stmt_expr_attributes)]

#[kani::proof]
fn for_loop_symbolic_range() {
    let start: usize = kani::any();
    let end: usize = kani::any();
    // Bound the iteration space for tractability. Crucially, `start <= end` is
    // NOT assumed here: the range may be empty (start > end), which is exactly
    // the case that made the model's `len()` underflow before the fix.
    kani::assume(start <= 16);
    kani::assume(end <= 16);

    let mut count: usize = 0;
    #[kani::loop_invariant(count == kani::index)]
    for _i in start..end {
        count += 1;
    }
}

using the following command line invocation:

kani for_loop_symbolic_range.rs -Z loop-contracts

with Kani version: 0.68.0 (CBMC 6.11.0)

I expected to see this happen: verification succeeds. A for i in a..b loop iterates zero times when a > b, so the empty-range case is well defined.

Instead, this happened: verification fails inside Kani's own loop model, not in user code (location lines elided):

Check 10: <std::ops::Range<usize> as kani::KaniIter>::len.assertion.1
	 - Status: FAILURE
	 - Description: "attempt to subtract with overflow"
	 - Location: library/kani_core/src/iter.rs:142:25 in function <std::ops::Range<usize> as kani::KaniIter>::len

Failed Checks: attempt to subtract with overflow
VERIFICATION:- FAILED

The loop-contract model rewrites for i in a..b into while kani_index < iter.len() (library/kani_macros/src/sysroot/loop_contracts/mod.rs). impl KaniIter for Range<$t> (library/kani_core/src/iter.rs) defines len() as (self.end - self.start) as usize, an unchecked subtraction. When the bounds are symbolic (or havoc'd by the loop contract) and start > end, that subtraction underflows. It is the sole failing check. The iterator's other arithmetic (for example nth's start + i) verifies.

Constraining via the iterator's assumption() hook doesn't help. Injecting kani::assume(self.start <= self.end) leaves the len() overflow check firing (measured: the sole FAILURE is unchanged with the assume added). The check in len() is reached independently of the assume, so the fix belongs in len().

This is distinct from the other open loop-contract issues: here the model resolves and the contract attaches, and the failure is in the range's own len() arithmetic under unconstrained bounds.

The fix makes len() total, matching Rust Range semantics (an empty range with start >= end has length zero), using abs_diff for the span so it cannot overflow the element type: if self.end > self.start { self.end.abs_diff(self.start) as usize } else { 0 }. PR attached. Every contracted for i in a..b loop whose range bounds are computed at runtime (so the verifier cannot assume a <= b) is affected.

Activity

  1. added
    [C] Bug
    [F] Spurious Failure
    on Nov 6, 2026
  2. added this to the Contracts milestone on Nov 6, 2026
  3. added
    Z-Contracts
    on Nov 6, 2026
  4. added theissue type on Nov 6, 2026
  5. added a commit that references this issue on Nov 7, 2026
    cbe0f83
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Z-Contracts[C] Bug[F] Spurious Failure

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions