Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
47 changes: 23 additions & 24 deletions llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -944,13 +944,14 @@ class ModuleAddressSanitizer {
bool shouldInstrumentGlobal(GlobalVariable *G) const;
bool ShouldUseMachOGlobalsSection() const;
StringRef getGlobalMetadataSection() const;
void poisonOneInitializer(Function &GlobalInit, GlobalValue *ModuleName);
void createInitializerPoisonCalls(GlobalValue *ModuleName);
void poisonOneInitializer(Function &GlobalInit);
void createInitializerPoisonCalls();
uint64_t getMinRedzoneSizeForGlobal() const {
return getRedzoneSizeForScale(Mapping.Scale);
}
uint64_t getRedzoneSizeForGlobal(uint64_t SizeInBytes) const;
int GetAsanVersion() const;
GlobalVariable *getOrCreateModuleName();

Module &M;
bool CompileKernel;
Expand Down Expand Up @@ -978,6 +979,7 @@ class ModuleAddressSanitizer {

Function *AsanCtorFunction = nullptr;
Function *AsanDtorFunction = nullptr;
GlobalVariable *ModuleName = nullptr;
};

// Stack poisoning does not play well with exception handling.
Expand Down Expand Up @@ -1965,14 +1967,14 @@ void AddressSanitizer::instrumentUnusualSizeOrAlignment(
}
}

void ModuleAddressSanitizer::poisonOneInitializer(Function &GlobalInit,
GlobalValue *ModuleName) {
void ModuleAddressSanitizer::poisonOneInitializer(Function &GlobalInit) {
// Set up the arguments to our poison/unpoison functions.
IRBuilder<> IRB(&GlobalInit.front(),
GlobalInit.front().getFirstInsertionPt());

// Add a call to poison all external globals before the given function starts.
Value *ModuleNameAddr = ConstantExpr::getPointerCast(ModuleName, IntptrTy);
Value *ModuleNameAddr =
ConstantExpr::getPointerCast(getOrCreateModuleName(), IntptrTy);
IRB.CreateCall(AsanPoisonGlobals, ModuleNameAddr);

// Add calls to unpoison all globals before each return instruction.
Expand All @@ -1981,8 +1983,7 @@ void ModuleAddressSanitizer::poisonOneInitializer(Function &GlobalInit,
CallInst::Create(AsanUnpoisonGlobals, "", RI->getIterator());
}

void ModuleAddressSanitizer::createInitializerPoisonCalls(
GlobalValue *ModuleName) {
void ModuleAddressSanitizer::createInitializerPoisonCalls() {
GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors");
if (!GV)
return;
Expand All @@ -2002,7 +2003,7 @@ void ModuleAddressSanitizer::createInitializerPoisonCalls(
// Don't instrument CTORs that will run before asan.module_ctor.
if (Priority->getLimitedValue() <= GetCtorAndDtorPriority(TargetTriple))
continue;
poisonOneInitializer(*F, ModuleName);
poisonOneInitializer(*F);
}
}
}
Expand Down Expand Up @@ -2537,16 +2538,6 @@ void ModuleAddressSanitizer::instrumentGlobals(IRBuilder<> &IRB,
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,
genName("module"))
: nullptr;

for (size_t i = 0; i < n; i++) {
GlobalVariable *G = GlobalsToChange[i];

Expand Down Expand Up @@ -2647,14 +2638,11 @@ void ModuleAddressSanitizer::instrumentGlobals(IRBuilder<> &IRB,
ConstantInt::get(IntptrTy, SizeInBytes),
ConstantInt::get(IntptrTy, SizeInBytes + RightRedzoneSize),
ConstantExpr::getPointerCast(Name, IntptrTy),
ConstantExpr::getPointerCast(ModuleName, IntptrTy),
ConstantExpr::getPointerCast(getOrCreateModuleName(), IntptrTy),
ConstantInt::get(IntptrTy, MD.IsDynInit),
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 @@ -2693,8 +2681,8 @@ void ModuleAddressSanitizer::instrumentGlobals(IRBuilder<> &IRB,
}

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

LLVM_DEBUG(dbgs() << M);
}
Expand Down Expand Up @@ -2734,6 +2722,17 @@ int ModuleAddressSanitizer::GetAsanVersion() const {
return Version;
}

GlobalVariable *ModuleAddressSanitizer::getOrCreateModuleName() {
if (!ModuleName) {
// We shouldn't merge same module names, as this string serves as unique
// module ID in runtime.
ModuleName =
createPrivateGlobalForString(M, M.getModuleIdentifier(),
/*AllowMerging*/ false, genName("module"));
}
return ModuleName;
}

bool ModuleAddressSanitizer::instrumentModule() {
initializeCallbacks();

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_module 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