blob: b9e94c5e7097090317275d2a543d06debbce36bc [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001# SPDX-License-Identifier: GPL-2.0
Mark Rutland34b55602019-08-14 15:31:57 +01002CFLAGS_KASAN_NOSANITIZE := -fno-builtin
3KASAN_SHADOW_OFFSET ?= $(CONFIG_KASAN_SHADOW_OFFSET)
Mark Rutland34b55602019-08-14 15:31:57 +01004
Arnd Bergmann5c595ac2021-04-16 15:45:57 -07005cc-param = $(call cc-option, -mllvm -$(1), $(call cc-option, --param $(1)))
6
Walter Wu02c58772021-04-16 15:46:00 -07007ifdef CONFIG_KASAN_STACK
8 stack_enable := 1
9else
10 stack_enable := 0
11endif
12
Andrey Konovalov2bd926b2018-12-28 00:29:53 -080013ifdef CONFIG_KASAN_GENERIC
14
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080015ifdef CONFIG_KASAN_INLINE
16 call_threshold := 10000
17else
18 call_threshold := 0
19endif
20
21CFLAGS_KASAN_MINIMAL := -fsanitize=kernel-address
22
Andrey Konovalov2bd926b2018-12-28 00:29:53 -080023# -fasan-shadow-offset fails without -fsanitize
24CFLAGS_KASAN_SHADOW := $(call cc-option, -fsanitize=kernel-address \
Andrey Ryabinin1a69e7c2018-02-06 15:36:08 -080025 -fasan-shadow-offset=$(KASAN_SHADOW_OFFSET), \
26 $(call cc-option, -fsanitize=kernel-address \
27 -mllvm -asan-mapping-offset=$(KASAN_SHADOW_OFFSET)))
28
Andrey Konovalov2bd926b2018-12-28 00:29:53 -080029ifeq ($(strip $(CFLAGS_KASAN_SHADOW)),)
30 CFLAGS_KASAN := $(CFLAGS_KASAN_MINIMAL)
31else
32 # Now add all the compiler specific options that are valid standalone
33 CFLAGS_KASAN := $(CFLAGS_KASAN_SHADOW) \
34 $(call cc-param,asan-globals=1) \
35 $(call cc-param,asan-instrumentation-with-call-threshold=$(call_threshold)) \
Andrey Konovalov2bd926b2018-12-28 00:29:53 -080036 $(call cc-param,asan-instrument-allocas=1)
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080037endif
Andrey Ryabininc5caf212016-12-12 16:44:59 -080038
Nathan Chancellor19532862021-09-24 15:44:00 -070039CFLAGS_KASAN += $(call cc-param,asan-stack=$(stack_enable))
40
Andrey Konovalov2bd926b2018-12-28 00:29:53 -080041endif # CONFIG_KASAN_GENERIC
Andrey Konovalov0e410e12018-02-06 15:36:00 -080042
Andrey Konovalov2bd926b2018-12-28 00:29:53 -080043ifdef CONFIG_KASAN_SW_TAGS
44
45ifdef CONFIG_KASAN_INLINE
Arnd Bergmann5c595ac2021-04-16 15:45:57 -070046 instrumentation_flags := $(call cc-param,hwasan-mapping-offset=$(KASAN_SHADOW_OFFSET))
Andrey Konovalov2bd926b2018-12-28 00:29:53 -080047else
Arnd Bergmann5c595ac2021-04-16 15:45:57 -070048 instrumentation_flags := $(call cc-param,hwasan-instrument-with-calls=1)
Andrey Konovalov2bd926b2018-12-28 00:29:53 -080049endif
50
51CFLAGS_KASAN := -fsanitize=kernel-hwaddress \
Walter Wu02c58772021-04-16 15:46:00 -070052 $(call cc-param,hwasan-instrument-stack=$(stack_enable)) \
Arnd Bergmann5c595ac2021-04-16 15:45:57 -070053 $(call cc-param,hwasan-use-short-granules=0) \
Peter Collingbourne1cbdf602021-05-26 10:49:27 -070054 $(call cc-param,hwasan-inline-all-checks=0) \
Andrey Konovalov2bd926b2018-12-28 00:29:53 -080055 $(instrumentation_flags)
56
57endif # CONFIG_KASAN_SW_TAGS
Masahiro Yamadabb273212020-09-10 22:44:29 +090058
59export CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE