Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit cc1d98f

Browse files
xairyojeda
authored andcommitted
kasan: simplify and clarify Makefile
When KASAN support was being added to the Linux kernel, GCC did not yet support all of the KASAN-related compiler options. Thus, the KASAN Makefile had to probe the compiler for supported options. Nowadays, the Linux kernel GCC version requirement is 5.1+, and thus we don't need the probing of the -fasan-shadow-offset parameter: it exists in all 5.1+ GCCs. Simplify the KASAN Makefile to drop CFLAGS_KASAN_MINIMAL. Also add a few more comments and unify the indentation. Signed-off-by: Andrey Konovalov <[email protected]> Acked-by: Marco Elver <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent ca627e6 commit cc1d98f

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

scripts/Makefile.kasan

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,31 @@ endif
2222
ifdef CONFIG_KASAN_GENERIC
2323

2424
ifdef CONFIG_KASAN_INLINE
25+
# When the number of memory accesses in a function is less than this
26+
# call threshold number, the compiler will use inline instrumentation.
27+
# 10000 is chosen offhand as a sufficiently large number to make all
28+
# kernel functions to be instrumented inline.
2529
call_threshold := 10000
2630
else
2731
call_threshold := 0
2832
endif
2933

30-
CFLAGS_KASAN_MINIMAL := -fsanitize=kernel-address
31-
32-
# -fasan-shadow-offset fails without -fsanitize
33-
CFLAGS_KASAN_SHADOW := $(call cc-option, -fsanitize=kernel-address \
34-
-fasan-shadow-offset=$(KASAN_SHADOW_OFFSET), \
35-
$(call cc-option, -fsanitize=kernel-address \
36-
-mllvm -asan-mapping-offset=$(KASAN_SHADOW_OFFSET)))
37-
38-
ifeq ($(strip $(CFLAGS_KASAN_SHADOW)),)
39-
CFLAGS_KASAN := $(CFLAGS_KASAN_MINIMAL)
40-
else
41-
# Now add all the compiler specific options that are valid standalone
42-
CFLAGS_KASAN := $(CFLAGS_KASAN_SHADOW) \
43-
$(call cc-param,asan-globals=1) \
44-
$(call cc-param,asan-instrumentation-with-call-threshold=$(call_threshold)) \
45-
$(call cc-param,asan-instrument-allocas=1)
46-
endif
47-
48-
CFLAGS_KASAN += $(call cc-param,asan-stack=$(stack_enable))
34+
# First, enable -fsanitize=kernel-address together with providing the shadow
35+
# mapping offset, as for GCC, -fasan-shadow-offset fails without -fsanitize
36+
# (GCC accepts the shadow mapping offset via -fasan-shadow-offset instead of
37+
# a --param like the other KASAN parameters).
38+
# Instead of ifdef-checking the compiler, rely on cc-option.
39+
CFLAGS_KASAN := $(call cc-option, -fsanitize=kernel-address \
40+
-fasan-shadow-offset=$(KASAN_SHADOW_OFFSET), \
41+
$(call cc-option, -fsanitize=kernel-address \
42+
-mllvm -asan-mapping-offset=$(KASAN_SHADOW_OFFSET)))
43+
44+
# Now, add other parameters enabled similarly in both GCC and Clang.
45+
# As some of them are not supported by older compilers, use cc-param.
46+
CFLAGS_KASAN += $(call cc-param,asan-instrumentation-with-call-threshold=$(call_threshold)) \
47+
$(call cc-param,asan-stack=$(stack_enable)) \
48+
$(call cc-param,asan-instrument-allocas=1) \
49+
$(call cc-param,asan-globals=1)
4950

5051
# Instrument memcpy/memset/memmove calls by using instrumented __asan_mem*()
5152
# instead. With compilers that don't support this option, compiler-inserted
@@ -57,9 +58,9 @@ endif # CONFIG_KASAN_GENERIC
5758
ifdef CONFIG_KASAN_SW_TAGS
5859

5960
ifdef CONFIG_KASAN_INLINE
60-
instrumentation_flags := $(call cc-param,hwasan-mapping-offset=$(KASAN_SHADOW_OFFSET))
61+
instrumentation_flags := $(call cc-param,hwasan-mapping-offset=$(KASAN_SHADOW_OFFSET))
6162
else
62-
instrumentation_flags := $(call cc-param,hwasan-instrument-with-calls=1)
63+
instrumentation_flags := $(call cc-param,hwasan-instrument-with-calls=1)
6364
endif
6465

6566
CFLAGS_KASAN := -fsanitize=kernel-hwaddress \
@@ -70,7 +71,7 @@ CFLAGS_KASAN := -fsanitize=kernel-hwaddress \
7071

7172
# Instrument memcpy/memset/memmove calls by using instrumented __hwasan_mem*().
7273
ifeq ($(call clang-min-version, 150000)$(call gcc-min-version, 130000),y)
73-
CFLAGS_KASAN += $(call cc-param,hwasan-kernel-mem-intrinsic-prefix=1)
74+
CFLAGS_KASAN += $(call cc-param,hwasan-kernel-mem-intrinsic-prefix=1)
7475
endif
7576

7677
endif # CONFIG_KASAN_SW_TAGS

0 commit comments

Comments
 (0)