Skip to content

Disable MIR inlining when stubbing is enabled - #5095

Open
r-near wants to merge 3 commits into
model-checking:mainfrom
r-near:fix-stub-mir-inline
Open

r-near wants to merge 3 commits into
model-checking:mainfrom
r-near:fix-stub-mir-inline

Conversation

@r-near

@r-near r-near commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

At opt-level 2/3, rustc's MIR inliner copies a stubbed function into its callers, and FnStubPass only replaces the function's own body, so the stub was silently skipped there. This passes -Z inline-mir=no to the crate under verification whenever stubbing is enabled (including via function contracts). Dependencies skip codegen, so they don't get it; passing it to them would only force a rebuild.

Adds tests/kani/Stubbing/stub_inlined_function.rs, tests/cargo-kani/stubbing-inlined-function and tests/expected/function-contract/stub_verified_inlined_requires_fail.rs, which fail before this change.

Resolves #5094

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.

@r-near
r-near requested review from a team as code owners October 28, 2026 17:20
@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 28, 2026
@feliperodri
feliperodri requested a balanced review from Copilot October 31, 2026 00:27

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.

🟢 Approval recommended

No active review issues are recorded for the change that disables MIR inlining when stubbing is enabled.

No open issues 🎉

What changed in this PR

Prevents MIR inlining from bypassing Kani function stubs at optimization levels 2 and 3, addressing the false verification results reported in #5094.

Changes:

  • Passes -Z inline-mir=no when stubbing is enabled, including through function contracts.
  • Adds an optimization-level-2 regression test checking that a stub applies through an intermediate caller.
File Description
tests/​kani/​Stubbing/​stub_inlined_function.rs Tests stub application through a caller with optimization enabled.
kani-driver/​src/​call_single_file.rs Disables MIR inlining when stubbing is enabled.

Review effort: Balanced

Dependencies skip codegen, so passing the flag to them only changed their
flags and forced a rebuild, which broke `build-cache-dirty` for
`-Z function-contracts`. Add a cargo test for the local-crate path.
@feliperodri feliperodri self-assigned this Nov 13, 2026

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

Thanks @r-near. Turning off MIR inlining for the crate under verification whenever stubbing or contracts are enabled is the right fix. It matters for soundness too, not only for #[kani::stub]: stub_verified replaces a call with the contract, which asserts the precondition, so an inlined call loses that check. With the base build at -Copt-level=2, this harness verifies even though caller violates x < 10; with your change it fails as it should:

#[kani::requires(x < 10)]
#[kani::ensures(|r: &u32| *r == x + 1)]
fn inc(x: u32) -> u32 { x + 1 }

fn caller() -> u32 { inc(50) }

#[kani::proof]
#[kani::stub_verified(inc)]
fn uses_contract() { let _ = caller(); }

Could you add this case as a regression test (an expected test with -Copt-level=2)? It's the most important case this PR fixes. I also checked a stub on a function inlined inside a path dependency at opt-level 2: it applies with and without your change, so leaving the flag off dependencies is fine. The stubbing and contract suites pass.

@feliperodri feliperodri added [F] Soundness Kani failed to detect an issue Z-Contracts Issue related to code contracts labels Nov 13, 2026
@feliperodri feliperodri assigned r-near and unassigned feliperodri Nov 13, 2026
@r-near

r-near commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

@feliperodri added it as tests/expected/function-contract/stub_verified_inlined_requires_fail.rs. It needed a proof_for_contract harness too, which also broke before the fix (inc got inlined into it, so Kani called it unreachable).

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

[F] Soundness Kani failed to detect an issue Z-CompilerBenchCI Tag a PR to run benchmark CI Z-Contracts Issue related to code contracts Z-EndToEndBenchCI Tag a PR to run benchmark CI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

#[kani::stub] silently not applied to MIR-inlined calls (opt-level 2/3): false proofs

3 participants