1# SPDX-License-Identifier: GPL-2.0 2CFLAGS_KASAN_NOSANITIZE := -fno-builtin 3KASAN_SHADOW_OFFSET ?= $(CONFIG_KASAN_SHADOW_OFFSET) 4 5cc-param = $(call cc-option, -mllvm -$(1), $(call cc-option, --param $(1))) 6 7ifdef CONFIG_KASAN_GENERIC 8 9ifdef CONFIG_KASAN_INLINE 10 call_threshold := 10000 11else 12 call_threshold := 0 13endif 14 15CFLAGS_KASAN_MINIMAL := -fsanitize=kernel-address 16 17# -fasan-shadow-offset fails without -fsanitize 18CFLAGS_KASAN_SHADOW := $(call cc-option, -fsanitize=kernel-address \ 19 -fasan-shadow-offset=$(KASAN_SHADOW_OFFSET), \ 20 $(call cc-option, -fsanitize=kernel-address \ 21 -mllvm -asan-mapping-offset=$(KASAN_SHADOW_OFFSET))) 22 23ifeq ($(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)) \ 30 $(call cc-param,asan-stack=$(CONFIG_KASAN_STACK)) \ 31 $(call cc-param,asan-instrument-allocas=1) 32endif 33 34endif # CONFIG_KASAN_GENERIC 35 36ifdef CONFIG_KASAN_SW_TAGS 37 38ifdef CONFIG_KASAN_INLINE 39 instrumentation_flags := $(call cc-param,hwasan-mapping-offset=$(KASAN_SHADOW_OFFSET)) 40else 41 instrumentation_flags := $(call cc-param,hwasan-instrument-with-calls=1) 42endif 43 44CFLAGS_KASAN := -fsanitize=kernel-hwaddress \ 45 $(call cc-param,hwasan-instrument-stack=$(CONFIG_KASAN_STACK)) \ 46 $(call cc-param,hwasan-use-short-granules=0) \ 47 $(instrumentation_flags) 48 49endif # CONFIG_KASAN_SW_TAGS 50 51export CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE 52