Skip to content

[NFC][asan] Create ModuleName lazily #104729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all 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
40 changes: 22 additions & 18 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 @@ -2539,14 +2540,6 @@ void ModuleAddressSanitizer::instrumentGlobals(IRBuilder<> &IRB,

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,7 +2640,7 @@ 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));
Expand Down Expand Up @@ -2694,7 +2687,7 @@ void ModuleAddressSanitizer::instrumentGlobals(IRBuilder<> &IRB,

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

LLVM_DEBUG(dbgs() << M);
}
Expand Down Expand Up @@ -2734,6 +2727,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
Loading