Skip to content

-Z uninit-checks: arrays of MaybeUninit<T> with non-trivial T are unsupported #5151

Description

@feliperodri

Related: #3300 (tracking issue for memory-initialization checks).

use core::mem::MaybeUninit;
struct D(u8);
impl Drop for D { fn drop(&mut self) {} }

#[kani::proof]
fn drop_uninit_in_array() {
    let mut 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.

Activity

  1. feliperodri commented on Nov 13, 2026

    @feliperodri
    MemberAuthor

    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.

  2. Tiramisu-Cake commented on Nov 16, 2026

    @Tiramisu-Cake
    Contributor

    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:

    1. two fixes found on the way, which do not depend on the rest: automatic harnesses never reset the initialization state, so kani autoharness -Z uninit-checks misses 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);
    2. initialization state for constants, together with the raw_eq checks;
    3. MaybeUninit storage and transfers across calls;
    4. closure environments.

    Questions.

    1. 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?
    2. 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?
    3. Does the split above work for you?

    Disclosure: this comment was prepared with the help of AI assistants.

  3. self-assigned this
    on Nov 17, 2026
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[E] Unsupported ConstructAdd support to an unsupported construct

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions