Repository navigation
Fix atomic store reads and initialization tracking - #5115
Conversation
Avoid reading the old destination for unit-returning atomic stores and mark the written memory initialized using the existing write instrumentation. Preserve the exchange read/return path. Add store/load regressions for integer and pointer values, exchange behavior, and negative checks for uninitialized reads, invalid destinations, and adjacent uninitialized memory.
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are recorded for the atomic-store and uninitialized-memory tracking changes.
No open issues 🎉
What changed in this PR
Fixes atomic-store handling in Kani’s code generation and uninitialized-memory tracking, addressing #5114 for volatile stores to allocated memory while preserving existing non-volatile checks.
Changes:
- Makes atomic stores write-only while preserving exchange’s previous-value return.
- Records store destinations as initialized and skips prior-initialization checks only for volatile stores.
- Adds positive and negative regression coverage for integer and pointer stores, exchanges, null destinations, and adjacent uninitialized memory.
| File | Description |
|---|---|
tests/kani/Uninit/atomic_store.rs |
Adds successful store-initialization and exchange regression cases. |
tests/expected/uninit/atomic/expected |
Updates expected diagnostics and failure summaries. |
tests/expected/uninit/atomic/atomic.rs |
Adds negative cases for invalid stores and uninitialized accesses. |
kani-compiler/src/kani_middle/transform/check_uninit/ptr_uninit/uninit_visitor.rs |
Tracks store initialization and retains pre-store checks for non-volatile operations. |
kani-compiler/src/codegen_cprover_gotoc/codegen/intrinsic.rs |
Removes old-value reads from stores and clarifies store versus exchange behavior. |
Review effort: Balanced
feliperodri
left a comment
There was a problem hiding this comment.
Thanks @v3risec. Splitting on VOLATILE follows the intrinsic's documented contract: a volatile store is store_volatile on a raw pointer, which may initialize memory, while a non-volatile store goes through Atomic::from_ptr, so keeping its check is the conservative choice. Not reading the old value for a store is also right.
I built the head and ran kani Uninit, expected uninit, kani Atomic and kani Intrinsics; all pass, and 2 of your 4 new harnesses fail on the base compiler. I also checked that initialization stays precise: a 1-byte store_volatile followed by a u32 read is still reported, while a full u32 store followed by a read now passes.
Optional: instance.args().0[2].expect_const() would panic if the intrinsic's generics change in a future toolchain. Something like instance.args().0.get(2).and_then(...), defaulting to non-volatile (which keeps the check), plus a comment naming the VOLATILE generic of atomic_store<T, ORD, VOLATILE>, would make that safer.
3e095c2
Summary
Kani incorrectly checks the destination of a volatile atomic store for prior initialization and fails to mark it as initialized after the write. As a result, a valid store to uninitialized allocated memory is rejected, and a subsequent read also fails its initialization check.
The code generator shared by atomic store and exchange also reads the previous destination value even for stores, although only exchange needs to return it.
Changes
VOLATILEargument istrue. Preserve the existing check for non-volatile stores pending clarification of theirfrom_ptr-based safety contract.Regression tests
Added coverage for:
Validation
Validated against upstream commit
c5e65fdfc3e15ec32293f2a4c07509aebec033c9, using nightly-2026-10-23, CBMC 6.11.0, and x86_64 Linux.Targeted validation:
-Zmiri-recursive-validationflag. This is supplementary evidence, not a definitive resolution of Rust's reference-validity rules.Broader regression results:
scripts/kani-regression.shpassed.Quantifiers/gcd_invariant.rsat 300 seconds. The same test also timed out on the unmodified baseline at 120 seconds. The focused atomic test passed separately.The full regression script stopped at the Kani-suite timeout. Expected and UI were run independently; later suites were not run. These results do not constitute a fully passing regression run.
Scope and limitations
The volatile-store reproducer relies on Rust's raw-pointer
store_volatilesafety contract and uses ordinary allocated memory.The initial reproducer used
VOLATILE=false, but its validity under thefrom_ptr-based safety contract has not been established. This change preserves the existing non-volatile initialization check and does not resolve whether non-volatile atomic stores may initialize uninitialized storage.This change does not implement complete volatile externally observable behavior, MMIO, or concurrency modeling.
Resolves #5114
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.