Skip to content

proof_for_contract harness fails in its own setup code when the element type's destructor writes a static (Kani 1640445, CBMC 6.11) #4992

Open

Description

@jrey8343

Summary

A #[kani::proof_for_contract] harness over a generic type instantiated with an element type whose destructor writes a static reports spurious failures in the harness's own setup code — pointer-offset and in-bounds checks on a freshly allocated buffer — although the destructor never runs in the harness. Changing the destructor to only read its field makes the same harness verify; so does turning the harness into a plain #[kani::proof] that calls the same function.

Found while rebasing model-checking/verify-rust-std#681 (VecDeque proofs) onto Kani 1640445. It did not happen at the previous pin (152c6a8, CBMC 6.10.0).

Environment

  • Kani at 1640445da3fa0d2cb19739652057dc992fef3f18 (0.68.0 + 72 commits), CBMC 6.11.0, toolchain nightly-2026-09-25, macOS arm64 (also reproduced on the project's Ubuntu CI configuration).
  • Flags: kani verify-std -Z unstable-options ./library -Z function-contracts -Z mem-predicates -Z float-lib -Z c-ffi -Z loop-contracts -Z quantifiers -Z stubbing --no-assert-contracts --cbmc-args --object-bits 12 (the verify-rust-std CI flags).

Reproducer

Branch kani-static-write-contract-repro of https://github.com/jrey8343/verify-rust-std (one commit on top of the #681 branch), file library/alloc/src/collections/vec_deque/mod.rs, module verify, section "Reproducer for the Kani issue":

scripts/run-kani.sh --kani-args --harness collections::vec_deque::verify::repro_ -j 4 --output-format=terse

Element types:

static mut DROPS: usize = 0;

#[derive(kani::Arbitrary)]
struct ReproStaticWrite(u8);
impl Drop for ReproStaticWrite {
    fn drop(&mut self) {
        unsafe { DROPS += 1 };            // <- the trigger
        core::hint::black_box(self.0);
    }
}

#[derive(kani::Arbitrary)]
struct ReproReadOnly(u8);                  // control
impl Drop for ReproReadOnly {
    fn drop(&mut self) { core::hint::black_box(self.0); }
}

Harness (the generator any_deque allocates a VecDeque<T> of symbolic capacity cap with cap * size_of::<T>() <= 2^48, picks a symbolic head/len and fills the initialized slots with ptr::write_bytes; buffer_read carries #[requires(off.as_index() < self.capacity() && core::ub_checks::can_dereference(unsafe { self.ptr().add(off.as_index()) }))]):

#[kani::proof_for_contract(VecDeque::<ReproStaticWrite>::buffer_read)]
fn repro_contract_static_write() {
    let mut deque = any_deque::<ReproStaticWrite>();
    let off: usize = kani::any();
    let value = unsafe { deque.buffer_read(wi(off)) };
    core::mem::forget(value);
    finish(deque);                          // mem::forget for needs_drop types
}

Observed

harness element destructor allocation result
repro_contract_static_write writes DROPS (a static also used elsewhere in the module) symbolic, ≤ 2^48 bytes FAILED
repro_contract_static_write_small_cap same cap <= 4 FAILED
repro_contract_fresh_static_write writes REPRO_FRESH (a static written only there, read nowhere) symbolic FAILED
repro_contract_fresh_static_write_small_cap same cap <= 4 FAILED
repro_contract_read_only reads only symbolic verifies
repro_plain_static_write (#[kani::proof], same call) writes DROPS symbolic verifies

Failing checks of repro_contract_static_write (5 of ~500), all located in the harness's generator or the contract's requires, none in buffer_read's body:

core::kani::rustc_intrinsics::offset::<ReproStaticWrite, *mut ReproStaticWrite, usize>.safety_check.2
  "Offset result address must equal original pointer address plus offset"
<usize as core::kani::rustc_intrinsics::ToISize>::to_isize.safety_check.1
  "Offset value overflows isize"
core::kani::mem::is_inbounds::<ReproStaticWrite>.unsupported_construct.1
  "Kani does not support reasoning about pointer to unallocated memory"
core::kani::mem::cbmc::same_allocation.unsupported_construct.1
  "Kani does not support reasoning about pointer to unallocated memory"
core::ptr::write_bytes::<ReproStaticWrite>.precondition_instance.1
  "memset destination region writeable"

With an AtomicUsize static and fetch_add in the destructor (the original code in #681) the harness fails the same way, and an atomic load of the static in the destructor is already enough. During the investigation one variant with the capacity bounded to 4 and a non-atomic increment did verify, so the dependence on the allocation size is not clear-cut; the branch as pushed fails at both sizes. Attempts at a standalone reproducer outside the standard library (a Buf<T> with std::alloc::alloc or Vec::with_capacity, same contract, same destructor, capacity up to 2^48) verify fine, so the std allocation path (RawVec/Global) or the verify-std crate context seems to be part of it.

Expected

The destructor is dead code in this harness; its body should not affect the verification of the generator's pointer arithmetic. The same harness (with the atomic counter) verified at Kani 152c6a8.

Workaround used

Keep the destructor free of static writes for the element type used with contract harnesses (a separate counting type is used only in small bounded harnesses).

Activity

  1. added
    [F] Spurious Failure
    Z-Contracts
    on Nov 6, 2026
  2. added this to the Contracts milestone on Nov 6, 2026
  3. added theissue type on Nov 6, 2026
  4. added
    [C] Bug
    on Nov 6, 2026
  5. jrey8343 commented on Nov 14, 2026

    @jrey8343
    Author

    I think the cause of this is #5088: Kani can emit a constant as a read of a static whose initializer has the same bytes. A fix is open as #5089.

    What I saw at Kani c5e65fd (CBMC 6.11.0, nightly-2026-10-23) in the harness module of model-checking/verify-rust-std#681:

    • static mut DROPS: usize = 0; has the same eight zero bytes as RawVec's constant ZERO_CAP.
    • Three checkouts of the same source. In one, RawVecInner::new_in read DROPS in place of ZERO_CAP (goto dump) and the harnesses failed. In a second, the harnesses I ran failed on the same checks; I made no dump there. In the third, new_in read __CPROVER_kani_contract_clause_depth (goto dump) and the harnesses passed.
    • Contract instrumentation starts DROPS with an arbitrary value. VecDeque::with_capacity(0) in the harness's setup then returned a deque with an arbitrary capacity over a dangling pointer.
    • The failed checks were the kind listed above: in the setup code and in the deallocation, with no destructor run.

    The dump lines, the three checkouts and two single-file reproducers are in #5088 (comment).

    This fits the table in the report. The static is part of the harness's program only when the destructor refers to it, so the read-only control verifies. In a plain #[kani::proof] the static keeps its initial value 0, so the constant still reads 0 and that control verifies too. It also explains why I found no standalone reproducer at the time: whether the constant lands on the static depends on the order in which Kani emits the items, and that order changes with the crate name and, in the verify-std build, with the checkout.

    Not checked: I have not gone back to 1640445, the commit this issue names, and I made no goto dump of the reproducer branch. At c5e65fd the six repro_ harnesses of that branch verified in the one checkout I ran them in. Because the result depends on the checkout, that is not evidence of a fix.

    Workaround now used in model-checking/verify-rust-std#681 (at 6b291878317): the harness statics have an initializer whose bytes no constant is likely to share.

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