[asan] don't instrument linker-initialized globals even with external linkage in...
authorKostya Serebryany <kcc@google.com>
Tue, 20 Nov 2012 13:11:32 +0000 (13:11 +0000)
committerKostya Serebryany <kcc@google.com>
Tue, 20 Nov 2012 13:11:32 +0000 (13:11 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@168367 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Instrumentation/AddressSanitizer.cpp
test/Instrumentation/AddressSanitizer/instrument_initializer_metadata.ll

index aad31b4c6ede5bbd85830fe8962c5728ed3b0643..4d5bb19aa84e684e0b390586ee20d603c1f6b3fd 100644 (file)
@@ -364,11 +364,9 @@ void AddressSanitizer::instrumentMop(Instruction *I) {
       if (!ClInitializers)
         return;
       // If a global variable does not have dynamic initialization we don't
-      // have to instrument it.  However, if a global has external linkage, we
-      // assume it has dynamic initialization, as it may have an initializer
-      // in a different TU.
-      if (G->getLinkage() != GlobalVariable::ExternalLinkage &&
-          !DynamicallyInitializedGlobals.Contains(G))
+      // have to instrument it.  However, if a global does not have initailizer
+      // at all, we assume it has dynamic initializer (in other TU).
+      if (G->hasInitializer() && !DynamicallyInitializedGlobals.Contains(G))
         return;
     }
   }
index fe13cd1b4e57f989151a187263a60b6b2e53df4a..c11a0498c3a310f872b69d04a73814fc58e973fc 100644 (file)
@@ -2,11 +2,14 @@
 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
 target triple = "x86_64-unknown-linux-gnu"
 @xxx = internal global i32 0, align 4  ; With dynamic initializer.
+@XXX = global i32 0, align 4           ; With dynamic initializer.
 @yyy = internal global i32 0, align 4  ; W/o dynamic initializer.
+@YYY = global i32 0, align 4           ; W/o dynamic initializer.
 ; Clang will emit the following metadata identifying @xxx as dynamically
 ; initialized.
 !0 = metadata !{i32* @xxx}
-!llvm.asan.dynamically_initialized_globals = !{!0}
+!1 = metadata !{i32* @XXX}
+!llvm.asan.dynamically_initialized_globals = !{!0, !1}
 
 define i32 @initializer() uwtable {
 entry:
@@ -45,6 +48,16 @@ define void @touch_xxx() address_safety {
 ; CHECK: ret void
 }
 
+; Check that XXX is instrumented.
+define void @touch_XXX() address_safety {
+  store i32 0, i32 *@XXX, align 4
+  ret void
+; CHECK: define void @touch_XXX
+; CHECK: call void @__asan_report_store4
+; CHECK: ret void
+}
+
+
 ; Check that yyy is NOT instrumented (as it does not have dynamic initializer).
 define void @touch_yyy() address_safety {
   store i32 0, i32 *@yyy, align 4
@@ -53,3 +66,12 @@ define void @touch_yyy() address_safety {
 ; CHECK-NOT: call void @__asan_report_store4
 ; CHECK: ret void
 }
+
+; Check that YYY is NOT instrumented (as it does not have dynamic initializer).
+define void @touch_YYY() address_safety {
+  store i32 0, i32 *@YYY, align 4
+  ret void
+; CHECK: define void @touch_YYY
+; CHECK-NOT: call void @__asan_report_store4
+; CHECK: ret void
+}