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 5 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
8 changes: 8 additions & 0 deletions clang/include/clang/Basic/Features.def
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,14 @@ 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_sanitizer, LangOpts.Sanitize.hasOneOf(SanitizerKind::CFI | SanitizerKind::CFICastStrict | SanitizerKind::KCFI))
Copy link
Contributor

Choose a reason for hiding this comment

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

FEATURE(cfi_sanitizer, LangOpts.Sanitize.hasOneOf(SanitizerKind::CFI))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in a new commit. Thanks!

FEATURE(cfi_cast_strict_sanitizer, LangOpts.Sanitize.has(SanitizerKind::CFICastStrict))
FEATURE(cfi_derived_cast_sanitizer, LangOpts.Sanitize.has(SanitizerKind::CFIDerivedCast))
FEATURE(cfi_icall_sanitizer, LangOpts.Sanitize.has(SanitizerKind::CFIICall))
FEATURE(cfi_mfcall_sanitizer, LangOpts.Sanitize.has(SanitizerKind::CFIMFCall))
FEATURE(cfi_unrelated_cast_sanitizer, LangOpts.Sanitize.has(SanitizerKind::CFIUnrelatedCast))
FEATURE(cfi_nvcall_sanitizer, LangOpts.Sanitize.has(SanitizerKind::CFINVCall))
FEATURE(cfi_vcall_sanitizer, LangOpts.Sanitize.has(SanitizerKind::CFIVCall))
FEATURE(kcfi, LangOpts.Sanitize.has(SanitizerKind::KCFI))
FEATURE(kcfi_arity, LangOpts.Sanitize.has(SanitizerKind::KCFI))
FEATURE(modules, LangOpts.Modules)
Expand Down
77 changes: 77 additions & 0 deletions clang/test/Lexer/has_feature_cfi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// RUN: %clang -E -fvisibility=hidden -flto -fno-sanitize-ignorelist -fsanitize=cfi -c %s -o - | FileCheck %s --check-prefix=CHECK-CFI
// RUN: %clang -E -fvisibility=hidden -flto -fno-sanitize-ignorelist -fsanitize=cfi -fsanitize-cfi-cross-dso -c %s -o - | FileCheck %s --check-prefix=CHECK-CFI
// RUN: %clang -E -fvisibility=hidden -flto -fno-sanitize-ignorelist -fsanitize=cfi -fno-sanitize=cfi-nvcall,cfi-vcall,cfi-mfcall,cfi-icall -c %s -o - | FileCheck %s --check-prefix=CHECK-CFI
// RUN: %clang -E -fsanitize=kcfi -c %s -o - | FileCheck %s --check-prefix=CHECK-CFI
// CHECK-CFI: CFISanitizerEnabled
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's keep kcfi separated for now before we decide to combine them.

// CHECK-KCFI: KCFISanitizerEnabled

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree to separate them, but I don't think we can change that later as soon as anyone uses that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good. Tried to fix it in a new commit.


// RUN: %clang -E -c %s -o - | FileCheck %s --check-prefix=CHECK-NO-CFI
// CHECK-NO-CFI: CFISanitizerDisabled

// RUN: %clang -E -fsanitize=cfi-cast-strict -c %s -o - | FileCheck %s --check-prefixes=CHECK-CFI,CHECK-CFI-CAST-STRICT
// CHECK-CFI-CAST-STRICT: CFICastStrictSanitizerEnabled

// RUN: %clang -E -fvisibility=hidden -flto -fno-sanitize-ignorelist -fsanitize=cfi-derived-cast -c %s -o - | FileCheck %s --check-prefixes=CHECK-CFI,CHECK-CFI-DERIVED-CAST
// CHECK-CFI-DERIVED-CAST: CFIDerivedCastSanitizerEnabled

// RUN: %clang -E -fvisibility=hidden -flto -fno-sanitize-ignorelist -fsanitize=cfi-icall -c %s -o - | FileCheck %s --check-prefixes=CHECK-CFI,CHECK-CFI-ICALL
// CHECK-CFI-ICALL: CFIICallSanitizerEnabled

// RUN: %clang -E -fvisibility=hidden -flto -fno-sanitize-ignorelist -fsanitize=cfi-mfcall -c %s -o - | FileCheck %s --check-prefixes=CHECK-CFI,CHECK-CFI-MFCALL
// CHECK-CFI-MFCALL: CFIMFCallSanitizerEnabled

// RUN: %clang -E -fvisibility=hidden -flto -fno-sanitize-ignorelist -fsanitize=cfi-unrelated-cast -c %s -o - | FileCheck %s --check-prefixes=CHECK-CFI,CHECK-CFI-UNRELATED-CAST
// CHECK-CFI-UNRELATED-CAST: CFIUnrelatedCastSanitizerEnabled

// RUN: %clang -E -fvisibility=hidden -flto -fno-sanitize-ignorelist -fsanitize=cfi-nvcall -c %s -o - | FileCheck %s --check-prefixes=CHECK-CFI,CHECK-CFI-NVCALL
// CHECK-CFI-NVCALL: CFINVCallSanitizerEnabled

// RUN: %clang -E -fvisibility=hidden -flto -fno-sanitize-ignorelist -fsanitize=cfi-vcall -c %s -o - | FileCheck %s --check-prefixes=CHECK-CFI,CHECK-CFI-VCALL
// CHECK-CFI-VCALL: CFIVCallSanitizerEnabled

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

#if __has_feature(cfi_cast_strict_sanitizer)
int CFICastStrictSanitizerEnabled();
#else
int CFICastStrictSanitizerDisabled();
#endif

#if __has_feature(cfi_derived_cast_sanitizer)
int CFIDerivedCastSanitizerEnabled();
#else
int CFIDerivedCastSanitizerDisabled();
#endif

#if __has_feature(cfi_icall_sanitizer)
int CFIICallSanitizerEnabled();
#else
int CFIICallSanitizerDisabled();
#endif

#if __has_feature(cfi_mfcall_sanitizer)
int CFIMFCallSanitizerEnabled();
#else
int CFIMFCallSanitizerDisabled();
#endif

#if __has_feature(cfi_unrelated_cast_sanitizer)
int CFIUnrelatedCastSanitizerEnabled();
#else
int CFIUnrelatedCastSanitizerDisabled();
#endif

#if __has_feature(cfi_nvcall_sanitizer)
int CFINVCallSanitizerEnabled();
#else
int CFINVCallSanitizerDisabled();
#endif

#if __has_feature(cfi_vcall_sanitizer)
int CFIVCallSanitizerEnabled();
#else
int CFIVCallSanitizerDisabled();
#endif