[PATCH] D92846: [KernelAddressSanitizer] Fix globals exclusion for indirect aliases

Marco Elver via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 8 07:48:44 PST 2020


melver updated this revision to Diff 310214.
melver added a comment.

Fix recursive case


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D92846

Files:
  llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp


Index: llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
===================================================================
--- llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
+++ llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
@@ -792,7 +792,8 @@
                                   StringRef InternalSuffix);
   Instruction *CreateAsanModuleDtor(Module &M);
 
-  bool canInstrumentAliasedGlobal(const GlobalAlias &GA) const;
+  const GlobalVariable *getExcludedAliasedGlobal(const GlobalAlias &GA,
+                                                 bool Rec = false) const;
   bool shouldInstrumentGlobal(GlobalVariable *G) const;
   bool ShouldUseMachOGlobalsSection() const;
   StringRef getGlobalMetadataSection() const;
@@ -1787,20 +1788,39 @@
   }
 }
 
-bool ModuleAddressSanitizer::canInstrumentAliasedGlobal(
-    const GlobalAlias &GA) const {
-  // In case this function should be expanded to include rules that do not just
-  // apply when CompileKernel is true, either guard all existing rules with an
-  // 'if (CompileKernel) { ... }' or be absolutely sure that all these rules
-  // should also apply to user space.
-  assert(CompileKernel && "Only expecting to be called when compiling kernel");
-
-  // When compiling the kernel, globals that are aliased by symbols prefixed
-  // by "__" are special and cannot be padded with a redzone.
-  if (GA.getName().startswith("__"))
-    return false;
+const GlobalVariable *
+ModuleAddressSanitizer::getExcludedAliasedGlobal(const GlobalAlias &GA,
+                                                 bool Rec) const {
+  if (!Rec) { // Non-recursive case.
+    // In case this function should be expanded to include rules that do not
+    // just apply when CompileKernel is true, either guard all existing rules
+    // with an 'if (CompileKernel) { ... }' or be absolutely sure that all these
+    // rules should also apply to user space.
+    assert(CompileKernel &&
+           "Only expecting to be called when compiling kernel");
+
+    // When compiling the kernel, globals that are aliased by symbols prefixed
+    // by "__" are special and cannot be padded with a redzone.
+    if (!GA.getName().startswith("__"))
+      return nullptr;
+  }
+
+  if (const auto *GV = dyn_cast<GlobalVariable>(GA.getAliasee())) {
+    // Find GlobalVariable from aliasee.
+    return GV;
+  } else if (const auto *CE = dyn_cast<ConstantExpr>(GA.getAliasee())) {
+    // Pointer expression into GlobalVariable; find it from one of the operands.
+    for (const Use &U : CE->operands()) {
+      if (const auto *GV = dyn_cast<GlobalVariable>(U))
+        return GV;
+    }
+  } else if (const auto *GAA = dyn_cast<GlobalAlias>(GA.getAliasee())) {
+    // Recursive GlobalAlias
+    return getExcludedAliasedGlobal(*GAA, true);
+  }
 
-  return true;
+  // Not a GlobalVariable alias, ignore.
+  return nullptr;
 }
 
 bool ModuleAddressSanitizer::shouldInstrumentGlobal(GlobalVariable *G) const {
@@ -2252,14 +2272,12 @@
   *CtorComdat = false;
 
   // Build set of globals that are aliased by some GA, where
-  // canInstrumentAliasedGlobal(GA) returns false.
+  // getExcludedAliasedGlobal(GA) returns the relevant GlobalVariable.
   SmallPtrSet<const GlobalVariable *, 16> AliasedGlobalExclusions;
   if (CompileKernel) {
     for (auto &GA : M.aliases()) {
-      if (const auto *GV = dyn_cast<GlobalVariable>(GA.getAliasee())) {
-        if (!canInstrumentAliasedGlobal(GA))
-          AliasedGlobalExclusions.insert(GV);
-      }
+      if (const GlobalVariable *GV = getExcludedAliasedGlobal(GA))
+        AliasedGlobalExclusions.insert(GV);
     }
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D92846.310214.patch
Type: text/x-patch
Size: 3638 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201208/3a381de1/attachment.bin>


More information about the llvm-commits mailing list