Skip to contentSkip to conversation

rust: make compiler-builtin stubs non-global

PATCH Superseded Gary Guo 3 participants 4 messages Dec 4, 2022 – Dec 5, 2022 Rust

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Gary Guo

author reviewer
#0 lore raw

PATCH v2 1/1 rust: make compiler-builtin stubs non-global

Currently we define a number of stubs for compiler-builtin intrinsics that compiled libcore generates. The defined stubs are weak so they will not conflict with genuine implementation of these intrinsics, but their effect is global and will cause non-libcore code that accidently generate these intrinsics calls compile and bug on runtime.

Instead of defining a stub that can affect all code, this patch uses objcopy's --redefine-sym flag to redirect these calls (from libcore only) to a prefixed version (e.g. redirect __multi3 to __rust_multi3), so we can define panciking stubs that are only visible to libcore.

This patch was previous discussed on GitHub.

rust/Makefile +14 -0
index ff70c4c916f8..aed6f7eca36f 100644
@@ -359,8 +359,22 @@ rust-analyzer:
 	$(Q)$(srctree)/scripts/generate_rust_analyzer.py $(srctree) $(objtree) \
 		$(RUST_LIB_SRC) > $(objtree)/rust-project.json
 
+redirect-intrinsics = \
+	__eqsf2 __gesf2 __lesf2 __nesf2 __unordsf2 \
+	__unorddf2 \
+	__muloti4 __multi3 \
+	__udivmodti4 __udivti3 __umodti3
+
+ifneq ($(or $(CONFIG_ARM64),$(and $(CONFIG_RISCV),$(CONFIG_64BIT))),)
+	# These intrinsics are defined for ARM64 and RISCV64
+	redirect-intrinsics += \
+		__ashrti3 \
+		__ashlti3 __lshrti3
+endif
+
 $(obj)/core.o: private skip_clippy = 1
 $(obj)/core.o: private skip_flags = -Dunreachable_pub
