Skip to content

Commit 3fac212

Browse files
nathanchanceakpm00
authored andcommitted
compiler-clang.h: define __SANITIZE_*__ macros only when undefined
Clang 22 recently added support for defining __SANITIZE__ macros similar to GCC [1], which causes warnings (or errors with CONFIG_WERROR=y or W=e) with the existing defines that the kernel creates to emulate this behavior with existing clang versions. In file included from <built-in>:3: In file included from include/linux/compiler_types.h:171: include/linux/compiler-clang.h:37:9: error: '__SANITIZE_THREAD__' macro redefined [-Werror,-Wmacro-redefined] 37 | #define __SANITIZE_THREAD__ | ^ <built-in>:352:9: note: previous definition is here 352 | #define __SANITIZE_THREAD__ 1 | ^ Refactor compiler-clang.h to only define the sanitizer macros when they are undefined and adjust the rest of the code to use these macros for checking if the sanitizers are enabled, clearing up the warnings and allowing the kernel to easily drop these defines when the minimum supported version of LLVM for building the kernel becomes 22.0.0 or newer. Link: https://lkml.kernel.org/r/20250902-clang-update-sanitize-defines-v1-1-cf3702ca3d92@kernel.org Link: llvm/llvm-project@568c23b [1] Signed-off-by: Nathan Chancellor <[email protected]> Reviewed-by: Justin Stitt <[email protected]> Cc: Alexander Potapenko <[email protected]> Cc: Andrey Konovalov <[email protected]> Cc: Andrey Ryabinin <[email protected]> Cc: Bill Wendling <[email protected]> Cc: Dmitriy Vyukov <[email protected]> Cc: Marco Elver <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 79357cd commit 3fac212

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

include/linux/compiler-clang.h

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,42 @@
1818
#define KASAN_ABI_VERSION 5
1919

2020
/*
21+
* Clang 22 added preprocessor macros to match GCC, in hopes of eventually
22+
* dropping __has_feature support for sanitizers:
23+
* https://github.com/llvm/llvm-project/commit/568c23bbd3303518c5056d7f03444dae4fdc8a9c
24+
* Create these macros for older versions of clang so that it is easy to clean
25+
* up once the minimum supported version of LLVM for building the kernel always
26+
* creates these macros.
27+
*
2128
* Note: Checking __has_feature(*_sanitizer) is only true if the feature is
2229
* enabled. Therefore it is not required to additionally check defined(CONFIG_*)
2330
* to avoid adding redundant attributes in other configurations.
2431
*/
32+
#if __has_feature(address_sanitizer) && !defined(__SANITIZE_ADDRESS__)
33+
#define __SANITIZE_ADDRESS__
34+
#endif
35+
#if __has_feature(hwaddress_sanitizer) && !defined(__SANITIZE_HWADDRESS__)
36+
#define __SANITIZE_HWADDRESS__
37+
#endif
38+
#if __has_feature(thread_sanitizer) && !defined(__SANITIZE_THREAD__)
39+
#define __SANITIZE_THREAD__
40+
#endif
2541

26-
#if __has_feature(address_sanitizer) || __has_feature(hwaddress_sanitizer)
27-
/* Emulate GCC's __SANITIZE_ADDRESS__ flag */
42+
/*
43+
* Treat __SANITIZE_HWADDRESS__ the same as __SANITIZE_ADDRESS__ in the kernel.
44+
*/
45+
#ifdef __SANITIZE_HWADDRESS__
2846
#define __SANITIZE_ADDRESS__
47+
#endif
48+
49+
#ifdef __SANITIZE_ADDRESS__
2950
#define __no_sanitize_address \
3051
__attribute__((no_sanitize("address", "hwaddress")))
3152
#else
3253
#define __no_sanitize_address
3354
#endif
3455

35-
#if __has_feature(thread_sanitizer)
36-
/* emulate gcc's __SANITIZE_THREAD__ flag */
37-
#define __SANITIZE_THREAD__
56+
#ifdef __SANITIZE_THREAD__
3857
#define __no_sanitize_thread \
3958
__attribute__((no_sanitize("thread")))
4059
#else

0 commit comments

Comments
 (0)