Skip to contentSkip to conversation

rust: compiler_builtins: Fix silent trap in prohibited intrinsics

PATCH Superseded FUJITA Tomonori 2 participants 3 messages Oct 3 Rust

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 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/intrinsics.rs:56:1:
__udivti3 called: u128 should not be used kernel BUG at rust/helpers/bug.c:7!

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

---
v2:
- Move the stubs to the kernel crate and use panic!() there, as Gary suggested.
- v1: https://lore.kernel.org/rust-for-linux/[email protected]/

---
 rust/compiler_builtins.rs                     | 93 +------------------
 .../intrinsics.rs}                            | 26 ++----
 rust/kernel/lib.rs                            |  2 +
 3 files changed, 12 insertions(+), 109 deletions(-)
 copy rust/{compiler_builtins.rs => kernel/intrinsics.rs} (74%)
rust/compiler_builtins.rs +3 -90
index fc6b54636dd5..87c19c22b107 100644
@@ -6,15 +6,9 @@
 //! Since we do not need the vast majority of them, we avoid the dependency
 //! by providing this file.
 //!
-//! 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
-//! in. We will work with upstream [`core`] to provide feature flags to disable
-//! the parts we do not need. For the moment, we define them to [`panic!`] at
-//! 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.
+//! This crate is empty, but `rustc` still needs a `#![compiler_builtins]`
+//! crate for every crate it builds. The stubs for the builtins that should
+//! not be used are in the `kernel` crate (`rust/kernel/intrinsics.rs`).
 //!
 //! [`compiler_builtins`]: https://github.com/rust-lang/compiler-builtins
 //! [`compiler-rt`]: https://compiler-rt.llvm.org/
@@ -24,84 +18,3 @@
 #![compiler_builtins]
 #![no_builtins]
 #![no_std]
-
-macro_rules! define_panicking_intrinsics(
-    ($reason: tt, { $($ident: ident, )* }) => {
-        $(
-            #[doc(hidden)]
-            #[export_name = concat!("__rust", stringify!($ident))]
-            pub extern "C" fn $ident() {
-                panic!($reason);
-            }
-        )*
-    }
-);
-
-define_panicking_intrinsics!("`f32` should not be used", {
-    __addsf3,
-    __eqsf2,
-    __extendsfdf2,
-    __gesf2,
-    __lesf2,
-    __ltsf2,
-    __mulsf3,
-    __nesf2,
-    __truncdfsf2,
-    __unordsf2,
-});
-
-define_panicking_intrinsics!("`f64` should not be used", {
-    __adddf3,
-    __eqdf2,
-    __ledf2,
-    __ltdf2,
-    __muldf3,
-    __unorddf2,
-});
-
-define_panicking_intrinsics!("`i128` should not be used", {
-    __ashrti3,
-    __muloti4,
-    __multi3,
-});
-
-define_panicking_intrinsics!("`u128` should not be used", {
-    __ashlti3,
-    __lshrti3,
-    __udivmodti4,
-    __udivti3,
-    __umodti3,
-});
-
-#[cfg(target_arch = "arm")]
-define_panicking_intrinsics!("`f32` should not be used", {
-    __aeabi_fadd,
-    __aeabi_fmul,
-    __aeabi_fcmpeq,
-    __aeabi_fcmple,
-    __aeabi_fcmplt,
-    __aeabi_fcmpun,
-});
-
-#[cfg(target_arch = "arm")]
-define_panicking_intrinsics!("`f64` should not be used", {
-    __aeabi_dadd,
-    __aeabi_dmul,
-    __aeabi_dcmple,
-    __aeabi_dcmplt,
-    __aeabi_dcmpun,
-});
-
-#[cfg(target_arch = "arm")]
-define_panicking_intrinsics!("`u64` division/modulo should not be used", {
-    __aeabi_uldivmod,
-});
-
-#[cfg(target_arch = "powerpc")]
-define_panicking_intrinsics!("`u64` division/modulo should not be used", {
-    __udivdi3,
-    __umoddi3,
-});
-
-// NOTE: if you are adding a new intrinsic here, you should also add it to
-// `redirect-intrinsics` in `rust/Makefile`.
rust/kernel/intrinsics.rs renamed from rust/compiler_builtins.rs +7 -19
similarity index 74%
copy from rust/compiler_builtins.rs
copy to rust/kernel/intrinsics.rs
index fc6b54636dd5..2df9841ea781 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.
+//! Stubs for builtins that should not be used.
 //!
 //! 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..b19d07f0c6f7 100644
