Skip to content

Uninit checks: support pointers to trait objects - #5192

Open
feliperodri wants to merge 2 commits into
model-checking:mainfrom
feliperodri:uninit-trait-objects
Open

feliperodri wants to merge 2 commits into
model-checking:mainfrom
feliperodri:uninit-trait-objects

Conversation

@feliperodri

@feliperodri feliperodri commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

#5188 (the toolchain upgrade) is merged, and this branch is rebased on main. The second commit addresses the review comments.

With -Z uninit-checks, any pointer to a trait object was reported as unsupported, even where nothing uninitialized is involved, e.g. size_of_val on a &dyn Debug (#5166). This blocks uninit checking on verify-rust-std's Challenge 2 solution (model-checking/verify-rust-std#649).

Changes

  • Checks on a trait object pointee: emit none. Nothing is read at type dyn Trait: an unsized value cannot be loaded, copied or stored, so a place *ptr of that type is only borrowed, re-pointed or used for its metadata. The bytes behind it are read by the concrete type's code that the vtable dispatches to, which is checked with the concrete layout.
  • Marking a trait object pointee initialized: skip it. This only happens where a pointer to an existing value is created (from a reference, or for a drop). Creating a pointer does not change which bytes are initialized, so the tracked state stays as it was; at worst bytes that were never tracked stay uninitialized, a false alarm rather than a missed one. Marking it uninitialized cannot be skipped soundly and is still reported as unsupported.
  • Unsizing a pointer to a sized value into *const dyn Trait: mark the value initialized with its concrete layout (new SetAs operation), as for any raw pointer created from a reference, instead of reporting "unsized pointers" as unsupported.
  • Vtables: reading a trait object's metadata (PtrMetadata, which ptr::metadata lowers to) marks its vtable header (size, alignment, drop) initialized (new SetVtable operation). The vtable is constant data emitted by the code generator, so its memory was never marked initialized, and a check such as can_dereference(vtable_ptr as *const [usize; 3]) failed.
  • kani::mem::is_initialized on a trait object pointer still reports that it cannot be answered.

Testing

  • New tests/expected/uninit/trait-objects: size_of_val/align_of_val on &dyn (the issue's reproducer), raw dyn pointers and their metadata, Box<dyn Trait>, reading a vtable header through can_dereference and a raw read, and reading a value back through a *const dyn pointer all verify; reading a padding byte through a pointer derived from a trait object is still reported.
  • tests/expected/uninit/dyn-pointee-write (reborrowing a *const dyn) now verifies.
  • scripts/kani-regression.sh passes in the Kani CI image, apart from compiler_defaults (needs jq) and cbmc_truncated_results (memory limit), which only fail in my container.
  • verify-rust-std#649: with this change backported to the Kani commit it pins (c5e65fd), its 11 trait-object harnesses (vtable_size, vtable_align, size_of_val_dyn, align_of_val_dyn, align_of_val usage) pass with -Z uninit-checks. The remaining uninit failures there come from unions nested in other types and raw pointers to unions (-Z uninit-checks: arrays of MaybeUninit<T> with non-trivial T are unsupported #5151), handled in a follow-up PR.

Resolves #5166

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.

@feliperodri
feliperodri requested review from a team as code owners November 18, 2026 18:16
@feliperodri feliperodri added [E] Unsupported Construct Add support to an unsupported construct Z-UnstableFeature Issues that only occur if a unstable feature is enabled labels Nov 18, 2026
@github-actions github-actions Bot added Z-EndToEndBenchCI Tag a PR to run benchmark CI Z-CompilerBenchCI Tag a PR to run benchmark CI labels Nov 18, 2026
@feliperodri
feliperodri requested a balanced review from Copilot November 18, 2026 18:52

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Raw-pointer unsizing incorrectly marks uninitialized memory as initialized, while blanket trait-object exemptions remove necessary checks, allowing uninitialized reads to go undetected.

2 open issues
What changed in this PR

Extends Kani’s uninitialized-memory checks to support trait-object pointers, addressing #5166. The branch also includes the toolchain upgrade and compatibility changes from #5188.

Changes:

  • Adds trait-object handling, concrete-layout initialization tracking, and vtable-header initialization.
  • Adds regression coverage for trait-object metadata, raw-pointer reads, padding, and fallback intrinsics.
  • Incorporates nightly compiler API updates and removes the driver’s chrono dependency.
File Description
tools/​scanner/​src/​bin/​scan.rs Avoids allocation when checking verbosity.
tests/​script-based-pre/​autoharness_sve_tuple/​check.sh Accepts the updated LLVM intrinsic suffix.
tests/​script-based-pre/​autoharness_pattern_type/​pattern_type_probe.rs Removes an obsolete feature gate.
tests/​script-based-pre/​autoharness_niche/​niche_probe.rs Removes an obsolete gate while preserving line numbers.
tests/​kani/​Uninit/​fallback_intrinsic.rs Tests uninit checks with fallback intrinsics.
tests/​kani/​Intrinsics/​MinNum/​minnumf64.rs Uses the generic minimum intrinsic.
tests/​kani/​Intrinsics/​MinNum/​minnumf32.rs Uses the generic minimum intrinsic.
tests/​kani/​Intrinsics/​MaxNum/​maxnumf64.rs Uses the generic maximum intrinsic.
tests/​kani/​Intrinsics/​MaxNum/​maxnumf32.rs Uses the generic maximum intrinsic.
tests/​kani/​Intrinsics/​CopySign/​copysignf64.rs Uses the generic copy-sign intrinsic.
tests/​kani/​Intrinsics/​CopySign/​copysignf32.rs Uses the generic copy-sign intrinsic.
tests/​expected/​valid-value-checks/​place_access_pattern.rs Removes an obsolete feature gate.
tests/​expected/​valid-value-checks/​custom_niche.rs Removes an obsolete gate while preserving line numbers.
tests/​expected/​uninit/​trait-objects/​main.rs Adds trait-object and padding-read regression harnesses.
tests/​expected/​uninit/​trait-objects/​expected Records expected successes and the padding-read failure.
tests/​expected/​uninit/​dyn-pointee-write/​main.rs Updates comments to describe supported reborrowing.
tests/​expected/​uninit/​dyn-pointee-write/​expected Changes the expected result to success.
rust-toolchain.toml Pins nightly-2026-11-18.
library/​kani/​src/​models/​mod.rs Avoids prohibited SIMD field projections in Clone.
kani-driver/​src/​session.rs Adds standard-library UTC timestamp formatting and tests.
kani-driver/​src/​concrete_playback/​playback.rs Avoids allocation when checking playback arguments.
kani-driver/​Cargo.toml Removes chrono.
kani-compiler/​src/​session.rs Updates diagnostic configuration for rustc.
kani-compiler/​src/​kani_middle/​transform/​loop_contracts.rs Avoids allocation when checking feature flags.
kani-compiler/​src/​kani_middle/​transform/​contracts.rs Avoids allocation when checking feature flags.
kani-compiler/​src/​kani_middle/​transform/​check_uninit/​relevant_instruction.rs Adds SetAs and SetVtable operations.
kani-compiler/​src/​kani_middle/​transform/​check_uninit/​ptr_uninit/​uninit_visitor.rs Detects trait-object initialization operations and permits fallback intrinsics.
kani-compiler/​src/​kani_middle/​transform/​check_uninit/​mod.rs Handles new operations and trait-object exemptions.
kani-compiler/​src/​kani_middle/​attributes.rs Replaces removed rustc attribute APIs.
kani-compiler/​src/​intrinsics.rs Maps generic float intrinsics to existing models.
kani-compiler/​src/​codegen_cprover_gotoc/​overrides/​hooks.rs Avoids allocation when checking loop-contract flags.
kani-compiler/​src/​codegen_cprover_gotoc/​context/​current_fn.rs Uses the replacement attribute-value helper.
kani-compiler/​src/​codegen_cprover_gotoc/​codegen/​typ.rs Adds explicit unsupported handling for view types.
kani-compiler/​src/​codegen_cprover_gotoc/​codegen/​statement.rs Avoids allocations in loop-contract flag checks.
kani-compiler/​src/​codegen_cprover_gotoc/​codegen/​function.rs Avoids allocation when checking loop-contract flags.
kani-compiler/​src/​codegen_cprover_gotoc/​codegen/​foreign_function.rs Avoids allocation when checking C-FFI flags.
Cargo.lock Removes chrono and its unused dependency entries.

Review effort: Balanced

-Z uninit-checks reported any pointer to a trait object as unsupported, even where
nothing uninitialized is involved, e.g. size_of_val on a &dyn (model-checking#5166).

- Nothing is read at type dyn Trait: an unsized value cannot be loaded, copied or
  stored. Checks on a trait object pointee have nothing to check, so emit none; the
  concrete type's code that the vtable dispatches to is checked with its own layout.
- Marking a trait object pointee initialized only happens where a pointer to an
  existing value is created (from a reference, or for a drop). Creating a pointer does
  not change which bytes are initialized, so skip it: at worst, untracked bytes stay
  uninitialized, a false alarm rather than a missed one. Marking it uninitialized
  cannot be skipped soundly and stays unsupported.
- Unsizing a pointer to a sized value into a raw pointer to a trait object marks the
  value initialized with its concrete layout, as for other raw pointers created from
  references, instead of reporting it as unsupported.
- Reading a trait object's metadata marks its vtable header (size, alignment, drop)
  initialized: the vtable is constant data that the code generator emits, so its
  memory was never marked initialized.

Resolves model-checking#5166

Signed-off-by: Felipe R. Monteiro <felisous@amazon.com>
Address review comments:

- Creating a reference from a raw pointer to a trait object skipped the
  check, but the value behind it must be valid, and reads through the
  reference in the concrete method are not checked. Report it as unsupported,
  as for a transmute from such a pointer to a reference. A `Box`, like a
  reference, points to a valid value, so pointers read out of a `Box` are
  exempt. Taking the address of `*p` and reading its metadata stay unchecked:
  they read nothing.
- Unsizing a raw pointer to a sized value into a raw pointer to a trait
  object marked the pointee initialized. Only do so for references; a raw
  pointer keeps its pointee's state.

Signed-off-by: Felipe R. Monteiro <felisous@amazon.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[E] Unsupported Construct Add support to an unsupported construct Z-CompilerBenchCI Tag a PR to run benchmark CI Z-EndToEndBenchCI Tag a PR to run benchmark CI Z-UnstableFeature Issues that only occur if a unstable feature is enabled

Projects

None yet

Development

Successfully merging this pull request may close these issues.

-Z uninit-checks: pointers to trait objects are unsupported

3 participants