Skip to content

-Z uninit-checks rejects valid atomic stores to uninitialized memory #5114

Closed

Description

@v3risec

Bug description

Kani incorrectly rejects a volatile atomic store to allocated, properly aligned, initially uninitialized memory when -Z uninit-checks is enabled.

The store should initialize the destination without reading its previous contents. However, Kani checks the destination for prior initialization and does not record the write as initializing the memory. A subsequent read also fails its initialization check.

This reproduces in a single-threaded harness using ordinary allocated memory, without MMIO or concurrent accesses.

Reproduce

I tried this code:

#![feature(core_intrinsics)]

use std::alloc::{alloc, dealloc, Layout};
use std::intrinsics::{atomic_store, AtomicOrdering};
use std::sync::atomic::AtomicU64;

#[kani::proof]
fn store_then_load() {
    let layout = Layout::new::<AtomicU64>();
    let ptr = unsafe { alloc(layout) }.cast::<u64>();
    kani::assume(!ptr.is_null());

    let value: u64 = kani::any();

    unsafe {
        atomic_store::<u64, { AtomicOrdering::Relaxed }, true>(ptr, value);
        assert_eq!(ptr.read(), value);
        dealloc(ptr.cast(), layout);
    }
}

Using the following command from a built source checkout:

./scripts/kani repro.rs -Z unstable-options -Z uninit-checks \
    --harness store_then_load --exact --jobs 1

Tested unmodified version: Kani 0.68.0, commit c5e65fdfc3e15ec32293f2a4c07509aebec033c9, nightly-2026-10-23, rustc 1.101.0-nightly, CBMC 6.11.0, x86_64 Linux.

Expected behavior

The harness should verify successfully.

For VOLATILE=true, the atomic store intrinsic follows the raw-pointer store_volatile safety contract. The destination is allocated, writable, and correctly aligned. The operation does not require the previous contents to be initialized.

After the store, the raw-pointer read should return the written value.

Actual behavior

The unmodified Kani compiler reports three failed checks in the minimal reproducer. These include initialization failures associated with the store and the subsequent read.

The revised compiler verifies the same reproducer without failed checks.

Root cause

There are two issues in the current implementation.

1. Atomic store code generation unnecessarily reads the previous value.

AtomicStore and AtomicXchg share codegen_atomic_store. The original implementation unconditionally initializes a temporary from *dst before writing the new value.

This old-value read is needed by exchange, which returns the previous value, but not by store, which returns ().

Source: codegen/intrinsic.rs

2. Initialization instrumentation treats volatile atomic stores as reads and does not track the write.

The uninitialized-memory visitor groups AtomicStore with atomic loads and read-modify-write operations, inserting MemoryInitOp::Check before the store without distinguishing its VOLATILE argument.

It also fails to emit MemoryInitOp::Set after the store. Consequently, the subsequent read still observes the destination as uninitialized in Kani's initialization-tracking state.

Source: uninit_visitor.rs

Scope

This report uses VOLATILE=true because its raw-pointer safety contract permits writing to valid, properly aligned, uninitialized allocated memory.

An earlier version of this report used VOLATILE=false. That reproducer is no longer treated as conclusively valid because the intrinsic specifies a from_ptr-based safety contract, which raises an unresolved reference-validity question for initially uninitialized storage.

The present report does not resolve that non-volatile contract question. It also does not claim complete volatile/MMIO or concurrent-program modeling.

Activity

  1. added
    [C] BugThis is a bug. Something isn't working.
    on Nov 3, 2026
  2. changed the title [-]-Z uninit-checks rejects valid atomic stores to uninitialized memory[/-] [+]`-Z uninit-checks` rejects valid atomic stores to uninitialized memory[/+] on Nov 4, 2026
  3. added
    [F] Spurious FailureIssues that cause Kani verification to fail despite the code being correct.
    Z-UnstableFeatureIssues that only occur if a unstable feature is enabled
    on Nov 6, 2026
  4. self-assigned this
    on Nov 6, 2026
  5. added theissue type on Nov 6, 2026
  6. added a commit that references this issue on Nov 13, 2026
    3e095c2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Z-UnstableFeatureIssues that only occur if a unstable feature is enabled[C] BugThis is a bug. Something isn't working.[F] Spurious FailureIssues that cause Kani verification to fail despite the code being correct.

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions