Repository navigation
Fix -Z uninit-checks crash on cross-crate constructors used as functions - #5113
acearyanarun wants to merge 1 commit into
Conversation
rustc no longer encodes optimized MIR for ADT constructors, so `rustc_public::Instance::body()` returns `None` for a constructor defined in another crate. Such a constructor is a reachable instance whenever it is used as a function value (e.g. `.map(Some)`). model-checking#4655 handled this in `BodyTransformation::body_ref` via `build_missing_body`, but the delayed UB pass and the points-to analysis still called `instance.body().unwrap()`, so `-Z uninit-checks` panicked in the delayed UB pass on any program reaching such a constructor. Expose the fallback as `transform::instance_body` and use it at the three unguarded sites: the delayed UB target search over reachable instances, the entry bodies passed to the points-to analysis, and callee bodies in `apply_regular_call_effect`. Other body-less instances still panic in `build_missing_body`, as before. Add tests/kani/Uninit/delayed_ub_cross_crate_ctor.rs: a constructor used as a function value, without and with a delayed UB target (the latter exercises the points-to call path), plus the issue's program. Resolves model-checking#4852.
|
@Tianshu-Huang @srivatsansamraj @CYJ904 @wodex1nhaoIeng Please take a look and review when you have the chance |
There was a problem hiding this comment.
Warning
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Copilot review overview
2 open issues
What changed in this PR
Fixes a -Z uninit-checks compiler panic in the delayed-UB pass when cross-crate ADT constructors are reachable as function values (e.g., .map(Some)) but rustc_public::Instance::body() returns None.
Changes:
- Add
transform::instance_body()helper to safely obtain an instance body with a constructor-aware fallback. - Replace
instance.body().unwrap()call sites in delayed-UB and points-to analysis withinstance_body(). - Add a regression test covering cross-crate constructor-as-function reachability under
-Z uninit-checks.
| File | Description |
|---|---|
| tests/kani/Uninit/delayed_ub_cross_crate_ctor.rs | Adds regression coverage for delayed-UB + points-to analysis reaching cross-crate constructors as function values. |
| kani-compiler/src/kani_middle/transform/mod.rs | Introduces instance_body() helper (with fallback to build_missing_body) and reuses it in BodyTransformation. |
| kani-compiler/src/kani_middle/transform/check_uninit/delayed_ub/mod.rs | Avoids panics by using instance_body() when scanning reachable instances and analyzing entry bodies. |
| kani-compiler/src/kani_middle/points_to/points_to_analysis.rs | Uses instance_body() to retrieve callee bodies without unwrap() panics for cross-crate constructors. |
Review effort: Lite
| // Here we simply call another function, so need to retrieve internal body for it. | ||
| let new_body = { | ||
| let stable_instance = rustc_internal::stable(instance); | ||
| let stable_body = stable_instance.body().unwrap(); | ||
| let stable_body = | ||
| crate::kani_middle::transform::instance_body(self.tcx, stable_instance); | ||
| stable_body.internal_mir(self.tcx) | ||
| }; |
| /// The original (untransformed) body of `instance`, including for the constructors whose body | ||
| /// `rustc_public::Instance::body()` does not surface, c.f. `build_missing_body`. | ||
| /// | ||
| /// Use this rather than `instance.body().unwrap()` for any reachable instance: a constructor | ||
| /// imported from another crate is reachable whenever it is used as a function value (e.g. | ||
| /// `.map(Some)`), and `body()` returns `None` for it. | ||
| pub(crate) fn instance_body(tcx: TyCtxt, instance: Instance) -> Body { | ||
| instance.body().unwrap_or_else(|| build_missing_body(tcx, instance)) | ||
| } |
feliperodri
left a comment
There was a problem hiding this comment.
The fix is right: three direct body().unwrap() calls now share one helper that uses the same fallback as codegen, and the tests cover the reported shape.
Before merge:
- The doc comment calls the fallback "original (untransformed)", but
build_missing_bodyinstantiates and normalizes. Please reword it. apply_regular_call_effectcan call the helper repeatedly for the same callee, and the fallback isn't cached. Please cache it, or show the cost is negligible on a large crate.- Six jobs are cancelled. Please re-run them.


