Repository navigation
Description
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, toolchainnightly-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=terseElement 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
jrey8343 commentedon Nov 14, 2026
I think the cause of this is #5088: Kani can emit a constant as a read of a
staticwhose 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 asRawVec's constantZERO_CAP.RawVecInner::new_inreadDROPSin place ofZERO_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_inread__CPROVER_kani_contract_clause_depth(goto dump) and the harnesses passed.DROPSwith 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 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. Atc5e65fdthe sixrepro_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.