Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit ca23d43

Browse files
committed
[asan] make sure that linker-initialized globals (non-extern) are not instrumented even in -asan-initialization-order mode. This time with a test
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@168366 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 426fe8b commit ca23d43

File tree

2 files changed

+51
-32
lines changed

2 files changed

+51
-32
lines changed

lib/Transforms/Instrumentation/AddressSanitizer.cpp

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,32 @@ static cl::opt<int> ClDebugMax("asan-debug-max", cl::desc("Debug man inst"),
148148
cl::Hidden, cl::init(-1));
149149

150150
namespace {
151+
/// A set of dynamically initialized globals extracted from metadata.
152+
class SetOfDynamicallyInitializedGlobals {
153+
public:
154+
void Init(Module& M) {
155+
// Clang generates metadata identifying all dynamically initialized globals.
156+
NamedMDNode *DynamicGlobals =
157+
M.getNamedMetadata("llvm.asan.dynamically_initialized_globals");
158+
if (!DynamicGlobals)
159+
return;
160+
for (int i = 0, n = DynamicGlobals->getNumOperands(); i < n; ++i) {
161+
MDNode *MDN = DynamicGlobals->getOperand(i);
162+
assert(MDN->getNumOperands() == 1);
163+
Value *VG = MDN->getOperand(0);
164+
// The optimizer may optimize away a global entirely, in which case we
165+
// cannot instrument access to it.
166+
if (!VG)
167+
continue;
168+
DynInitGlobals.insert(cast<GlobalVariable>(VG));
169+
}
170+
}
171+
bool Contains(GlobalVariable *G) { return DynInitGlobals.count(G) != 0; }
172+
private:
173+
SmallSet<GlobalValue*, 32> DynInitGlobals;
174+
};
175+
176+
151177
/// AddressSanitizer: instrument the code in module to find memory bugs.
152178
struct AddressSanitizer : public FunctionPass {
153179
AddressSanitizer();
@@ -195,7 +221,6 @@ struct AddressSanitizer : public FunctionPass {
195221
Value *ShadowBase, bool DoPoison);
196222
bool LooksLikeCodeInBug11395(Instruction *I);
197223
void FindDynamicInitializers(Module &M);
198-
bool HasDynamicInitializer(GlobalVariable *G);
199224

200225
LLVMContext *C;
201226
DataLayout *TD;
@@ -214,8 +239,8 @@ struct AddressSanitizer : public FunctionPass {
214239
// This array is indexed by AccessIsWrite and log2(AccessSize).
215240
Function *AsanErrorCallback[2][kNumberOfAccessSizes];
216241
InlineAsm *EmptyAsm;
217-
SmallSet<GlobalValue*, 32> DynamicallyInitializedGlobals;
218242
SmallSet<GlobalValue*, 32> GlobalsCreatedByAsan;
243+
SetOfDynamicallyInitializedGlobals DynamicallyInitializedGlobals;
219244
};
220245

221246
} // namespace
@@ -328,30 +353,6 @@ static Value *isInterestingMemoryAccess(Instruction *I, bool *IsWrite) {
328353
return NULL;
329354
}
330355

331-
void AddressSanitizer::FindDynamicInitializers(Module& M) {
332-
// Clang generates metadata identifying all dynamically initialized globals.
333-
NamedMDNode *DynamicGlobals =
334-
M.getNamedMetadata("llvm.asan.dynamically_initialized_globals");
335-
if (!DynamicGlobals)
336-
return;
337-
for (int i = 0, n = DynamicGlobals->getNumOperands(); i < n; ++i) {
338-
MDNode *MDN = DynamicGlobals->getOperand(i);
339-
assert(MDN->getNumOperands() == 1);
340-
Value *VG = MDN->getOperand(0);
341-
// The optimizer may optimize away a global entirely, in which case we
342-
// cannot instrument access to it.
343-
if (!VG)
344-
continue;
345-
346-
GlobalVariable *G = cast<GlobalVariable>(VG);
347-
DynamicallyInitializedGlobals.insert(G);
348-
}
349-
}
350-
// Returns true if a global variable is initialized dynamically in this TU.
351-
bool AddressSanitizer::HasDynamicInitializer(GlobalVariable *G) {
352-
return DynamicallyInitializedGlobals.count(G);
353-
}
354-
355356
void AddressSanitizer::instrumentMop(Instruction *I) {
356357
bool IsWrite = false;
357358
Value *Addr = isInterestingMemoryAccess(I, &IsWrite);
@@ -367,7 +368,7 @@ void AddressSanitizer::instrumentMop(Instruction *I) {
367368
// assume it has dynamic initialization, as it may have an initializer
368369
// in a different TU.
369370
if (G->getLinkage() != GlobalVariable::ExternalLinkage &&
370-
!HasDynamicInitializer(G))
371+
!DynamicallyInitializedGlobals.Contains(G))
371372
return;
372373
}
373374
}
@@ -590,9 +591,6 @@ bool AddressSanitizer::insertGlobalRedzones(Module &M) {
590591

591592
IRBuilder<> IRB(CtorInsertBefore);
592593

593-
if (ClInitializers)
594-
FindDynamicInitializers(M);
595-
596594
// The addresses of the first and last dynamically initialized globals in
597595
// this TU. Used in initialization order checking.
598596
Value *FirstDynamic = 0, *LastDynamic = 0;
@@ -606,7 +604,8 @@ bool AddressSanitizer::insertGlobalRedzones(Module &M) {
606604
(RedzoneSize - (SizeInBytes % RedzoneSize));
607605
Type *RightRedZoneTy = ArrayType::get(IRB.getInt8Ty(), RightRedzoneSize);
608606
// Determine whether this global should be poisoned in initialization.
609-
bool GlobalHasDynamicInitializer = HasDynamicInitializer(G);
607+
bool GlobalHasDynamicInitializer =
608+
DynamicallyInitializedGlobals.Contains(G);
610609
// Don't check initialization order if this global is blacklisted.
611610
GlobalHasDynamicInitializer &= !BL->isInInit(*G);
612611

@@ -704,6 +703,7 @@ bool AddressSanitizer::doInitialization(Module &M) {
704703
if (!TD)
705704
return false;
706705
BL.reset(new BlackList(ClBlackListFile));
706+
DynamicallyInitializedGlobals.Init(M);
707707

708708
C = &(M.getContext());
709709
LongSize = TD->getPointerSizeInBits();

test/Instrumentation/AddressSanitizer/instrument_initializer_metadata.ll

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
; RUN: opt < %s -asan -asan-initialization-order -S | FileCheck %s
22
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"
33
target triple = "x86_64-unknown-linux-gnu"
4-
@xxx = global i32 0, align 4
4+
@xxx = internal global i32 0, align 4 ; With dynamic initializer.
5+
@yyy = internal global i32 0, align 4 ; W/o dynamic initializer.
56
; Clang will emit the following metadata identifying @xxx as dynamically
67
; initialized.
78
!0 = metadata !{i32* @xxx}
@@ -34,3 +35,21 @@ entry:
3435
; CHECK: call void @__cxx_global_var_init
3536
; CHECK: call void @__asan_after_dynamic_init
3637
; CHECK: ret
38+
39+
; Check that xxx is instrumented.
40+
define void @touch_xxx() address_safety {
41+
store i32 0, i32 *@xxx, align 4
42+
ret void
43+
; CHECK: define void @touch_xxx
44+
; CHECK: call void @__asan_report_store4
45+
; CHECK: ret void
46+
}
47+
48+
; Check that yyy is NOT instrumented (as it does not have dynamic initializer).
49+
define void @touch_yyy() address_safety {
50+
store i32 0, i32 *@yyy, align 4
51+
ret void
52+
; CHECK: define void @touch_yyy
53+
; CHECK-NOT: call void @__asan_report_store4
54+
; CHECK: ret void
55+
}

0 commit comments

Comments
 (0)