Skip to content

-Z valid-value-checks reports invalid values during initialization and raw field-address formation #5056

Closed

Description

@v3risec

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_initialization

Tested 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

  1. added
    [C] Bug
    on Oct 23, 2026
  2. feliperodri commented on Oct 23, 2026

    @feliperodri
    Member

    Another variant, from Weak::inner in alloc::sync: borrowing one field while another field holds an invalid bit pattern is reported too.

    use std::mem::MaybeUninit;
    struct Inner { count: usize, data: bool }
    
    #[kani::proof]
    fn borrow_field_next_to_invalid_bool() {
        let mut slot = MaybeUninit::<Inner>::uninit();
        let p: *mut Inner = slot.as_mut_ptr();
        unsafe {
            (&raw mut (*p).count).write(1);
            (&raw mut (*p).data).cast::<u8>().write(2); // not a valid bool
            let c: &usize = &(*p).count;
            assert!(*c == 1);
        }
    }

    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 Arc harnesses 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, at Weak::inner and Arc::initialize_arcinner.

  3. v3risec commented on Oct 24, 2026

    @v3risec
    ContributorAuthor

    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 existing mutable cast UnsupportedCheck (*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 reports Invalid 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.

  4. added
    [F] Spurious Failure
    Z-UnstableFeature
    on Nov 6, 2026
  5. added theissue type on Nov 6, 2026
  6. added a commit that references this issue on Nov 13, 2026
    7be389c
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-UnstableFeature[C] Bug[F] Spurious Failure

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions