Skip to content

Loop contracts: explicit loop_modifies rejects writes to temporaries declared in the loop body (Check that var_N is assignable) #4906

Closed

Description

@Talha-Dmr

I tried this code:

#![feature(stmt_expr_attributes)]
#![feature(proc_macro_hygiene)]

#[kani::proof]
fn get_unchecked_in_body() {
    let a: [u8; 4] = kani::any();
    let mut i: usize = 0;
    let mut sum: u32 = 0;
    #[kani::loop_invariant(i <= 4)]
    #[kani::loop_modifies(&i, &sum)]
    while i < 4 {
        let x = unsafe { a.get_unchecked(i) };
        sum = sum.wrapping_add(*x as u32);
        i += 1;
    }
    assert!(i == 4);
}

// Same loop with plain indexing: SUCCESSFUL.
#[kani::proof]
fn index_in_body() {
    let a: [u8; 4] = kani::any();
    let mut i: usize = 0;
    let mut sum: u32 = 0;
    #[kani::loop_invariant(i <= 4)]
    #[kani::loop_modifies(&i, &sum)]
    while i < 4 {
        let x: u8 = a[i];
        sum = sum.wrapping_add(x as u32);
        i += 1;
    }
    assert!(i == 4);
}

using the following command line invocation:

kani loop_body_temps.rs -Z loop-contracts --harness get_unchecked_in_body --exact

with Kani version: 0.67.0, built from source at 152c6a8; CBMC 6.10.0; Arch Linux x86_64.

I expected to see this happen: verification succeeds. The loop writes only i and sum outside its own body. x and the temporaries for the get_unchecked call are local to the loop body.

Instead, this happened:

SUMMARY:
 ** 4 of 73 failed (1 unreachable)
Failed Checks: Check that var_15 is assignable
 File: "loop_body_temps.rs", line 13, in get_unchecked_in_body
Failed Checks: Check that var_14 is assignable
 File: "loop_body_temps.rs", line 13, in get_unchecked_in_body
Failed Checks: Check that var_16 is assignable
 File: "loop_body_temps.rs", line 13, in get_unchecked_in_body
Failed Checks: Check that x is assignable
 File: "loop_body_temps.rs", line 13, in get_unchecked_in_body

VERIFICATION:- FAILED

index_in_body, which differs only in how the element is read, verifies (0 of 59 failed).

The same happens when the body advances an iterator temporary. Then the write inside the callee is flagged too:

    #[kani::loop_invariant(i <= 4)]
    #[kani::loop_modifies(&i, &sum)]
    while i < 4 {
        let x = unsafe { a.get_unchecked(i..) }.iter().next().unwrap();
        sum = sum.wrapping_add(*x as u32);
        i += 1;
    }
 ** 10 of 140 failed (1 unreachable)
Failed Checks: Check that var_14 is assignable   (and var_15 ... var_21)
Failed Checks: Check that x is assignable
Failed Checks: Check that self->ptr is assignable
 File: ".../library/core/src/slice/iter/macros.rs", line 185, in <std::slice::Iter<'_, u8> as std::iter::Iterator>::next

(This variant did not finish at the default --object-bits 16; we stopped it after 150 s under a 3 GB limit. The output above is with -Z unstable-options --cbmc-args --object-bits 8, 1.5 s.)

We hit this on String::retain: get_unchecked(idx..len).chars().next() in the loop body gives <slice::Iter<u8> as Iterator>::next.assigns: Check that self->ptr is assignable. The flagged objects are compiler temporaries, so they cannot be listed in loop_modifies. As far as we can tell, the only way to get a passing loop contract is to rewrite the loop body. That is not an option when the loop is the code under verification.

Possible cause: the comment on StorageLive/StorageDead in codegen/statement.rs says MIR declares all locals at function scope and Kani emits one declaration per local. So DFCC does not see body temporaries as loop-local and requires them to be in the loop's write set. The docs' loop-contract limitation 2 describes a related case for inferred modifies ("fields of structs are modified by some other functions called in the loops"). The case here uses an explicit loop_modifies and the targets cannot be named. #4790 reports the same assigns failure for locals of CBMC's memcmp model.

Possible fixes: treat locals whose StorageLive/StorageDead lie inside the loop as loop-local for the assigns check (or declare them at StorageLive), or add every body-scoped local to the loop write set automatically.

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

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions