Skip to content

Commit 6571afe

Browse files
committed
rust: Enable KASAN support
Rust supports KASAN via LLVM, but prior to this patch, the flags aren't set properly. Suggested-by: Miguel Ojeda <[email protected]> Signed-off-by: Matthew Maurer <[email protected]>
1 parent cbee227 commit 6571afe

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

scripts/Makefile.kasan

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ endif
1212
KASAN_SHADOW_OFFSET ?= $(CONFIG_KASAN_SHADOW_OFFSET)
1313

1414
cc-param = $(call cc-option, -mllvm -$(1), $(call cc-option, --param $(1)))
15+
rustc-param = $(call rustc-option, -Cllvm-args=-$(1),)
1516

1617
ifdef CONFIG_KASAN_STACK
1718
stack_enable := 1
@@ -28,6 +29,7 @@ else
2829
endif
2930

3031
CFLAGS_KASAN_MINIMAL := -fsanitize=kernel-address
32+
RUSTFLAGS_KASAN_MINIMAL := -Zsanitizer=kernel-address -Zsanitizer-recover=kernel-address
3133

3234
# -fasan-shadow-offset fails without -fsanitize
3335
CFLAGS_KASAN_SHADOW := $(call cc-option, -fsanitize=kernel-address \
@@ -36,13 +38,36 @@ CFLAGS_KASAN_SHADOW := $(call cc-option, -fsanitize=kernel-address \
3638
-mllvm -asan-mapping-offset=$(KASAN_SHADOW_OFFSET)))
3739

3840
ifeq ($(strip $(CFLAGS_KASAN_SHADOW)),)
41+
KASAN_SHADOW_SUPPORTED := n
42+
else
43+
KASAN_SHADOW_SUPPORTED := y
44+
endif
45+
46+
ifdef CONFIG_RUST
47+
RUSTFLAGS_KASAN_SHADOW := $(call rustc-option $(RUSTFLAGS_KASAN_MINIMAL) \
48+
-Cllvm-args=-asan-mapping-offset=$(KASAN_SHADOW_OFFSET))
49+
ifeq ($(strip $(RUSTFLAGS_KASAN_SHADOW)),)
50+
KASAN_SHADOW_SUPPORTED := n
51+
endif
52+
endif
53+
54+
ifeq ($(KASAN_SHADOW_SUPPORTED),y)
3955
CFLAGS_KASAN := $(CFLAGS_KASAN_MINIMAL)
56+
ifdef CONFIG_RUST
57+
RUSTFLAGS_KASAN := $(RUSTFLAGS_KASAN_MINIMAL)
58+
endif
4059
else
4160
# Now add all the compiler specific options that are valid standalone
4261
CFLAGS_KASAN := $(CFLAGS_KASAN_SHADOW) \
4362
$(call cc-param,asan-globals=1) \
4463
$(call cc-param,asan-instrumentation-with-call-threshold=$(call_threshold)) \
4564
$(call cc-param,asan-instrument-allocas=1)
65+
ifdef CONFIG_RUST
66+
RUSTFLAGS_KASAN := $(RUSTFLAGS_KASAN_SHADOW) \
67+
$(call rustc-param,asan-globals=1) \
68+
$(call rustc-param,asan-instrumentation-with-call-threshold=$(call_threshold)) \
69+
$(call rustc-param,asan-instrument-allocas=1)
70+
endif
4671
endif
4772

4873
CFLAGS_KASAN += $(call cc-param,asan-stack=$(stack_enable))
@@ -52,6 +77,11 @@ CFLAGS_KASAN += $(call cc-param,asan-stack=$(stack_enable))
5277
# memintrinsics won't be checked by KASAN on GENERIC_ENTRY architectures.
5378
CFLAGS_KASAN += $(call cc-param,asan-kernel-mem-intrinsic-prefix=1)
5479

80+
ifdef CONFIG_RUST
81+
RUSTFLAGS_KASAN += $(call rustc-param,asan-stack=$(stack_enable))
82+
RUSTFLAGS_KASAN += $(call rustc-param,asan-kernel-mem-intrinsic-prefix=1)
83+
endif
84+
5585
endif # CONFIG_KASAN_GENERIC
5686

5787
ifdef CONFIG_KASAN_SW_TAGS
@@ -73,6 +103,20 @@ ifeq ($(call clang-min-version, 150000)$(call gcc-min-version, 130000),y)
73103
CFLAGS_KASAN += $(call cc-param,hwasan-kernel-mem-intrinsic-prefix=1)
74104
endif
75105

106+
ifdef CONFIG_RUST
107+
ifdef CONFIG_KASAN_INLINE
108+
rust_instrumentation_flags := $(call rustc-param,hwasan-mapping-offset=$(KASAN_SHADOW_OFFSET))
109+
else
110+
rust_instrumentation_flags := $(call rustc-param,hwasan-instrument-with-calls=1)
111+
endif
112+
RUSTFLAGS_KASAN := -Zsanitizer=kernel-hwaddress -Zsanitizer-recover=kernel-hwaddress \
113+
$(call rustc-param,hwasan-instrument-stack=$(stack_enable)) \
114+
$(call rustc-param,hwasan-use-short-granules=0) \
115+
$(call rustc-param,hwasan-inline-all-checks=0) \
116+
$(call rustc-param,hwasan-kernel-mem-intrinsic-prefix=1) \
117+
$(instrumentation_flags)
118+
endif
119+
76120
endif # CONFIG_KASAN_SW_TAGS
77121

78-
export CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE
122+
export CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE RUSTFLAGS_KASAN

scripts/Makefile.lib

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ ifneq ($(CONFIG_KASAN_HW_TAGS),y)
167167
_c_flags += $(if $(patsubst n%,, \
168168
$(KASAN_SANITIZE_$(target-stem).o)$(KASAN_SANITIZE)$(is-kernel-object)), \
169169
$(CFLAGS_KASAN), $(CFLAGS_KASAN_NOSANITIZE))
170+
_rust_flags += $(if $(patsubst n%,, \
171+
$(KASAN_SANITIZE_$(target-stem).o)$(KASAN_SANITIZE)$(is-kernel-object)), \
172+
$(RUSTFLAGS_KASAN))
170173
endif
171174
endif
172175

0 commit comments

Comments
 (0)