b24413180f560 (Greg Kroah-Hartman 2017-11-01 15:07:57 +0100 1) # SPDX-License-Identifier: GPL-2.0
34b5560db40d2 (Mark Rutland 2019-08-14 15:31:57 +0100 2) CFLAGS_KASAN_NOSANITIZE := -fno-builtin
34b5560db40d2 (Mark Rutland 2019-08-14 15:31:57 +0100 3) KASAN_SHADOW_OFFSET ?= $(CONFIG_KASAN_SHADOW_OFFSET)
34b5560db40d2 (Mark Rutland 2019-08-14 15:31:57 +0100 4)
5c595ac4c776c (Arnd Bergmann 2021-04-16 15:45:57 -0700 5) cc-param = $(call cc-option, -mllvm -$(1), $(call cc-option, --param $(1)))
5c595ac4c776c (Arnd Bergmann 2021-04-16 15:45:57 -0700 6)
02c587733c816 (Walter Wu 2021-04-16 15:46:00 -0700 7) ifdef CONFIG_KASAN_STACK
02c587733c816 (Walter Wu 2021-04-16 15:46:00 -0700 8) stack_enable := 1
02c587733c816 (Walter Wu 2021-04-16 15:46:00 -0700 9) else
02c587733c816 (Walter Wu 2021-04-16 15:46:00 -0700 10) stack_enable := 0
02c587733c816 (Walter Wu 2021-04-16 15:46:00 -0700 11) endif
02c587733c816 (Walter Wu 2021-04-16 15:46:00 -0700 12)
2bd926b439b4c (Andrey Konovalov 2018-12-28 00:29:53 -0800 13) ifdef CONFIG_KASAN_GENERIC
2bd926b439b4c (Andrey Konovalov 2018-12-28 00:29:53 -0800 14)
0b24becc810dc (Andrey Ryabinin 2015-02-13 14:39:17 -0800 15) ifdef CONFIG_KASAN_INLINE
0b24becc810dc (Andrey Ryabinin 2015-02-13 14:39:17 -0800 16) call_threshold := 10000
0b24becc810dc (Andrey Ryabinin 2015-02-13 14:39:17 -0800 17) else
0b24becc810dc (Andrey Ryabinin 2015-02-13 14:39:17 -0800 18) call_threshold := 0
0b24becc810dc (Andrey Ryabinin 2015-02-13 14:39:17 -0800 19) endif
0b24becc810dc (Andrey Ryabinin 2015-02-13 14:39:17 -0800 20)
0b24becc810dc (Andrey Ryabinin 2015-02-13 14:39:17 -0800 21) CFLAGS_KASAN_MINIMAL := -fsanitize=kernel-address
0b24becc810dc (Andrey Ryabinin 2015-02-13 14:39:17 -0800 22)
2bd926b439b4c (Andrey Konovalov 2018-12-28 00:29:53 -0800 23) # -fasan-shadow-offset fails without -fsanitize
2bd926b439b4c (Andrey Konovalov 2018-12-28 00:29:53 -0800 24) CFLAGS_KASAN_SHADOW := $(call cc-option, -fsanitize=kernel-address \
1a69e7ce8391a (Andrey Ryabinin 2018-02-06 15:36:08 -0800 25) -fasan-shadow-offset=$(KASAN_SHADOW_OFFSET), \
1a69e7ce8391a (Andrey Ryabinin 2018-02-06 15:36:08 -0800 26) $(call cc-option, -fsanitize=kernel-address \
1a69e7ce8391a (Andrey Ryabinin 2018-02-06 15:36:08 -0800 27) -mllvm -asan-mapping-offset=$(KASAN_SHADOW_OFFSET)))
1a69e7ce8391a (Andrey Ryabinin 2018-02-06 15:36:08 -0800 28)
2bd926b439b4c (Andrey Konovalov 2018-12-28 00:29:53 -0800 29) ifeq ($(strip $(CFLAGS_KASAN_SHADOW)),)
2bd926b439b4c (Andrey Konovalov 2018-12-28 00:29:53 -0800 30) CFLAGS_KASAN := $(CFLAGS_KASAN_MINIMAL)
2bd926b439b4c (Andrey Konovalov 2018-12-28 00:29:53 -0800 31) else
2bd926b439b4c (Andrey Konovalov 2018-12-28 00:29:53 -0800 32) # Now add all the compiler specific options that are valid standalone
2bd926b439b4c (Andrey Konovalov 2018-12-28 00:29:53 -0800 33) CFLAGS_KASAN := $(CFLAGS_KASAN_SHADOW) \
2bd926b439b4c (Andrey Konovalov 2018-12-28 00:29:53 -0800 34) $(call cc-param,asan-globals=1) \
2bd926b439b4c (Andrey Konovalov 2018-12-28 00:29:53 -0800 35) $(call cc-param,asan-instrumentation-with-call-threshold=$(call_threshold)) \
2bd926b439b4c (Andrey Konovalov 2018-12-28 00:29:53 -0800 36) $(call cc-param,asan-instrument-allocas=1)
0b24becc810dc (Andrey Ryabinin 2015-02-13 14:39:17 -0800 37) endif
c5caf21ab0cf8 (Andrey Ryabinin 2016-12-12 16:44:59 -0800 38)
19532869feb9b (Nathan Chancellor 2021-09-24 15:44:00 -0700 39) CFLAGS_KASAN += $(call cc-param,asan-stack=$(stack_enable))
19532869feb9b (Nathan Chancellor 2021-09-24 15:44:00 -0700 40)
2bd926b439b4c (Andrey Konovalov 2018-12-28 00:29:53 -0800 41) endif # CONFIG_KASAN_GENERIC
0e410e158e5ba (Andrey Konovalov 2018-02-06 15:36:00 -0800 42)
2bd926b439b4c (Andrey Konovalov 2018-12-28 00:29:53 -0800 43) ifdef CONFIG_KASAN_SW_TAGS
2bd926b439b4c (Andrey Konovalov 2018-12-28 00:29:53 -0800 44)
2bd926b439b4c (Andrey Konovalov 2018-12-28 00:29:53 -0800 45) ifdef CONFIG_KASAN_INLINE
5c595ac4c776c (Arnd Bergmann 2021-04-16 15:45:57 -0700 46) instrumentation_flags := $(call cc-param,hwasan-mapping-offset=$(KASAN_SHADOW_OFFSET))
2bd926b439b4c (Andrey Konovalov 2018-12-28 00:29:53 -0800 47) else
5c595ac4c776c (Arnd Bergmann 2021-04-16 15:45:57 -0700 48) instrumentation_flags := $(call cc-param,hwasan-instrument-with-calls=1)
2bd926b439b4c (Andrey Konovalov 2018-12-28 00:29:53 -0800 49) endif
2bd926b439b4c (Andrey Konovalov 2018-12-28 00:29:53 -0800 50)
2bd926b439b4c (Andrey Konovalov 2018-12-28 00:29:53 -0800 51) CFLAGS_KASAN := -fsanitize=kernel-hwaddress \
02c587733c816 (Walter Wu 2021-04-16 15:46:00 -0700 52) $(call cc-param,hwasan-instrument-stack=$(stack_enable)) \
5c595ac4c776c (Arnd Bergmann 2021-04-16 15:45:57 -0700 53) $(call cc-param,hwasan-use-short-granules=0) \
1cbdf60bd1b74 (Peter Collingbourne 2021-05-26 10:49:27 -0700 54) $(call cc-param,hwasan-inline-all-checks=0) \
2bd926b439b4c (Andrey Konovalov 2018-12-28 00:29:53 -0800 55) $(instrumentation_flags)
2bd926b439b4c (Andrey Konovalov 2018-12-28 00:29:53 -0800 56)
2bd926b439b4c (Andrey Konovalov 2018-12-28 00:29:53 -0800 57) endif # CONFIG_KASAN_SW_TAGS
bb2732112bc52 (Masahiro Yamada 2020-09-10 22:44:29 +0900 58)
bb2732112bc52 (Masahiro Yamada 2020-09-10 22:44:29 +0900 59) export CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE