Skip to content

Failed to stub_verified contracts with slices in kani::modifies #3682

Closed

Description

@qinheping

I tried this code:

#[kani::modifies(x)]
#[kani::requires(x.len() > 5)]
fn modify_slice(x: &mut [u8]) {
    let mut i = 3;
    while i > 0{
        x[i] = 1;
        i -= 1;
    }
}

#[kani::proof]
#[kani::stub_verified(modify_slice)]
fn test_stubbing() {
    let mut x = [kani::any(), kani::any(), kani::any(), kani::any(), kani::any(), kani::any()];
    modify_slice( &mut x);
}

using the following command line invocation:

kani -Zfunction-contracts test_stubbing.rs

with Kani version: 0.56.0

I expected to see this happen: Verification success.

Instead, this happened:

thread 'rustc' panicked at kani-compiler/src/kani_middle/reachability.rs:446:69:
called `Result::unwrap()` on an `Err` value: Error("Failed to resolve `FnDef(DefId { id: 46, name: \"std::iter::IntoIterator::into_iter\" })` with `GenericArgs([Type(Ty { id: 106, kind: RigidTy(Ref(Region { kind: ReErased }, Ty { id: 115, kind: RigidTy(Slice(Ty { id: 21, kind: RigidTy(Slice(Ty { id: 2, kind: RigidTy(Uint(U8)) })) })) }, Mut)) })])`")

Activity

  1. added
    [C] BugThis is a bug. Something isn't working.
    on Nov 5, 2024
  2. added theissue type on Mar 27, 2026
  3. jrey8343 commented on Sep 8, 2026

    @jrey8343

    Still reproducible at d4df833c (0.67.0-dev, 2026-01-18), and it looks like #4749 ("Fix compiler crash on slice-modifies verified stubs", closing #4748) fixed the same root cause: AnyModifiesPass::replace_any_modifies resolved write_any_slice with the generic args of write_any::<[T]>, i.e. with [T] as the element type, which is where the IntoIterator::into_iter for &mut [[u8]] resolution failure came from.

    Minimal reproducer at d4df833c (kani t.rs -Z function-contracts -Z mem-predicates), same reachability.rs panic as above:

    struct D { buf: Vec<u8> }
    impl D {
        fn cap(&self) -> usize { self.buf.len() }
        fn ptr(&mut self) -> *mut u8 { self.buf.as_mut_ptr() }
        #[kani::requires(n <= self.cap() && dst <= self.cap() - n)]
        #[kani::ensures(|_| *written == old(*written) + n)]
        #[kani::modifies(written, core::ptr::slice_from_raw_parts_mut(self.ptr().add(dst), n))]
        unsafe fn fill(&mut self, dst: usize, n: usize, written: &mut usize) {
            for i in 0..n { unsafe { self.ptr().add(dst + i).write(1) }; }
            *written += n;
        }
        unsafe fn fill_twice(&mut self, n: usize) -> usize {
            let mut w = 0;
            let a = n.min(self.cap());
            unsafe { self.fill(0, a, &mut w) };
            unsafe { self.fill(0, a, &mut w) };
            w
        }
    }
    
    #[kani::proof_for_contract(D::fill)]
    #[kani::unwind(5)]
    fn check_fill() {
        let cap: usize = kani::any();
        kani::assume(cap <= 3);
        let mut d = D { buf: vec![0u8; cap] };
        let mut w = 0;
        unsafe { d.fill(kani::any(), kani::any(), &mut w) };
    }
    
    #[kani::proof]
    #[kani::stub_verified(D::fill)]
    fn check_fill_twice() {
        let cap: usize = kani::any();
        kani::assume(cap <= 1 << 20);
        let mut d = D { buf: vec![0u8; cap] };
        let w = unsafe { d.fill_twice(kani::any()) };
        assert!(w <= 2 * cap);
    }

    This probably can be closed as fixed by #4749 (I have not rebuilt a post-#4749 Kani to confirm on this exact reproducer).

  4. feliperodri commented on Sep 25, 2026

    @feliperodri
    Member

    With Kani 0.68.0, the original reproducer no longer crashes the compiler. After adding the now-required #[kani::proof_for_contract(modify_slice)] harness, kani -Zfunction-contracts -Zstubbing test_stubbing.rs reports VERIFICATION:- SUCCESSFUL for test_stubbing (and for the contract proof). A negative harness asserting an element is unchanged after the verified stub fails as expected. This was fixed by #4749 (in 0.68.0), so closing.

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-ContractsIssue related to code contracts[C] BugThis is a bug. Something isn't working.

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions