Merge tag 'sched_ext-for-6.12' of git://git.kernel.org/pub/scm/linux/kernel/git/tj...
[linux-2.6-block.git] / scripts / Makefile.kasan
1 # SPDX-License-Identifier: GPL-2.0
2
3 ifdef CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX
4 # Safe for compiler to generate meminstrinsic calls in uninstrumented files.
5 CFLAGS_KASAN_NOSANITIZE :=
6 else
7 # Don't let compiler generate memintrinsic calls in uninstrumented files
8 # because they are instrumented.
9 CFLAGS_KASAN_NOSANITIZE := -fno-builtin
10 endif
11
12 KASAN_SHADOW_OFFSET ?= $(CONFIG_KASAN_SHADOW_OFFSET)
13
14 cc-param = $(call cc-option, -mllvm -$(1), $(call cc-option, --param $(1)))
15
16 ifdef CONFIG_KASAN_STACK
17         stack_enable := 1
18 else
19         stack_enable := 0
20 endif
21
22 ifdef CONFIG_KASAN_GENERIC
23
24 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.
29         call_threshold := 10000
30 else
31         call_threshold := 0
32 endif
33
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)
50
51 # Instrument memcpy/memset/memmove calls by using instrumented __asan_mem*()
52 # instead. With compilers that don't support this option, compiler-inserted
53 # memintrinsics won't be checked by KASAN on GENERIC_ENTRY architectures.
54 CFLAGS_KASAN += $(call cc-param,asan-kernel-mem-intrinsic-prefix=1)
55
56 endif # CONFIG_KASAN_GENERIC
57
58 ifdef CONFIG_KASAN_SW_TAGS
59
60 ifdef CONFIG_KASAN_INLINE
61         instrumentation_flags := $(call cc-param,hwasan-mapping-offset=$(KASAN_SHADOW_OFFSET))
62 else
63         instrumentation_flags := $(call cc-param,hwasan-instrument-with-calls=1)
64 endif
65
66 CFLAGS_KASAN := -fsanitize=kernel-hwaddress \
67                 $(call cc-param,hwasan-instrument-stack=$(stack_enable)) \
68                 $(call cc-param,hwasan-use-short-granules=0) \
69                 $(call cc-param,hwasan-inline-all-checks=0) \
70                 $(instrumentation_flags)
71
72 # Instrument memcpy/memset/memmove calls by using instrumented __hwasan_mem*().
73 ifeq ($(call clang-min-version, 150000)$(call gcc-min-version, 130000),y)
74         CFLAGS_KASAN += $(call cc-param,hwasan-kernel-mem-intrinsic-prefix=1)
75 endif
76
77 endif # CONFIG_KASAN_SW_TAGS
78
79 export CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE