You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
-Z uninit-checks: arrays of MaybeUninit<T> with non-trivial T are unsupported #5151
Related: #3300 (tracking issue for memory-initialization checks).
use core::mem::MaybeUninit;structD(u8);implDropforD{fndrop(&mutself){}}#[kani::proof]fndrop_uninit_in_array(){letmut a:[MaybeUninit<D>;2] = [const{MaybeUninit::uninit()};2];
a[0].write(D(1));unsafe{ core::ptr::drop_in_place(a.as_mut_ptr().cast::<D>().add(1))};// slot 1 uninit}
cargo kani -Z uninit-checks on main reports: Checking memory initialization of type [std::mem::MaybeUninit<D>; 2] is not supported. Cannot determine layout for a type that contains union of type std::mem::MaybeUninit<D> as a field.
(Kani 0.68.0 instead ICEs on a larger crate, in check_uninit/delayed_ub/initial_target_visitor.rs:107, when a fmt fn pointer is
reachable. That is fixed on main.)
[MaybeUninit<T>; N] is the standard buffer pattern in std (MapWindows, array::IntoIter, ArrayVec-style code). Supporting it would let Kani flag rust-lang/rust#156501 natively.
Environment: Kani main @ bf9c58e (2026-11-10), CBMC 6.11.0, x86_64 Linux. Also reproduced with Kani 0.68.0. Context: found while modelling the std memory-safety bugs from https://forge-blog.github.io/posts/rust-std-memory-safety/ with Kani.
More data on how much this blocks: verify-rust-std's Challenge 2 solution (model-checking/verify-rust-std#649), run with -Z uninit-checks at Kani c5e65fd. 91 of its 394 intrinsics::verify harnesses fail on unsupported checks, and 77 of them come from a union in the buffer layout:
58 build their buffers as [MaybeUninit<T>; N] ("Checking memory initialization of type [MaybeUninit<[u8; 2]>; 2] is not supported. Cannot determine layout for a type that contains union ...");
19 use Kani's own PointerGenerator<N>, which fails the same way because it holds a MaybeUninit<[u8; N]> ("Checking memory initialization of type kani::arbitrary_ptr::PointerGenerator<16> is not supported. Cannot determine layout for a type that contains union of type MaybeUninit<[u8; 16]> as a field").
The other 14 are trait-object pointers (#5166). Between them they cover every harness that touches memory: copy, copy_nonoverlapping, the five volatile intrinsics, volatile_load/volatile_store, compare_bytes, read_via_copy, write_via_move, write_bytes, typed_swap, ptr_offset_from(_unsigned). None reported an actual uninitialized read. So while this is open, "reading from uninitialized memory" can't be checked for those intrinsics at all, and PointerGenerator itself can't be used with -Z uninit-checks.
We have a prototype that makes this pattern checkable, built on top of #5172, and would like your view on the approach and on how to split it before opening more PRs.
What it tracks. With -Z uninit-checks, MaybeUninit<T>, [MaybeUninit<T>; N], [MaybeUninit<T>] and MaybeUninit<[T; N]> get per-byte initialization state instead of the "unsupported construct" error:
typed copies and moves carry the state of T's data bytes and drop padding; raw copies carry every byte;
by-value arguments and return values carry their state across direct calls;
constants start from the initialization state of their allocation's bytes;
raw_eq checks every byte of both operands.
Results. On the example in this issue, Kani 0aac16e reports the unsupported construct. The prototype reports the drop_in_place of the uninitialized slot 1 as "Reading from an uninitialized pointer of type *mut D", and dropping the initialized slot 0 verifies. The full regression suite passes on Linux x86_64, Linux Arm and macOS (run).
Not covered.MaybeUninit arrays held in a struct field (as in array::IntoIter), state transfer under stub_verified and kani::recursion, loop contracts (#5179, #5181), and calls through function pointers or trait objects, which stay unsupported as in #5172.
Code.Whole diff: about 2,900 added lines in the compiler and kani_core; the rest is tests. We would send it as small PRs, each with tests that fail on main:
initialization state for constants, together with the raw_eq checks;
MaybeUninit storage and transfers across calls;
closure environments.
Questions.
Constants get their state by patching the generated body of initialize_memory_initialization_state after codegen. Is that acceptable, or is there a hook you would prefer?
Call transfers keep the state of by-value arguments and return values in a small buffer in kani_core::mem_init: the caller stores it before the call and the callee loads it on entry. Is that the right place for it?
Does the split above work for you?
Disclosure: this comment was prepared with the help of AI assistants.
Activity
feliperodri commentedon Nov 13, 2026
More data on how much this blocks: verify-rust-std's Challenge 2 solution (model-checking/verify-rust-std#649), run with
-Z uninit-checksat Kanic5e65fd. 91 of its 394intrinsics::verifyharnesses fail on unsupported checks, and 77 of them come from a union in the buffer layout:[MaybeUninit<T>; N]("Checking memory initialization of type[MaybeUninit<[u8; 2]>; 2]is not supported. Cannot determine layout for a type that contains union ...");PointerGenerator<N>, which fails the same way because it holds aMaybeUninit<[u8; N]>("Checking memory initialization of typekani::arbitrary_ptr::PointerGenerator<16>is not supported. Cannot determine layout for a type that contains union of typeMaybeUninit<[u8; 16]>as a field").The other 14 are trait-object pointers (#5166). Between them they cover every harness that touches memory:
copy,copy_nonoverlapping, the five volatile intrinsics,volatile_load/volatile_store,compare_bytes,read_via_copy,write_via_move,write_bytes,typed_swap,ptr_offset_from(_unsigned). None reported an actual uninitialized read. So while this is open, "reading from uninitialized memory" can't be checked for those intrinsics at all, andPointerGeneratoritself can't be used with-Z uninit-checks.Tiramisu-Cake commentedon Nov 16, 2026
We have a prototype that makes this pattern checkable, built on top of #5172, and would like your view on the approach and on how to split it before opening more PRs.
What it tracks. With
-Z uninit-checks,MaybeUninit<T>,[MaybeUninit<T>; N],[MaybeUninit<T>]andMaybeUninit<[T; N]>get per-byte initialization state instead of the "unsupported construct" error:T's data bytes and drop padding; raw copies carry every byte;raw_eqchecks every byte of both operands.Results. On the example in this issue, Kani
0aac16ereports the unsupported construct. The prototype reports thedrop_in_placeof the uninitialized slot 1 as "Reading from an uninitialized pointer of type*mut D", and dropping the initialized slot 0 verifies. The full regression suite passes on Linux x86_64, Linux Arm and macOS (run).Not covered.
MaybeUninitarrays held in a struct field (as inarray::IntoIter), state transfer understub_verifiedandkani::recursion, loop contracts (#5179, #5181), and calls through function pointers or trait objects, which stay unsupported as in #5172.Code. Whole diff: about 2,900 added lines in the compiler and
kani_core; the rest is tests. We would send it as small PRs, each with tests that fail onmain:kani autoharness -Z uninit-checksmisses uninitialized reads (Reset memory initialization state in automatic harnesses #5183); and recursion reachable from a delayed UB target overflows the compiler's stack (Report recursive calls in delayed UB analysis instead of overflowing the stack #5184);raw_eqchecks;MaybeUninitstorage and transfers across calls;Questions.
initialize_memory_initialization_stateafter codegen. Is that acceptable, or is there a hook you would prefer?kani_core::mem_init: the caller stores it before the call and the callee loads it on entry. Is that the right place for it?Disclosure: this comment was prepared with the help of AI assistants.