Skip to content

Commit 5af3dfb

Browse files
authored
[NFC][asan] Create ModuleName lazily (llvm#104729)
Avoids tracking conditions when it's needed.
1 parent 6a125c7 commit 5af3dfb

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -944,13 +944,14 @@ class ModuleAddressSanitizer {
944944
bool shouldInstrumentGlobal(GlobalVariable *G) const;
945945
bool ShouldUseMachOGlobalsSection() const;
946946
StringRef getGlobalMetadataSection() const;
947-
void poisonOneInitializer(Function &GlobalInit, GlobalValue *ModuleName);
948-
void createInitializerPoisonCalls(GlobalValue *ModuleName);
947+
void poisonOneInitializer(Function &GlobalInit);
948+
void createInitializerPoisonCalls();
949949
uint64_t getMinRedzoneSizeForGlobal() const {
950950
return getRedzoneSizeForScale(Mapping.Scale);
951951
}
952952
uint64_t getRedzoneSizeForGlobal(uint64_t SizeInBytes) const;
953953
int GetAsanVersion() const;
954+
GlobalVariable *getOrCreateModuleName();
954955

955956
Module &M;
956957
bool CompileKernel;
@@ -978,6 +979,7 @@ class ModuleAddressSanitizer {
978979

979980
Function *AsanCtorFunction = nullptr;
980981
Function *AsanDtorFunction = nullptr;
982+
GlobalVariable *ModuleName = nullptr;
981983
};
982984

983985
// Stack poisoning does not play well with exception handling.
@@ -1965,14 +1967,14 @@ void AddressSanitizer::instrumentUnusualSizeOrAlignment(
19651967
}
19661968
}
19671969

1968-
void ModuleAddressSanitizer::poisonOneInitializer(Function &GlobalInit,
1969-
GlobalValue *ModuleName) {
1970+
void ModuleAddressSanitizer::poisonOneInitializer(Function &GlobalInit) {
19701971
// Set up the arguments to our poison/unpoison functions.
19711972
IRBuilder<> IRB(&GlobalInit.front(),
19721973
GlobalInit.front().getFirstInsertionPt());
19731974

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

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

1984-
void ModuleAddressSanitizer::createInitializerPoisonCalls(
1985-
GlobalValue *ModuleName) {
1986+
void ModuleAddressSanitizer::createInitializerPoisonCalls() {
19861987
GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors");
19871988
if (!GV)
19881989
return;
@@ -2002,7 +2003,7 @@ void ModuleAddressSanitizer::createInitializerPoisonCalls(
20022003
// Don't instrument CTORs that will run before asan.module_ctor.
20032004
if (Priority->getLimitedValue() <= GetCtorAndDtorPriority(TargetTriple))
20042005
continue;
2005-
poisonOneInitializer(*F, ModuleName);
2006+
poisonOneInitializer(*F);
20062007
}
20072008
}
20082009
}
@@ -2539,14 +2540,6 @@ void ModuleAddressSanitizer::instrumentGlobals(IRBuilder<> &IRB,
25392540

25402541
bool HasDynamicallyInitializedGlobals = false;
25412542

2542-
// We shouldn't merge same module names, as this string serves as unique
2543-
// module ID in runtime.
2544-
GlobalVariable *ModuleName =
2545-
n != 0 ? createPrivateGlobalForString(M, M.getModuleIdentifier(),
2546-
/*AllowMerging*/ false,
2547-
genName("module"))
2548-
: nullptr;
2549-
25502543
for (size_t i = 0; i < n; i++) {
25512544
GlobalVariable *G = GlobalsToChange[i];
25522545

@@ -2647,7 +2640,7 @@ void ModuleAddressSanitizer::instrumentGlobals(IRBuilder<> &IRB,
26472640
ConstantInt::get(IntptrTy, SizeInBytes),
26482641
ConstantInt::get(IntptrTy, SizeInBytes + RightRedzoneSize),
26492642
ConstantExpr::getPointerCast(Name, IntptrTy),
2650-
ConstantExpr::getPointerCast(ModuleName, IntptrTy),
2643+
ConstantExpr::getPointerCast(getOrCreateModuleName(), IntptrTy),
26512644
ConstantInt::get(IntptrTy, MD.IsDynInit),
26522645
Constant::getNullValue(IntptrTy),
26532646
ConstantExpr::getPointerCast(ODRIndicator, IntptrTy));
@@ -2694,7 +2687,7 @@ void ModuleAddressSanitizer::instrumentGlobals(IRBuilder<> &IRB,
26942687

26952688
// Create calls for poisoning before initializers run and unpoisoning after.
26962689
if (HasDynamicallyInitializedGlobals)
2697-
createInitializerPoisonCalls(ModuleName);
2690+
createInitializerPoisonCalls();
26982691

26992692
LLVM_DEBUG(dbgs() << M);
27002693
}
@@ -2734,6 +2727,17 @@ int ModuleAddressSanitizer::GetAsanVersion() const {
27342727
return Version;
27352728
}
27362729

2730+
GlobalVariable *ModuleAddressSanitizer::getOrCreateModuleName() {
2731+
if (!ModuleName) {
2732+
// We shouldn't merge same module names, as this string serves as unique
2733+
// module ID in runtime.
2734+
ModuleName =
2735+
createPrivateGlobalForString(M, M.getModuleIdentifier(),
2736+
/*AllowMerging*/ false, genName("module"));
2737+
}
2738+
return ModuleName;
2739+
}
2740+
27372741
bool ModuleAddressSanitizer::instrumentModule() {
27382742
initializeCallbacks();
27392743

0 commit comments

Comments
 (0)