Skip to contentSkip to conversation

rust: compiler_builtins: Fix silent trap in prohibited intrinsics

PATCH Superseded FUJITA Tomonori 3 participants 10 messages Oct 9 – Nov 4 Rust

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 rust: compiler_builtins: Fix silent trap in prohibited intrinsics

From: FUJITA Tomonori <[email protected]>

When core calls one of the stubs for builtins that should not be used, the stub runs a trap instruction that prints no message, and the Oops shows a wrong name. For example, when __rust__udivti3() is called on x86_64, the Oops shows:

  Oops: invalid opcode: 0000 [#1] SMP
  RIP: 0010:__rust__adddf3+0x0/0x10

The stubs call panic!(), but since Rust 1.79, rustc does not allow code in a #![compiler_builtins] crate to link to functions in other crates, and it silently turns such calls that do not return into a trap [1]. In addition, all the stubs have the same code, so LLVM merges them into one function.

Move the stubs to the kernel crate, where panic!() works as usual. Add the name of each stub to the panic message, so the stubs are no longer merged:

rust_kernel: panicked at rust/kernel/core_builtins.rs:56:1:
__udivti3 called: u128 should not be used kernel BUG at rust/helpers/bug.c:7!

Weaken the stubs in kernel.o, as was done in compiler_builtins.o. Otherwise objtool treats them as noreturn functions and warns.

compiler_builtins.rs is now empty, but the crate is still needed because rustc requires a #![compiler_builtins] crate.

---
v4:
- Weaken the stubs in kernel.o to fix an objtool warning
https://lore.kernel.org/oe-kbuild-all/[email protected]/
v3: https://lore.kernel.org/rust-for-linux/[email protected]/
- Rename rust/kernel/intrinsics.rs to rust/kernel/core_builtins.rs (Gary)
- Reword the doc of compiler_builtins.rs (Gary)
v2: https://lore.kernel.org/rust-for-linux/[email protected]/
- Move the stubs to the kernel crate and use panic!() there (Gary)
v1: https://lore.kernel.org/rust-for-linux/[email protected]/

---
 rust/Makefile                                 |  1 +
 rust/compiler_builtins.rs                     | 97 +------------------
 .../core_builtins.rs}                         | 26 ++---
 rust/kernel/lib.rs                            |  2 +
 4 files changed, 14 insertions(+), 112 deletions(-)
 copy rust/{compiler_builtins.rs => kernel/core_builtins.rs} (74%)
rust/Makefile +1 -0
index 320ab6bb36c4..80bf698353a0 100644
@@ -789,6 +789,7 @@ $(obj)/uapi.o: $(src)/uapi/lib.rs \
     $(obj)/uapi/uapi_generated.rs FORCE
 	+$(call if_changed_rule,rustc_library)
 
+$(obj)/kernel.o: private rustc_objcopy = -w -W '__rust__*'
 $(obj)/kernel.o: private rustc_target_flags = --extern ffi --extern pin_init \
     --extern build_error --extern macros --extern bindings --extern uapi \
     --extern zerocopy --extern zerocopy_derive
rust/compiler_builtins.rs +4 -93

106 lines not on this page · raw message

rust/kernel/core_builtins.rs renamed from rust/compiler_builtins.rs +7 -19
similarity index 74%
copy from rust/compiler_builtins.rs
copy to rust/kernel/core_builtins.rs
index fc6b54636dd5..b147475156b4 100644
@@ -1,10 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
 
-//! Our own `compiler_builtins`.
-//!
-//! Rust provides [`compiler_builtins`] as a port of LLVM's [`compiler-rt`].
-//! Since we do not need the vast majority of them, we avoid the dependency
-//! by providing this file.
+//! Builtins that `core` needs.
 //!
 //! At the moment, some builtins are required that should not be. For instance,
 //! [`core`] has 128-bit integers functionality which we should not be compiling
@@ -13,25 +9,17 @@
 //! runtime for simplicity to catch mistakes, instead of performing surgery
 //! on `core.o`.
 //!
