Repository navigation
Fix seven compiler crashes found by @nuczyc - #5006
Conversation
The delayed UB pass looks for calls to the copy intrinsics, and crashed on any call it could not resolve to a function item. A call through a function pointer or vtable cannot be an intrinsic, and its callee's own writes are collected when that body is visited, so skip it. Indexing a slice reaches such a call in core's panic formatting.
The points-to analysis behind the delayed UB checks cannot follow a call whose callee is not known statically: it hit unimplemented! on calls through function pointers and unwrapped a missing body on trait object method calls. Skip such calls in the analysis, and guard each of them with an unsupported check in the instrumentation, which stops execution before the call, so the skipped aliasing edges are never needed. One predicate decides both.
The memory initialization checks asserted that a raw pointer is only transmuted to a reference to the same type. Transmuting to a different type is valid when the bytes are initialized: check the source pointee when its layout covers the target's non-padding bytes, and report the transmute as unsupported otherwise.
Reading the initialization state through a pointer to a trait object was already reported as unsupported, but updating it hit unreachable!: the layout depends on the concrete type behind the vtable. Report it the same way rather than skipping the update, which could leave uninitialized memory marked as initialized. Resolves model-checking#4853.
The assigns clause for a fat pointer only handled slices and str, and hit unreachable! for a pointer to a trait object. Cover the object behind it, whose size the vtable records, as size_of_val does.
Kani turns a quantifier's predicate into a pure expression from the closure's body. kani::forall! and kani::exists! always pass a closure, but calling the underlying kani::internal function with a function pointer crashed the compiler looking for one. Report it as unsupported instead.
A struct whose last field is an extern type is unsized, but has no pointer metadata. Dereferencing a pointer to one took the fat pointer path and crashed reading its data member, and the pointer validity check then underflowed computing the size of the extern type. Dereference such pointers like sized ones, and give the extern type size 0 and align 1, as rustc lays it out, so the check covers the fields before it. Resolves model-checking#2256.
There was a problem hiding this comment.
🟡 Changes recommended
The new transmute check can reject valid partial initialization, while zero-sized extern pointees can allow null references.
2 open findings
What changed in this PR
This PR prevents seven Kani compiler crashes by supporting valid constructs where possible and emitting unsupported diagnostics otherwise.
Changes:
- Hardens memory-initialization and points-to analysis.
- Supports trait-object modifies targets and extern-type tails.
- Adds regression coverage for all seven crashes.
| File | Description |
|---|---|
tests/kani/Uninit/delayed_ub_fn_ptr_in_std.rs |
Tests delayed-UB function-pointer handling. |
tests/kani/ForeignItems/extern_type_tail.rs |
Tests extern-type tail pointers. |
tests/expected/uninit/transmute-ptr-to-ref/main.rs |
Exercises pointer-to-reference transmutation. |
tests/expected/uninit/transmute-ptr-to-ref/expected |
Defines expected transmute results. |
tests/expected/uninit/dyn-pointee-write/main.rs |
Exercises trait-object initialization updates. |
tests/expected/uninit/dyn-pointee-write/expected |
Expects the unsupported diagnostic. |
tests/expected/uninit/delayed-ub-unsupported-call/main.rs |
Tests unsupported indirect calls. |
tests/expected/uninit/delayed-ub-unsupported-call/expected |
Defines indirect-call diagnostics. |
tests/expected/quantifiers/non_closure_predicate.rs |
Tests non-closure predicates. |
tests/expected/quantifiers/non_closure_predicate.expected |
Defines the quantifier diagnostic. |
tests/expected/function-contract/modifies/dyn_pointer.rs |
Tests trait-object modifies clauses. |
tests/expected/function-contract/modifies/dyn_pointer.expected |
Defines contract verification results. |
kani-compiler/src/kani_middle/transform/check_uninit/ptr_uninit/uninit_visitor.rs |
Handles differing pointee layouts. |
kani-compiler/src/kani_middle/transform/check_uninit/mod.rs |
Diagnoses trait-object state updates. |
kani-compiler/src/kani_middle/transform/check_uninit/delayed_ub/instrumentation_visitor.rs |
Guards unsupported indirect calls. |
kani-compiler/src/kani_middle/transform/check_uninit/delayed_ub/initial_target_visitor.rs |
Skips unresolved non-intrinsic calls. |
kani-compiler/src/kani_middle/points_to/points_to_analysis.rs |
Classifies unsupported call targets. |
kani-compiler/src/kani_middle/points_to/mod.rs |
Exports call classification. |
kani-compiler/src/codegen_cprover_gotoc/overrides/hooks.rs |
Diagnoses non-closure quantifiers. |
kani-compiler/src/codegen_cprover_gotoc/codegen/place.rs |
Dereferences thin extern-tail pointers. |
kani-compiler/src/codegen_cprover_gotoc/codegen/intrinsic.rs |
Computes extern-type size and alignment. |
kani-compiler/src/codegen_cprover_gotoc/codegen/contract.rs |
Supports trait-object modifies extents. |
🧠 Review effort: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
The pointer validity check skips __CPROVER_r_ok when there is nothing to read, and so accepted any address. rustc's own null check catches this for sized ZSTs but skips unsized pointees, so a null &T to a struct that is only an extern type verified. Keep the shortcut for raw pointers, which may be null, but require a reference to be non-null.
Transmuting *const T to &U checked the initialization of every data byte of T, which reports UB when only U's bytes are initialized, and was reported as unsupported when U is larger than T. Check U's data bytes instead, through the pointer cast to *const U. Bytes outside the allocation count as uninitialized, so a larger U is still caught.
|
Thanks! Small nit: but non blocking, so approving. Ran this on aarch64: the 8 new tests and the |
947fcec


@nuczyc found these seven compiler crashes and published a minimal reproducer for each in nuczyc/PoC-Kani, after reporting #4577 earlier. Thank you! The small harnesses made every one of them quick to pin down.
All seven still crashed on
main. Six need an unstable flag (-Z uninit-checks,-Z quantifiersor-Z function-contracts); theextern typeone crashes without any flag. None of them made Kani report a wrong success, since they abort compilation, but a crash takes down every harness in the crate, not only the one that reaches the construct.Each commit fixes one crash and adds a regression test that fails on
main:core's panic formatting, reached by slice indexing)dynmethod call or a function pointer calltransmutefrom*const Tto&UwithU != Tunder-Z uninit-checksU's bytes must be initialized*const dyn Trait#[kani::modifies(ptr)]withptr: *mut dyn Traitkani::internal::kani_forallwith a function pointer predicateforall!/exists!macros always pass a closureextern typetailTwo choices worth a second look:
ptr_uninitalready does for function pointer calls. That check stops execution, so the edges the analysis skips are never needed. The cost: a-Z uninit-checksharness that reaches adyncall now fails as unsupported where it used to crash. Real support is part of Tracking Issue: Automatically Verify Memory Initialization #3300.extern typesize 0 and align 1 in the pointer validity check, which is how rustc lays it out. A reference to such a struct is then checked for the fields before the tail. ICE: Can't apply .member operation to ... #2256's reproducer now verifies, and the same harness with a wrong expected value fails.Resolves #2256.
Resolves #4853.
Verified locally: the full regression suite passes. Under my 6 GB CBMC memory cap only
push_slowandcell_stubfailed, and both pass without the cap.kani-fmt.sh --checkand all three clippy gates (--workspace --tests,--cfg=kani_sysroot,--features llbc) are clean.