@@ -88,6 +88,8 @@
 pub mod init;
 pub mod interop;
 pub mod interrupt;
+#[cfg(not(testlib))]
+mod intrinsics;
 pub mod io;
 pub mod ioctl;
 pub mod iommu;

base-commit: 2dee3d3adcadcc57d62ba6608cd02f50b0c10264

Signature · 1 line
2.43.0
reviewer
#1 lore raw
Quoted 47 lines · FUJITA Tomonori

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/intrinsics.rs:56:1:
__udivti3 called: u128 should not be used kernel BUG at rust/helpers/bug.c:7!

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

---
v2:
- Move the stubs to the kernel crate and use panic!() there, as Gary suggested.
- v1: https://lore.kernel.org/rust-for-linux/[email protected]/

---
 rust/compiler_builtins.rs                     | 93 +------------------
 .../intrinsics.rs}                            | 26 ++----
 rust/kernel/lib.rs                            |  2 +
 3 files changed, 12 insertions(+), 109 deletions(-)
 copy rust/{compiler_builtins.rs => kernel/intrinsics.rs} (74%)
rust/compiler_builtins.rs +0 -0
index fc6b54636dd5..87c19c22b107 100644

rust/compiler_builtins.rs line 7

Quoted 2 lines
rust/compiler_builtins.rs +0 -0
@@ -6,15 +6,9 @@
 //! Since we do not need the vast majority of them, we avoid the dependency
 //! by providing this file.

The above paragraph needs some rewording too.

Quoted 157 lines
rust/compiler_builtins.rs +3 -90
 //!
-//! 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
-//! in. We will work with upstream [`core`] to provide feature flags to disable
-//! the parts we do not need. For the moment, we define them to [`panic!`] at
-//! 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.
+//! This crate is empty, but `rustc` still needs a `#![compiler_builtins]`
+//! crate for every crate it builds. The stubs for the builtins that should
+//! not be used are in the `kernel` crate (`rust/kernel/intrinsics.rs`).
 //!
 //! [`compiler_builtins`]: https://github.com/rust-lang/compiler-builtins
 //! [`compiler-rt`]: https://compiler-rt.llvm.org/
@@ -24,84 +18,3 @@
 #![compiler_builtins]
 #![no_builtins]
 #![no_std]
