blob: 127012f45166ee6ba334941215ee018bfb9a5a33 [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 Bergmann393200a2021-04-16 15:45:57 -07005cc-param = $(call cc-option, -mllvm -$(1), $(call cc-option, --param $(1)))
6
Andrey Konovalov2bd926b2018-12-28 00:29:53 -08007ifdef CONFIG_KASAN_GENERIC
8
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -08009ifdef CONFIG_KASAN_INLINE
10 call_threshold := 10000
11else
12 call_threshold := 0
13endif
14
15CFLAGS_KASAN_MINIMAL := -fsanitize=kernel-address
16
Andrey Konovalov2bd926b2018-12-28 00:29:53 -080017# -fasan-shadow-offset fails without -fsanitize
18CFLAGS_KASAN_SHADOW := $(call cc-option, -fsanitize=kernel-address \
Andrey Ryabinin1a69e7c2018-02-06 15:36:08 -080019 -fasan-shadow-offset=$(KASAN_SHADOW_OFFSET), \
20 $(call cc-option, -fsanitize=kernel-address \
21 -mllvm -asan-mapping-offset=$(KASAN_SHADOW_OFFSET)))
22
Andrey Konovalov2bd926b2018-12-28 00:29:53 -080023ifeq ($(strip $(CFLAGS_KASAN_SHADOW)),)
24 CFLAGS_KASAN := $(CFLAGS_KASAN_MINIMAL)
25else
26 # Now add all the compiler specific options that are valid standalone
27 CFLAGS_KASAN := $(CFLAGS_KASAN_SHADOW) \
28 $(call cc-param,asan-globals=1) \
29 $(call cc-param,asan-instrumentation-with-call-threshold=$(call_threshold)) \
Arnd Bergmann6baec882019-02-28 16:21:58 -080030 $(call cc-param,asan-stack=$(CONFIG_KASAN_STACK)) \
Andrey Konovalov2bd926b2018-12-28 00:29:53 -080031 $(call cc-param,asan-instrument-allocas=1)
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080032endif
Andrey Ryabininc5caf212016-12-12 16:44:59 -080033
Andrey Konovalov2bd926b2018-12-28 00:29:53 -080034endif # CONFIG_KASAN_GENERIC
Andrey Konovalov0e410e12018-02-06 15:36:00 -080035
Andrey Konovalov2bd926b2018-12-28 00:29:53 -080036ifdef CONFIG_KASAN_SW_TAGS
37
38ifdef CONFIG_KASAN_INLINE
Arnd Bergmann393200a2021-04-16 15:45:57 -070039 instrumentation_flags := $(call cc-param,hwasan-mapping-offset=$(KASAN_SHADOW_OFFSET))
Andrey Konovalov2bd926b2018-12-28 00:29:53 -080040else
Arnd Bergmann393200a2021-04-16 15:45:57 -070041 instrumentation_flags := $(call cc-param,hwasan-instrument-with-calls=1)
Andrey Konovalov2bd926b2018-12-28 00:29:53 -080042endif
43
44CFLAGS_KASAN := -fsanitize=kernel-hwaddress \
Arnd Bergmann393200a2021-04-16 15:45:57 -070045 $(call cc-param,hwasan-instrument-stack=$(CONFIG_KASAN_STACK)) \
46 $(call cc-param,hwasan-use-short-granules=0) \
Andrey Konovalov2bd926b2018-12-28 00:29:53 -080047 $(instrumentation_flags)
48
49endif # CONFIG_KASAN_SW_TAGS
Masahiro Yamadabb273212020-09-10 22:44:29 +090050
51export CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE