Skip to content

Fix seven compiler crashes found by @nuczyc - #5006

Merged
feliperodri merged 10 commits into
model-checking:mainfrom
feliperodri:fix-poc-crashes
Oct 22, 2026
Merged

feliperodri merged 10 commits into
model-checking:mainfrom
feliperodri:fix-poc-crashes

Conversation

@feliperodri

@feliperodri feliperodri commented Oct 14, 2026 •

Copy link
Copy Markdown
Member

@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 quantifiers or -Z function-contracts); the extern type one 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:

PoC Construct Now
1 Delayed UB targets, with a call through a function pointer (core's panic formatting, reached by slice indexing) Skipped: such a call can't be a copy intrinsic
3 Delayed UB points-to analysis, with a dyn method call or a function pointer call Unsupported check before the call
4 transmute from *const T to &U with U != T under -Z uninit-checks Checked: U's bytes must be initialized
5 Updating initialization state through a *const dyn Trait Unsupported, as reads already were (#4853)
7 #[kani::modifies(ptr)] with ptr: *mut dyn Trait Supported: covers the object, sized from the vtable
6 kani::internal::kani_forall with a function pointer predicate Unsupported; the forall!/exists! macros always pass a closure
2 Struct with an extern type tail Thin pointer, as rustc treats it (#2256)

Two choices worth a second look:

  • Commit 2 doesn't teach the points-to analysis about calls it can't resolve. It puts an unsupported check before each one, the same as ptr_uninit already does for function pointer calls. That check stops execution, so the edges the analysis skips are never needed. The cost: a -Z uninit-checks harness that reaches a dyn call now fails as unsupported where it used to crash. Real support is part of Tracking Issue: Automatically Verify Memory Initialization #3300.
  • Commit 7 gives an extern type size 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_slow and cell_stub failed, and both pass without the cap. kani-fmt.sh --check and all three clippy gates (--workspace --tests, --cfg=kani_sysroot, --features llbc) are clean.

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.
@feliperodri
feliperodri requested review from a team as code owners October 14, 2026 13:47
@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 Oct 14, 2026
@feliperodri
feliperodri requested a balanced review from Copilot October 14, 2026 15:25

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

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.
@rajath-mk

Copy link
Copy Markdown
Contributor

Thanks! Small nit: unsupported_call reports a failed Instance::resolve as a "trait object method call", which would be misleading if it ever happens for another reason.

but non blocking, so approving. Ran this on aarch64: the 8 new tests and the expected/uninit and kani/Uninit suites pass, and each of nuczyc's crash shapes I tried crashes on the merge-base and now either verifies or fails honestly.

@feliperodri
feliperodri added this pull request to the merge queue Oct 22, 2026
Merged via the queue into model-checking:main with commit 947fcec Oct 22, 2026
36 of 37 checks passed
@feliperodri
feliperodri deleted the fix-poc-crashes branch October 22, 2026 21:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Z-CompilerBenchCI Tag a PR to run benchmark CI Z-EndToEndBenchCI Tag a PR to run benchmark CI

Projects

None yet

3 participants