Skip to content

[Clang][Basic] Add __has_feature checks for CFI sanitizers #151348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Aug 8, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clang/include/clang/Basic/Features.def
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ FEATURE(is_trivially_assignable, LangOpts.CPlusPlus)
FEATURE(is_trivially_constructible, LangOpts.CPlusPlus)
FEATURE(is_trivially_copyable, LangOpts.CPlusPlus)
FEATURE(is_union, LangOpts.CPlusPlus)
FEATURE(cfi, LangOpts.Sanitize.hasOneOf(SanitizerKind::CFI | SanitizerKind::KCFI))
FEATURE(kcfi, LangOpts.Sanitize.has(SanitizerKind::KCFI))
FEATURE(kcfi_arity, LangOpts.Sanitize.has(SanitizerKind::KCFI))
FEATURE(modules, LangOpts.Modules)
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/Basic/Sanitizers.def
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ SANITIZER("cfi-nvcall", CFINVCall)
SANITIZER("cfi-vcall", CFIVCall)
SANITIZER_GROUP("cfi", CFI,
CFIDerivedCast | CFIICall | CFIMFCall | CFIUnrelatedCast |
CFINVCall | CFIVCall)
CFINVCall | CFIVCall | CFICastStrict)

// Kernel Control Flow Integrity
SANITIZER("kcfi", KCFI)
Expand Down
31 changes: 31 additions & 0 deletions clang/test/Lexer/has_feature_cfi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// RUN: %clang_cc1 -E -fsanitize=cfi-cast-strict -o - %s | FileCheck --check-prefix=CHECK-CFISAN %s
// RUN: %clang_cc1 -E -fsanitize=cfi-derived-cast -o - %s | FileCheck --check-prefix=CHECK-CFISAN %s
// RUN: %clang_cc1 -E -fsanitize=cfi-icall -o - %s | FileCheck --check-prefix=CHECK-CFISAN %s
// RUN: %clang_cc1 -E -fsanitize=cfi-mfcall -o - %s | FileCheck --check-prefix=CHECK-CFISAN %s
// RUN: %clang_cc1 -E -fsanitize=cfi-unrelated-cast -o - %s | FileCheck --check-prefix=CHECK-CFISAN %s
// RUN: %clang_cc1 -E -fsanitize=cfi-nvcall -o - %s | FileCheck --check-prefix=CHECK-CFISAN %s
// RUN: %clang_cc1 -E -fsanitize=cfi-vcall -o - %s | FileCheck --check-prefix=CHECK-CFISAN %s

// RUN: %clang_cc1 -E -fsanitize=kcfi -o - %s | FileCheck --check-prefix=CHECK-CFISAN %s

// RUN: %clang -E --target=x86_64-linux-gnu -fvisibility=hidden -fno-sanitize-ignorelist -fsanitize=cfi -flto -c %s -o - | FileCheck %s --check-prefix=CHECK-CFISAN
// RUN: %clang -E --target=x86_64-linux-gnu -fvisibility=hidden -fno-sanitize-ignorelist -fsanitize=cfi -fsanitize-cfi-cross-dso -flto -c %s -o - | FileCheck %s --check-prefix=CHECK-CFISAN

// Disable CFI sanitizers.
// RUN: %clang -E --target=x86_64-linux-gnu -fvisibility=hidden -fno-sanitize=cfi -flto -c %s -o - | FileCheck %s --check-prefix=CHECK-NO-CFISAN

// Disable some but not all CFI schemes.
// RUN: %clang -E --target=x86_64-linux-gnu -fvisibility=hidden -fno-sanitize-ignorelist -fsanitize=cfi -fno-sanitize-cfi-cross-dso -fno-sanitize=cfi-nvcall,cfi-vcall,cfi-mfcall,cfi-icall -flto -c %s -o - | FileCheck %s --check-prefix=CHECK-CFISAN

// Disable all CFI schemes. This essentially disables CFI sanitizers.
// RUN: %clang -E --target=x86_64-linux-gnu -fvisibility=hidden -fsanitize=cfi -fno-sanitize-cfi-cross-dso -fno-sanitize=cfi-nvcall,cfi-vcall,cfi-mfcall,cfi-icall,cfi-cast-strict,cfi-derived-cast,cfi-unrelated-cast -flto -c %s -o - | FileCheck %s --check-prefix=CHECK-NO-CFISAN
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're testing for x86_64-linux-gnu only, you'll probably want // REQUIRES: x86-registered-target also

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought, since I'm just running the preprocessor (i.e. clang -E), I probably don't need to set the target at all. I removed the target specification in the new commit. Let me know if you would like me to keep it and require x86-registered-target.


#if __has_feature(cfi)
int CFISanitizerEnabled();
#else
int CFISanitizerDisabled();
#endif

// CHECK-CFISAN: CFISanitizerEnabled
// CHECK-NO-CFISAN: CFISanitizerDisabled

Loading