Skip to content

Commit 70de37c

Browse files
authored
Merge pull request #424 from secure-software-engineering/f-FixModuleSlotTrackerCaching
fix caching of llvm::ModuleSlotTracker instances
2 parents 4864dd8 + acab8c1 commit 70de37c

4 files changed

Lines changed: 49 additions & 12 deletions

File tree

include/phasar/Utils/LLVMShorthands.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include "llvm/IR/Function.h"
2424
#include "llvm/IR/Instructions.h"
25+
#include "llvm/IR/ModuleSlotTracker.h"
2526
#include "llvm/IR/Value.h"
2627

2728
#include "phasar/Utils/Utilities.h"
@@ -216,6 +217,23 @@ bool isVarAnnotationIntrinsic(const llvm::Function *F);
216217
*
217218
*/
218219
llvm::StringRef getVarAnnotationIntrinsicName(const llvm::CallInst *CallInst);
220+
221+
class ModulesToSlotTracker {
222+
friend class ProjectIRDB;
223+
friend class LLVMBasedICFG;
224+
friend class LLVMZeroValue;
225+
226+
private:
227+
static inline llvm::SmallDenseMap<const llvm::Module *,
228+
std::unique_ptr<llvm::ModuleSlotTracker>, 2>
229+
MToST;
230+
231+
static void updateMSTForModule(const llvm::Module *);
232+
static void deleteMSTForModule(const llvm::Module *);
233+
234+
public:
235+
static llvm::ModuleSlotTracker &getSlotTrackerForModule(const llvm::Module *);
236+
};
219237
} // namespace psr
220238

221239
#endif

lib/DB/ProjectIRDB.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ ProjectIRDB::ProjectIRDB(IRDBOptions Options) : Options(Options) {
5555
MPM.addPass(ValueAnnotationPass());
5656
// just to be sure that none of the passes messed up the module!
5757
MPM.addPass(llvm::VerifierPass());
58+
ModulesToSlotTracker::updateMSTForModule(LLVMZeroValueMod.get());
5859
}
5960

6061
ProjectIRDB::ProjectIRDB(const std::vector<std::string> &IRFiles,
@@ -105,6 +106,9 @@ ProjectIRDB::ProjectIRDB(const std::vector<llvm::Module *> &Modules,
105106
}
106107

107108
ProjectIRDB::~ProjectIRDB() {
109+
for (auto &[File, Module] : Modules) {
110+
ModulesToSlotTracker::deleteMSTForModule(Module.get());
111+
}
108112
// release resources if IRDB does not own
109113
if (!(Options & IRDBOptions::OWNS)) {
110114
for (auto &Context : Contexts) {
@@ -134,6 +138,7 @@ void ProjectIRDB::preprocessModule(llvm::Module *M) {
134138
RetOrResInstructions.insert(RRInsts.begin(), RRInsts.end());
135139
STOP_TIMER("LLVM Passes", PAMM_SEVERITY_LEVEL::Full);
136140
buildIDModuleMapping(M);
141+
ModulesToSlotTracker::updateMSTForModule(M);
137142
}
138143

139144
void ProjectIRDB::linkForWPA() {
@@ -199,6 +204,7 @@ void ProjectIRDB::linkForWPA() {
199204
// to link at all. But we have to update the WPAMOD pointer!
200205
WPAModule = Modules.begin()->second.get();
201206
}
207+
ModulesToSlotTracker::updateMSTForModule(WPAModule);
202208
}
203209

204210
void ProjectIRDB::preprocessAllModules() {

lib/PhasarLLVM/ControlFlow/LLVMBasedICFG.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -669,10 +669,12 @@ LLVMBasedICFG::getReturnSitesOfCallAt(const llvm::Instruction *N) const {
669669
if (const auto *Invoke = llvm::dyn_cast<llvm::InvokeInst>(N)) {
670670
const llvm::Instruction *NormalSucc = &Invoke->getNormalDest()->front();
671671
auto *UnwindSucc = &Invoke->getUnwindDest()->front();
672-
if (!IgnoreDbgInstructions && llvm::isa<llvm::DbgInfoIntrinsic>(NormalSucc)) {
672+
if (!IgnoreDbgInstructions &&
673+
llvm::isa<llvm::DbgInfoIntrinsic>(NormalSucc)) {
673674
NormalSucc = NormalSucc->getNextNonDebugInstruction();
674675
}
675-
if (!IgnoreDbgInstructions && llvm::isa<llvm::DbgInfoIntrinsic>(UnwindSucc)) {
676+
if (!IgnoreDbgInstructions &&
677+
llvm::isa<llvm::DbgInfoIntrinsic>(UnwindSucc)) {
676678
UnwindSucc = UnwindSucc->getNextNonDebugInstruction();
677679
}
678680
if (NormalSucc != nullptr) {
@@ -947,6 +949,8 @@ LLVMBasedICFG::buildCRuntimeGlobalCtorsDtorsModel(llvm::Module &M) {
947949
IRB.CreateRetVoid();
948950
}
949951

952+
ModulesToSlotTracker::updateMSTForModule(&M);
953+
950954
return GlobModel;
951955
}
952956

lib/Utils/LLVMShorthands.cpp

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,18 +140,10 @@ bool matchesSignature(const llvm::FunctionType *FType1,
140140
return false;
141141
}
142142

143-
static llvm::ModuleSlotTracker &getModuleSlotTrackerFor(const llvm::Value *V) {
144-
static llvm::SmallDenseMap<const llvm::Module *,
145-
std::unique_ptr<llvm::ModuleSlotTracker>, 2>
146-
ModuleToSlotTracker;
143+
llvm::ModuleSlotTracker &getModuleSlotTrackerFor(const llvm::Value *V) {
147144
const auto *M = getModuleFromVal(V);
148145

149-
auto &ret = ModuleToSlotTracker[M];
150-
if (!ret) {
151-
ret = std::make_unique<llvm::ModuleSlotTracker>(M);
152-
}
153-
154-
return *ret;
146+
return ModulesToSlotTracker::getSlotTrackerForModule(M);
155147
}
156148

157149
std::string llvmIRToString(const llvm::Value *V) {
@@ -437,4 +429,21 @@ llvm::StringRef getVarAnnotationIntrinsicName(const llvm::CallInst *CallInst) {
437429
return data->getAsCString();
438430
}
439431

432+
llvm::ModuleSlotTracker &
433+
ModulesToSlotTracker::getSlotTrackerForModule(const llvm::Module *M) {
434+
auto &ret = MToST[M];
435+
if (M == nullptr && ret == nullptr) {
436+
ret = std::make_unique<llvm::ModuleSlotTracker>(M);
437+
}
438+
assert(ret != nullptr && "no ModuleSlotTracker instance for module cached");
439+
return *ret;
440+
}
441+
442+
void ModulesToSlotTracker::updateMSTForModule(const llvm::Module *M) {
443+
MToST[M] = std::make_unique<llvm::ModuleSlotTracker>(M);
444+
}
445+
void ModulesToSlotTracker::deleteMSTForModule(const llvm::Module *M) {
446+
MToST.erase(M);
447+
}
448+
440449
} // namespace psr

0 commit comments

Comments
 (0)