Makefile.kasan 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # SPDX-License-Identifier: GPL-2.0
  2. CFLAGS_KASAN_NOSANITIZE := -fno-builtin
  3. KASAN_SHADOW_OFFSET ?= $(CONFIG_KASAN_SHADOW_OFFSET)
  4. cc-param = $(call cc-option, -mllvm -$(1), $(call cc-option, --param $(1)))
  5. ifdef CONFIG_KASAN_STACK
  6. stack_enable := 1
  7. else
  8. stack_enable := 0
  9. endif
  10. ifdef CONFIG_KASAN_GENERIC
  11. ifdef CONFIG_KASAN_INLINE
  12. call_threshold := 10000
  13. else
  14. call_threshold := 0
  15. endif
  16. CFLAGS_KASAN_MINIMAL := -fsanitize=kernel-address
  17. # -fasan-shadow-offset fails without -fsanitize
  18. CFLAGS_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. ifeq ($(strip $(CFLAGS_KASAN_SHADOW)),)
  23. CFLAGS_KASAN := $(CFLAGS_KASAN_MINIMAL)
  24. else
  25. # Now add all the compiler specific options that are valid standalone
  26. CFLAGS_KASAN := $(CFLAGS_KASAN_SHADOW) \
  27. $(call cc-param,asan-globals=1) \
  28. $(call cc-param,asan-instrumentation-with-call-threshold=$(call_threshold)) \
  29. $(call cc-param,asan-stack=$(stack_enable)) \
  30. $(call cc-param,asan-instrument-allocas=1)
  31. endif
  32. endif # CONFIG_KASAN_GENERIC
  33. ifdef CONFIG_KASAN_SW_TAGS
  34. ifdef CONFIG_KASAN_INLINE
  35. instrumentation_flags := $(call cc-param,hwasan-mapping-offset=$(KASAN_SHADOW_OFFSET))
  36. else
  37. instrumentation_flags := $(call cc-param,hwasan-instrument-with-calls=1)
  38. endif
  39. CFLAGS_KASAN := -fsanitize=kernel-hwaddress \
  40. $(call cc-param,hwasan-instrument-stack=$(stack_enable)) \
  41. $(call cc-param,hwasan-use-short-granules=0) \
  42. $(instrumentation_flags)
  43. endif # CONFIG_KASAN_SW_TAGS
  44. export CFLAGS_KASAN CFLAGS_KASAN_NOSANITIZE