[clang] df430c3 - [Frontend] Define __SANITIZE__ macros for kernel address variants (#156543)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Sep 8 11:34:01 PDT 2025
Author: Nathan Chancellor
Date: 2025-09-08T11:33:57-07:00
New Revision: df430c33a71ffb22b8d33d24538775c5fc9de6e1
URL: https://github.com/llvm/llvm-project/commit/df430c33a71ffb22b8d33d24538775c5fc9de6e1
DIFF: https://github.com/llvm/llvm-project/commit/df430c33a71ffb22b8d33d24538775c5fc9de6e1.diff
LOG: [Frontend] Define __SANITIZE__ macros for kernel address variants (#156543)
GCC defines these macros for both userspace and kernel address
sanitizers:
$ gcc -E -dM -fsanitize=address -x c /dev/null &| string match -er
SANITIZE
#define __SANITIZE_ADDRESS__ 1
$ gcc -E -dM -fsanitize=kernel-address -x c /dev/null &| string match
-er SANITIZE
#define __SANITIZE_ADDRESS__ 1
$ gcc -E -dM -fsanitize=hwaddress -x c /dev/null &| string match -er
SANITIZE
#define __SANITIZE_HWADDRESS__ 1
$ gcc -E -dM -fsanitize=kernel-hwaddress -x c /dev/null &| string match
-er SANITIZE
#define __SANITIZE_HWADDRESS__ 1
PR #153888 added these same defines for clang but only for the userspace
address sanitizers:
$ clang -E -dM -fsanitize=address -x c /dev/null &| string match -er
SANITIZE
#define __SANITIZE_ADDRESS__ 1
$ clang -E -dM -fsanitize=kernel-address -x c /dev/null &| string match
-er SANITIZE
$ clang -E -dM -fsanitize=hwaddress -x c /dev/null &| string match -er
SANITIZE
#define __SANITIZE_HWADDRESS__ 1
$ clang -E -dM -fsanitize=kernel-hwaddress -x c /dev/null &| string
match -er SANITIZE
Match GCC's behavior so that the Linux kernel can eventually drop its
own internal defines.
Added:
Modified:
clang/lib/Frontend/InitPreprocessor.cpp
clang/test/Preprocessor/sanitizer-predefines.c
Removed:
################################################################################
diff --git a/clang/lib/Frontend/InitPreprocessor.cpp b/clang/lib/Frontend/InitPreprocessor.cpp
index 4865c0b889044..e65c8b4f6facf 100644
--- a/clang/lib/Frontend/InitPreprocessor.cpp
+++ b/clang/lib/Frontend/InitPreprocessor.cpp
@@ -1519,9 +1519,11 @@ static void InitializePredefinedMacros(const TargetInfo &TI,
if (TI.getTriple().isOSBinFormatELF())
Builder.defineMacro("__ELF__");
- if (LangOpts.Sanitize.has(SanitizerKind::Address))
+ if (LangOpts.Sanitize.hasOneOf(SanitizerKind::Address |
+ SanitizerKind::KernelAddress))
Builder.defineMacro("__SANITIZE_ADDRESS__");
- if (LangOpts.Sanitize.has(SanitizerKind::HWAddress))
+ if (LangOpts.Sanitize.hasOneOf(SanitizerKind::HWAddress |
+ SanitizerKind::KernelHWAddress))
Builder.defineMacro("__SANITIZE_HWADDRESS__");
if (LangOpts.Sanitize.has(SanitizerKind::Thread))
Builder.defineMacro("__SANITIZE_THREAD__");
diff --git a/clang/test/Preprocessor/sanitizer-predefines.c b/clang/test/Preprocessor/sanitizer-predefines.c
index 9d2f6bf2517a2..d903a40de050f 100644
--- a/clang/test/Preprocessor/sanitizer-predefines.c
+++ b/clang/test/Preprocessor/sanitizer-predefines.c
@@ -1,7 +1,9 @@
// RUN: %clang_cc1 -E -dM -triple aarch64-unknown-linux -fsanitize=address %s | FileCheck %s --check-prefix=ASAN
+// RUN: %clang_cc1 -E -dM -triple aarch64-unknown-linux -fsanitize=kernel-address %s | FileCheck %s --check-prefix=ASAN
// ASAN: #define __SANITIZE_ADDRESS__ 1
// RUN: %clang_cc1 -E -dM -triple aarch64-unknown-linux -fsanitize=hwaddress %s | FileCheck %s --check-prefix=HWASAN
+// RUN: %clang_cc1 -E -dM -triple aarch64-unknown-linux -fsanitize=kernel-hwaddress %s | FileCheck %s --check-prefix=HWASAN
// HWASAN: #define __SANITIZE_HWADDRESS__ 1
// RUN: %clang_cc1 -E -dM -triple aarch64-unknown-linux -fsanitize=thread %s | FileCheck %s --check-prefix=TSAN
More information about the cfe-commits
mailing list