rust: compiler_builtins: Fix silent trap in prohibited intrinsics
PATCH v5 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 converts calls to panic!() in a #![compiler_builtins] crate into core::intrinsics::abort(), i.e. a trap that prints no message [1][2]. In addition, all the stubs have the same code, so LLVM merges them into one function.
Move the stubs to a new small crate, core_builtins, 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/core_builtins.rs:64:1:
__udivti3 called: u128 should not be used kernel BUG at rust/helpers/bug.c:7!
Weaken the stubs in core_builtins.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. Remove the weakening of compiler_builtins.o, as it has no symbols now.
---
v5:
- Move the stubs to a new crate, core_builtins, instead of the kernel crate (Miguel)
- Remove the weakening of compiler_builtins.o (Miguel)
- Explain why the stubs are weakened in core_builtins.rs (Miguel)
- Reword why the stubs are not in compiler_builtins and add links (Miguel)
v4: https://lore.kernel.org/rust-for-linux/[email protected]/
- 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 | 8 +-
rust/compiler_builtins.rs | 98 +------------------
...{compiler_builtins.rs => core_builtins.rs} | 28 +++---
3 files changed, 23 insertions(+), 111 deletions(-)
copy rust/{compiler_builtins.rs => core_builtins.rs} (73%)rust/Makefile +6 -2
@@ -6,7 +6,7 @@ rustdoc_output := $(objtree)/Documentation/output/rust/rustdoc # Clean generated host directory clean-files := host/ -obj-$(CONFIG_RUST) += core.o compiler_builtins.o ffi.o +obj-$(CONFIG_RUST) += core.o compiler_builtins.o core_builtins.o ffi.o always-$(CONFIG_RUST) += exports_core_generated.h obj-$(CONFIG_RUST) += zerocopy.o
@@ -745,10 +745,14 @@ endif KCOV_INSTRUMENT_core.o := n $(obj)/compiler_builtins.o: private skip_gendwarfksyms = 1 -$(obj)/compiler_builtins.o: private rustc_objcopy = -w -W '__*' $(obj)/compiler_builtins.o: $(src)/compiler_builtins.rs $(obj)/core.o FORCE +$(call if_changed_rule,rustc_library) +$(obj)/core_builtins.o: private skip_gendwarfksyms = 1 +$(obj)/core_builtins.o: private rustc_objcopy = -w -W '__rust__*' +$(obj)/core_builtins.o: $(src)/core_builtins.rs $(obj)/compiler_builtins.o FORCE + +$(call if_changed_rule,rustc_library) + $(obj)/zerocopy.o: private skip_clippy = 1 $(obj)/zerocopy.o: private skip_gendwarfksyms = 1 $(obj)/zerocopy.o: private rustc_target_envs := $(zerocopy-envs)
rust/compiler_builtins.rs +5 -93
107 lines not on this page · raw message
rust/core_builtins.rs renamed from rust/compiler_builtins.rs +12 -16
@@ -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,25 @@//! 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. +//! 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 and +//! https://github.com/rust-lang/rust/issues/165399. //! -//! [`compiler_builtins`]: https://github.com/rust-lang/compiler-builtins -//! [`compiler-rt`]: https://compiler-rt.llvm.org/ +//! The stubs are weakened in `rust/Makefile`. Otherwise, when `objtool` checks +//! `vmlinux.o` (e.g. with `CONFIG_X86_KERNEL_IBT`), it finds that the stubs do +//! not return and warns that they are not marked as noreturn. `objtool` does +//! not do this check for weak functions. -#![allow(internal_features)] -#![feature(compiler_builtins)] -#![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); + extern "C" fn $ident() { + panic!(concat!(stringify!($ident), " called: ", $reason)); } )* }
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
Signature · 1 line
2.43.0