yln created this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Kernel ASan is basically ASan with a few specific settings, but has a
seperate front-end flag. Provide a helper that checks for both, so new
comitters like myself are less likely to forget the Kernel variant.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D57722

Files:
  clang/include/clang/Basic/Features.def
  clang/include/clang/Basic/Sanitizers.h
  clang/lib/CodeGen/CGCall.cpp
  clang/lib/CodeGen/CodeGenFunction.cpp
  clang/lib/CodeGen/SanitizerMetadata.cpp

Index: clang/lib/CodeGen/SanitizerMetadata.cpp
===================================================================
--- clang/lib/CodeGen/SanitizerMetadata.cpp
+++ clang/lib/CodeGen/SanitizerMetadata.cpp
@@ -24,10 +24,7 @@
                                            SourceLocation Loc, StringRef Name,
                                            QualType Ty, bool IsDynInit,
                                            bool IsBlacklisted) {
-  if (!CGM.getLangOpts().Sanitize.hasOneOf(SanitizerKind::Address |
-                                           SanitizerKind::KernelAddress |
-                                           SanitizerKind::HWAddress |
-                                           SanitizerKind::KernelHWAddress))
+  if (!CGM.getLangOpts().Sanitize.hasASanOrKASanOrHWASanOrHWKASan())
     return;
   IsDynInit &= !CGM.isInSanitizerBlacklist(GV, Loc, Ty, "init");
   IsBlacklisted |= CGM.isInSanitizerBlacklist(GV, Loc, Ty);
@@ -58,10 +55,7 @@
 
 void SanitizerMetadata::reportGlobalToASan(llvm::GlobalVariable *GV,
                                            const VarDecl &D, bool IsDynInit) {
-  if (!CGM.getLangOpts().Sanitize.hasOneOf(SanitizerKind::Address |
-                                           SanitizerKind::KernelAddress |
-                                           SanitizerKind::HWAddress |
-                                           SanitizerKind::KernelHWAddress))
+  if (!CGM.getLangOpts().Sanitize.hasASanOrKASanOrHWASanOrHWKASan())
     return;
   std::string QualName;
   llvm::raw_string_ostream OS(QualName);
@@ -78,10 +72,7 @@
 void SanitizerMetadata::disableSanitizerForGlobal(llvm::GlobalVariable *GV) {
   // For now, just make sure the global is not modified by the ASan
   // instrumentation.
-  if (CGM.getLangOpts().Sanitize.hasOneOf(SanitizerKind::Address |
-                                          SanitizerKind::KernelAddress |
-                                          SanitizerKind::HWAddress |
-                                          SanitizerKind::KernelHWAddress))
+  if (CGM.getLangOpts().Sanitize.hasASanOrKASanOrHWASanOrHWKASan())
     reportGlobalToASan(GV, SourceLocation(), "", QualType(), false, true);
 }
 
Index: clang/lib/CodeGen/CodeGenFunction.cpp
===================================================================
--- clang/lib/CodeGen/CodeGenFunction.cpp
+++ clang/lib/CodeGen/CodeGenFunction.cpp
@@ -876,9 +876,9 @@
   }
 
   // Apply sanitizer attributes to the function.
-  if (SanOpts.hasOneOf(SanitizerKind::Address | SanitizerKind::KernelAddress))
+  if (SanOpts.hasASanOrKASan())
     Fn->addFnAttr(llvm::Attribute::SanitizeAddress);
-  if (SanOpts.hasOneOf(SanitizerKind::HWAddress | SanitizerKind::KernelHWAddress))
+  if (SanOpts.hasHWASanOrHWKASan())
     Fn->addFnAttr(llvm::Attribute::SanitizeHWAddress);
   if (SanOpts.has(SanitizerKind::Thread))
     Fn->addFnAttr(llvm::Attribute::SanitizeThread);
Index: clang/lib/CodeGen/CGCall.cpp
===================================================================
--- clang/lib/CodeGen/CGCall.cpp
+++ clang/lib/CodeGen/CGCall.cpp
@@ -4403,8 +4403,7 @@
 
       // Avoid incompatibility with ASan which relies on the `noreturn`
       // attribute to insert handler calls.
-      if (SanOpts.hasOneOf(SanitizerKind::Address |
-                           SanitizerKind::KernelAddress)) {
+      if (SanOpts.hasASanOrKASan()) {
         SanitizerScope SanScope(this);
         llvm::IRBuilder<>::InsertPointGuard IPGuard(Builder);
         Builder.SetInsertPoint(CI);
Index: clang/include/clang/Basic/Sanitizers.h
===================================================================
--- clang/include/clang/Basic/Sanitizers.h
+++ clang/include/clang/Basic/Sanitizers.h
@@ -70,6 +70,25 @@
 
   /// Bitmask of enabled sanitizers.
   SanitizerMask Mask = 0;
+
+  /// Check if ASan or Kernel ASan are enabled.
+  bool hasASanOrKASan() const {
+    return hasOneOf(SanitizerKind::Address | SanitizerKind::KernelAddress);
+  }
+
+  /// Check if Hardware ASan or Hardware Kernel ASan are enabled.
+  bool hasHWASanOrHWKASan() const {
+    return hasOneOf(SanitizerKind::HWAddress | SanitizerKind::KernelHWAddress);
+  }
+
+  /// Check if any of the following are enabled:
+  /// * ASan
+  /// * Kernel ASan
+  /// * Hardware ASan
+  /// * Hardware Kernel ASan
+  bool hasASanOrKASanOrHWASanOrHWKASan() const {
+    return hasASanOrKASan() || hasHWASanOrHWKASan();
+  }
 };
 
 /// Parse a single value from a -fsanitize= or -fno-sanitize= value list.
Index: clang/include/clang/Basic/Features.def
===================================================================
--- clang/include/clang/Basic/Features.def
+++ clang/include/clang/Basic/Features.def
@@ -36,12 +36,8 @@
 #define EXTENSION(Name, Predicate)
 #endif
 
-FEATURE(address_sanitizer,
-        LangOpts.Sanitize.hasOneOf(SanitizerKind::Address |
-                                   SanitizerKind::KernelAddress))
-FEATURE(hwaddress_sanitizer,
-        LangOpts.Sanitize.hasOneOf(SanitizerKind::HWAddress |
-                                   SanitizerKind::KernelHWAddress))
+FEATURE(address_sanitizer, LangOpts.Sanitize.hasASanOrKASan())
+FEATURE(hwaddress_sanitizer, LangOpts.Sanitize.hasHWASanOrHWKASan())
 FEATURE(xray_instrument, LangOpts.XRayInstrument)
 FEATURE(undefined_behavior_sanitizer,
         LangOpts.Sanitize.hasOneOf(SanitizerKind::Undefined))
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to