Repository navigation
Description
Bug description
Kani's experimental -Z valid-value-checks generates spurious invalid-value checks when initializing storage or forming raw pointers to fields of partially initialized structures.
The checker instruments a raw-pointer ProjectionElem::Deref with validity requirements for the entire pointee, without sufficiently accounting for the full Place projection or how the Place is used.
Reproduce
I tried this code:
use std::mem::{self, MaybeUninit};
#[repr(C)]
struct Pair {
initialized: bool,
pending: bool,
}
#[kani::proof]
fn raw_mut_field_address() {
let mut storage = MaybeUninit::<Pair>::uninit();
let ptr = storage.as_mut_ptr();
let field = unsafe { &raw mut (*ptr).initialized };
unsafe {
field.write(true);
assert!(*field);
}
}
#[kani::proof]
fn box_initialization() {
let value: bool = kani::any();
let boxed = Box::new(Pair {
initialized: value,
pending: false,
});
assert_eq!(boxed.initialized, value);
mem::forget(boxed);
}Using:
kani -Z valid-value-checks repro.rs --harness raw_mut_field_address
kani -Z valid-value-checks repro.rs --harness box_initializationTested unmodified version: Kani 0.68.0, commit 9461e07af7ec929f050aa4192f29363c9be07779, Rust nightly-2026-10-12, CBMC 6.11.0.
Expected behavior
Both harnesses should verify successfully. Forming a raw field pointer does not read the field's value, and initializing storage must not require its previous contents to be valid.
Actual behavior
Both harnesses fail with spurious Undefined Behavior: Invalid value checks.
The same issue was encountered while verifying Rc<bool> in verify-rust-std, including a failure at library/alloc/src/rcs/rc.rs:2514 during initialization of the strong reference-count field.
Root cause
CheckValueVisitor::visit_place checks the validity of the entire raw-pointer pointee as soon as it encounters ProjectionElem::Deref. This can cause checks of uninitialized bytes before a store or raw field-address operation.
The instrumentation should distinguish stores and raw address formation from value reads and reference creation, and use the final projected type where appropriate.
Related to #2998. The separate mutable-cast and copy_nonoverlapping unsupported checks are outside the scope of this report.
Activity
feliperodri commentedon Oct 23, 2026
Another variant, from
Weak::innerinalloc::sync: borrowing one field while another field holds an invalid bit pattern is reported too.Kani 6ce55b9 with
-Z valid-value-checks:Undefined Behavior: Invalid value of type Inner. Miri (nightly-2026-10-11) runs this and the uninitialized-write case from the description without reporting UB.On 277 of the
Archarnesses in model-checking/verify-rust-std#587, the flag makes 148 fail. 141 of the failing checks are unsupported constructs (copy_nonoverlapping, raw-pointer dereference, mutable cast); the other 7 are this issue, atWeak::innerandArc::initialize_arcinner.v3risec commentedon Oct 24, 2026
Thanks @feliperodri for the additional reproducer!
I tested this case with #5057. The patched checker no longer reports
Invalid value of Inner, but the original reproducer still fails due to the existingmutable castUnsupportedCheck (*mut bool→*mut u8), which is outside the scope of this PR.I also tested an isolated
repr(C)variant that avoids the cast. The unmodified checker reportsInvalid value of Inner, while the patched version verifies successfully with the cover property satisfied.So #5057 addresses this field-borrow false positive, but does not resolve the unrelated UnsupportedChecks or guarantee that all Arc/Weak harnesses will pass.
Fix value-validity checks for initialization stores and field project…