@@ -944,13 +944,14 @@ class ModuleAddressSanitizer {
944
944
bool shouldInstrumentGlobal (GlobalVariable *G) const ;
945
945
bool ShouldUseMachOGlobalsSection () const ;
946
946
StringRef getGlobalMetadataSection () const ;
947
- void poisonOneInitializer (Function &GlobalInit, GlobalValue *ModuleName );
948
- void createInitializerPoisonCalls (GlobalValue *ModuleName );
947
+ void poisonOneInitializer (Function &GlobalInit);
948
+ void createInitializerPoisonCalls ();
949
949
uint64_t getMinRedzoneSizeForGlobal () const {
950
950
return getRedzoneSizeForScale (Mapping.Scale );
951
951
}
952
952
uint64_t getRedzoneSizeForGlobal (uint64_t SizeInBytes) const ;
953
953
int GetAsanVersion () const ;
954
+ GlobalVariable *getOrCreateModuleName ();
954
955
955
956
Module &M;
956
957
bool CompileKernel;
@@ -978,6 +979,7 @@ class ModuleAddressSanitizer {
978
979
979
980
Function *AsanCtorFunction = nullptr ;
980
981
Function *AsanDtorFunction = nullptr ;
982
+ GlobalVariable *ModuleName = nullptr ;
981
983
};
982
984
983
985
// Stack poisoning does not play well with exception handling.
@@ -1965,14 +1967,14 @@ void AddressSanitizer::instrumentUnusualSizeOrAlignment(
1965
1967
}
1966
1968
}
1967
1969
1968
- void ModuleAddressSanitizer::poisonOneInitializer (Function &GlobalInit,
1969
- GlobalValue *ModuleName) {
1970
+ void ModuleAddressSanitizer::poisonOneInitializer (Function &GlobalInit) {
1970
1971
// Set up the arguments to our poison/unpoison functions.
1971
1972
IRBuilder<> IRB (&GlobalInit.front (),
1972
1973
GlobalInit.front ().getFirstInsertionPt ());
1973
1974
1974
1975
// 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);
1976
1978
IRB.CreateCall (AsanPoisonGlobals, ModuleNameAddr);
1977
1979
1978
1980
// Add calls to unpoison all globals before each return instruction.
@@ -1981,8 +1983,7 @@ void ModuleAddressSanitizer::poisonOneInitializer(Function &GlobalInit,
1981
1983
CallInst::Create (AsanUnpoisonGlobals, " " , RI->getIterator ());
1982
1984
}
1983
1985
1984
- void ModuleAddressSanitizer::createInitializerPoisonCalls (
1985
- GlobalValue *ModuleName) {
1986
+ void ModuleAddressSanitizer::createInitializerPoisonCalls () {
1986
1987
GlobalVariable *GV = M.getGlobalVariable (" llvm.global_ctors" );
1987
1988
if (!GV)
1988
1989
return ;
@@ -2002,7 +2003,7 @@ void ModuleAddressSanitizer::createInitializerPoisonCalls(
2002
2003
// Don't instrument CTORs that will run before asan.module_ctor.
2003
2004
if (Priority->getLimitedValue () <= GetCtorAndDtorPriority (TargetTriple))
2004
2005
continue ;
2005
- poisonOneInitializer (*F, ModuleName );
2006
+ poisonOneInitializer (*F);
2006
2007
}
2007
2008
}
2008
2009
}
@@ -2539,14 +2540,6 @@ void ModuleAddressSanitizer::instrumentGlobals(IRBuilder<> &IRB,
2539
2540
2540
2541
bool HasDynamicallyInitializedGlobals = false ;
2541
2542
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
-
2550
2543
for (size_t i = 0 ; i < n; i++) {
2551
2544
GlobalVariable *G = GlobalsToChange[i];
2552
2545
@@ -2647,7 +2640,7 @@ void ModuleAddressSanitizer::instrumentGlobals(IRBuilder<> &IRB,
2647
2640
ConstantInt::get (IntptrTy, SizeInBytes),
2648
2641
ConstantInt::get (IntptrTy, SizeInBytes + RightRedzoneSize),
2649
2642
ConstantExpr::getPointerCast (Name, IntptrTy),
2650
- ConstantExpr::getPointerCast (ModuleName , IntptrTy),
2643
+ ConstantExpr::getPointerCast (getOrCreateModuleName () , IntptrTy),
2651
2644
ConstantInt::get (IntptrTy, MD.IsDynInit ),
2652
2645
Constant::getNullValue (IntptrTy),
2653
2646
ConstantExpr::getPointerCast (ODRIndicator, IntptrTy));
@@ -2694,7 +2687,7 @@ void ModuleAddressSanitizer::instrumentGlobals(IRBuilder<> &IRB,
2694
2687
2695
2688
// Create calls for poisoning before initializers run and unpoisoning after.
2696
2689
if (HasDynamicallyInitializedGlobals)
2697
- createInitializerPoisonCalls (ModuleName );
2690
+ createInitializerPoisonCalls ();
2698
2691
2699
2692
LLVM_DEBUG (dbgs () << M);
2700
2693
}
@@ -2734,6 +2727,17 @@ int ModuleAddressSanitizer::GetAsanVersion() const {
2734
2727
return Version;
2735
2728
}
2736
2729
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
+
2737
2741
bool ModuleAddressSanitizer::instrumentModule () {
2738
2742
initializeCallbacks ();
2739
2743
0 commit comments