Skip to content

Commit f44f026

Browse files
authored
[asan] Catch initialization-order-fiasco in modules without globals (#104621)
Those modules still can have global constructors and access globals in other modules which are not initialized yet.
1 parent 6e0fc15 commit f44f026

File tree

3 files changed

+5
-14
lines changed

3 files changed

+5
-14
lines changed

compiler-rt/test/asan/TestCases/initialization-bug-no-global.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// RUN: %clangxx_asan %min_macos_deployment_target=10.11 -O0 %s %p/Helpers/initialization-bug-extra.cpp -o %t
22
// RUN: %env_asan_opts=check_initialization_order=true:strict_init_order=true not %run %t 2>&1 | FileCheck %s
33

4-
// Not implemented.
5-
// XFAIL: *
6-
74
// Do not test with optimization -- the error may be optimized away.
85

96
// FIXME: https://code.google.com/p/address-sanitizer/issues/detail?id=186

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2531,15 +2531,10 @@ void ModuleAddressSanitizer::instrumentGlobals(IRBuilder<> &IRB, Module &M,
25312531
SmallVector<GlobalVariable *, 16> NewGlobals(n);
25322532
SmallVector<Constant *, 16> Initializers(n);
25332533

2534-
bool HasDynamicallyInitializedGlobals = false;
2535-
25362534
// We shouldn't merge same module names, as this string serves as unique
25372535
// module ID in runtime.
2538-
GlobalVariable *ModuleName =
2539-
n != 0
2540-
? createPrivateGlobalForString(M, M.getModuleIdentifier(),
2541-
/*AllowMerging*/ false, kAsanGenPrefix)
2542-
: nullptr;
2536+
GlobalVariable *ModuleName = createPrivateGlobalForString(
2537+
M, M.getModuleIdentifier(), /*AllowMerging*/ false, kAsanGenPrefix);
25432538

25442539
for (size_t i = 0; i < n; i++) {
25452540
GlobalVariable *G = GlobalsToChange[i];
@@ -2646,9 +2641,6 @@ void ModuleAddressSanitizer::instrumentGlobals(IRBuilder<> &IRB, Module &M,
26462641
Constant::getNullValue(IntptrTy),
26472642
ConstantExpr::getPointerCast(ODRIndicator, IntptrTy));
26482643

2649-
if (ClInitializers && MD.IsDynInit)
2650-
HasDynamicallyInitializedGlobals = true;
2651-
26522644
LLVM_DEBUG(dbgs() << "NEW GLOBAL: " << *NewGlobal << "\n");
26532645

26542646
Initializers[i] = Initializer;
@@ -2688,7 +2680,7 @@ void ModuleAddressSanitizer::instrumentGlobals(IRBuilder<> &IRB, Module &M,
26882680
}
26892681

26902682
// Create calls for poisoning before initializers run and unpoisoning after.
2691-
if (HasDynamicallyInitializedGlobals)
2683+
if (ClInitializers)
26922684
createInitializerPoisonCalls(M, ModuleName);
26932685

26942686
LLVM_DEBUG(dbgs() << M);

llvm/test/Instrumentation/AddressSanitizer/instrument_initializer_without_global.ll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ define internal void @__late_ctor() sanitize_address section ".text.startup" {
1818
; CHECK-LABEL: define internal void @__late_ctor(
1919
; CHECK-SAME: ) #[[ATTR1:[0-9]+]] section ".text.startup" {
2020
; CHECK-NEXT: [[ENTRY:.*:]]
21+
; CHECK-NEXT: call void @__asan_before_dynamic_init(i64 ptrtoint (ptr @___asan_gen_ to i64))
2122
; CHECK-NEXT: call void @initializer()
23+
; CHECK-NEXT: call void @__asan_after_dynamic_init()
2224
; CHECK-NEXT: ret void
2325
;
2426
; NOINIT-LABEL: define internal void @__late_ctor(

0 commit comments

Comments
 (0)