Skip to content

Commit badb1d4

Browse files
committed
rust: kasan/kbuild: fix rustc-option when cross-compiling
The Makefile version of rustc-option currently checks whether the option exists for the host target instead of the target actually being compiled for. It was done this way in commit 46e24a5 ("rust: kasan/kbuild: fix missing flags on first build") to avoid a circular dependency on target.json. However, because of this, rustc-option currently does not function when cross-compiling from x86_64 to aarch64 if CONFIG_SHADOW_CALL_STACK is enabled. This is because KBUILD_RUSTFLAGS contains -Zfixed-x18 under this configuration. Since that flag does not exist on the host target, rustc-option runs into a compilation failure every time, leading to all flags being rejected as unsupported. To fix this, update rustc-option to pass a --target parameter so that the host target is not used. For targets using target.json, use a built-in target that is as close as possible to the target created with target.json to avoid the circular dependency on target.json. One scenario where this causes a boot failure: * Cross-compiled from x86_64 to aarch64. * With CONFIG_SHADOW_CALL_STACK=y * With CONFIG_KASAN_SW_TAGS=y * With CONFIG_KASAN_INLINE=n Then the resulting kernel image will fail to boot when it first calls into Rust code with a crash along the lines of "Unable to handle kernel paging request at virtual address 0ffffffc08541796". This is because the call threshold is not specified, so rustc will inline kasan operations, but the kasan shadow offset is not specified, which leads to the inlined kasan instructions being incorrect. The above scenario actually also fails without -Zfixed-x18 since the SW_TAGS sanitizer itself is aarch64-specific. But it's a problem with normal KASAN too because of -Zfixed-x18. Cc: [email protected] Fixes: 46e24a5 ("rust: kasan/kbuild: fix missing flags on first build") Signed-off-by: Alice Ryhl <[email protected]>
1 parent 620c627 commit badb1d4

3 files changed

Lines changed: 13 additions & 1 deletion

File tree

arch/x86/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ KBUILD_CFLAGS += -mno-sse -mno-mmx -mno-sse2 -mno-3dnow -mno-avx -mno-sse4a
7777
KBUILD_RUSTFLAGS += --target=$(objtree)/scripts/target.json
7878
KBUILD_RUSTFLAGS += -Ctarget-feature=-sse,-sse2,-sse3,-ssse3,-sse4.1,-sse4.2,-avx,-avx2
7979

80+
# The target.json file is not available when invoking rustc-option, so use the
81+
# built-in target when checking whether flags are supported instead.
82+
KBUILD_RUSTFLAGS_OPTION_CHKS += --target=x86_64-unknown-none
83+
8084
#
8185
# CFLAGS for compiling floating point code inside the kernel.
8286
#

arch/x86/Makefile.um

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ endif
1414

1515
KBUILD_RUSTFLAGS += --target=$(objtree)/scripts/target.json
1616

17+
# The target.json file is not available when invoking rustc-option, so use the
18+
# built-in target when checking whether flags are supported instead.
19+
ifeq ($(CONFIG_X86_32),y)
20+
KBUILD_RUSTFLAGS_OPTION_CHKS += --target=i686-unknown-linux-gnu
21+
else
22+
KBUILD_RUSTFLAGS_OPTION_CHKS += --target=x86_64-unknown-linux-gnu
23+
endif
24+
1725
ifeq ($(CONFIG_X86_32),y)
1826
START := 0x8048000
1927

scripts/Makefile.compiler

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ ld-option = $(call try-run, $(LD) $(KBUILD_LDFLAGS) $(1) -v,$(1),$(2),$(3))
8080
# TODO: remove RUSTC_BOOTSTRAP=1 when we raise the minimum GNU Make version to 4.4
8181
__rustc-option = $(call try-run,\
8282
echo '$(pound)![allow(missing_docs)]$(pound)![feature(no_core)]$(pound)![no_core]' | RUSTC_BOOTSTRAP=1\
83-
$(1) --sysroot=/dev/null $(filter-out --sysroot=/dev/null --target=%,$(2)) $(3)\
83+
$(1) --sysroot=/dev/null $(KBUILD_RUSTFLAGS_OPTION_CHKS) $(filter-out --sysroot=/dev/null --target=%target.json,$(2)) $(3)\
8484
--crate-type=rlib --out-dir=$(TMPOUT) --emit=obj=- - >/dev/null,$(3),$(4))
8585

8686
# rustc-option

0 commit comments

Comments
 (0)