Makefile.kasan 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # SPDX-License-Identifier: GPL-2.0
  2. ifdef CONFIG_KASAN_GENERIC
  3. ifdef CONFIG_KASAN_INLINE
  4. call_threshold := 10000
  5. else
  6. call_threshold := 0
  7. endif
  8. KASAN_SHADOW_OFFSET ?= $(CONFIG_KASAN_SHADOW_OFFSET)
  9. CFLAGS_KASAN_MINIMAL := -fsanitize=kernel-address
  10. ifeq ($(call cc-option, $(CFLAGS_KASAN_MINIMAL) -Werror),)
  11. ifneq ($(CONFIG_COMPILE_TEST),y)
  12. $(error Cannot use CONFIG_KASAN_GENERIC: \
  13. -fsanitize=kernel-address is not supported by compiler)
  14. endif
  15. endif
  16. cc-param = $(call cc-option, -mllvm -$(1), $(call cc-option, --param $(1)))
  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=$(CONFIG_KASAN_STACK)) \
  30. $(call cc-param,asan-instrument-allocas=1)
  31. endif
  32. endif # CONFIG_KASAN_GENERIC
  33. ifdef CONFIG_KASAN_SW_TAGS
  34. CFLAGS_KASAN_MINIMAL := -fsanitize=kernel-hwaddress
  35. ifeq ($(call cc-option, $(CFLAGS_KASAN_MINIMAL) -Werror),)
  36. ifneq ($(CONFIG_COMPILE_TEST),y)
  37. $(error Cannot use CONFIG_KASAN_SW_TAGS: \
  38. -fsanitize=kernel-hwaddress is not supported by compiler)
  39. endif
  40. endif
  41. ifdef CONFIG_KASAN_INLINE
  42. instrumentation_flags := -mllvm -hwasan-mapping-offset=$(KASAN_SHADOW_OFFSET)
  43. else
  44. instrumentation_flags := -mllvm -hwasan-instrument-with-calls=1
  45. endif
  46. CFLAGS_KASAN := -fsanitize=kernel-hwaddress \
  47. -mllvm -hwasan-instrument-stack=0 \
  48. $(instrumentation_flags)
  49. endif # CONFIG_KASAN_SW_TAGS
  50. ifdef CONFIG_KASAN
  51. CFLAGS_KASAN_NOSANITIZE := -fno-builtin
  52. endif