-//! In any case, all these symbols are weakened to ensure we do not override
-//! those that may be provided by the rest of the kernel.
-//!
-//! [`compiler_builtins`]: https://github.com/rust-lang/compiler-builtins
-//! [`compiler-rt`]: https://compiler-rt.llvm.org/
-
-#![allow(internal_features)]
-#![feature(compiler_builtins)]
-#![compiler_builtins]
-#![no_builtins]
-#![no_std]
+//! The stubs are here and not in `compiler_builtins`, because code in a
+//! `#![compiler_builtins]` crate cannot link to functions in other crates.
+//! There, `rustc` turns a call to [`panic!`] into a trap that prints no
+//! message.
 
 macro_rules! define_panicking_intrinsics(
     ($reason: tt, { $($ident: ident, )* }) => {
         $(
-            #[doc(hidden)]
             #[export_name = concat!("__rust", stringify!($ident))]
-            pub extern "C" fn $ident() {
-                panic!($reason);
+            extern "C" fn $ident() {
+                panic!(concat!(stringify!($ident), " called: ", $reason));
             }
         )*
     }
rust/kernel/lib.rs +2 -0
index 7225abc64084..b4fdba2a3eb0 100644
@@ -57,6 +57,8 @@
 pub mod clk;
 #[cfg(CONFIG_CONFIGFS_FS)]
 pub mod configfs;
+#[cfg(not(testlib))]
+mod core_builtins;
 pub mod cpu;
 #[cfg(CONFIG_CPU_FREQ)]
 pub mod cpufreq;

base-commit: 2dee3d3adcadcc57d62ba6608cd02f50b0c10264

Signature · 1 line
2.43.0
#1 lore raw
Quoted 27 lines · On Sat, 10 Oct 2026 07:46:26 +0900 FUJITA Tomonori <[email protected]> wrote:

From: FUJITA Tomonori <[email protected]>

When core calls one of the stubs for builtins that should not be used, the stub runs a trap instruction that prints no message, and the Oops shows a wrong name. For example, when __rust__udivti3() is called on x86_64, the Oops shows:

  Oops: invalid opcode: 0000 [#1] SMP
  RIP: 0010:__rust__adddf3+0x0/0x10

The stubs call panic!(), but since Rust 1.79, rustc does not allow code in a #![compiler_builtins] crate to link to functions in other crates, and it silently turns such calls that do not return into a trap [1]. In addition, all the stubs have the same code, so LLVM merges them into one function.

Move the stubs to the kernel crate, where panic!() works as usual. Add the name of each stub to the panic message, so the stubs are no longer merged:

rust_kernel: panicked at rust/kernel/core_builtins.rs:56:1:
__udivti3 called: u128 should not be used kernel BUG at rust/helpers/bug.c:7!

Weaken the stubs in kernel.o, as was done in compiler_builtins.o. Otherwise objtool treats them as noreturn functions and warns.

Quoted 6 lines

compiler_builtins.rs is now empty, but the crate is still needed because rustc requires a #![compiler_builtins] crate.

Gentle ping. The patch still applies cleanly on top of v7.4-rc1.

#2 lore raw
In reply to FUJITA Tomonori
Quoted 2 lines · FUJITA Tomonori

+$(obj)/kernel.o: private rustc_objcopy = -w -W '__rust__*'

Hmm... I am a bit worried about doing this here, in kernel, because it is a big crate with many other things around. The pattern shouldn't match something else, though.

By the way, since this is here (in kernel), do we need then the original one in compiler_builtins?

Actually, taking a look at that, I don't think we needed it (for the original reason, at least) after commit

cb7d9defdafb ("rust: compiler_builtins: make stubs non-global")

Gary: did you keep it for another reason? I asked an LLM, and apparently the only reason it can think of is about an unrelated
side-effect: objtool assumes weak symbols return, so if we were to remove it, we would get warnings in IBT/LTO builds because then it can see how core uses them doesn't make sense if they are noreturn.

And now, here, in kernel, we don't seem to need it either for the original reason, except for the objtool, but also for yet another
reason: if they weren't weak, they would be exported (EXPORT_SYMBOL_RUST_GPL). Which again points to the fact that we should probably clarify all this with a good comment or that we should do this sort of thing in another way, e.g. in a separate, small crate.

So this paragraph we have (please see below) was outdated anyway already, no? i.e. this one:

Review

Quoted 2 lines
(quoted diff) +0 -2
-//! In any case, all these symbols are weakened to ensure we do not override
-//! those that may be provided by the rest of the kernel.

So we should probably move this weakening explanation somewhere else if we still need to do it (e.g. to the top of the new -W or to the new file).

And if we didn't actually need to weaken the symbols after cb7d9defdafb, and it was just about objtool, then we could consider doing something else, e.g. an exception in objtool for this set of symbols. (Of course, that can be a different patch, but we should understand that part above for this one anyway).

rust/compiler_builtins.rs line 9

Quoted 4 lines
rust/compiler_builtins.rs +4 -0
+//! Since we do not need the vast majority of them, we avoid the dependency.
+//! But `rustc` still needs a `#![compiler_builtins]` crate for every crate it
+//! builds, so this crate is provided and left empty. The few builtins that
+//! `core` needs are in the `kernel` crate (`rust/kernel/core_builtins.rs`).

I was going to say we should say "why" here, e.g.

    //! ....... so that `panic!` works as usual, without getting converted into
    //! `core::intrinsics::abort`, i.e. a trap, by the compiler
    //! (https://github.com/rust-lang/rust/pull/122580).

but part of this is in the other file -- so maybe here we can just add:

    //!  -- please see the details there.

(But please see below)

rust/kernel/core_builtins.rs line 15

Quoted 4 lines
rust/kernel/core_builtins.rs renamed from rust/compiler_builtins.rs +4 -0
+//! The stubs are here and not in `compiler_builtins`, because code in a
+//! `#![compiler_builtins]` crate cannot link to functions in other crates.
+//! There, `rustc` turns a call to [`panic!`] into a trap that prints no
+//! message.

I think this explanation may be helped by mentioning the PR link, and perhaps shifting it to say it is a decision by the compiler, e.g.

    //! The stubs are here and not in `compiler_builtins`, because the compiler
    //! would otherwise decide to convert calls to [`panic!`] into
`core::intrinsics::abort`,
    //! i.e. a trap that would print no message -- please see
    //! https://github.com/rust-lang/rust/pull/122580.

I mention it because the compiler's behavior is quite surprising, to be honest (even if it is only within this very special crate), and I see in the compiler PR that others were surprised too. In addition, the compiler could have decided to do something else, e.g. failing the build, or perhaps supporting particular cases somehow (like with EIIs). In other words, it is not that the compiler is forced to take a particular action or that the system in general must behave like that, which is how the original paragraph reads to me.

I hope that makes some sense...

Cheers,
Miguel

#3 lore raw
Quoted 4 lines · Miguel Ojeda

I mention it because the compiler's behavior is quite surprising, to be honest (even if it is only within this very special crate), and I see in the compiler PR that others were surprised too. In addition,

There is now an issue for it:

https://github.com/rust-lang/rust/issues/165399

I linked it from our usual rustc list.

Cheers,
Miguel

#4 lore raw
In reply to Miguel Ojeda
Quoted 7 lines · On Tue, 3 Nov 2026 11:54:26 +0100 Miguel Ojeda…
Quoted 2 lines · FUJITA Tomonori

+$(obj)/kernel.o: private rustc_objcopy = -w -W '__rust__*'

Hmm... I am a bit worried about doing this here, in kernel, because it is a big crate with many other things around. The pattern shouldn't match something else, though.

Agreed. In v5, I will move the stubs to a separate, small crate, as you suggested below.

Quoted 2 lines

By the way, since this is here (in kernel), do we need then the original one in compiler_builtins?

No. With this patch, compiler_builtins.rs is empty, so I will drop it.

Quoted 4 lines

Actually, taking a look at that, I don't think we needed it (for the original reason, at least) after commit

cb7d9defdafb ("rust: compiler_builtins: make stubs non-global")

Right, the stubs have the __rust prefix, so that reason is gone.

Quoted 6 lines

And now, here, in kernel, we don't seem to need it either for the original reason, except for the objtool, but also for yet another
reason: if they weren't weak, they would be exported (EXPORT_SYMBOL_RUST_GPL). Which again points to the fact that we should probably clarify all this with a good comment or that we should do this sort of thing in another way, e.g. in a separate, small crate.

Right. The new crate is not in exports.c, so the stubs are not exported. Then the weakening is needed only for objtool. I tried the new crate. Without the weakening, I got an objtool warning with CONFIG_X86_KERNEL_IBT=y:

vmlinux.o: warning: objtool: _RNvNtNtCshy5ABSPeoCG_4core3fmt3num8exp_u128+0x381: __rust__udivti3() missing __noreturn in .c/.h or NORETURN() in noreturns.h

Quoted 9 lines

So this paragraph we have (please see below) was outdated anyway already, no? i.e. this one:

Review

Quoted 2 lines
(quoted diff) +0 -2
-//! In any case, all these symbols are weakened to ensure we do not override
-//! those that may be provided by the rest of the kernel.

So we should probably move this weakening explanation somewhere else if we still need to do it (e.g. to the top of the new -W or to the new file).

Yes. I will explain the weakening (for objtool) in the new file.

Quoted 5 lines

And if we didn't actually need to weaken the symbols after cb7d9defdafb, and it was just about objtool, then we could consider doing something else, e.g. an exception in objtool for this set of symbols. (Of course, that can be a different patch, but we should understand that part above for this one anyway).

Yeah, Josh's objtool series would solve this, I guess.

Quoted 17 lines

rust/compiler_builtins.rs line 9

Quoted 4 lines
rust/compiler_builtins.rs +4 -0
+//! Since we do not need the vast majority of them, we avoid the dependency.
+//! But `rustc` still needs a `#![compiler_builtins]` crate for every crate it
+//! builds, so this crate is provided and left empty. The few builtins that
+//! `core` needs are in the `kernel` crate (`rust/kernel/core_builtins.rs`).

I was going to say we should say "why" here, e.g.

    //! ....... so that `panic!` works as usual, without getting converted into
    //! `core::intrinsics::abort`, i.e. a trap, by the compiler
    //! (https://github.com/rust-lang/rust/pull/122580).

but part of this is in the other file -- so maybe here we can just add:

    //!  -- please see the details there.

(But please see below)

Quoted 7 lines

rust/kernel/core_builtins.rs line 15

Quoted 4 lines
rust/kernel/core_builtins.rs renamed from rust/compiler_builtins.rs +4 -0
+//! The stubs are here and not in `compiler_builtins`, because code in a
+//! `#![compiler_builtins]` crate cannot link to functions in other crates.
+//! There, `rustc` turns a call to [`panic!`] into a trap that prints no
+//! message.

I think this explanation may be helped by mentioning the PR link, and perhaps shifting it to say it is a decision by the compiler, e.g.

Makes sense. I will update the comments as you suggested, with links to the PR and to the new issue (rust-lang/rust#165399).

reviewer
#5 lore raw
Quoted 11 lines · FUJITA Tomonori
Quoted 7 lines · On Tue, 3 Nov 2026 11:54:26 +0100 Miguel Ojeda…
Quoted 2 lines · FUJITA Tomonori

+$(obj)/kernel.o: private rustc_objcopy = -w -W '__rust__*'

Hmm... I am a bit worried about doing this here, in kernel, because it is a big crate with many other things around. The pattern shouldn't match something else, though.

Quoted 7 lines

Agreed. In v5, I will move the stubs to a separate, small crate, as you suggested below.

Quoted 2 lines

By the way, since this is here (in kernel), do we need then the original one in compiler_builtins?

No. With this patch, compiler_builtins.rs is empty, so I will drop it.

compiler_builtins as a crate must exist, so you cannot just drop it.

Quoted 15 lines
Quoted 5 lines

Actually, taking a look at that, I don't think we needed it (for the original reason, at least) after commit

cb7d9defdafb ("rust: compiler_builtins: make stubs non-global")

Right, the stubs have the __rust prefix, so that reason is gone.

Quoted 6 lines

And now, here, in kernel, we don't seem to need it either for the original reason, except for the objtool, but also for yet another
reason: if they weren't weak, they would be exported (EXPORT_SYMBOL_RUST_GPL). Which again points to the fact that we should probably clarify all this with a good comment or that we should do this sort of thing in another way, e.g. in a separate, small crate.

Quoted 6 lines

Right. The new crate is not in exports.c, so the stubs are not exported. Then the weakening is needed only for objtool. I tried the new crate. Without the weakening, I got an objtool warning with CONFIG_X86_KERNEL_IBT=y:

vmlinux.o: warning: objtool: _RNvNtNtCshy5ABSPeoCG_4core3fmt3num8exp_u128+0x381: __rust__udivti3() missing __noreturn in .c/.h or NORETURN() in noreturns.h

I think that can be fixed by adding out-of-band annotation for these functions in objtool? That sounds more preferrable then weakening the symbol.

Best,
Gary

#6 lore raw
Quoted 6 lines · On Wed, 04 Nov 2026 13:42:27 +0800 "Gary Guo" <[email protected]> wrote:
Quoted 4 lines
Quoted 2 lines

By the way, since this is here (in kernel), do we need then the original one in compiler_builtins?

No. With this patch, compiler_builtins.rs is empty, so I will drop it.

compiler_builtins as a crate must exist, so you cannot just drop it.

Sorry, by "it" I meant the -w -W '__*' line for compiler_builtins.o, not the crate. The crate stays as an empty crate.

Quoted 12 lines
Quoted 12 lines
Quoted 6 lines

And now, here, in kernel, we don't seem to need it either for the original reason, except for the objtool, but also for yet another
reason: if they weren't weak, they would be exported (EXPORT_SYMBOL_RUST_GPL). Which again points to the fact that we should probably clarify all this with a good comment or that we should do this sort of thing in another way, e.g. in a separate, small crate.

Right. The new crate is not in exports.c, so the stubs are not exported. Then the weakening is needed only for objtool. I tried the new crate. Without the weakening, I got an objtool warning with CONFIG_X86_KERNEL_IBT=y:

Quoted 4 lines
Quoted 1 line

vmlinux.o: warning: objtool: _RNvNtNtCshy5ABSPeoCG_4core3fmt3num8exp_u128+0x381: __rust__udivti3() missing __noreturn in .c/.h or NORETURN() in noreturns.h

I think that can be fixed by adding out-of-band annotation for these functions in objtool? That sounds more preferrable then weakening the symbol.

Josh's objtool series adds an annotation for these stubs. v5 keeps the weakening.

#7 lore raw
In reply to Gary Guo
Quoted 3 lines · Gary Guo

I think that can be fixed by adding out-of-band annotation for these functions in objtool? That sounds more preferrable then weakening the symbol.

Yeah, but just to clarify for future reference: this one requires something else than the existing Rust symbol patterns we have in objtool -- that is why we need either the weakening or the new annotation Tomo mentions.

i.e. core has code after the calls, so what we need is to tell objtool to treat the intrinsics as returning (which is what making them weak does, plus avoiding the export for kernel), rather than as noreturn (as our usual patterns do), i.e. "the other way around", so to speak.

Cc'ing Josh so that he is aware of this thread:

https://lore.kernel.org/rust-for-linux/CANiq72nxjrWpBjwzgdWncKECKwzj6zfjS3XC=da62xr8wiSxEQ@mail.gmail.com/

Cheers,
Miguel

reviewer
#8 lore raw
In reply to Miguel Ojeda
Quoted 14 lines · Miguel Ojeda
Quoted 2 lines · FUJITA Tomonori

+$(obj)/kernel.o: private rustc_objcopy = -w -W '__rust__*'

Hmm... I am a bit worried about doing this here, in kernel, because it is a big crate with many other things around. The pattern shouldn't match something else, though.

By the way, since this is here (in kernel), do we need then the original one in compiler_builtins?

Actually, taking a look at that, I don't think we needed it (for the original reason, at least) after commit

Quoted 7 lines

cb7d9defdafb ("rust: compiler_builtins: make stubs non-global")

Gary: did you keep it for another reason? I asked an LLM, and apparently the only reason it can think of is about an unrelated
side-effect: objtool assumes weak symbols return, so if we were to remove it, we would get warnings in IBT/LTO builds because then it can see how core uses them doesn't make sense if they are noreturn.

It took me a while to understand what "it" means here, I assume you mean

    $(obj)/compiler_builtins.o: private rustc_objcopy = -w -W '__*'

I don't think that's needed anymore. I probably just didn't think of removing that line when making the change.

Best,
Gary

#9 lore raw
Quoted 7 lines · Gary Guo

It took me a while to understand what "it" means here, I assume you mean

    $(obj)/compiler_builtins.o: private rustc_objcopy = -w -W '__*'

I don't think that's needed anymore. I probably just didn't think of removing that line when making the change.

Yeah, I was referring to that one -- sorry, I could have been clearer.

Cheers,
Miguel