[PATCH] D119816: [SanitizerBounds] Add support for NoSanitizeBounds function

Marco Elver via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 25 04:42:36 PST 2022


melver added inline comments.


================
Comment at: clang/lib/CodeGen/CodeGenFunction.cpp:757
         SanOpts.set(SanitizerKind::HWAddress, false);
+      if (mask & SanitizerKind::LocalBounds)
+        Fn->addFnAttr(llvm::Attribute::NoSanitizeBounds);
----------------
These 2 checks can be reduced to 1 due to SanitizerKind::Bounds including both. However, as noted, this only affects local-bounds, so I'm not sure if we want to include both -- perhaps for completeness it makes sense, but in the array-bounds only case this attribute will be a nop (AFAIK).

Also, I think we don't want to attach the attribute if bounds checking isn't enabled -- at least it seems unnecessary to do so.

See the following suggested change:

```
diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp
index 842ce0245d45..c1f3a3014a19 100644
--- a/clang/lib/CodeGen/CodeGenFunction.cpp
+++ b/clang/lib/CodeGen/CodeGenFunction.cpp
@@ -740,6 +740,7 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
   } while (false);
 
   if (D) {
+    const bool SanitizeBounds = SanOpts.hasOneOf(SanitizerKind::Bounds);
     bool NoSanitizeCoverage = false;
 
     for (auto Attr : D->specific_attrs<NoSanitizeAttr>()) {
@@ -754,16 +755,15 @@ void CodeGenFunction::StartFunction(GlobalDecl GD, QualType RetTy,
         SanOpts.set(SanitizerKind::KernelHWAddress, false);
       if (mask & SanitizerKind::KernelHWAddress)
         SanOpts.set(SanitizerKind::HWAddress, false);
-      if (mask & SanitizerKind::LocalBounds)
-        Fn->addFnAttr(llvm::Attribute::NoSanitizeBounds);
-      if (mask & SanitizerKind::ArrayBounds)
-        Fn->addFnAttr(llvm::Attribute::NoSanitizeBounds);
 
       // SanitizeCoverage is not handled by SanOpts.
       if (Attr->hasCoverage())
         NoSanitizeCoverage = true;
     }
 
+    if (SanitizeBounds && !SanOpts.hasOneOf(SanitizerKind::Bounds))
+      Fn->addFnAttr(llvm::Attribute::NoSanitizeBounds);
+
     if (NoSanitizeCoverage && CGM.getCodeGenOpts().hasSanitizeCoverage())
       Fn->addFnAttr(llvm::Attribute::NoSanitizeCoverage);
   }

```


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D119816/new/

https://reviews.llvm.org/D119816



More information about the cfe-commits mailing list