@@ -719,7 +719,8 @@ struct AddressSanitizer {
719
719
bool Recover = false , bool UseAfterScope = false ,
720
720
AsanDetectStackUseAfterReturnMode UseAfterReturn =
721
721
AsanDetectStackUseAfterReturnMode::Runtime)
722
- : CompileKernel(ClEnableKasan.getNumOccurrences() > 0 ? ClEnableKasan
722
+ : M(M),
723
+ CompileKernel (ClEnableKasan.getNumOccurrences() > 0 ? ClEnableKasan
723
724
: CompileKernel),
724
725
Recover(ClRecover.getNumOccurrences() > 0 ? ClRecover : Recover),
725
726
UseAfterScope(UseAfterScope || ClUseAfterScope),
@@ -803,7 +804,7 @@ struct AddressSanitizer {
803
804
private:
804
805
friend struct FunctionStackPoisoner ;
805
806
806
- void initializeCallbacks (Module &M, const TargetLibraryInfo *TLI);
807
+ void initializeCallbacks (const TargetLibraryInfo *TLI);
807
808
808
809
bool LooksLikeCodeInBug11395 (Instruction *I);
809
810
bool GlobalIsLinkerInitialized (GlobalVariable *G);
@@ -826,6 +827,7 @@ struct AddressSanitizer {
826
827
}
827
828
};
828
829
830
+ Module &M;
829
831
LLVMContext *C;
830
832
const DataLayout *DL;
831
833
Triple TargetTriple;
@@ -868,7 +870,8 @@ class ModuleAddressSanitizer {
868
870
bool UseGlobalsGC = true , bool UseOdrIndicator = true ,
869
871
AsanDtorKind DestructorKind = AsanDtorKind::Global,
870
872
AsanCtorKind ConstructorKind = AsanCtorKind::Global)
871
- : CompileKernel(ClEnableKasan.getNumOccurrences() > 0 ? ClEnableKasan
873
+ : M(M),
874
+ CompileKernel (ClEnableKasan.getNumOccurrences() > 0 ? ClEnableKasan
872
875
: CompileKernel),
873
876
InsertVersionCheck(ClInsertVersionCheck.getNumOccurrences() > 0
874
877
? ClInsertVersionCheck
@@ -906,45 +909,46 @@ class ModuleAddressSanitizer {
906
909
assert (this ->DestructorKind != AsanDtorKind::Invalid);
907
910
}
908
911
909
- bool instrumentModule (Module & );
912
+ bool instrumentModule ();
910
913
911
914
private:
912
- void initializeCallbacks (Module &M );
915
+ void initializeCallbacks ();
913
916
914
- void instrumentGlobals (IRBuilder<> &IRB, Module &M, bool *CtorComdat);
915
- void InstrumentGlobalsCOFF (IRBuilder<> &IRB, Module &M,
917
+ void instrumentGlobals (IRBuilder<> &IRB, bool *CtorComdat);
918
+ void InstrumentGlobalsCOFF (IRBuilder<> &IRB,
916
919
ArrayRef<GlobalVariable *> ExtendedGlobals,
917
920
ArrayRef<Constant *> MetadataInitializers);
918
- void instrumentGlobalsELF (IRBuilder<> &IRB, Module &M,
921
+ void instrumentGlobalsELF (IRBuilder<> &IRB,
919
922
ArrayRef<GlobalVariable *> ExtendedGlobals,
920
923
ArrayRef<Constant *> MetadataInitializers,
921
924
const std::string &UniqueModuleId);
922
- void InstrumentGlobalsMachO (IRBuilder<> &IRB, Module &M,
925
+ void InstrumentGlobalsMachO (IRBuilder<> &IRB,
923
926
ArrayRef<GlobalVariable *> ExtendedGlobals,
924
927
ArrayRef<Constant *> MetadataInitializers);
925
928
void
926
- InstrumentGlobalsWithMetadataArray (IRBuilder<> &IRB, Module &M,
929
+ InstrumentGlobalsWithMetadataArray (IRBuilder<> &IRB,
927
930
ArrayRef<GlobalVariable *> ExtendedGlobals,
928
931
ArrayRef<Constant *> MetadataInitializers);
929
932
930
- GlobalVariable *CreateMetadataGlobal (Module &M, Constant *Initializer,
933
+ GlobalVariable *CreateMetadataGlobal (Constant *Initializer,
931
934
StringRef OriginalName);
932
935
void SetComdatForGlobalMetadata (GlobalVariable *G, GlobalVariable *Metadata,
933
936
StringRef InternalSuffix);
934
- Instruction *CreateAsanModuleDtor (Module &M );
937
+ Instruction *CreateAsanModuleDtor ();
935
938
936
939
const GlobalVariable *getExcludedAliasedGlobal (const GlobalAlias &GA) const ;
937
940
bool shouldInstrumentGlobal (GlobalVariable *G) const ;
938
941
bool ShouldUseMachOGlobalsSection () const ;
939
942
StringRef getGlobalMetadataSection () const ;
940
943
void poisonOneInitializer (Function &GlobalInit, GlobalValue *ModuleName);
941
- void createInitializerPoisonCalls (Module &M, GlobalValue *ModuleName);
944
+ void createInitializerPoisonCalls (GlobalValue *ModuleName);
942
945
uint64_t getMinRedzoneSizeForGlobal () const {
943
946
return getRedzoneSizeForScale (Mapping.Scale );
944
947
}
945
948
uint64_t getRedzoneSizeForGlobal (uint64_t SizeInBytes) const ;
946
- int GetAsanVersion (const Module &M ) const ;
949
+ int GetAsanVersion () const ;
947
950
951
+ Module &M;
948
952
bool CompileKernel;
949
953
bool InsertVersionCheck;
950
954
bool Recover;
@@ -1271,7 +1275,7 @@ PreservedAnalyses AddressSanitizerPass::run(Module &M,
1271
1275
const TargetLibraryInfo &TLI = FAM.getResult <TargetLibraryAnalysis>(F);
1272
1276
Modified |= FunctionSanitizer.instrumentFunction (F, &TLI);
1273
1277
}
1274
- Modified |= ModuleSanitizer.instrumentModule (M );
1278
+ Modified |= ModuleSanitizer.instrumentModule ();
1275
1279
if (!Modified)
1276
1280
return PreservedAnalyses::all ();
1277
1281
@@ -1974,7 +1978,7 @@ void ModuleAddressSanitizer::poisonOneInitializer(Function &GlobalInit,
1974
1978
}
1975
1979
1976
1980
void ModuleAddressSanitizer::createInitializerPoisonCalls (
1977
- Module &M, GlobalValue *ModuleName) {
1981
+ GlobalValue *ModuleName) {
1978
1982
GlobalVariable *GV = M.getGlobalVariable (" llvm.global_ctors" );
1979
1983
if (!GV)
1980
1984
return ;
@@ -2198,7 +2202,7 @@ StringRef ModuleAddressSanitizer::getGlobalMetadataSection() const {
2198
2202
llvm_unreachable (" unsupported object format" );
2199
2203
}
2200
2204
2201
- void ModuleAddressSanitizer::initializeCallbacks (Module &M ) {
2205
+ void ModuleAddressSanitizer::initializeCallbacks () {
2202
2206
IRBuilder<> IRB (*C);
2203
2207
2204
2208
// Declare our poisoning and unpoisoning functions.
@@ -2268,7 +2272,7 @@ void ModuleAddressSanitizer::SetComdatForGlobalMetadata(
2268
2272
// Create a separate metadata global and put it in the appropriate ASan
2269
2273
// global registration section.
2270
2274
GlobalVariable *
2271
- ModuleAddressSanitizer::CreateMetadataGlobal (Module &M, Constant *Initializer,
2275
+ ModuleAddressSanitizer::CreateMetadataGlobal (Constant *Initializer,
2272
2276
StringRef OriginalName) {
2273
2277
auto Linkage = TargetTriple.isOSBinFormatMachO ()
2274
2278
? GlobalVariable::InternalLinkage
@@ -2283,7 +2287,7 @@ ModuleAddressSanitizer::CreateMetadataGlobal(Module &M, Constant *Initializer,
2283
2287
return Metadata;
2284
2288
}
2285
2289
2286
- Instruction *ModuleAddressSanitizer::CreateAsanModuleDtor (Module &M ) {
2290
+ Instruction *ModuleAddressSanitizer::CreateAsanModuleDtor () {
2287
2291
AsanDtorFunction = Function::createWithDefaultAttr (
2288
2292
FunctionType::get (Type::getVoidTy (*C), false ),
2289
2293
GlobalValue::InternalLinkage, 0 , kAsanModuleDtorName , &M);
@@ -2296,7 +2300,7 @@ Instruction *ModuleAddressSanitizer::CreateAsanModuleDtor(Module &M) {
2296
2300
}
2297
2301
2298
2302
void ModuleAddressSanitizer::InstrumentGlobalsCOFF (
2299
- IRBuilder<> &IRB, Module &M, ArrayRef<GlobalVariable *> ExtendedGlobals,
2303
+ IRBuilder<> &IRB, ArrayRef<GlobalVariable *> ExtendedGlobals,
2300
2304
ArrayRef<Constant *> MetadataInitializers) {
2301
2305
assert (ExtendedGlobals.size () == MetadataInitializers.size ());
2302
2306
auto &DL = M.getDataLayout ();
@@ -2305,8 +2309,7 @@ void ModuleAddressSanitizer::InstrumentGlobalsCOFF(
2305
2309
for (size_t i = 0 ; i < ExtendedGlobals.size (); i++) {
2306
2310
Constant *Initializer = MetadataInitializers[i];
2307
2311
GlobalVariable *G = ExtendedGlobals[i];
2308
- GlobalVariable *Metadata =
2309
- CreateMetadataGlobal (M, Initializer, G->getName ());
2312
+ GlobalVariable *Metadata = CreateMetadataGlobal (Initializer, G->getName ());
2310
2313
MDNode *MD = MDNode::get (M.getContext (), ValueAsMetadata::get (G));
2311
2314
Metadata->setMetadata (LLVMContext::MD_associated, MD);
2312
2315
MetadataGlobals[i] = Metadata;
@@ -2329,7 +2332,7 @@ void ModuleAddressSanitizer::InstrumentGlobalsCOFF(
2329
2332
}
2330
2333
2331
2334
void ModuleAddressSanitizer::instrumentGlobalsELF (
2332
- IRBuilder<> &IRB, Module &M, ArrayRef<GlobalVariable *> ExtendedGlobals,
2335
+ IRBuilder<> &IRB, ArrayRef<GlobalVariable *> ExtendedGlobals,
2333
2336
ArrayRef<Constant *> MetadataInitializers,
2334
2337
const std::string &UniqueModuleId) {
2335
2338
assert (ExtendedGlobals.size () == MetadataInitializers.size ());
@@ -2344,7 +2347,7 @@ void ModuleAddressSanitizer::instrumentGlobalsELF(
2344
2347
for (size_t i = 0 ; i < ExtendedGlobals.size (); i++) {
2345
2348
GlobalVariable *G = ExtendedGlobals[i];
2346
2349
GlobalVariable *Metadata =
2347
- CreateMetadataGlobal (M, MetadataInitializers[i], G->getName ());
2350
+ CreateMetadataGlobal (MetadataInitializers[i], G->getName ());
2348
2351
MDNode *MD = MDNode::get (M.getContext (), ValueAsMetadata::get (G));
2349
2352
Metadata->setMetadata (LLVMContext::MD_associated, MD);
2350
2353
MetadataGlobals[i] = Metadata;
@@ -2389,7 +2392,7 @@ void ModuleAddressSanitizer::instrumentGlobalsELF(
2389
2392
// We also need to unregister globals at the end, e.g., when a shared library
2390
2393
// gets closed.
2391
2394
if (DestructorKind != AsanDtorKind::None && !MetadataGlobals.empty ()) {
2392
- IRBuilder<> IrbDtor (CreateAsanModuleDtor (M ));
2395
+ IRBuilder<> IrbDtor (CreateAsanModuleDtor ());
2393
2396
IrbDtor.CreateCall (AsanUnregisterElfGlobals,
2394
2397
{IRB.CreatePointerCast (RegisteredFlag, IntptrTy),
2395
2398
IRB.CreatePointerCast (StartELFMetadata, IntptrTy),
@@ -2398,7 +2401,7 @@ void ModuleAddressSanitizer::instrumentGlobalsELF(
2398
2401
}
2399
2402
2400
2403
void ModuleAddressSanitizer::InstrumentGlobalsMachO (
2401
- IRBuilder<> &IRB, Module &M, ArrayRef<GlobalVariable *> ExtendedGlobals,
2404
+ IRBuilder<> &IRB, ArrayRef<GlobalVariable *> ExtendedGlobals,
2402
2405
ArrayRef<Constant *> MetadataInitializers) {
2403
2406
assert (ExtendedGlobals.size () == MetadataInitializers.size ());
2404
2407
@@ -2411,8 +2414,7 @@ void ModuleAddressSanitizer::InstrumentGlobalsMachO(
2411
2414
for (size_t i = 0 ; i < ExtendedGlobals.size (); i++) {
2412
2415
Constant *Initializer = MetadataInitializers[i];
2413
2416
GlobalVariable *G = ExtendedGlobals[i];
2414
- GlobalVariable *Metadata =
2415
- CreateMetadataGlobal (M, Initializer, G->getName ());
2417
+ GlobalVariable *Metadata = CreateMetadataGlobal (Initializer, G->getName ());
2416
2418
2417
2419
// On recent Mach-O platforms, we emit the global metadata in a way that
2418
2420
// allows the linker to properly strip dead globals.
@@ -2451,14 +2453,14 @@ void ModuleAddressSanitizer::InstrumentGlobalsMachO(
2451
2453
// We also need to unregister globals at the end, e.g., when a shared library
2452
2454
// gets closed.
2453
2455
if (DestructorKind != AsanDtorKind::None) {
2454
- IRBuilder<> IrbDtor (CreateAsanModuleDtor (M ));
2456
+ IRBuilder<> IrbDtor (CreateAsanModuleDtor ());
2455
2457
IrbDtor.CreateCall (AsanUnregisterImageGlobals,
2456
2458
{IRB.CreatePointerCast (RegisteredFlag, IntptrTy)});
2457
2459
}
2458
2460
}
2459
2461
2460
2462
void ModuleAddressSanitizer::InstrumentGlobalsWithMetadataArray (
2461
- IRBuilder<> &IRB, Module &M, ArrayRef<GlobalVariable *> ExtendedGlobals,
2463
+ IRBuilder<> &IRB, ArrayRef<GlobalVariable *> ExtendedGlobals,
2462
2464
ArrayRef<Constant *> MetadataInitializers) {
2463
2465
assert (ExtendedGlobals.size () == MetadataInitializers.size ());
2464
2466
unsigned N = ExtendedGlobals.size ();
@@ -2482,7 +2484,7 @@ void ModuleAddressSanitizer::InstrumentGlobalsWithMetadataArray(
2482
2484
// We also need to unregister globals at the end, e.g., when a shared library
2483
2485
// gets closed.
2484
2486
if (DestructorKind != AsanDtorKind::None) {
2485
- IRBuilder<> IrbDtor (CreateAsanModuleDtor (M ));
2487
+ IRBuilder<> IrbDtor (CreateAsanModuleDtor ());
2486
2488
IrbDtor.CreateCall (AsanUnregisterGlobals,
2487
2489
{IRB.CreatePointerCast (AllGlobals, IntptrTy),
2488
2490
ConstantInt::get (IntptrTy, N)});
@@ -2494,7 +2496,7 @@ void ModuleAddressSanitizer::InstrumentGlobalsWithMetadataArray(
2494
2496
// redzones and inserts this function into llvm.global_ctors.
2495
2497
// Sets *CtorComdat to true if the global registration code emitted into the
2496
2498
// asan constructor is comdat-compatible.
2497
- void ModuleAddressSanitizer::instrumentGlobals (IRBuilder<> &IRB, Module &M,
2499
+ void ModuleAddressSanitizer::instrumentGlobals (IRBuilder<> &IRB,
2498
2500
bool *CtorComdat) {
2499
2501
// Build set of globals that are aliased by some GA, where
2500
2502
// getExcludedAliasedGlobal(GA) returns the relevant GlobalVariable.
@@ -2670,26 +2672,25 @@ void ModuleAddressSanitizer::instrumentGlobals(IRBuilder<> &IRB, Module &M,
2670
2672
// function will be called. The module destructor is not created when n ==
2671
2673
// 0.
2672
2674
*CtorComdat = true ;
2673
- instrumentGlobalsELF (IRB, M, NewGlobals, Initializers,
2674
- getUniqueModuleId (&M));
2675
+ instrumentGlobalsELF (IRB, NewGlobals, Initializers, getUniqueModuleId (&M));
2675
2676
} else if (n == 0 ) {
2676
2677
// When UseGlobalsGC is false, COMDAT can still be used if n == 0, because
2677
2678
// all compile units will have identical module constructor/destructor.
2678
2679
*CtorComdat = TargetTriple.isOSBinFormatELF ();
2679
2680
} else {
2680
2681
*CtorComdat = false ;
2681
2682
if (UseGlobalsGC && TargetTriple.isOSBinFormatCOFF ()) {
2682
- InstrumentGlobalsCOFF (IRB, M, NewGlobals, Initializers);
2683
+ InstrumentGlobalsCOFF (IRB, NewGlobals, Initializers);
2683
2684
} else if (UseGlobalsGC && ShouldUseMachOGlobalsSection ()) {
2684
- InstrumentGlobalsMachO (IRB, M, NewGlobals, Initializers);
2685
+ InstrumentGlobalsMachO (IRB, NewGlobals, Initializers);
2685
2686
} else {
2686
- InstrumentGlobalsWithMetadataArray (IRB, M, NewGlobals, Initializers);
2687
+ InstrumentGlobalsWithMetadataArray (IRB, NewGlobals, Initializers);
2687
2688
}
2688
2689
}
2689
2690
2690
2691
// Create calls for poisoning before initializers run and unpoisoning after.
2691
2692
if (HasDynamicallyInitializedGlobals)
2692
- createInitializerPoisonCalls (M, ModuleName);
2693
+ createInitializerPoisonCalls (ModuleName);
2693
2694
2694
2695
LLVM_DEBUG (dbgs () << M);
2695
2696
}
@@ -2719,7 +2720,7 @@ ModuleAddressSanitizer::getRedzoneSizeForGlobal(uint64_t SizeInBytes) const {
2719
2720
return RZ;
2720
2721
}
2721
2722
2722
- int ModuleAddressSanitizer::GetAsanVersion (const Module &M ) const {
2723
+ int ModuleAddressSanitizer::GetAsanVersion () const {
2723
2724
int LongSize = M.getDataLayout ().getPointerSizeInBits ();
2724
2725
bool isAndroid = Triple (M.getTargetTriple ()).isAndroid ();
2725
2726
int Version = 8 ;
@@ -2729,8 +2730,8 @@ int ModuleAddressSanitizer::GetAsanVersion(const Module &M) const {
2729
2730
return Version;
2730
2731
}
2731
2732
2732
- bool ModuleAddressSanitizer::instrumentModule (Module &M ) {
2733
- initializeCallbacks (M );
2733
+ bool ModuleAddressSanitizer::instrumentModule () {
2734
+ initializeCallbacks ();
2734
2735
2735
2736
// Create a module constructor. A destructor is created lazily because not all
2736
2737
// platforms, and not all modules need it.
@@ -2740,13 +2741,13 @@ bool ModuleAddressSanitizer::instrumentModule(Module &M) {
2740
2741
// need the init and version check calls.
2741
2742
AsanCtorFunction = createSanitizerCtor (M, kAsanModuleCtorName );
2742
2743
} else {
2743
- std::string AsanVersion = std::to_string (GetAsanVersion (M ));
2744
+ std::string AsanVersion = std::to_string (GetAsanVersion ());
2744
2745
std::string VersionCheckName =
2745
2746
InsertVersionCheck ? (kAsanVersionCheckNamePrefix + AsanVersion) : " " ;
2746
2747
std::tie (AsanCtorFunction, std::ignore) =
2747
- createSanitizerCtorAndInitFunctions (M, kAsanModuleCtorName ,
2748
- kAsanInitName , /* InitArgTypes=*/ {},
2749
- /* InitArgs=*/ {}, VersionCheckName);
2748
+ createSanitizerCtorAndInitFunctions (
2749
+ M, kAsanModuleCtorName , kAsanInitName , /* InitArgTypes=*/ {},
2750
+ /* InitArgs=*/ {}, VersionCheckName);
2750
2751
}
2751
2752
}
2752
2753
@@ -2755,10 +2756,10 @@ bool ModuleAddressSanitizer::instrumentModule(Module &M) {
2755
2756
assert (AsanCtorFunction || ConstructorKind == AsanCtorKind::None);
2756
2757
if (AsanCtorFunction) {
2757
2758
IRBuilder<> IRB (AsanCtorFunction->getEntryBlock ().getTerminator ());
2758
- instrumentGlobals (IRB, M, &CtorComdat);
2759
+ instrumentGlobals (IRB, &CtorComdat);
2759
2760
} else {
2760
2761
IRBuilder<> IRB (*C);
2761
- instrumentGlobals (IRB, M, &CtorComdat);
2762
+ instrumentGlobals (IRB, &CtorComdat);
2762
2763
}
2763
2764
}
2764
2765
@@ -2786,7 +2787,7 @@ bool ModuleAddressSanitizer::instrumentModule(Module &M) {
2786
2787
return true ;
2787
2788
}
2788
2789
2789
- void AddressSanitizer::initializeCallbacks (Module &M, const TargetLibraryInfo *TLI) {
2790
+ void AddressSanitizer::initializeCallbacks (const TargetLibraryInfo *TLI) {
2790
2791
IRBuilder<> IRB (*C);
2791
2792
// Create __asan_report* callbacks.
2792
2793
// IsWrite, TypeSize and Exp are encoded in the function name.
@@ -2970,7 +2971,7 @@ bool AddressSanitizer::instrumentFunction(Function &F,
2970
2971
2971
2972
LLVM_DEBUG (dbgs () << " ASAN instrumenting:\n " << F << " \n " );
2972
2973
2973
- initializeCallbacks (*F. getParent (), TLI);
2974
+ initializeCallbacks (TLI);
2974
2975
2975
2976
FunctionStateRAII CleanupObj (this );
2976
2977
0 commit comments