Skip to content

Fix -Z uninit-checks crash on cross-crate constructors used as functions - #5113

Open
acearyanarun wants to merge 1 commit into
model-checking:mainfrom
acearyanarun:fix-4852-uninit-ctor-body
Open

acearyanarun wants to merge 1 commit into
model-checking:mainfrom
acearyanarun:fix-4852-uninit-ctor-body

Conversation

@acearyanarun

Copy link
Copy Markdown
Contributor

Problem

#4852 reports that the compiler panics under -Z uninit-checks with called Option::unwrap() on a None value at kani-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() returns None for 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_body for this case, but only BodyTransformation::body_ref uses it. Three call sites outside that path still used instance.body().unwrap():

  • the delayed UB pass's target search over all reachable instances (delayed_ub/mod.rs; this is the panic in the issue);
  • the entry bodies handed to the points-to analysis (delayed_ub/mod.rs);
  • callee bodies in PointsToAnalysis::apply_regular_call_effect (points_to_analysis.rs).

Change

  • Add pub(crate) fn transform::instance_body(tcx, instance), which returns instance.body() and falls back to build_missing_body. body_ref now uses it too, with no change in behavior.
  • Replace the three body().unwrap() calls with instance_body.
  • Other body-less instances still panic inside build_missing_body, exactly as before. Only constructors gain a body.

The constructor body that build_missing_body produces is the same one the codegen path already uses through body_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 on main (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), comparing main at e8afed3 with this branch. cargo build-dev passed on both.

Check main (e8afed3) This branch
check_ctor_as_fn_value alone (--harness … --exact) Compiler panic at delayed_ub/mod.rs:75 VERIFICATION:- SUCCESSFUL
check_ctor_as_fn_value_with_target alone Compiler panic at delayed_ub/mod.rs:75 VERIFICATION:- SUCCESSFUL
check_issue_4852_reproducer alone VERIFICATION:- SUCCESSFUL VERIFICATION:- SUCCESSFUL
Whole test file Compiler panic at delayed_ub/mod.rs:75 3/3 SUCCESSFUL
compiletest kani Uninit — 9 passed
compiletest expected uninit — 30 passed
compiletest script-based-pre mem-init — 1 passed
compiletest script-based-pre autoharness — 42 passed
Tests passing on main but failing on this branch (suites above) — none
./scripts/kani-fmt.sh --check; cargo clippy --workspace --tests -- -D warnings; RUSTFLAGS="--cfg=kani_sysroot" cargo clippy --workspace -- -D warnings — passed

Not run: the full ./scripts/kani-regression.sh and Linux. Both are left to CI.

The issue's exact command

The issue's command also passes --run-sanity-checks -Z unstable-options:

kani autoharness -Z autoharness --run-sanity-checks -Z unstable-options -Z uninit-checks code.rs

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-checks only adds goto-instrument --validate-goto-model after 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 reports Complete - 1 successfully verified functions on both main and 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 on main by 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.

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.
@acearyanarun
acearyanarun requested review from a team as code owners November 3, 2026 05:57
@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 3, 2026
@acearyanarun

Copy link
Copy Markdown
Contributor Author

@Tianshu-Huang @srivatsansamraj @CYJ904 @wodex1nhaoIeng Please take a look and review when you have the chance

@feliperodri feliperodri added [C] Bug This is a bug. Something isn't working. [F] Crash Kani crashed Z-Autoharness Issue related to autoharness subcommand labels Nov 6, 2026
@feliperodri feliperodri added this to the Autoharness milestone Nov 6, 2026
@feliperodri feliperodri removed the Z-Autoharness Issue related to autoharness subcommand label Nov 6, 2026
@feliperodri feliperodri removed this from the Autoharness milestone Nov 6, 2026
@feliperodri
feliperodri requested a balanced review from Copilot November 6, 2026 19:13
@feliperodri feliperodri self-assigned this Nov 6, 2026

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.

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 with instance_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

Comment on lines 455 to 461
// 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)
};
Comment on lines +68 to +76
/// 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 feliperodri left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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:

  1. The doc comment calls the fallback "original (untransformed)", but build_missing_body instantiates and normalizes. Please reword it.
  2. apply_regular_call_effect can 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.
  3. Six jobs are cancelled. Please re-run them.

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

[C] Bug This is a bug. Something isn't working. [F] Crash Kani crashed Z-CompilerBenchCI Tag a PR to run benchmark CI Z-EndToEndBenchCI Tag a PR to run benchmark CI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants