162306a36Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci
362306a36Sopenharmony_ciifdef CONFIG_CC_HAS_KASAN_MEMINTRINSIC_PREFIX
462306a36Sopenharmony_ci# Safe for compiler to generate meminstrinsic calls in uninstrumented files.
562306a36Sopenharmony_ciCFLAGS_KASAN_NOSANITIZE :=
662306a36Sopenharmony_cielse
762306a36Sopenharmony_ci# Don't let compiler generate memintrinsic calls in uninstrumented files
862306a36Sopenharmony_ci# because they are instrumented.
962306a36Sopenharmony_ciCFLAGS_KASAN_NOSANITIZE := -fno-builtin
1062306a36Sopenharmony_ciendif
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ciKASAN_SHADOW_OFFSET ?= $(CONFIG_KASAN_SHADOW_OFFSET)
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_cicc-param = $(call cc-option, -mllvm -$(1), $(call cc-option, --param $(1)))
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ciifdef CONFIG_KASAN_STACK
1762306a36Sopenharmony_ci	stack_enable := 1
1862306a36Sopenharmony_cielse
1962306a36Sopenharmony_ci	stack_enable := 0
2062306a36Sopenharmony_ciendif
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ciifdef CONFIG_KASAN_GENERIC
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ciifdef CONFIG_KASAN_INLINE
2562306a36Sopenharmony_ci	call_threshold := 10000
2662306a36Sopenharmony_cielse
2762306a36Sopenharmony_ci	call_threshold := 0
2862306a36Sopenharmony_ciendif
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ciCFLAGS_KASAN_MINIMAL := -fsanitize=kernel-address
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci# -fasan-shadow-offset fails without -fsanitize
3362306a36Sopenharmony_ciCFLAGS_KASAN_SHADOW := $(call cc-option, -fsanitize=kernel-address \
3462306a36Sopenharmony_ci			-fasan-shadow-offset=$(KASAN_SHADOW_OFFSET), \
3562306a36Sopenharmony_ci			$(call cc-option, -fsanitize=kernel-address \
3662306a36Sopenharmony_ci			-mllvm -asan-mapping-offset=$(KASAN_SHADOW_OFFSET)))
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ciifeq ($(strip $(CFLAGS_KASAN_SHADOW)),)
3962306a36Sopenharmony_ci	CFLAGS_KASAN := $(CFLAGS_KASAN_MINIMAL)
4062306a36Sopenharmony_cielse
4162306a36Sopenharmony_ci	# Now add all the compiler specific options that are valid standalone
4262306a36Sopenharmony_ci	CFLAGS_KASAN := $(CFLAGS_KASAN_SHADOW) \
4362306a36Sopenharmony_ci	 $(call cc-param,asan-globals=1) \
4462306a36Sopenharmony_ci	 $(call cc-param,asan-instrumentation-with-call-threshold=$(call_threshold)) \
4562306a36Sopenharmony_ci	 $(call cc-param,asan-instrument-allocas=1)
4662306a36Sopenharmony_ciendif
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ciCFLAGS_KASAN += $(call cc-param,asan-stack=$(stack_enable))
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci# Instrument memcpy/memset/memmove calls by using instrumented __asan_mem*()
5162306a36Sopenharmony_ci# instead. With compilers that don't support this option, compiler-inserted
5262306a36Sopenharmony_ci# memintrinsics won't be checked by KASAN on GENERIC_ENTRY architectures.
5362306a36Sopenharmony_ciCFLAGS_KASAN += $(call cc-param,asan-kernel-mem-intrinsic-prefix=1)
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ciendif # CONFIG_KASAN_GENERIC
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ciifdef CONFIG_KASAN_SW_TAGS
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ciifdef CONFIG_KASAN_INLINE
6062306a36Sopenharmony_ci    instrumentation_flags := $(call cc-param,hwasan-mapping-offset=$(KASAN_SHADOW_OFFSET))
6162306a36Sopenharmony_cielse
6262306a36Sopenharmony_ci    instrumentation_flags := $(call cc-param,hwasan-instrument-with-calls=1)
6362306a36Sopenharmony_ciendif
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ciCFLAGS_KASAN := -fsanitize=kernel-hwaddress \
6662306a36Sopenharmony_ci		$(call cc-param,hwasan-instrument-stack=$(stack_enable)) \
6762306a36Sopenharmony_ci		$(call cc-param,hwasan-use-short-granules=0) \
6862306a36Sopenharmony_ci		$(call cc-param,hwasan-inline-all-checks=0) \
6962306a36Sopenharmony_ci		$(instrumentation_flags)
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci# Instrument memcpy/memset/memmove calls by using instrumented __hwasan_mem*().
7262306a36Sopenharmony_ciifeq ($(call clang-min-version, 150000)$(call gcc-min-version, 130000),y)
7362306a36Sopenharmony_ciCFLAGS_KASAN += $(call cc-param,hwasan-kernel-mem-intrinsic-prefix=1)
7462306a36Sopenharmony_ciendif
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ciendif # CONFIG_KASAN_SW_TAGS
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ciexport CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE
79