Static Value-Flow Analysis
Loading...
Searching...
No Matches
LLVMModule.h
Go to the documentation of this file.
1//===- LLVMModule.h -- LLVM Module class-----------------------------------------//
2//
3// SVF: Static Value-Flow Analysis
4//
5// Copyright (C) <2013-> <Yulei Sui>
6//
7
8// This program is free software: you can redistribute it and/or modify
9// it under the terms of the GNU Affero General Public License as published by
10// the Free Software Foundation, either version 3 of the License, or
11// (at your option) any later version.
12
13// This program is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU Affero General Public License for more details.
17
18// You should have received a copy of the GNU Affero General Public License
19// along with this program. If not, see <http://www.gnu.org/licenses/>.
20//
21//===----------------------------------------------------------------------===//
22
23/*
24 * LLVMModule.h
25 *
26 * Created on: 23 Mar.2020
27 * Author: Yulei Sui
28 */
29
30#ifndef INCLUDE_SVF_FE_LLVMMODULE_H_
31#define INCLUDE_SVF_FE_LLVMMODULE_H_
32
33#include "SVF-LLVM/BasicTypes.h"
34#include "SVFIR/SVFValue.h"
35#include "SVFIR/SVFModule.h"
36#include "Util/Options.h"
37
38namespace SVF
39{
40
41class SymbolTableInfo;
42class ObjTypeInference;
43
45{
46 friend class SVFIRBuilder;
47 friend class ICFGBuilder;
48
49public:
50
51 typedef std::vector<const Function*> FunctionSetType;
55
68
74
75private:
77 static bool preProcessed;
81 std::unique_ptr<LLVMContext> owned_ctx;
82 std::vector<std::unique_ptr<Module>> owned_modules;
83 std::vector<std::reference_wrapper<Module>> modules;
84
91
103
111
113
116
117 void build();
118
119public:
121
123 {
124 if (!llvmModuleSet)
126 return llvmModuleSet;
127 }
128
130 {
131 delete llvmModuleSet;
132 llvmModuleSet = nullptr;
133 }
134
135 // Build an SVF module from a given LLVM Module instance (for use e.g. in a LLVM pass)
136 static SVFModule* buildSVFModule(Module& mod);
137
138 // Build an SVF module from the bitcode files provided in `moduleNameVec`
139 static SVFModule* buildSVFModule(const std::vector<std::string>& moduleNameVec);
140
142 {
143 return svfModule;
144 }
145
146 static void preProcessBCs(std::vector<std::string>& moduleNameVec);
147
149 {
150 return modules.size();
151 }
152
153 const std::vector<std::reference_wrapper<Module>>& getLLVMModules() const
154 {
155 return modules;
156 }
157
159 {
160 return &getModuleRef(idx);
161 }
162
164 {
165 assert(idx < getModuleNum() && "Out of range.");
166 return modules[idx];
167 }
168
169 // Dump modules to files
170 void dumpModulesToFile(const std::string& suffix);
171
172 inline void addFunctionMap(const Function* func, SVFFunction* svfFunc)
173 {
175 setValueAttr(func,svfFunc);
176 }
177
178 void addFunctionMap(const Function* func, CallGraphNode* svfFunc);
179
181 {
182 LLVMBB2SVFBB[bb] = svfBB;
184 }
186 {
188 setValueAttr(inst,svfInst);
189 }
191 {
192 CSToCallNodeMap[inst] = svfInst;
194 }
196 {
197 CSToRetNodeMap[inst] = svfInst;
199 }
201 {
204 }
205
212 {
213 if (auto glob_var = llvm::dyn_cast<llvm::GlobalVariable>(glob);
215 {
217 }
220 }
236
237 SVFValue* getSVFValue(const Value* value);
238
239 const Value* getLLVMValue(const SVFValue* value) const
240 {
241 SVFValue2LLVMValueMap::const_iterator it = SVFValue2LLVMValue.find(value);
242 assert(it!=SVFValue2LLVMValue.end() && "can't find corresponding llvm value!");
243 return it->second;
244 }
245
246 const Value* getLLVMValue(const SVFBaseNode* value) const
247 {
248 SVFBaseNode2LLVMValueMap ::const_iterator it = SVFBaseNode2LLVMValue.find(value);
249 assert(it!=SVFBaseNode2LLVMValue.end() && "can't find corresponding llvm value!");
250 return it->second;
251 }
252
253 inline CallGraphNode* getCallGraphNode(const Function* fun) const
254 {
255 LLVMFun2CallGraphNodeMap::const_iterator it = LLVMFunc2CallGraphNode.find(fun);
256 assert(it!=LLVMFunc2CallGraphNode.end() && "SVF Function not found!");
257 return it->second;
258 }
259
260 inline SVFFunction* getSVFFunction(const Function* fun) const
261 {
262 LLVMFun2SVFFunMap::const_iterator it = LLVMFunc2SVFFunc.find(fun);
263 assert(it!=LLVMFunc2SVFFunc.end() && "SVF Function not found!");
264 return it->second;
265 }
266
268 {
269 LLVMBB2SVFBBMap::const_iterator it = LLVMBB2SVFBB.find(bb);
270 assert(it!=LLVMBB2SVFBB.end() && "SVF BasicBlock not found!");
271 return it->second;
272 }
273
275 {
276 LLVMInst2SVFInstMap::const_iterator it = LLVMInst2SVFInst.find(inst);
277 assert(it!=LLVMInst2SVFInst.end() && "SVF Instruction not found!");
278 return it->second;
279 }
280
282 {
283 LLVMArgument2SVFArgumentMap::const_iterator it = LLVMArgument2SVFArgument.find(arg);
284 assert(it!=LLVMArgument2SVFArgument.end() && "SVF Argument not found!");
285 return it->second;
286 }
287
289 {
290 if (auto glob_var = llvm::dyn_cast<llvm::GlobalVariable>(g);
292 {
294 }
295 LLVMConst2SVFConstMap::const_iterator it = LLVMConst2SVFConst.find(g);
296 assert(it!=LLVMConst2SVFConst.end() && "SVF Global not found!");
297 assert(SVFUtil::isa<SVFGlobalValue>(it->second) && "not a SVFGlobal type!");
298 return SVFUtil::cast<SVFGlobalValue>(it->second);
299 }
300
303
305
307 inline const SVFFunction* getSVFFunction(const std::string& name)
308 {
309 Function* fun = nullptr;
310
311 for (u32_t i = 0; i < llvmModuleSet->getModuleNum(); ++i)
312 {
314 fun = mod->getFunction(name);
315 if (fun)
316 {
317 return llvmModuleSet->getSVFFunction(fun);
318 }
319 }
320 return nullptr;
321 }
322
323 ICFGNode* getICFGNode(const Instruction* inst);
324
325 bool hasICFGNode(const Instruction* inst);
326
333
336 {
338 assert(b && "Function entry not created?");
339 return b;
340 }
343 {
345 assert(b && "Function exit not created?");
346 return b;
347 }
348
349
351 bool hasGlobalRep(const GlobalVariable* val) const
352 {
353 GlobalDefToRepMapTy::const_iterator it = GlobalDefToRepMap.find(val);
354 return it != GlobalDefToRepMap.end();
355 }
356
358 {
359 GlobalDefToRepMapTy::const_iterator it = GlobalDefToRepMap.find(val);
360 assert(it != GlobalDefToRepMap.end() && "has no rep?");
361 return it->second;
362 }
363
365 {
366 assert(!empty() && "empty LLVM module!!");
367 for (size_t i = 0; i < getModuleNum(); ++i)
368 {
369 Module& module = getModuleRef(i);
370 if (module.getName().str() != ExtAPI::getExtAPI()->getExtBcPath())
371 {
372 return &module;
373 }
374 }
375 assert(false && "no main module found!");
376 return nullptr;
377 }
378
380 {
381 assert(!empty() && "empty LLVM module!!");
382 return getMainLLVMModule()->getContext();
383 }
384
385 bool empty() const
386 {
387 return getModuleNum() == 0;
388 }
389
391 SVFType* getSVFType(const Type* T);
393 const Type* getLLVMType(const SVFType* T) const;
394
396
397 inline ICFG* getICFG()
398 {
399 return icfg;
400 }
401
402 DominatorTree& getDomTree(const Function* fun);
403
404private:
406 SVFType* addSVFTypeInfo(const Type* t);
408 StInfo* collectTypeInfo(const Type* ty);
415
416 std::vector<const Function*> getLLVMGlobalFunctions(const GlobalVariable* global);
417
418 void loadModules(const std::vector<std::string>& moduleNameVec);
419 // Loads ExtAPI bitcode file; uses LLVMContext made while loading module bitcode files or from Module
420 void loadExtAPIModules();
421 void addSVFMain();
422
424 void createSVFFunction(const Function* func);
425 void initSVFFunction();
426 void initSVFBasicBlock(const Function* func);
427 void initDomTree(SVFFunction* func, const Function* f);
428 void setValueAttr(const Value* val, SVFValue* value);
430 void buildFunToFunMap();
433 void prePassSchedule();
434 void buildSymbolTable() const;
435 void collectExtFunAnnotations(const Module* mod);
436
439 {
440 CSToCallNodeMapTy::const_iterator it = CSToCallNodeMap.find(cs);
441 if (it == CSToCallNodeMap.end())
442 return nullptr;
443 return it->second;
444 }
445
448 {
449 CSToRetNodeMapTy::const_iterator it = CSToRetNodeMap.find(cs);
450 if (it == CSToRetNodeMap.end())
451 return nullptr;
452 return it->second;
453 }
454
456 {
457 InstToBlockNodeMapTy::const_iterator it = InstToBlockNodeMap.find(inst);
458 if (it == InstToBlockNodeMap.end())
459 return nullptr;
460 return it->second;
461 }
462
465 {
466 FunToFunEntryNodeMapTy::const_iterator it = FunToFunEntryNodeMap.find(fun);
467 if (it == FunToFunEntryNodeMap.end())
468 return nullptr;
469 return it->second;
470 }
471
474 {
475 FunToFunExitNodeMapTy::const_iterator it = FunToFunExitNodeMap.find(fun);
476 if (it == FunToFunExitNodeMap.end())
477 return nullptr;
478 return it->second;
479 }
480};
481
482} // End namespace SVF
483
484#endif /* INCLUDE_SVF_FE_LLVMMODULE_H_ */
const cJSON *const b
Definition cJSON.h:255
const char *const name
Definition cJSON.h:264
std::string getExtBcPath()
Definition ExtAPI.cpp:119
static ExtAPI * getExtAPI()
Definition ExtAPI.cpp:42
void addOtherValueMap(const Value *ov, SVFOtherValue *svfov)
Definition LLVMModule.h:231
void createSVFFunction(const Function *func)
IntraICFGNode * getIntraBlock(const Instruction *inst)
Definition LLVMModule.h:455
Map< const Instruction *, SVFInstruction * > LLVMInst2SVFInstMap
Definition LLVMModule.h:59
LLVMType2SVFTypeMap LLVMType2SVFType
Definition LLVMModule.h:100
std::vector< const Function * > getLLVMGlobalFunctions(const GlobalVariable *global)
bool hasGlobalRep(const GlobalVariable *val) const
Global to rep.
Definition LLVMModule.h:351
SVFOtherValue * getSVFOtherValue(const Value *ov)
LLVMBB2SVFBBMap LLVMBB2SVFBB
Definition LLVMModule.h:94
const SVFFunction * getSVFFunction(const std::string &name)
Get the corresponding Function based on its name.
Definition LLVMModule.h:307
FunExitICFGNode * getFunExitBlock(const Function *fun)
Get/Add a function exit node.
Definition LLVMModule.h:473
void buildSymbolTable() const
std::vector< const Function * > FunctionSetType
Definition LLVMModule.h:51
void addInstructionMap(const Instruction *inst, SVFInstruction *svfInst)
Definition LLVMModule.h:185
Module * getMainLLVMModule() const
Definition LLVMModule.h:364
LLVMFun2CallGraphNodeMap LLVMFunc2CallGraphNode
Map an LLVM Function to an CallGraph Node.
Definition LLVMModule.h:93
StInfo * collectTypeInfo(const Type *ty)
Collect a type info.
void initSVFBasicBlock(const Function *func)
Map< const Value *, SVFOtherValue * > LLVMValue2SVFOtherValueMap
Definition LLVMModule.h:62
InstToBlockNodeMapTy InstToBlockNodeMap
map a basic block to its ICFGNode
Definition LLVMModule.h:107
static LLVMModuleSet * getLLVMModuleSet()
Definition LLVMModule.h:122
GlobalVariable * getGlobalRep(const GlobalVariable *val) const
Definition LLVMModule.h:357
SVFConstantData * getSVFConstantData(const ConstantData *cd)
Map< const Function *, FunEntryICFGNode * > FunToFunEntryNodeMapTy
Definition LLVMModule.h:72
void loadModules(const std::vector< std::string > &moduleNameVec)
ObjTypeInference * typeInference
Definition LLVMModule.h:102
static void releaseLLVMModuleSet()
Definition LLVMModule.h:129
static void preProcessBCs(std::vector< std::string > &moduleNameVec)
static bool preProcessed
Definition LLVMModule.h:77
Module & getModuleRef(u32_t idx) const
Definition LLVMModule.h:163
FunEntryICFGNode * getFunEntryICFGNode(const Function *fun)
Add a function entry node.
Definition LLVMModule.h:335
void addInstructionMap(const Instruction *inst, CallICFGNode *svfInst)
Definition LLVMModule.h:190
Map< const Type *, StInfo * > Type2TypeInfoMap
Definition LLVMModule.h:66
void addInstructionMap(const Instruction *inst, RetICFGNode *svfInst)
Definition LLVMModule.h:195
void addToLLVMVal2SVFVarMap(const Value *val, SVFBaseNode *svfBaseNode)
void addBasicBlockMap(const BasicBlock *bb, SVFBasicBlock *svfBB)
Definition LLVMModule.h:180
Map< const BasicBlock *, SVFBasicBlock * > LLVMBB2SVFBBMap
Definition LLVMModule.h:58
DominatorTree & getDomTree(const Function *fun)
LLVMInst2SVFInstMap LLVMInst2SVFInst
Definition LLVMModule.h:95
SVFModule * svfModule
Borrowed from singleton SVFModule::svfModule.
Definition LLVMModule.h:79
RetICFGNode * getRetBlock(const Instruction *cs)
Get/Add a return node.
Definition LLVMModule.h:447
std::unique_ptr< LLVMContext > owned_ctx
Definition LLVMModule.h:81
LLVMConst2SVFConstMap LLVMConst2SVFConst
Definition LLVMModule.h:97
void createSVFDataStructure()
Map< const GlobalVariable *, GlobalVariable * > GlobalDefToRepMapTy
Definition LLVMModule.h:54
StInfo * collectSimpleTypeInfo(const Type *T)
Collect simple type (non-aggregate) info.
SymbolTableInfo * symInfo
Definition LLVMModule.h:78
CallGraphNode * getCallGraphNode(const Function *fun) const
Definition LLVMModule.h:253
void addArgumentMap(const Argument *arg, SVFArgument *svfArg)
Definition LLVMModule.h:206
Map< const Instruction *, RetICFGNode * > CSToRetNodeMapTy
Definition LLVMModule.h:70
Map< const Instruction *, IntraICFGNode * > InstToBlockNodeMapTy
Definition LLVMModule.h:71
const Value * getLLVMValue(const SVFValue *value) const
Definition LLVMModule.h:239
void addOtherConstantMap(const Constant *cons, SVFConstant *svfcons)
Definition LLVMModule.h:226
SVFType * getSVFType(const Type *T)
Get or create SVFType and typeinfo.
const Type * getLLVMType(const SVFType *T) const
Get LLVM Type.
Map< const Function *, FunExitICFGNode * > FunToFunExitNodeMapTy
Definition LLVMModule.h:73
bool hasICFGNode(const Instruction *inst)
CallICFGNode * getCallBlock(const Instruction *cs)
Get/Add a call node.
Definition LLVMModule.h:438
ICFGNode * getICFGNode(const Instruction *inst)
Get a basic block ICFGNode.
SVFBasicBlock * getSVFBasicBlock(const BasicBlock *bb) const
Definition LLVMModule.h:267
SVFConstant * getOtherSVFConstant(const Constant *oc)
SVFArgument * getSVFArgument(const Argument *arg) const
Definition LLVMModule.h:281
CallICFGNode * getCallICFGNode(const Instruction *cs)
get a call node
Fun2AnnoMap ExtFun2Annotations
Record annotations of function in extapi.bc.
Definition LLVMModule.h:88
void setValueAttr(const Value *val, SVFValue *value)
void addGlobalValueMap(const GlobalValue *glob, SVFGlobalValue *svfglob)
Definition LLVMModule.h:211
SVFValue2LLVMValueMap SVFValue2LLVMValue
Definition LLVMModule.h:99
CallGraph * callgraph
Definition LLVMModule.h:110
LLVMArgument2SVFArgumentMap LLVMArgument2SVFArgument
Definition LLVMModule.h:96
Module * getModule(u32_t idx) const
Definition LLVMModule.h:158
RetICFGNode * getRetICFGNode(const Instruction *cs)
get a return node
void prePassSchedule()
Invoke llvm passes to modify module.
void initDomTree(SVFFunction *func, const Function *f)
StInfo * collectStructInfo(const StructType *structTy, u32_t &numFields)
Collect the struct info and set the number of fields after flattening.
CSToCallNodeMapTy CSToCallNodeMap
map a callsite to its CallICFGNode
Definition LLVMModule.h:105
SVFFunction * getSVFFunction(const Function *fun) const
Definition LLVMModule.h:260
const std::vector< std::reference_wrapper< Module > > & getLLVMModules() const
Definition LLVMModule.h:153
LLVMContext & getContext() const
Definition LLVMModule.h:379
Map< const Function *, DominatorTree > FunToDominatorTree
Definition LLVMModule.h:112
CSToRetNodeMapTy CSToRetNodeMap
map a callsite to its RetICFGNode
Definition LLVMModule.h:106
LLVMFun2SVFFunMap LLVMFunc2SVFFunc
Map an LLVM Function to an SVF Function.
Definition LLVMModule.h:92
SVFBaseNode2LLVMValueMap SVFBaseNode2LLVMValue
Definition LLVMModule.h:104
FunExitICFGNode * getFunExitICFGNode(const Function *fun)
Add a function exit node.
Definition LLVMModule.h:342
Map< const SVFValue *, const Value * > SVFValue2LLVMValueMap
Definition LLVMModule.h:63
StInfo * collectArrayInfo(const ArrayType *T)
Collect the array info.
LLVMModuleSet()
Constructor.
SVFType * addSVFTypeInfo(const Type *t)
Create SVFTypes.
SVFGlobalValue * getSVFGlobalValue(const GlobalValue *g) const
Definition LLVMModule.h:288
FunEntryICFGNode * getFunEntryBlock(const Function *fun)
Get/Add a function entry node.
Definition LLVMModule.h:464
void addFunctionMap(const Function *func, SVFFunction *svfFunc)
Definition LLVMModule.h:172
Map< const Instruction *, CallICFGNode * > CSToCallNodeMapTy
Definition LLVMModule.h:69
void addInstructionMap(const Instruction *inst, IntraICFGNode *svfInst)
Definition LLVMModule.h:200
FunctionSetType ExtFuncsVec
Record some "sse_" function declarations used in other ext function definition, e....
Definition LLVMModule.h:86
Map< const Argument *, SVFArgument * > LLVMArgument2SVFArgumentMap
Definition LLVMModule.h:60
FunToFunEntryNodeMapTy FunToFunEntryNodeMap
map a function to its FunExitICFGNode
Definition LLVMModule.h:108
const Value * getLLVMValue(const SVFBaseNode *value) const
Definition LLVMModule.h:246
Map< const Constant *, SVFConstant * > LLVMConst2SVFConstMap
Definition LLVMModule.h:61
SVFModule * getSVFModule()
Definition LLVMModule.h:141
LLVMValue2SVFOtherValueMap LLVMValue2SVFOtherValue
Definition LLVMModule.h:98
static LLVMModuleSet * llvmModuleSet
Definition LLVMModule.h:76
IntraICFGNode * getIntraICFGNode(const Instruction *inst)
get a intra node
Map< const SVFBaseNode *, const Value * > SVFBaseNode2LLVMValueMap
Definition LLVMModule.h:64
std::vector< std::reference_wrapper< Module > > modules
Definition LLVMModule.h:83
Map< const Function *, const Function * > FunDeclToDefMapTy
Definition LLVMModule.h:52
Map< const Function *, SVFFunction * > LLVMFun2SVFFunMap
Definition LLVMModule.h:56
FunToFunExitNodeMapTy FunToFunExitNodeMap
map a function to its FunEntryICFGNode
Definition LLVMModule.h:109
SVFInstruction * getSVFInstruction(const Instruction *inst) const
Definition LLVMModule.h:274
bool empty() const
Definition LLVMModule.h:385
Map< std::string, std::vector< std::string > > Fun2AnnoMap
Definition LLVMModule.h:67
u32_t getModuleNum() const
Definition LLVMModule.h:148
GlobalDefToRepMapTy GlobalDefToRepMap
Global definition to a rep definition map.
Definition LLVMModule.h:90
std::vector< std::unique_ptr< Module > > owned_modules
Definition LLVMModule.h:82
static SVFModule * buildSVFModule(Module &mod)
Map< const Function *, CallGraphNode * > LLVMFun2CallGraphNodeMap
Definition LLVMModule.h:57
Map< const Type *, SVFType * > LLVMType2SVFTypeMap
Definition LLVMModule.h:65
Type2TypeInfoMap Type2TypeInfo
Definition LLVMModule.h:101
Map< const Function *, FunctionSetType > FunDefToDeclsMapTy
Definition LLVMModule.h:53
void dumpModulesToFile(const std::string &suffix)
void addConstantDataMap(const ConstantData *cd, SVFConstantData *svfcd)
Definition LLVMModule.h:221
void collectExtFunAnnotations(const Module *mod)
ObjTypeInference * getTypeInference()
SVFValue * getSVFValue(const Value *value)
for isBitcode
Definition BasicTypes.h:68
llvm::GlobalVariable GlobalVariable
Definition BasicTypes.h:130
llvm::ArrayType ArrayType
Definition BasicTypes.h:95
llvm::Type Type
Definition BasicTypes.h:83
llvm::BasicBlock BasicBlock
Definition BasicTypes.h:86
llvm::StructType StructType
LLVM types.
Definition BasicTypes.h:94
llvm::Argument Argument
Definition BasicTypes.h:145
llvm::Function Function
Definition BasicTypes.h:85
llvm::GlobalValue GlobalValue
Definition BasicTypes.h:88
llvm::ConstantData ConstantData
Definition BasicTypes.h:116
llvm::Instruction Instruction
Definition BasicTypes.h:87
llvm::Constant Constant
Definition BasicTypes.h:124
llvm::Value Value
LLVM Basic classes.
Definition BasicTypes.h:82
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:74
llvm::Module Module
Definition BasicTypes.h:84
unsigned u32_t
Definition GeneralType.h:46
llvm::DominatorTree DominatorTree
LLVM Dominators.
Definition BasicTypes.h:133
llvm::LLVMContext LLVMContext
Definition BasicTypes.h:70