12
12
KASAN_SHADOW_OFFSET ?= $(CONFIG_KASAN_SHADOW_OFFSET)
13
13
14
14
cc-param = $(call cc-option, -mllvm -$(1), $(call cc-option, --param $(1)))
15
+ rustc-param = $(call rustc-option, -Cllvm-args=-$(1),)
15
16
16
17
ifdef CONFIG_KASAN_STACK
17
18
stack_enable := 1
28
29
endif
29
30
30
31
CFLAGS_KASAN_MINIMAL := -fsanitize=kernel-address
32
+ RUSTFLAGS_KASAN_MINIMAL := -Zsanitizer=kernel-address -Zsanitizer-recover=kernel-address
31
33
32
34
# -fasan-shadow-offset fails without -fsanitize
33
35
CFLAGS_KASAN_SHADOW := $(call cc-option, -fsanitize=kernel-address \
@@ -36,13 +38,36 @@ CFLAGS_KASAN_SHADOW := $(call cc-option, -fsanitize=kernel-address \
36
38
-mllvm -asan-mapping-offset=$(KASAN_SHADOW_OFFSET)))
37
39
38
40
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)
39
55
CFLAGS_KASAN := $(CFLAGS_KASAN_MINIMAL)
56
+ ifdef CONFIG_RUST
57
+ RUSTFLAGS_KASAN := $(RUSTFLAGS_KASAN_MINIMAL)
58
+ endif
40
59
else
41
60
# Now add all the compiler specific options that are valid standalone
42
61
CFLAGS_KASAN := $(CFLAGS_KASAN_SHADOW) \
43
62
$(call cc-param,asan-globals=1) \
44
63
$(call cc-param,asan-instrumentation-with-call-threshold=$(call_threshold)) \
45
64
$(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
46
71
endif
47
72
48
73
CFLAGS_KASAN += $(call cc-param,asan-stack=$(stack_enable))
@@ -52,6 +77,11 @@ CFLAGS_KASAN += $(call cc-param,asan-stack=$(stack_enable))
52
77
# memintrinsics won't be checked by KASAN on GENERIC_ENTRY architectures.
53
78
CFLAGS_KASAN += $(call cc-param,asan-kernel-mem-intrinsic-prefix=1)
54
79
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
+
55
85
endif # CONFIG_KASAN_GENERIC
56
86
57
87
ifdef CONFIG_KASAN_SW_TAGS
@@ -73,6 +103,20 @@ ifeq ($(call clang-min-version, 150000)$(call gcc-min-version, 130000),y)
73
103
CFLAGS_KASAN += $(call cc-param,hwasan-kernel-mem-intrinsic-prefix=1)
74
104
endif
75
105
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
+
76
120
endif # CONFIG_KASAN_SW_TAGS
77
121
78
- export CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE
122
+ export CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE RUSTFLAGS_KASAN
0 commit comments