xref: /openbmc/linux/scripts/Makefile.kasan (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1b2441318SGreg Kroah-Hartman# SPDX-License-Identifier: GPL-2.0
236be5cbaSMarco Elver
336be5cbaSMarco Elverifdef CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX
436be5cbaSMarco Elver# Safe for compiler to generate meminstrinsic calls in uninstrumented files.
536be5cbaSMarco ElverCFLAGS_KASAN_NOSANITIZE :=
636be5cbaSMarco Elverelse
736be5cbaSMarco Elver# Don't let compiler generate memintrinsic calls in uninstrumented files
836be5cbaSMarco Elver# because they are instrumented.
934b5560dSMark RutlandCFLAGS_KASAN_NOSANITIZE := -fno-builtin
1036be5cbaSMarco Elverendif
1136be5cbaSMarco Elver
1234b5560dSMark RutlandKASAN_SHADOW_OFFSET ?= $(CONFIG_KASAN_SHADOW_OFFSET)
1334b5560dSMark Rutland
145c595ac4SArnd Bergmanncc-param = $(call cc-option, -mllvm -$(1), $(call cc-option, --param $(1)))
155c595ac4SArnd Bergmann
1602c58773SWalter Wuifdef CONFIG_KASAN_STACK
1702c58773SWalter Wu	stack_enable := 1
1802c58773SWalter Wuelse
1902c58773SWalter Wu	stack_enable := 0
2002c58773SWalter Wuendif
2102c58773SWalter Wu
222bd926b4SAndrey Konovalovifdef CONFIG_KASAN_GENERIC
232bd926b4SAndrey Konovalov
240b24beccSAndrey Ryabininifdef CONFIG_KASAN_INLINE
250b24beccSAndrey Ryabinin	call_threshold := 10000
260b24beccSAndrey Ryabininelse
270b24beccSAndrey Ryabinin	call_threshold := 0
280b24beccSAndrey Ryabininendif
290b24beccSAndrey Ryabinin
300b24beccSAndrey RyabininCFLAGS_KASAN_MINIMAL := -fsanitize=kernel-address
310b24beccSAndrey Ryabinin
321a69e7ceSAndrey Ryabinin# -fasan-shadow-offset fails without -fsanitize
331a69e7ceSAndrey RyabininCFLAGS_KASAN_SHADOW := $(call cc-option, -fsanitize=kernel-address \
341a69e7ceSAndrey Ryabinin			-fasan-shadow-offset=$(KASAN_SHADOW_OFFSET), \
351a69e7ceSAndrey Ryabinin			$(call cc-option, -fsanitize=kernel-address \
361a69e7ceSAndrey Ryabinin			-mllvm -asan-mapping-offset=$(KASAN_SHADOW_OFFSET)))
371a69e7ceSAndrey Ryabinin
381a69e7ceSAndrey Ryabininifeq ($(strip $(CFLAGS_KASAN_SHADOW)),)
390b24beccSAndrey Ryabinin	CFLAGS_KASAN := $(CFLAGS_KASAN_MINIMAL)
401a69e7ceSAndrey Ryabininelse
411a69e7ceSAndrey Ryabinin	# Now add all the compiler specific options that are valid standalone
421a69e7ceSAndrey Ryabinin	CFLAGS_KASAN := $(CFLAGS_KASAN_SHADOW) \
431a69e7ceSAndrey Ryabinin	 $(call cc-param,asan-globals=1) \
441a69e7ceSAndrey Ryabinin	 $(call cc-param,asan-instrumentation-with-call-threshold=$(call_threshold)) \
45342061eeSPaul Lawrence	 $(call cc-param,asan-instrument-allocas=1)
460b24beccSAndrey Ryabininendif
471a69e7ceSAndrey Ryabinin
4819532869SNathan ChancellorCFLAGS_KASAN += $(call cc-param,asan-stack=$(stack_enable))
4919532869SNathan Chancellor
5051287dcbSMarco Elver# Instrument memcpy/memset/memmove calls by using instrumented __asan_mem*()
5151287dcbSMarco Elver# instead. With compilers that don't support this option, compiler-inserted
5251287dcbSMarco Elver# memintrinsics won't be checked by KASAN on GENERIC_ENTRY architectures.
5351287dcbSMarco ElverCFLAGS_KASAN += $(call cc-param,asan-kernel-mem-intrinsic-prefix=1)
5451287dcbSMarco Elver
552bd926b4SAndrey Konovalovendif # CONFIG_KASAN_GENERIC
560e410e15SAndrey Konovalov
572bd926b4SAndrey Konovalovifdef CONFIG_KASAN_SW_TAGS
582bd926b4SAndrey Konovalov
592bd926b4SAndrey Konovalovifdef CONFIG_KASAN_INLINE
605c595ac4SArnd Bergmann    instrumentation_flags := $(call cc-param,hwasan-mapping-offset=$(KASAN_SHADOW_OFFSET))
612bd926b4SAndrey Konovalovelse
625c595ac4SArnd Bergmann    instrumentation_flags := $(call cc-param,hwasan-instrument-with-calls=1)
632bd926b4SAndrey Konovalovendif
642bd926b4SAndrey Konovalov
652bd926b4SAndrey KonovalovCFLAGS_KASAN := -fsanitize=kernel-hwaddress \
6602c58773SWalter Wu		$(call cc-param,hwasan-instrument-stack=$(stack_enable)) \
675c595ac4SArnd Bergmann		$(call cc-param,hwasan-use-short-granules=0) \
681cbdf60bSPeter Collingbourne		$(call cc-param,hwasan-inline-all-checks=0) \
692bd926b4SAndrey Konovalov		$(instrumentation_flags)
702bd926b4SAndrey Konovalov
7151287dcbSMarco Elver# Instrument memcpy/memset/memmove calls by using instrumented __hwasan_mem*().
72*90fd8336SArnd Bergmannifeq ($(call clang-min-version, 150000)$(call gcc-min-version, 130000),y)
7351287dcbSMarco ElverCFLAGS_KASAN += $(call cc-param,hwasan-kernel-mem-intrinsic-prefix=1)
74*90fd8336SArnd Bergmannendif
7551287dcbSMarco Elver
762bd926b4SAndrey Konovalovendif # CONFIG_KASAN_SW_TAGS
77bb273211SMasahiro Yamada
78bb273211SMasahiro Yamadaexport CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE
79