Skip to content

Commit dda1350

Browse files
nbdd0121ojeda
authored andcommitted
rust: build: remap path to avoid absolute path
When building with an out directory (O=), absolute paths can end up in the file name in `#[track_caller]` or the panic message. This is not desirable as this leaks the exact path being used to build the kernel and means that the same location can appear in two forms (relative or absolute). This is reported by Asahi [1] and is being workaround in [2] previously to force everything to be absolute path. Using absolute path for everything solves the inconsistency, however it does not address the reproducibility issue. So, fix this by remap all absolute paths to srctree to relative path instead. This is previously attempted in commit dbdffaf ("kbuild, rust: use -fremap-path-prefix to make paths relative") but that was reverted as remapping debug info causes some tool (e.g. objdump) to be unable to find sources. Therefore, use `--remap-path-scope` to only remap macros but leave debuginfo untouched. `--remap-path-scope` is only stable in Rust 1.95, so use `rustc-option` to detect its presence. This feature has been available as `-Zremap-path-scope` for all versions that we support; however due to bugs in the Rust compiler, it does not work reliably until 1.94. I opted to not enable it for 1.94 as it's just a single version that we missed. This change can be validated by building a kernel with O=, strip debug info on vmlinux, and then check if the absolute path exists in `strings vmlinux`, e.g. `strings vmlinux |grep \/home`. Reported-by: Janne Grunau <j@jannau.net> Reported-by: Asahi Lina <lina+kernel@asahilina.net> Closes: https://rust-for-linux.zulipchat.com/#narrow/channel/288089-General/topic/Per-call-site.20data.20and.20lock.20class.20keys/near/572466559 [1] Link: AsahiLinux@54ab888 [2] Signed-off-by: Gary Guo <gary@garyguo.net> Acked-by: Nicolas Schier <nsc@kernel.org> # kbuild Link: https://patch.msgid.link/20260226152112.3222886-1-gary@kernel.org [ Reworded for few typos. - Miguel ] Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent e174dd1 commit dda1350

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

‎Makefile‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,9 @@ KBUILD_CFLAGS += -fno-builtin-wcslen
11131113
# change __FILE__ to the relative path to the source directory
11141114
ifdef building_out_of_srctree
11151115
KBUILD_CPPFLAGS += -fmacro-prefix-map=$(srcroot)/=
1116+
ifeq ($(call rustc-option-yn, --remap-path-scope=macro),y)
1117+
KBUILD_RUSTFLAGS += --remap-path-prefix=$(srcroot)/= --remap-path-scope=macro
1118+
endif
11161119
endif
11171120

11181121
# include additional Makefiles when needed

‎rust/Makefile‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ doctests_modifiers_workaround := $(rustdoc_modifiers_workaround)$(if $(call rust
148148
quiet_cmd_rustdoc = RUSTDOC $(if $(rustdoc_host),H, ) $<
149149
cmd_rustdoc = \
150150
OBJTREE=$(abspath $(objtree)) \
151-
$(RUSTDOC) $(filter-out $(skip_flags) --remap-path-prefix=%,$(if $(rustdoc_host),$(rust_common_flags),$(rust_flags))) \
151+
$(RUSTDOC) $(filter-out $(skip_flags) --remap-path-prefix=% --remap-path-scope=%, \
152+
$(if $(rustdoc_host),$(rust_common_flags),$(rust_flags))) \
152153
$(rustc_target_flags) -L$(objtree)/$(obj) \
153154
-Zunstable-options --generate-link-to-definition \
154155
--output $(rustdoc_output) \
@@ -334,7 +335,7 @@ quiet_cmd_rustdoc_test_kernel = RUSTDOC TK $<
334335
rm -rf $(objtree)/$(obj)/test/doctests/kernel; \
335336
mkdir -p $(objtree)/$(obj)/test/doctests/kernel; \
336337
OBJTREE=$(abspath $(objtree)) \
337-
$(RUSTDOC) --test $(filter-out --remap-path-prefix=%,$(rust_flags)) \
338+
$(RUSTDOC) --test $(filter-out --remap-path-prefix=% --remap-path-scope=%,$(rust_flags)) \
338339
-L$(objtree)/$(obj) --extern ffi --extern pin_init \
339340
--extern kernel --extern build_error --extern macros \
340341
--extern bindings --extern uapi \

0 commit comments

Comments
 (0)