Problem
#4852 reports that the compiler panics under
-Z uninit-checkswithcalled Option::unwrap() on a None valueatkani-compiler/src/kani_middle/transform/check_uninit/delayed_ub/mod.rs:75, in the delayed UB pass.Root cause
rustc no longer encodes optimized MIR for ADT constructors, so
rustc_public::Instance::body()returnsNonefor a constructor defined in another crate. A constructor becomes a reachable instance whenever it is used as a function value, e.g..map(Some).#4655 added
build_missing_bodyfor this case, but onlyBodyTransformation::body_refuses it. Three call sites outside that path still usedinstance.body().unwrap():delayed_ub/mod.rs; this is the panic in the issue);delayed_ub/mod.rs);PointsToAnalysis::apply_regular_call_effect(points_to_analysis.rs).Change
pub(crate) fn transform::instance_body(tcx, instance), which returnsinstance.body()and falls back tobuild_missing_body.body_refnow uses it too, with no change in behavior.body().unwrap()calls withinstance_body.build_missing_body, exactly as before. Only constructors gain a body.The constructor body that
build_missing_bodyproduces is the same one the codegen path already uses throughbody_ref. So the delayed UB analysis now sees exactly the code that gets verified.Test
tests/kani/Uninit/delayed_ub_cross_crate_ctor.rs(-Z uninit-checks):check_ctor_as_fn_value:Some(x).map(Some), which reaches the delayed UB target search.check_ctor_as_fn_value_with_target: the same with a delayed UB target present (a write through a pointer cast to a type with padding). This makes the points-to analysis run and follow the call into the constructor.check_issue_4852_reproducer: the issue's program as a proof harness. It does not panic onmain(see below), so it serves as coverage of the reported program rather than as a reproducer.Results
Run on macOS with the pinned toolchain (
nightly-2026-10-23), comparingmainate8afed3with this branch.cargo build-devpassed on both.main(e8afed3)check_ctor_as_fn_valuealone (--harness … --exact)delayed_ub/mod.rs:75VERIFICATION:- SUCCESSFULcheck_ctor_as_fn_value_with_targetalonedelayed_ub/mod.rs:75VERIFICATION:- SUCCESSFULcheck_issue_4852_reproduceraloneVERIFICATION:- SUCCESSFULVERIFICATION:- SUCCESSFULdelayed_ub/mod.rs:75SUCCESSFULkaniUninitexpecteduninitscript-based-premem-initscript-based-preautoharnessmainbut failing on this branch (suites above)./scripts/kani-fmt.sh --check;cargo clippy --workspace --tests -- -D warnings;RUSTFLAGS="--cfg=kani_sysroot" cargo clippy --workspace -- -D warningsNot run: the full
./scripts/kani-regression.shand Linux. Both are left to CI.The issue's exact command
The issue's command also passes
--run-sanity-checks -Z unstable-options:With the pinned toolchain on macOS, that command does not panic on
main, and it behaves the same with this change. In both cases it stops at a goto-instrument model validation error ("member expression type mismatch").--run-sanity-checksonly addsgoto-instrument --validate-goto-modelafter code generation, and Kani's goto models do not yet pass CBMC's consistency checks in general (#957). This PR does not change that.Without
--run-sanity-checks, the same autoharness run reportsComplete - 1 successfully verified functionson bothmainand this branch. So the issue's program by itself no longer reaches the panic here, and it is not used as the regression test. The panic site from the issue is reproduced onmainby the two constructor harnesses above.This PR fixes the delayed UB pass panic that the issue reports. It does not make the issue's exact command pass: that command still stops at the separate goto-model validation error tracked by #957. That is why this PR addresses #4852 but does not close it.
Addresses #4852
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.