Skip to content

Commit

Permalink
[asan] Catch initialization-order-fiasco in modules without globals (
Browse files Browse the repository at this point in the history
…#104621)

Those modules still can have global constructors and access
globals in other modules which are not initialized yet.
  • Loading branch information
vitalybuka authored Aug 17, 2024
1 parent 6e0fc15 commit f44f026
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// RUN: %clangxx_asan %min_macos_deployment_target=10.11 -O0 %s %p/Helpers/initialization-bug-extra.cpp -o %t
// RUN: %env_asan_opts=check_initialization_order=true:strict_init_order=true not %run %t 2>&1 | FileCheck %s

// Not implemented.
// XFAIL: *

// Do not test with optimization -- the error may be optimized away.

// FIXME: https://code.google.com/p/address-sanitizer/issues/detail?id=186
Expand Down
14 changes: 3 additions & 11 deletions llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2531,15 +2531,10 @@ void ModuleAddressSanitizer::instrumentGlobals(IRBuilder<> &IRB, Module &M,
SmallVector<GlobalVariable *, 16> NewGlobals(n);
SmallVector<Constant *, 16> Initializers(n);

bool HasDynamicallyInitializedGlobals = false;

// We shouldn't merge same module names, as this string serves as unique
// module ID in runtime.
GlobalVariable *ModuleName =
n != 0
? createPrivateGlobalForString(M, M.getModuleIdentifier(),
/*AllowMerging*/ false, kAsanGenPrefix)
: nullptr;
GlobalVariable *ModuleName = createPrivateGlobalForString(
M, M.getModuleIdentifier(), /*AllowMerging*/ false, kAsanGenPrefix);

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

if (ClInitializers && MD.IsDynInit)
HasDynamicallyInitializedGlobals = true;

LLVM_DEBUG(dbgs() << "NEW GLOBAL: " << *NewGlobal << "\n");

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

// Create calls for poisoning before initializers run and unpoisoning after.
if (HasDynamicallyInitializedGlobals)
if (ClInitializers)
createInitializerPoisonCalls(M, ModuleName);

LLVM_DEBUG(dbgs() << M);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ define internal void @__late_ctor() sanitize_address section ".text.startup" {
; CHECK-LABEL: define internal void @__late_ctor(
; CHECK-SAME: ) #[[ATTR1:[0-9]+]] section ".text.startup" {
; CHECK-NEXT: [[ENTRY:.*:]]
; CHECK-NEXT: call void @__asan_before_dynamic_init(i64 ptrtoint (ptr @___asan_gen_ to i64))
; CHECK-NEXT: call void @initializer()
; CHECK-NEXT: call void @__asan_after_dynamic_init()
; CHECK-NEXT: ret void
;
; NOINIT-LABEL: define internal void @__late_ctor(
Expand Down

0 comments on commit f44f026

Please sign in to comment.