-
-macro_rules! define_panicking_intrinsics(
-    ($reason: tt, { $($ident: ident, )* }) => {
-        $(
-            #[doc(hidden)]
-            #[export_name = concat!("__rust", stringify!($ident))]
-            pub extern "C" fn $ident() {
-                panic!($reason);
-            }
-        )*
-    }
-);
-
-define_panicking_intrinsics!("`f32` should not be used", {
-    __addsf3,
-    __eqsf2,
-    __extendsfdf2,
-    __gesf2,
-    __lesf2,
-    __ltsf2,
-    __mulsf3,
-    __nesf2,
-    __truncdfsf2,
-    __unordsf2,
-});
-
-define_panicking_intrinsics!("`f64` should not be used", {
-    __adddf3,
-    __eqdf2,
-    __ledf2,
-    __ltdf2,
-    __muldf3,
-    __unorddf2,
-});
-
-define_panicking_intrinsics!("`i128` should not be used", {
-    __ashrti3,
-    __muloti4,
-    __multi3,
-});
-
-define_panicking_intrinsics!("`u128` should not be used", {
-    __ashlti3,
-    __lshrti3,
-    __udivmodti4,
-    __udivti3,
-    __umodti3,
-});
-
-#[cfg(target_arch = "arm")]
-define_panicking_intrinsics!("`f32` should not be used", {
-    __aeabi_fadd,
-    __aeabi_fmul,
-    __aeabi_fcmpeq,
-    __aeabi_fcmple,
-    __aeabi_fcmplt,
-    __aeabi_fcmpun,
-});
-
-#[cfg(target_arch = "arm")]
-define_panicking_intrinsics!("`f64` should not be used", {
-    __aeabi_dadd,
-    __aeabi_dmul,
-    __aeabi_dcmple,
-    __aeabi_dcmplt,
-    __aeabi_dcmpun,
-});
-
-#[cfg(target_arch = "arm")]
-define_panicking_intrinsics!("`u64` division/modulo should not be used", {
-    __aeabi_uldivmod,
-});
-
-#[cfg(target_arch = "powerpc")]
-define_panicking_intrinsics!("`u64` division/modulo should not be used", {
-    __udivdi3,
-    __umoddi3,
-});
-
-// NOTE: if you are adding a new intrinsic here, you should also add it to
-// `redirect-intrinsics` in `rust/Makefile`.
rust/kernel/intrinsics.rs renamed from rust/compiler_builtins.rs +7 -19
similarity index 74%
copy from rust/compiler_builtins.rs
copy to rust/kernel/intrinsics.rs
index fc6b54636dd5..2df9841ea781 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.
+//! Stubs for builtins that should not be used.
 //!
 //! 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 +0 -0
index 7225abc64084..b19d07f0c6f7 100644

rust/kernel/lib.rs line 92

Quoted 5 lines
rust/kernel/lib.rs +2 -0
@@ -88,6 +88,8 @@
 pub mod init;
 pub mod interop;
 pub mod interrupt;
+#[cfg(not(testlib))]
+mod intrinsics;

We might want to explicit that this for redirected intrinsics from libcore?

Best,
Gary

Quoted 5 lines
rust/kernel/lib.rs +0 -0
 pub mod io;
 pub mod ioctl;
 pub mod iommu;

base-commit: 2dee3d3adcadcc57d62ba6608cd02f50b0c10264

#2 lore raw
Quoted 2 lines · On Sat, 03 Oct 2026 14:31:22 +0100 "Gary Guo" <[email protected]> wrote:
Quoted 1 line · FUJITA Tomonori

From: FUJITA Tomonori <[email protected]>

[...]

Quoted 7 lines
Quoted 7 lines
---
 rust/compiler_builtins.rs                     | 93 +------------------
 .../intrinsics.rs}                            | 26 ++----
 rust/kernel/lib.rs                            |  2 +
 3 files changed, 12 insertions(+), 109 deletions(-)
 copy rust/{compiler_builtins.rs => kernel/intrinsics.rs} (74%)
Quoted 9 lines

rust/compiler_builtins.rs line 7

Quoted 7 lines
rust/compiler_builtins.rs +0 -0
index fc6b54636dd5..87c19c22b107 100644
@@ -6,15 +6,9 @@
 //! Since we do not need the vast majority of them, we avoid the dependency
 //! by providing this file.

The above paragraph needs some rewording too.

How about the following?

//! 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. //! 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).

Quoted 5 lines
Quoted 5 lines
rust/kernel/lib.rs +0 -0
index 7225abc64084..b19d07f0c6f7 100644
@@ -88,6 +88,8 @@
Quoted 8 lines

rust/kernel/lib.rs line 92

Quoted 5 lines
rust/kernel/lib.rs +2 -0
 pub mod init;
 pub mod interop;
 pub mod interrupt;
+#[cfg(not(testlib))]
+mod intrinsics;

We might want to explicit that this for redirected intrinsics from libcore?

How about renaming it to core_builtins.rs? core_intrinsics is also an option, but I think that it could be confused with the core::intrinsics module.