compiler-clang.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __LINUX_COMPILER_TYPES_H
  3. #error "Please don't include <linux/compiler-clang.h> directly, include <linux/compiler.h> instead."
  4. #endif
  5. /* Compiler specific definitions for Clang compiler */
  6. /* same as gcc, this was present in clang-2.6 so we can assume it works
  7. * with any version that can compile the kernel
  8. */
  9. #define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
  10. /* all clang versions usable with the kernel support KASAN ABI version 5 */
  11. #define KASAN_ABI_VERSION 5
  12. /*
  13. * Note: Checking __has_feature(*_sanitizer) is only true if the feature is
  14. * enabled. Therefore it is not required to additionally check defined(CONFIG_*)
  15. * to avoid adding redundant attributes in other configurations.
  16. */
  17. #if __has_feature(address_sanitizer) || __has_feature(hwaddress_sanitizer)
  18. /* Emulate GCC's __SANITIZE_ADDRESS__ flag */
  19. #define __SANITIZE_ADDRESS__
  20. #define __no_sanitize_address \
  21. __attribute__((no_sanitize("address", "hwaddress")))
  22. #else
  23. #define __no_sanitize_address
  24. #endif
  25. #if __has_feature(thread_sanitizer)
  26. /* emulate gcc's __SANITIZE_THREAD__ flag */
  27. #define __SANITIZE_THREAD__
  28. #define __no_sanitize_thread \
  29. __attribute__((no_sanitize("thread")))
  30. #else
  31. #define __no_sanitize_thread
  32. #endif
  33. #if defined(CONFIG_ARCH_USE_BUILTIN_BSWAP)
  34. #define __HAVE_BUILTIN_BSWAP32__
  35. #define __HAVE_BUILTIN_BSWAP64__
  36. #define __HAVE_BUILTIN_BSWAP16__
  37. #endif /* CONFIG_ARCH_USE_BUILTIN_BSWAP */
  38. #if __has_feature(undefined_behavior_sanitizer)
  39. /* GCC does not have __SANITIZE_UNDEFINED__ */
  40. #define __no_sanitize_undefined \
  41. __attribute__((no_sanitize("undefined")))
  42. #else
  43. #define __no_sanitize_undefined
  44. #endif
  45. #if __has_feature(memory_sanitizer)
  46. #define __SANITIZE_MEMORY__
  47. /*
  48. * Unlike other sanitizers, KMSAN still inserts code into functions marked with
  49. * no_sanitize("kernel-memory"). Using disable_sanitizer_instrumentation
  50. * provides the behavior consistent with other __no_sanitize_ attributes,
  51. * guaranteeing that __no_sanitize_memory functions remain uninstrumented.
  52. */
  53. #define __no_sanitize_memory __disable_sanitizer_instrumentation
  54. /*
  55. * The __no_kmsan_checks attribute ensures that a function does not produce
  56. * false positive reports by:
  57. * - initializing all local variables and memory stores in this function;
  58. * - skipping all shadow checks;
  59. * - passing initialized arguments to this function's callees.
  60. */
  61. #define __no_kmsan_checks __attribute__((no_sanitize("kernel-memory")))
  62. #else
  63. #define __no_sanitize_memory
  64. #define __no_kmsan_checks
  65. #endif
  66. /*
  67. * Support for __has_feature(coverage_sanitizer) was added in Clang 13 together
  68. * with no_sanitize("coverage"). Prior versions of Clang support coverage
  69. * instrumentation, but cannot be queried for support by the preprocessor.
  70. */
  71. #if __has_feature(coverage_sanitizer)
  72. #define __no_sanitize_coverage __attribute__((no_sanitize("coverage")))
  73. #else
  74. #define __no_sanitize_coverage
  75. #endif
  76. #if __has_feature(shadow_call_stack)
  77. # define __noscs __attribute__((__no_sanitize__("shadow-call-stack")))
  78. #endif
  79. #if __has_feature(kcfi)
  80. /* Disable CFI checking inside a function. */
  81. #define __nocfi __attribute__((__no_sanitize__("kcfi")))
  82. #endif
  83. /*
  84. * Turn individual warnings and errors on and off locally, depending
  85. * on version.
  86. */
  87. #define __diag_clang(version, severity, s) \
  88. __diag_clang_ ## version(__diag_clang_ ## severity s)
  89. /* Severity used in pragma directives */
  90. #define __diag_clang_ignore ignored
  91. #define __diag_clang_warn warning
  92. #define __diag_clang_error error
  93. #define __diag_str1(s) #s
  94. #define __diag_str(s) __diag_str1(s)
  95. #define __diag(s) _Pragma(__diag_str(clang diagnostic s))
  96. #if CONFIG_CLANG_VERSION >= 110000
  97. #define __diag_clang_11(s) __diag(s)
  98. #else
  99. #define __diag_clang_11(s)
  100. #endif
  101. #define __diag_ignore_all(option, comment) \
  102. __diag_clang(11, ignore, option)