-
Notifications
You must be signed in to change notification settings - Fork 169
Fix compiler_builtins upstream monomorphizations errors by inlining kani_contract_mode #4312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zjp-CN
wants to merge
1
commit into
model-checking:main
Choose a base branch
from
os-checker:fix-compiler_builtins-upstream-monomorphizations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't inlining break Kani's ability to replace the function by a mode set at Kani runtime?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have no idea on this. Is there a test for your concern?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hope we do; the code of concern is the following:
kani/kani-compiler/src/kani_middle/transform/contracts.rs
Lines 424 to 435 in 8b8b897
I don't think this would continue to work if the call had been inlined.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think kani tests have already covered this logic, because the referenced snippet is from
iterator.find_map(__find__kani_contract_mode__).unwrap(), meaning ifkani_contract_modecall is not found, kani will panic and current tests will fail.inlineis a codegen attribute, while kani is an alternative rustc backend. I think code won't be eagerly "inlined" until codegen, so kani will seekani_contract_modein MIR the Rust IR that only exists before codegen.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What efforts are needed to have this merged?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tautschnig are we good here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tautschnig I dug into this to try to settle it, since the concern is the right one to raise. Short answer: inlining does not break mode replacement, but not quite for the reason given above (and it's worth writing down).
The mechanism.
set_modescans the MIR blocks for aCallterminator whose callee carries thekani_contract_modemarker, and.unwrap()s the result:https://github.com/model-checking/kani/blob/main/kani-compiler/src/kani_middle/transform/contracts.rs#L419-L440
So the failure mode you were worried about is a loud panic, not a silently wrong mode. @zjp-CN is right that the existing contract tests would catch it.
Where the "not inlined until codegen" argument is incomplete. Kani overrides
optimized_mir(kani_middle/provide.rs:20-21) and reads bodies viatcx.instance_mir, so it sees MIR after rustc's MIR passes, which include the MIRInlinepass.#[inline]is not merely a codegen-time hint from Kani's point of view. The reason it is safe is more specific: that pass is off at the optimization level Kani compiles with.Measured on
nightly-2026-03-02, dumping theruntime-optimizedMIR (whatoptimized_mirreturns) for a#[inline]callee:-Zmir-opt-level=2-O_0 = const 7_u8; scope 1 (inlined marker)-Zinline-mir=yes-O -Zmir-enable-passes=-InlineKani passes no
-C opt-level, so the default applies and the call is still there forset_modeto find. Note it is-O/-Zinline-mirthat matters, not-Zmir-opt-levelon its own.Test evidence. With this PR merged onto today's
main:expected/function-contract109 passed / 0 failed, and thekanisuite'sContracttests 8 passed / 0 failed. Sinceset_modeunwraps, a lost call is an immediate ICE, so these passing is direct evidence that both CHECK and REPLACE still find their marker.The one caveat, which cuts the other way.
base_rustc_flagsappends the user'sRUSTFLAGSverbatim (call_single_file.rs:290), so a user can turn the inliner on. I ran that A/B on a contract harness withproof_for_contract+stub_verified:maintoday (#[inline(never)]) +-Zinline-mir=yes→ ICE:panicked at codegen/contract.rs:135: Function '_RNCNCNvCs..._1c7add_ones0_0s_0B5_' is not declared#[inline]) +-Zinline-mir=yes→ clean diagnostic:error: The function specified in the proof_for_contract attribute, add_one, is not reachable from the harnessSame with
-O. So contracts are already broken under MIR inlining onmain, this PR does not introduce that, and it happens to turn an ICE into an actionable error. In neither case didset_mode'sunwrapfire; under inlining the contracted function itself gets inlined into the harness and the reachability check fires first.Why the other markers are fine, which @zjp-CN flagged as unexplained.
kani_force_fn_once,kani_force_fn_once_with_argsandkani_register_contractare all generic, so they are monomorphized into whichever crate calls them and never go through an upstream instantiation.kani_contract_modeis the only non-generic one, so its single instantiation lives in the defining crate and a call fromcompiler_builtinsis exactly the upstream monomorphization that rustc rejects.#[inline]makes rustc emit a local copy in each CGU that needs it. That makes the fix principled rather than incidental and it suggests making the function generic would work too, though#[inline]is the smaller change.Two optional hardenings, both better as separate PRs. Kani could pass
-Zmir-enable-passes=-Inlineso the invariant holds regardless of what a user puts inRUSTFLAGS(verified above that it preserves the call even under-O), andset_mode's.unwrap()could become a diagnostic that names the function instead of a panic. Neither is needed for this PR to be correct today.One thing I did not verify: that this actually fixes the original
compiler_builtinserror, since I could not reproduce that build locally. That part still rests on @zjp-CN's report.