blob: 801c415bac59d014d96a8d34cd587e212d0487b2 [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)) \
Walter Wu02c58772021-04-16 15:46:00 -070036 $(call cc-param,asan-stack=$(stack_enable)) \
Andrey Konovalov2bd926b2018-12-28 00:29:53 -080037 $(call cc-param,asan-instrument-allocas=1)
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080038endif
Andrey Ryabininc5caf212016-12-12 16:44:59 -080039
Andrey Konovalov2bd926b2018-12-28 00:29:53 -080040endif # CONFIG_KASAN_GENERIC
Andrey Konovalov0e410e12018-02-06 15:36:00 -080041
Andrey Konovalov2bd926b2018-12-28 00:29:53 -080042ifdef CONFIG_KASAN_SW_TAGS
43
44ifdef CONFIG_KASAN_INLINE
Arnd Bergmann5c595ac2021-04-16 15:45:57 -070045 instrumentation_flags := $(call cc-param,hwasan-mapping-offset=$(KASAN_SHADOW_OFFSET))
Andrey Konovalov2bd926b2018-12-28 00:29:53 -080046else
Arnd Bergmann5c595ac2021-04-16 15:45:57 -070047 instrumentation_flags := $(call cc-param,hwasan-instrument-with-calls=1)
Andrey Konovalov2bd926b2018-12-28 00:29:53 -080048endif
49
50CFLAGS_KASAN := -fsanitize=kernel-hwaddress \
Walter Wu02c58772021-04-16 15:46:00 -070051 $(call cc-param,hwasan-instrument-stack=$(stack_enable)) \
Arnd Bergmann5c595ac2021-04-16 15:45:57 -070052 $(call cc-param,hwasan-use-short-granules=0) \
Peter Collingbourne1cbdf602021-05-26 10:49:27 -070053 $(call cc-param,hwasan-inline-all-checks=0) \
Andrey Konovalov2bd926b2018-12-28 00:29:53 -080054 $(instrumentation_flags)
55
56endif # CONFIG_KASAN_SW_TAGS
Masahiro Yamadabb273212020-09-10 22:44:29 +090057
58export CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE