blob: ab85095c3a752e61fc085d66ac3b09bbc50b2bf8 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001# SPDX-License-Identifier: GPL-2.0
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -08002ifdef CONFIG_KASAN
3ifdef CONFIG_KASAN_INLINE
4 call_threshold := 10000
5else
6 call_threshold := 0
7endif
8
Andrey Ryabinin39d114d2015-10-12 18:52:58 +03009KASAN_SHADOW_OFFSET ?= $(CONFIG_KASAN_SHADOW_OFFSET)
10
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080011CFLAGS_KASAN_MINIMAL := -fsanitize=kernel-address
12
Andrey Ryabinin1a69e7c2018-02-06 15:36:08 -080013cc-param = $(call cc-option, -mllvm -$(1), $(call cc-option, --param $(1)))
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080014
15ifeq ($(call cc-option, $(CFLAGS_KASAN_MINIMAL) -Werror),)
Andrey Ryabinin6e54aba2015-04-16 12:44:58 -070016 ifneq ($(CONFIG_COMPILE_TEST),y)
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080017 $(warning Cannot use CONFIG_KASAN: \
18 -fsanitize=kernel-address is not supported by compiler)
Andrey Ryabinin6e54aba2015-04-16 12:44:58 -070019 endif
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080020else
Andrey Ryabinin1a69e7c2018-02-06 15:36:08 -080021 # -fasan-shadow-offset fails without -fsanitize
22 CFLAGS_KASAN_SHADOW := $(call cc-option, -fsanitize=kernel-address \
23 -fasan-shadow-offset=$(KASAN_SHADOW_OFFSET), \
24 $(call cc-option, -fsanitize=kernel-address \
25 -mllvm -asan-mapping-offset=$(KASAN_SHADOW_OFFSET)))
26
27 ifeq ($(strip $(CFLAGS_KASAN_SHADOW)),)
28 CFLAGS_KASAN := $(CFLAGS_KASAN_MINIMAL)
29 else
30 # Now add all the compiler specific options that are valid standalone
31 CFLAGS_KASAN := $(CFLAGS_KASAN_SHADOW) \
32 $(call cc-param,asan-globals=1) \
33 $(call cc-param,asan-instrumentation-with-call-threshold=$(call_threshold)) \
34 $(call cc-param,asan-stack=1) \
35 $(call cc-param,asan-use-after-scope=1)
36 endif
37
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080038endif
Andrey Ryabininc5caf212016-12-12 16:44:59 -080039
40CFLAGS_KASAN += $(call cc-option, -fsanitize-address-use-after-scope)
Andrey Konovalov0e410e12018-02-06 15:36:00 -080041
42CFLAGS_KASAN_NOSANITIZE := -fno-builtin
43
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080044endif