Skip to content

Fix invalid instantiation of fake intrinsic bodies - #165769

Merged
rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
oli-obk:lmao-unreachable-body-in-any-intrinsic
Nov 24, 2026
Merged

rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
oli-obk:lmao-unreachable-body-in-any-intrinsic

Conversation

@oli-obk

@oli-obk oli-obk commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

fixes #164984

In #122037 I created those fake bodies because we wanted to avoid all the checks I added here, and the intrinsics are always overridden anyway. Well, so we thought. If you're creative you can get at them, and it's possible there are cases that could be exposed on stable. Seems generally nicer to make it more robust

@rustbot

rustbot commented Nov 10, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 10, 2026
@rustbot

rustbot commented Nov 10, 2026

Copy link
Copy Markdown
Collaborator

r? @jieyouxu

rustbot has assigned @jieyouxu.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler, mir, mir-opt
  • compiler, mir, mir-opt expanded to 79 candidates
  • Random selection from 22 candidates

@oli-obk

oli-obk commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

cc @RalfJung

@rust-log-analyzer

This comment has been minimized.

@oli-obk
oli-obk force-pushed the lmao-unreachable-body-in-any-intrinsic branch from ab84fb6 to 8714e50 Compare November 11, 2026 10:36

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

Comment thread compiler/rustc_interface/src/passes.rs Outdated
sess.time("MIR_borrow_checking", || {
tcx.par_hir_body_owners(|def_id| {
if tcx.is_intrinsic_that_must_be_overridden(def_id) {
return;

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.

Suggested change
return;
return; // there is no MIR here to check

Comment thread compiler/rustc_mir_transform/src/lib.rs Outdated
@@ -334,7 +334,10 @@ fn mir_keys(tcx: TyCtxt<'_>, (): ()) -> FxIndexSet<LocalDefId> {

// Remove the fake bodies for `global_asm!`, since they're not useful

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 comment needs to be updated

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.

Where does this nice error come from? I expected this to ICE.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Checking upstream crates' mir goes through is_mir_available as MIR isn't encoded unless the function is inline or generic

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.

Sure -- but why does that not ICE? Hitting this error is always a bug, isn't it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

iirc there are some tests for weird combinations of cli flags that can cause this. But yea, ideally this would be an ICE

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.

This error can happen without a bug in rustc when a dependency is only available in rmeta form while doing codegen I believe. Or at least it can if dependency_formats is called after codegen (which we did in the past IIRC and have to do again to fix -Zno-link/-Zlink-only)

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.

That's still a bug in cargo if it ever happens, isn't it?

@rustbot rustbot assigned RalfJung and unassigned jieyouxu Nov 11, 2026
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 11, 2026
@rustbot

rustbot commented Nov 11, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@oli-obk
oli-obk force-pushed the lmao-unreachable-body-in-any-intrinsic branch from 8714e50 to 1e52c52 Compare November 12, 2026 11:20
@oli-obk

oli-obk commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 12, 2026
@oli-obk
oli-obk force-pushed the lmao-unreachable-body-in-any-intrinsic branch from 1e52c52 to b474d01 Compare November 12, 2026 15:06
@rustbot

This comment has been minimized.

Comment thread compiler/rustc_mir_transform/src/lib.rs Outdated
Comment on lines +335 to +336
// Remove the fake bodies for `global_asm!` and intrinsics without fallback bodies,
// since they're not useful to be emitted (`--emit=mir`) or encoded (in metadata).

@RalfJung RalfJung Nov 15, 2026 •

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.

I am confused... we don't even generate fake bodies any more, why do they have to be removed?

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

the issue is that set is all hir_body_owners, and at the HIR level we still treat intrinsics as if they have a body (and as you probably noticed, or at least left a comment in the code), this is very hard to avoid without being an ICE hazard.

so we have to limit the list of DefIds that have a body to those that actually have one at the MIR level. This is pretty robust, if you screw it up you hit the new span_bug

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.

If I understood correctly, that means the comment is quite confusing? (I did not write that comment.)
I posted suggestions for an improved comment, but I am not sure it is accurate.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oh I meant the comment you wrote in #135031

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.

Oh I see. I wonder what happens with that infinite loop... having a MIR body be an infinite loop is still better than UB, arguably.

Comment thread compiler/rustc_mir_transform/src/lib.rs Resolved
Comment thread compiler/rustc_mir_transform/src/lib.rs Resolved
Comment thread compiler/rustc_mir_transform/src/lib.rs Resolved
@oli-obk
oli-obk force-pushed the lmao-unreachable-body-in-any-intrinsic branch from b474d01 to a371ccb Compare November 23, 2026 08:35
@RalfJung

Copy link
Copy Markdown
Member

The problematic code in MIR-build still exists, right?

if self.tcx.intrinsic(self.def_id).is_some_and(|i| i.must_be_overridden)
|| self.tcx.is_sdylib_interface_build()
{
let source_info = self.source_info(rustc_span::DUMMY_SP);
self.cfg.terminate(block, source_info, TerminatorKind::Unreachable);
self.cfg.start_new_block().unit()

What do we need this for?

@rust-log-analyzer

This comment has been minimized.

@oli-obk
oli-obk force-pushed the lmao-unreachable-body-in-any-intrinsic branch from a371ccb to 7d1f0a9 Compare November 23, 2026 16:38
@rustbot

rustbot commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

clippy is developed in its own repository. If possible, consider making this change to rust-lang/rust-clippy instead.

cc @rust-lang/clippy

@rustbot rustbot added the T-clippy Relevant to the Clippy team. label Nov 23, 2026
@rustbot

rustbot commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

if self.tcx.intrinsic(self.def_id).is_some_and(|i| i.must_be_overridden)
|| self.tcx.is_sdylib_interface_build()
{
if self.tcx.is_sdylib_interface_build() {

@RalfJung RalfJung Nov 23, 2026 •

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.

This still looks suspicious, but orthogonal...
@bjorn3 @Bryanskiy why is it necessary and okay to just mark these is_sdylib_interface_build functions as unreachable, silently introducing UB if we accidentally invoke them anyway? That's a huge footgun.

View changes since the review

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.

I don't recall the specifics, but already when sdylibs were originally introduced I had my concerns about the design of them (see the various comments I left). And I'm not aware of any work that has happened on them since the initial PR beyond fixing a couple of bugs. As such I wouldn't be surprised if this is another way it is unsound (beyond nothing checking that the sdylib interface files actually match the dylib that is linked against and the lack of crate disambiguators causing conflicts for two sdylibs with the same crate name but distinct -Cmetadata)

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.

Okay, thanks for the context -- I left a note in the tracking issue.

@RalfJung

Copy link
Copy Markdown
Member

LGTM for the intrinsics, thanks!
@bors r+

@rust-bors

rust-bors Bot commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 7d1f0a9 has been approved by RalfJung

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 23, 2026

@RalfJung RalfJung Nov 23, 2026 •

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.

Can clippy not deal with bodyless intrinsics, or what is going on here?

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Well, previously it was generating MIR for everything. I could probably make it stop doing that, but it also isn't very important for clippy, as that is expected to be run on non-internal code

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.

I thought we are running clippy on the compiler as well... so probably one day also on the standard library.

But I suppose that's a problem we can solve when it actually arises.

rust-bors Bot pushed a commit that referenced this pull request Nov 24, 2026
Rollup of 11 pull requests

Successful merges:

 - #165769 (Fix invalid instantiation of fake intrinsic bodies)
 - #166374 (Undo accidental duplication checking for attributes on macro calls)
 - #166386 (Replace manual `AttributeParser` construction by `parse_limited_all`)
 - #162565 (Add `c_buffer_length` lint helper)
 - #166096 (llvm-wrapper: Pass more arguments to PreCodeGenCallback)
 - #166395 (std: fix positioned read/write on VxWorks)
 - #166404 (Change `ChildExt::kill_process_group` to be `&self`)
 - #166405 (Check for errors before encoding metadata)
 - #166412 (std: Fix locking test on GNU/Hurd)
 - #166413 (std: Fix symlink chmod test on GNU/Hurd)
 - #166421 (core: Stabilize `float_bits_const`)
rust-bors Bot pushed a commit that referenced this pull request Nov 24, 2026
Rollup merge of #165769 - oli-obk:lmao-unreachable-body-in-any-intrinsic, r=RalfJung

Fix invalid instantiation of fake intrinsic bodies

fixes #164984

In #122037 I created those fake bodies because we wanted to avoid all the checks I added here, and the intrinsics are always overridden anyway. Well, so we thought. If you're creative you can get at them, and it's possible there are cases that could be exposed on stable. Seems generally nicer to make it more robust
@rust-bors
rust-bors Bot merged commit ead0290 into rust-lang:main Nov 24, 2026
14 checks passed
@rustbot rustbot added this to the 1.102.0 milestone Nov 24, 2026
@rust-timer

Copy link
Copy Markdown

Note

This PR was benchmarked as part of triage of its containing rollup: triage URL.

Finished benchmarking commit (0de6426): comparison URL.

Overall result: ❌✅ regressions and improvements - please read:

Our benchmarks found a performance regression caused by this PR.
This might be an actual regression, but it can also be just noise.

Next Steps:

  • If the regression was expected or you think it can be justified,
    please write a comment with sufficient written justification, and add
    @rustbot label: +perf-regression-triaged to it, to mark the regression as triaged.
  • If you think that you know of a way to resolve the regression, try to create
    a new PR with a fix for the regression.
  • If you do not understand the regression or you think that it is just noise,
    you can ask the @rust-lang/wg-compiler-performance working group for help (members of this group
    were not yet notified of this PR).

@rustbot label: +perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.2% [0.1%, 0.3%] 7
Regressions ❌
(secondary)
0.3% [0.2%, 0.3%] 5
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.2% [-0.2%, -0.2%] 1
All ❌✅ (primary) 0.2% [0.1%, 0.3%] 7

Max RSS (memory usage)

Results (primary 1.8%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
1.8% [1.8%, 1.8%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 1.8% [1.8%, 1.8%] 1

Cycles

Results (secondary 12.8%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
12.8% [12.8%, 12.8%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Binary size

This perf run didn't have relevant results for this metric.

Artifact size: 406.38 MiB -> 405.72 MiB (-0.16%)

@rustbot rustbot added the perf-regression Performance regression. label Nov 24, 2026
@oli-obk

oli-obk commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

@rustbot label: +perf-regression-triaged

Somewhat expected small regressions due to extra query cache hits for the new checks

@rustbot rustbot added the perf-regression-triaged The performance regression has been triaged. label Nov 24, 2026
flip1995 pushed a commit to flip1995/rust-clippy that referenced this pull request Nov 26, 2026
Rollup of 11 pull requests

Successful merges:

 - rust-lang/rust#165769 (Fix invalid instantiation of fake intrinsic bodies)
 - rust-lang/rust#166374 (Undo accidental duplication checking for attributes on macro calls)
 - rust-lang/rust#166386 (Replace manual `AttributeParser` construction by `parse_limited_all`)
 - rust-lang/rust#162565 (Add `c_buffer_length` lint helper)
 - rust-lang/rust#166096 (llvm-wrapper: Pass more arguments to PreCodeGenCallback)
 - rust-lang/rust#166395 (std: fix positioned read/write on VxWorks)
 - rust-lang/rust#166404 (Change `ChildExt::kill_process_group` to be `&self`)
 - rust-lang/rust#166405 (Check for errors before encoding metadata)
 - rust-lang/rust#166412 (std: Fix locking test on GNU/Hurd)
 - rust-lang/rust#166413 (std: Fix symlink chmod test on GNU/Hurd)
 - rust-lang/rust#166421 (core: Stabilize `float_bits_const`)
fs-rachel added a commit to ferrocene/ferrocene that referenced this pull request Nov 27, 2026
This is necessary due to a rearrangement of the `has_ffi_unwind_calls` by
rust-lang/rust#165769 . This query now early-exits
in panic=abort configurations, which changes the failing query stack (and
thus the printed error message) in some tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

perf-regression Performance regression. perf-regression-triaged The performance regression has been triaged. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UB with use std::intrinsics::abort_immediate as main;

7 participants