+$(obj)/core.o: private rustc_objcopy = $(foreach sym,$(redirect-intrinsics),--redefine-sym $(sym)=__rust$(sym))
 $(obj)/core.o: private rustc_target_flags = $(core-cfgs)
 $(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs $(obj)/target.json FORCE
 	$(call if_changed_dep,rustc_library)
rust/compiler_builtins.rs +4 -1
index f8f39a3e6855..5f833dcca131 100644
@@ -28,7 +28,7 @@ macro_rules! define_panicking_intrinsics(
     ($reason: tt, { $($ident: ident, )* }) => {
         $(
             #[doc(hidden)]
-            #[no_mangle]
+            #[export_name = concat!("__rust", stringify!($ident))]
             pub extern "C" fn $ident() {
                 panic!($reason);
             }
@@ -61,3 +61,6 @@ define_panicking_intrinsics!("`u128` should not be used", {
     __udivti3,
     __umodti3,
 });
+
+// NOTE: if you are adding a new intrinsic is added here, you should also add it to
+// `redirect-intrinsics` in `rust/Makefile`.
Signature · 1 line
2.34.1
#1 lore raw
Quoted 2 lines · Gary Guo

This patch was previous discussed on GitHub.

previous -> previously

Quoted 3 lines

Hmm... Something happened to the patch (compared to v1): the triple dashes, the diffstat, the base-commit etc. are not there. It can still be applied, so no worries, but it feels a bit strange.

The newline between the tags should not be there. Instead, please put it on top of Link: to keep tags separated (if you wanted to keep it close to the sentence to refer to it, you can use "[1]" instead and then "Link: https... [1]" in the tag).

rust/compiler_builtins.rs define_panicking_intrinsics!("`u128` should not be used", { · line 66

Quoted 2 lines
rust/compiler_builtins.rs +2 -0
+// NOTE: if you are adding a new intrinsic is added here, you should also add it to
+// `redirect-intrinsics` in `rust/Makefile`.

"is added" -> "". Also, what about putting the message at the top instead (below the docs or perhaps after the macro but before the calls to it)?

For the title of the patch, in the end I went with "rust: xxx: ..." (where xxx is typically the name of the crate or the kernel crate module), so perhaps "rust: compiler_builtins: make stubs non-global" or similar?

Thanks a lot!

Cheers,
Miguel

author reviewer
#2 lore raw
Quoted 6 lines · On Mon, 5 Dec 2022 21:26:27 +0100 Miguel Ojeda…
Quoted 2 lines · Gary Guo

This patch was previous discussed on GitHub.

previous -> previously

Quoted 7 lines
Quoted 3 lines

Hmm... Something happened to the patch (compared to v1): the triple dashes, the diffstat, the base-commit etc. are not there. It can still be applied, so no worries, but it feels a bit strange.

Checked my bash history, and realised that there is a mistakenly added p in my argument to git format-patch :)

Quoted 6 lines

The newline between the tags should not be there. Instead, please put it on top of Link: to keep tags separated (if you wanted to keep it close to the sentence to refer to it, you can use "[1]" instead and then "Link: https... [1]" in the tag).

Quoted 6 lines

rust/compiler_builtins.rs define_panicking_intrinsics!("`u128` should not be used", { · line 66

Quoted 2 lines
rust/compiler_builtins.rs +2 -0
+// NOTE: if you are adding a new intrinsic is added here, you should also add it to
+// `redirect-intrinsics` in `rust/Makefile`.  

"is added" -> "". Also, what about putting the message at the top instead (below the docs or perhaps after the macro but before the calls to it)?

People usually add new things to the bottom, and that's my rationale for adding the note there.

Quoted 5 lines

For the title of the patch, in the end I went with "rust: xxx: ..." (where xxx is typically the name of the crate or the kernel crate module), so perhaps "rust: compiler_builtins: make stubs non-global" or similar?

Will address, along with other suggestions, in v3.

Best,
Gary

Acked-by on v2 1/1
#3 lore raw
In reply to Gary Guo
Quoted 12 lines · Gary Guo

Currently we define a number of stubs for compiler-builtin intrinsics that compiled libcore generates. The defined stubs are weak so they will not conflict with genuine implementation of these intrinsics, but their effect is global and will cause non-libcore code that accidently generate these intrinsics calls compile and bug on runtime.

Instead of defining a stub that can affect all code, this patch uses objcopy's --redefine-sym flag to redirect these calls (from libcore only) to a prefixed version (e.g. redirect __multi3 to __rust_multi3), so we can define panciking stubs that are only visible to libcore.

Quoted 4 lines

This patch was previous discussed on GitHub.

Thank you for addressing my feedback!

Quoted 51 lines
rust/Makefile +14 -0
index ff70c4c916f8..aed6f7eca36f 100644
@@ -359,8 +359,22 @@ rust-analyzer:
        $(Q)$(srctree)/scripts/generate_rust_analyzer.py $(srctree) $(objtree) \
                $(RUST_LIB_SRC) > $(objtree)/rust-project.json
 
+redirect-intrinsics = \
+       __eqsf2 __gesf2 __lesf2 __nesf2 __unordsf2 \
+       __unorddf2 \
+       __muloti4 __multi3 \
+       __udivmodti4 __udivti3 __umodti3
+
+ifneq ($(or $(CONFIG_ARM64),$(and $(CONFIG_RISCV),$(CONFIG_64BIT))),)
+       # These intrinsics are defined for ARM64 and RISCV64
+       redirect-intrinsics += \
+               __ashrti3 \
+               __ashlti3 __lshrti3
+endif
+
 $(obj)/core.o: private skip_clippy = 1
 $(obj)/core.o: private skip_flags = -Dunreachable_pub
+$(obj)/core.o: private rustc_objcopy = $(foreach sym,$(redirect-intrinsics),--redefine-sym $(sym)=__rust$(sym))
 $(obj)/core.o: private rustc_target_flags = $(core-cfgs)
 $(obj)/core.o: $(RUST_LIB_SRC)/core/src/lib.rs $(obj)/target.json FORCE
        $(call if_changed_dep,rustc_library)
rust/compiler_builtins.rs +4 -2
index f8f39a3e6855..5f833dcca131 100644
@@ -28,7 +28,7 @@ macro_rules! define_panicking_intrinsics(
     ($reason: tt, { $($ident: ident, )* }) => {
         $(
             #[doc(hidden)]
-            #[no_mangle]
+            #[export_name = concat!("__rust", stringify!($ident))]
             pub extern "C" fn $ident() {
                 panic!($reason);
             }
@@ -61,3 +61,6 @@ define_panicking_intrinsics!("`u128` should not be used", {
     __udivti3,
     __umodti3,
 });
+
+// NOTE: if you are adding a new intrinsic is added here, you should also add it to
+// `redirect-intrinsics` in `rust/Makefile`.
--

2.34.1

Signature · 2 lines
Thanks,
~Nick Desaulniers