From 407790604b8f71f7172bbdfb76c27e1799d241c2 Mon Sep 17 00:00:00 2001 From: Kostya Serebryany Date: Tue, 20 Nov 2012 13:11:32 +0000 Subject: [PATCH] [asan] don't instrument linker-initialized globals even with external linkage in -asan-initialization-order mode git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@168367 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../Instrumentation/AddressSanitizer.cpp | 8 +++---- .../instrument_initializer_metadata.ll | 24 ++++++++++++++++++- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/lib/Transforms/Instrumentation/AddressSanitizer.cpp b/lib/Transforms/Instrumentation/AddressSanitizer.cpp index aad31b4c6ed..4d5bb19aa84 100644 --- a/lib/Transforms/Instrumentation/AddressSanitizer.cpp +++ b/lib/Transforms/Instrumentation/AddressSanitizer.cpp @@ -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; } } diff --git a/test/Instrumentation/AddressSanitizer/instrument_initializer_metadata.ll b/test/Instrumentation/AddressSanitizer/instrument_initializer_metadata.ll index fe13cd1b4e5..c11a0498c3a 100644 --- a/test/Instrumentation/AddressSanitizer/instrument_initializer_metadata.ll +++ b/test/Instrumentation/AddressSanitizer/instrument_initializer_metadata.ll @@ -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 +}