Repository navigation
Uninit checks: support pointers to trait objects - #5192
Open
feliperodri wants to merge 2 commits into
Open
feliperodri wants to merge 2 commits into
feliperodri wants to merge 2 commits into
Conversation
Contributor
There was a problem hiding this comment.
🟡 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
chronodependency.
| 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>
feliperodri
force-pushed
the
uninit-trait-objects
branch
from
November 18, 2026 21:40
0041fa2 to
15bd8ef
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

#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_valon a&dyn Debug(#5166). This blocks uninit checking on verify-rust-std's Challenge 2 solution (model-checking/verify-rust-std#649).Changes
dyn Trait: an unsized value cannot be loaded, copied or stored, so a place*ptrof 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.*const dyn Trait: mark the value initialized with its concrete layout (newSetAsoperation), as for any raw pointer created from a reference, instead of reporting "unsized pointers" as unsupported.PtrMetadata, whichptr::metadatalowers to) marks its vtable header (size, alignment, drop) initialized (newSetVtableoperation). The vtable is constant data emitted by the code generator, so its memory was never marked initialized, and a check such ascan_dereference(vtable_ptr as *const [usize; 3])failed.kani::mem::is_initializedon a trait object pointer still reports that it cannot be answered.Testing
tests/expected/uninit/trait-objects:size_of_val/align_of_valon&dyn(the issue's reproducer), rawdynpointers and their metadata,Box<dyn Trait>, reading a vtable header throughcan_dereferenceand a raw read, and reading a value back through a*const dynpointer 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.shpasses in the Kani CI image, apart fromcompiler_defaults(needsjq) andcbmc_truncated_results(memory limit), which only fail in my container.c5e65fd), its 11 trait-object harnesses (vtable_size,vtable_align,size_of_val_dyn,align_of_val_dyn,align_of_valusage) 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 ofMaybeUninit<T>with non-trivialTare 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.