blob: 69552a39951d144e9af34a6b70165df175228d70 [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) \
Paul Lawrence342061e2018-02-06 15:36:11 -080035 $(call cc-param,asan-use-after-scope=1) \
36 $(call cc-param,asan-instrument-allocas=1)
Andrey Ryabinin1a69e7c2018-02-06 15:36:08 -080037 endif
38
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080039endif
Andrey Ryabininc5caf212016-12-12 16:44:59 -080040
Arnd Bergmanne7c52b82018-02-06 15:41:41 -080041ifdef CONFIG_KASAN_EXTRA
Andrey Ryabininc5caf212016-12-12 16:44:59 -080042CFLAGS_KASAN += $(call cc-option, -fsanitize-address-use-after-scope)
Arnd Bergmanne7c52b82018-02-06 15:41:41 -080043endif
Andrey Konovalov0e410e12018-02-06 15:36:00 -080044
45CFLAGS_KASAN_NOSANITIZE := -fno-builtin
46
Andrey Ryabinin0b24bec2015-02-13 14:39:17 -080047endif