Skip to content

Commit 8041bf4

Browse files
committed
[NFC][asan] Make 'Module &M' class member
1 parent 2f89c1c commit 8041bf4

File tree

1 file changed

+50
-49
lines changed

1 file changed

+50
-49
lines changed

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

Lines changed: 50 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,8 @@ struct AddressSanitizer {
719719
bool Recover = false, bool UseAfterScope = false,
720720
AsanDetectStackUseAfterReturnMode UseAfterReturn =
721721
AsanDetectStackUseAfterReturnMode::Runtime)
722-
: CompileKernel(ClEnableKasan.getNumOccurrences() > 0 ? ClEnableKasan
722+
: M(M),
723+
CompileKernel(ClEnableKasan.getNumOccurrences() > 0 ? ClEnableKasan
723724
: CompileKernel),
724725
Recover(ClRecover.getNumOccurrences() > 0 ? ClRecover : Recover),
725726
UseAfterScope(UseAfterScope || ClUseAfterScope),
@@ -803,7 +804,7 @@ struct AddressSanitizer {
803804
private:
804805
friend struct FunctionStackPoisoner;
805806

806-
void initializeCallbacks(Module &M, const TargetLibraryInfo *TLI);
807+
void initializeCallbacks(const TargetLibraryInfo *TLI);
807808

808809
bool LooksLikeCodeInBug11395(Instruction *I);
809810
bool GlobalIsLinkerInitialized(GlobalVariable *G);
@@ -826,6 +827,7 @@ struct AddressSanitizer {
826827
}
827828
};
828829

830+
Module &M;
829831
LLVMContext *C;
830832
const DataLayout *DL;
831833
Triple TargetTriple;
@@ -868,7 +870,8 @@ class ModuleAddressSanitizer {
868870
bool UseGlobalsGC = true, bool UseOdrIndicator = true,
869871
AsanDtorKind DestructorKind = AsanDtorKind::Global,
870872
AsanCtorKind ConstructorKind = AsanCtorKind::Global)
871-
: CompileKernel(ClEnableKasan.getNumOccurrences() > 0 ? ClEnableKasan
873+
: M(M),
874+
CompileKernel(ClEnableKasan.getNumOccurrences() > 0 ? ClEnableKasan
872875
: CompileKernel),
873876
InsertVersionCheck(ClInsertVersionCheck.getNumOccurrences() > 0
874877
? ClInsertVersionCheck
@@ -906,45 +909,46 @@ class ModuleAddressSanitizer {
906909
assert(this->DestructorKind != AsanDtorKind::Invalid);
907910
}
908911

909-
bool instrumentModule(Module &);
912+
bool instrumentModule();
910913

911914
private:
912-
void initializeCallbacks(Module &M);
915+
void initializeCallbacks();
913916

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,
916919
ArrayRef<GlobalVariable *> ExtendedGlobals,
917920
ArrayRef<Constant *> MetadataInitializers);
918-
void instrumentGlobalsELF(IRBuilder<> &IRB, Module &M,
921+
void instrumentGlobalsELF(IRBuilder<> &IRB,
919922
ArrayRef<GlobalVariable *> ExtendedGlobals,
920923
ArrayRef<Constant *> MetadataInitializers,
921924
const std::string &UniqueModuleId);
922-
void InstrumentGlobalsMachO(IRBuilder<> &IRB, Module &M,
925+
void InstrumentGlobalsMachO(IRBuilder<> &IRB,
923926
ArrayRef<GlobalVariable *> ExtendedGlobals,
924927
ArrayRef<Constant *> MetadataInitializers);
925928
void
926-
InstrumentGlobalsWithMetadataArray(IRBuilder<> &IRB, Module &M,
929+
InstrumentGlobalsWithMetadataArray(IRBuilder<> &IRB,
927930
ArrayRef<GlobalVariable *> ExtendedGlobals,
928931
ArrayRef<Constant *> MetadataInitializers);
929932

930-
GlobalVariable *CreateMetadataGlobal(Module &M, Constant *Initializer,
933+
GlobalVariable *CreateMetadataGlobal(Constant *Initializer,
931934
StringRef OriginalName);
932935
void SetComdatForGlobalMetadata(GlobalVariable *G, GlobalVariable *Metadata,
933936
StringRef InternalSuffix);
934-
Instruction *CreateAsanModuleDtor(Module &M);
937+
Instruction *CreateAsanModuleDtor();
935938

936939
const GlobalVariable *getExcludedAliasedGlobal(const GlobalAlias &GA) const;
937940
bool shouldInstrumentGlobal(GlobalVariable *G) const;
938941
bool ShouldUseMachOGlobalsSection() const;
939942
StringRef getGlobalMetadataSection() const;
940943
void poisonOneInitializer(Function &GlobalInit, GlobalValue *ModuleName);
941-
void createInitializerPoisonCalls(Module &M, GlobalValue *ModuleName);
944+
void createInitializerPoisonCalls(GlobalValue *ModuleName);
942945
uint64_t getMinRedzoneSizeForGlobal() const {
943946
return getRedzoneSizeForScale(Mapping.Scale);
944947
}
945948
uint64_t getRedzoneSizeForGlobal(uint64_t SizeInBytes) const;
946-
int GetAsanVersion(const Module &M) const;
949+
int GetAsanVersion() const;
947950

951+
Module &M;
948952
bool CompileKernel;
949953
bool InsertVersionCheck;
950954
bool Recover;
@@ -1271,7 +1275,7 @@ PreservedAnalyses AddressSanitizerPass::run(Module &M,
12711275
const TargetLibraryInfo &TLI = FAM.getResult<TargetLibraryAnalysis>(F);
12721276
Modified |= FunctionSanitizer.instrumentFunction(F, &TLI);
12731277
}
1274-
Modified |= ModuleSanitizer.instrumentModule(M);
1278+
Modified |= ModuleSanitizer.instrumentModule();
12751279
if (!Modified)
12761280
return PreservedAnalyses::all();
12771281

@@ -1974,7 +1978,7 @@ void ModuleAddressSanitizer::poisonOneInitializer(Function &GlobalInit,
19741978
}
19751979

19761980
void ModuleAddressSanitizer::createInitializerPoisonCalls(
1977-
Module &M, GlobalValue *ModuleName) {
1981+
GlobalValue *ModuleName) {
19781982
GlobalVariable *GV = M.getGlobalVariable("llvm.global_ctors");
19791983
if (!GV)
19801984
return;
@@ -2198,7 +2202,7 @@ StringRef ModuleAddressSanitizer::getGlobalMetadataSection() const {
21982202
llvm_unreachable("unsupported object format");
21992203
}
22002204

2201-
void ModuleAddressSanitizer::initializeCallbacks(Module &M) {
2205+
void ModuleAddressSanitizer::initializeCallbacks() {
22022206
IRBuilder<> IRB(*C);
22032207

22042208
// Declare our poisoning and unpoisoning functions.
@@ -2268,7 +2272,7 @@ void ModuleAddressSanitizer::SetComdatForGlobalMetadata(
22682272
// Create a separate metadata global and put it in the appropriate ASan
22692273
// global registration section.
22702274
GlobalVariable *
2271-
ModuleAddressSanitizer::CreateMetadataGlobal(Module &M, Constant *Initializer,
2275+
ModuleAddressSanitizer::CreateMetadataGlobal(Constant *Initializer,
22722276
StringRef OriginalName) {
22732277
auto Linkage = TargetTriple.isOSBinFormatMachO()
22742278
? GlobalVariable::InternalLinkage
@@ -2283,7 +2287,7 @@ ModuleAddressSanitizer::CreateMetadataGlobal(Module &M, Constant *Initializer,
22832287
return Metadata;
22842288
}
22852289

2286-
Instruction *ModuleAddressSanitizer::CreateAsanModuleDtor(Module &M) {
2290+
Instruction *ModuleAddressSanitizer::CreateAsanModuleDtor() {
22872291
AsanDtorFunction = Function::createWithDefaultAttr(
22882292
FunctionType::get(Type::getVoidTy(*C), false),
22892293
GlobalValue::InternalLinkage, 0, kAsanModuleDtorName, &M);
@@ -2296,7 +2300,7 @@ Instruction *ModuleAddressSanitizer::CreateAsanModuleDtor(Module &M) {
22962300
}
22972301

22982302
void ModuleAddressSanitizer::InstrumentGlobalsCOFF(
2299-
IRBuilder<> &IRB, Module &M, ArrayRef<GlobalVariable *> ExtendedGlobals,
2303+
IRBuilder<> &IRB, ArrayRef<GlobalVariable *> ExtendedGlobals,
23002304
ArrayRef<Constant *> MetadataInitializers) {
23012305
assert(ExtendedGlobals.size() == MetadataInitializers.size());
23022306
auto &DL = M.getDataLayout();
@@ -2305,8 +2309,7 @@ void ModuleAddressSanitizer::InstrumentGlobalsCOFF(
23052309
for (size_t i = 0; i < ExtendedGlobals.size(); i++) {
23062310
Constant *Initializer = MetadataInitializers[i];
23072311
GlobalVariable *G = ExtendedGlobals[i];
2308-
GlobalVariable *Metadata =
2309-
CreateMetadataGlobal(M, Initializer, G->getName());
2312+
GlobalVariable *Metadata = CreateMetadataGlobal(Initializer, G->getName());
23102313
MDNode *MD = MDNode::get(M.getContext(), ValueAsMetadata::get(G));
23112314
Metadata->setMetadata(LLVMContext::MD_associated, MD);
23122315
MetadataGlobals[i] = Metadata;
@@ -2329,7 +2332,7 @@ void ModuleAddressSanitizer::InstrumentGlobalsCOFF(
23292332
}
23302333

23312334
void ModuleAddressSanitizer::instrumentGlobalsELF(
2332-
IRBuilder<> &IRB, Module &M, ArrayRef<GlobalVariable *> ExtendedGlobals,
2335+
IRBuilder<> &IRB, ArrayRef<GlobalVariable *> ExtendedGlobals,
23332336
ArrayRef<Constant *> MetadataInitializers,
23342337
const std::string &UniqueModuleId) {
23352338
assert(ExtendedGlobals.size() == MetadataInitializers.size());
@@ -2344,7 +2347,7 @@ void ModuleAddressSanitizer::instrumentGlobalsELF(
23442347
for (size_t i = 0; i < ExtendedGlobals.size(); i++) {
23452348
GlobalVariable *G = ExtendedGlobals[i];
23462349
GlobalVariable *Metadata =
2347-
CreateMetadataGlobal(M, MetadataInitializers[i], G->getName());
2350+
CreateMetadataGlobal(MetadataInitializers[i], G->getName());
23482351
MDNode *MD = MDNode::get(M.getContext(), ValueAsMetadata::get(G));
23492352
Metadata->setMetadata(LLVMContext::MD_associated, MD);
23502353
MetadataGlobals[i] = Metadata;
@@ -2389,7 +2392,7 @@ void ModuleAddressSanitizer::instrumentGlobalsELF(
23892392
// We also need to unregister globals at the end, e.g., when a shared library
23902393
// gets closed.
23912394
if (DestructorKind != AsanDtorKind::None && !MetadataGlobals.empty()) {
2392-
IRBuilder<> IrbDtor(CreateAsanModuleDtor(M));
2395+
IRBuilder<> IrbDtor(CreateAsanModuleDtor());
23932396
IrbDtor.CreateCall(AsanUnregisterElfGlobals,
23942397
{IRB.CreatePointerCast(RegisteredFlag, IntptrTy),
23952398
IRB.CreatePointerCast(StartELFMetadata, IntptrTy),
@@ -2398,7 +2401,7 @@ void ModuleAddressSanitizer::instrumentGlobalsELF(
23982401
}
23992402

24002403
void ModuleAddressSanitizer::InstrumentGlobalsMachO(
2401-
IRBuilder<> &IRB, Module &M, ArrayRef<GlobalVariable *> ExtendedGlobals,
2404+
IRBuilder<> &IRB, ArrayRef<GlobalVariable *> ExtendedGlobals,
24022405
ArrayRef<Constant *> MetadataInitializers) {
24032406
assert(ExtendedGlobals.size() == MetadataInitializers.size());
24042407

@@ -2411,8 +2414,7 @@ void ModuleAddressSanitizer::InstrumentGlobalsMachO(
24112414
for (size_t i = 0; i < ExtendedGlobals.size(); i++) {
24122415
Constant *Initializer = MetadataInitializers[i];
24132416
GlobalVariable *G = ExtendedGlobals[i];
2414-
GlobalVariable *Metadata =
2415-
CreateMetadataGlobal(M, Initializer, G->getName());
2417+
GlobalVariable *Metadata = CreateMetadataGlobal(Initializer, G->getName());
24162418

24172419
// On recent Mach-O platforms, we emit the global metadata in a way that
24182420
// allows the linker to properly strip dead globals.
@@ -2451,14 +2453,14 @@ void ModuleAddressSanitizer::InstrumentGlobalsMachO(
24512453
// We also need to unregister globals at the end, e.g., when a shared library
24522454
// gets closed.
24532455
if (DestructorKind != AsanDtorKind::None) {
2454-
IRBuilder<> IrbDtor(CreateAsanModuleDtor(M));
2456+
IRBuilder<> IrbDtor(CreateAsanModuleDtor());
24552457
IrbDtor.CreateCall(AsanUnregisterImageGlobals,
24562458
{IRB.CreatePointerCast(RegisteredFlag, IntptrTy)});
24572459
}
24582460
}
24592461

24602462
void ModuleAddressSanitizer::InstrumentGlobalsWithMetadataArray(
2461-
IRBuilder<> &IRB, Module &M, ArrayRef<GlobalVariable *> ExtendedGlobals,
2463+
IRBuilder<> &IRB, ArrayRef<GlobalVariable *> ExtendedGlobals,
24622464
ArrayRef<Constant *> MetadataInitializers) {
24632465
assert(ExtendedGlobals.size() == MetadataInitializers.size());
24642466
unsigned N = ExtendedGlobals.size();
@@ -2482,7 +2484,7 @@ void ModuleAddressSanitizer::InstrumentGlobalsWithMetadataArray(
24822484
// We also need to unregister globals at the end, e.g., when a shared library
24832485
// gets closed.
24842486
if (DestructorKind != AsanDtorKind::None) {
2485-
IRBuilder<> IrbDtor(CreateAsanModuleDtor(M));
2487+
IRBuilder<> IrbDtor(CreateAsanModuleDtor());
24862488
IrbDtor.CreateCall(AsanUnregisterGlobals,
24872489
{IRB.CreatePointerCast(AllGlobals, IntptrTy),
24882490
ConstantInt::get(IntptrTy, N)});
@@ -2494,7 +2496,7 @@ void ModuleAddressSanitizer::InstrumentGlobalsWithMetadataArray(
24942496
// redzones and inserts this function into llvm.global_ctors.
24952497
// Sets *CtorComdat to true if the global registration code emitted into the
24962498
// asan constructor is comdat-compatible.
2497-
void ModuleAddressSanitizer::instrumentGlobals(IRBuilder<> &IRB, Module &M,
2499+
void ModuleAddressSanitizer::instrumentGlobals(IRBuilder<> &IRB,
24982500
bool *CtorComdat) {
24992501
// Build set of globals that are aliased by some GA, where
25002502
// getExcludedAliasedGlobal(GA) returns the relevant GlobalVariable.
@@ -2670,26 +2672,25 @@ void ModuleAddressSanitizer::instrumentGlobals(IRBuilder<> &IRB, Module &M,
26702672
// function will be called. The module destructor is not created when n ==
26712673
// 0.
26722674
*CtorComdat = true;
2673-
instrumentGlobalsELF(IRB, M, NewGlobals, Initializers,
2674-
getUniqueModuleId(&M));
2675+
instrumentGlobalsELF(IRB, NewGlobals, Initializers, getUniqueModuleId(&M));
26752676
} else if (n == 0) {
26762677
// When UseGlobalsGC is false, COMDAT can still be used if n == 0, because
26772678
// all compile units will have identical module constructor/destructor.
26782679
*CtorComdat = TargetTriple.isOSBinFormatELF();
26792680
} else {
26802681
*CtorComdat = false;
26812682
if (UseGlobalsGC && TargetTriple.isOSBinFormatCOFF()) {
2682-
InstrumentGlobalsCOFF(IRB, M, NewGlobals, Initializers);
2683+
InstrumentGlobalsCOFF(IRB, NewGlobals, Initializers);
26832684
} else if (UseGlobalsGC && ShouldUseMachOGlobalsSection()) {
2684-
InstrumentGlobalsMachO(IRB, M, NewGlobals, Initializers);
2685+
InstrumentGlobalsMachO(IRB, NewGlobals, Initializers);
26852686
} else {
2686-
InstrumentGlobalsWithMetadataArray(IRB, M, NewGlobals, Initializers);
2687+
InstrumentGlobalsWithMetadataArray(IRB, NewGlobals, Initializers);
26872688
}
26882689
}
26892690

26902691
// Create calls for poisoning before initializers run and unpoisoning after.
26912692
if (HasDynamicallyInitializedGlobals)
2692-
createInitializerPoisonCalls(M, ModuleName);
2693+
createInitializerPoisonCalls(ModuleName);
26932694

26942695
LLVM_DEBUG(dbgs() << M);
26952696
}
@@ -2719,7 +2720,7 @@ ModuleAddressSanitizer::getRedzoneSizeForGlobal(uint64_t SizeInBytes) const {
27192720
return RZ;
27202721
}
27212722

2722-
int ModuleAddressSanitizer::GetAsanVersion(const Module &M) const {
2723+
int ModuleAddressSanitizer::GetAsanVersion() const {
27232724
int LongSize = M.getDataLayout().getPointerSizeInBits();
27242725
bool isAndroid = Triple(M.getTargetTriple()).isAndroid();
27252726
int Version = 8;
@@ -2729,8 +2730,8 @@ int ModuleAddressSanitizer::GetAsanVersion(const Module &M) const {
27292730
return Version;
27302731
}
27312732

2732-
bool ModuleAddressSanitizer::instrumentModule(Module &M) {
2733-
initializeCallbacks(M);
2733+
bool ModuleAddressSanitizer::instrumentModule() {
2734+
initializeCallbacks();
27342735

27352736
// Create a module constructor. A destructor is created lazily because not all
27362737
// platforms, and not all modules need it.
@@ -2740,13 +2741,13 @@ bool ModuleAddressSanitizer::instrumentModule(Module &M) {
27402741
// need the init and version check calls.
27412742
AsanCtorFunction = createSanitizerCtor(M, kAsanModuleCtorName);
27422743
} else {
2743-
std::string AsanVersion = std::to_string(GetAsanVersion(M));
2744+
std::string AsanVersion = std::to_string(GetAsanVersion());
27442745
std::string VersionCheckName =
27452746
InsertVersionCheck ? (kAsanVersionCheckNamePrefix + AsanVersion) : "";
27462747
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);
27502751
}
27512752
}
27522753

@@ -2755,10 +2756,10 @@ bool ModuleAddressSanitizer::instrumentModule(Module &M) {
27552756
assert(AsanCtorFunction || ConstructorKind == AsanCtorKind::None);
27562757
if (AsanCtorFunction) {
27572758
IRBuilder<> IRB(AsanCtorFunction->getEntryBlock().getTerminator());
2758-
instrumentGlobals(IRB, M, &CtorComdat);
2759+
instrumentGlobals(IRB, &CtorComdat);
27592760
} else {
27602761
IRBuilder<> IRB(*C);
2761-
instrumentGlobals(IRB, M, &CtorComdat);
2762+
instrumentGlobals(IRB, &CtorComdat);
27622763
}
27632764
}
27642765

@@ -2786,7 +2787,7 @@ bool ModuleAddressSanitizer::instrumentModule(Module &M) {
27862787
return true;
27872788
}
27882789

2789-
void AddressSanitizer::initializeCallbacks(Module &M, const TargetLibraryInfo *TLI) {
2790+
void AddressSanitizer::initializeCallbacks(const TargetLibraryInfo *TLI) {
27902791
IRBuilder<> IRB(*C);
27912792
// Create __asan_report* callbacks.
27922793
// IsWrite, TypeSize and Exp are encoded in the function name.
@@ -2970,7 +2971,7 @@ bool AddressSanitizer::instrumentFunction(Function &F,
29702971

29712972
LLVM_DEBUG(dbgs() << "ASAN instrumenting:\n" << F << "\n");
29722973

2973-
initializeCallbacks(*F.getParent(), TLI);
2974+
initializeCallbacks(TLI);
29742975

29752976
FunctionStateRAII CleanupObj(this);
29762977

0 commit comments

Comments
 (0)