[llvm] 760ed8d - [AddressSanitizer] Only instrument globals of default address space

Karl-Johan Karlsson via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 30 01:46:14 PDT 2019


Author: Karl-Johan Karlsson
Date: 2019-10-30T09:32:19+01:00
New Revision: 760ed8da98e3c4cd80e92bbdcc78c181f36f71d4

URL: https://github.com/llvm/llvm-project/commit/760ed8da98e3c4cd80e92bbdcc78c181f36f71d4
DIFF: https://github.com/llvm/llvm-project/commit/760ed8da98e3c4cd80e92bbdcc78c181f36f71d4.diff

LOG: [AddressSanitizer] Only instrument globals of default address space

The address sanitizer ignore memory accesses from different address
spaces, however when instrumenting globals the check for different
address spaces is missing. This result in assertion failure. The fault
was found in an out of tree target.

The patch skip all globals of non default address space.

Reviewed By: leonardchan, vitalybuka

Differential Revision: https://reviews.llvm.org/D68790

Added: 
    llvm/test/Instrumentation/AddressSanitizer/global_addrspace.ll

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

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
index d92ee11c2e1a..554def59af1a 100644
--- a/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
@@ -1808,6 +1808,8 @@ bool ModuleAddressSanitizer::ShouldInstrumentGlobal(GlobalVariable *G) {
   if (GlobalsMD.get(G).IsBlacklisted) return false;
   if (!Ty->isSized()) return false;
   if (!G->hasInitializer()) return false;
+  // Only instrument globals of default address spaces
+  if (G->getAddressSpace()) return false;
   if (GlobalWasGeneratedByCompiler(G)) return false; // Our own globals.
   // Two problems with thread-locals:
   //   - The address of the main thread's copy can't be computed at link-time.

diff  --git a/llvm/test/Instrumentation/AddressSanitizer/global_addrspace.ll b/llvm/test/Instrumentation/AddressSanitizer/global_addrspace.ll
new file mode 100644
index 000000000000..19b76e651251
--- /dev/null
+++ b/llvm/test/Instrumentation/AddressSanitizer/global_addrspace.ll
@@ -0,0 +1,31 @@
+; Only verify that asan don't crash on global variables of 
diff erent
+; address space. The global variable should be unmodified by asan.
+
+; RUN: opt < %s -asan -asan-module -S | FileCheck %s
+
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+ at a = internal addrspace(42) global [1 x i32] zeroinitializer, align 4
+
+; CHECK: @a = internal addrspace(42) global [1 x i32] zeroinitializer, align 4
+
+define void @b(i32 %c) {
+entry:
+  %conv = sext i32 %c to i64
+  %0 = inttoptr i64 %conv to i32 addrspace(42)*
+  %cmp = icmp ugt i32 addrspace(42)* %0, getelementptr inbounds ([1 x i32], [1 x i32] addrspace(42)* @a, i64 0, i64 0)
+  br i1 %cmp, label %if.then, label %if.end
+
+if.then:
+  %call = tail call i32 (...) @e()
+  br label %if.end
+
+if.end:
+  ret void
+}
+
+declare i32 @e(...)
+
+!llvm.asan.globals = !{!0}
+!0 = !{[1 x i32] addrspace(42)* @a, null, !"a", i1 false, i1 false}


        


More information about the llvm-commits mailing list