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 "Util/Options.h"
35#include "Graphs/BasicBlockG.h"
36
37namespace SVF
38{
39
40class ObjTypeInference;
41
43{
44 friend class SVFIRBuilder;
45 friend class ICFGBuilder;
46 friend class SymbolTableBuilder;
47
48public:
49
50 typedef std::vector<const Function*> FunctionSetType;
54
61
67
71
73
74 typedef std::vector<const Function*> FunctionSet;
77
78private:
80 static bool preProcessed;
82 std::unique_ptr<LLVMContext> owned_ctx;
83 std::vector<std::unique_ptr<Module>> owned_modules;
84 std::vector<std::reference_wrapper<Module>> modules;
85
90
91 // Map SVFFunction to its annotations
93
96
102
109
111
116
120
123
124 void build();
125
126public:
128
130 {
131 if (!llvmModuleSet)
133 return llvmModuleSet;
134 }
135
137 {
138 delete llvmModuleSet;
139 llvmModuleSet = nullptr;
140 }
141
142 // Build an SVF module from a given LLVM Module instance (for use e.g. in a LLVM pass)
143 static void buildSVFModule(Module& mod);
144
145 // Build an SVF module from the bitcode files provided in `moduleNameVec`
146 static void buildSVFModule(const std::vector<std::string>& moduleNameVec);
147
148 static void preProcessBCs(std::vector<std::string>& moduleNameVec);
149
151 {
152 return modules.size();
153 }
154
155 const std::vector<std::reference_wrapper<Module>>& getLLVMModules() const
156 {
157 return modules;
158 }
159
161 {
162 return &getModuleRef(idx);
163 }
164
166 {
167 assert(idx < getModuleNum() && "Out of range.");
168 return modules[idx];
169 }
170
171 // Dump modules to files
172 void dumpModulesToFile(const std::string& suffix);
173
174public:
175
176 inline const BasicBlock* getFunExitBB(const Function* fun) const
177 {
178 auto it = funToExitBB.find(fun);
179 if (it == funToExitBB.end()) return nullptr;
180 else return it->second;
181 }
182
183 inline const Function* getRealDefFun(const Function* fun) const
184 {
185 auto it = funToRealDefFun.find(fun);
186 if (it == funToRealDefFun.end()) return nullptr;
187 else return it->second;
188 }
189
190 inline const FunctionSet& getFunctionSet() const
191 {
192 return funSet;
193 }
194
195 inline u32_t getValueNodeNum() const
196 {
197 return valSymMap.size();
198 }
199
200 inline u32_t getObjNodeNum() const
201 {
202 return objSymMap.size();
203 }
204
206 {
207 return valSymMap;
208 }
209
211 {
212 return objSymMap;
213 }
214
217 NodeID getValueNode(const Value* V);
218
219 bool hasValueNode(const Value* V);
220
223 NodeID getObjectNode(const Value* V);
224
225 void dumpSymTable();
226
227public:
228
229 // create a SVFBasicBlock according to LLVM BasicBlock, then add it to SVFFunction's BasicBlockGraph
230 inline void addBasicBlock(FunObjVar* fun, const BasicBlock* bb)
231 {
232 SVFBasicBlock* svfBB = fun->getBasicBlockGraph()->addBasicBlock(bb->getName().str());
233 LLVMBB2SVFBB[bb] = svfBB;
235 }
236
238 {
239 CSToCallNodeMap[inst] = svfInst;
241 }
243 {
244 CSToRetNodeMap[inst] = svfInst;
246 }
248 {
251 }
252
253 inline bool hasLLVMValue(const SVFValue* value) const
254 {
255 return SVFBaseNode2LLVMValue.find(value) != SVFBaseNode2LLVMValue.end();
256 }
257
258 const Value* getLLVMValue(const SVFValue* value) const
259 {
260 SVFBaseNode2LLVMValueMap ::const_iterator it = SVFBaseNode2LLVMValue.find(value);
261 assert(it != SVFBaseNode2LLVMValue.end() && "can't find corresponding llvm value!");
262 return it->second;
263 }
264
265 inline const FunObjVar* getFunObjVar(const Function* fun) const
266 {
267 LLVMFun2FunObjVarMap::const_iterator it = LLVMFun2FunObjVar.find(fun);
268 assert(it!=LLVMFun2FunObjVar.end() && "SVF Function not found!");
269 return it->second;
270 }
271
273 {
274 return returnSymMap;
275 }
276
278 {
279 return varargSymMap;
280 }
281
283 {
284 FunToIDMapTy::const_iterator iter = returnSymMap.find(func);
285 assert(iter!=returnSymMap.end() && "ret sym not found");
286 return iter->second;
287 }
288
290 {
291 FunToIDMapTy::const_iterator iter = varargSymMap.find(func);
292 assert(iter!=varargSymMap.end() && "vararg sym not found");
293 return iter->second;
294 }
295
297 {
298 LLVMBB2SVFBBMap::const_iterator it = LLVMBB2SVFBB.find(bb);
299 assert(it!=LLVMBB2SVFBB.end() && "SVF BasicBlock not found!");
300 return it->second;
301 }
302
304 inline const Function* getFunction(const std::string& name)
305 {
306 Function* fun = nullptr;
307
308 for (u32_t i = 0; i < llvmModuleSet->getModuleNum(); ++i)
309 {
311 fun = mod->getFunction(name);
312 if (fun)
313 {
314 return fun;
315 }
316 }
317 return nullptr;
318 }
319
321 const FunObjVar* getFunObjVar(const std::string& name);
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 DominatorTree& getDomTree(const Function* fun);
398
399 std::string getExtFuncAnnotation(const Function* fun, const std::string& funcAnnotation);
400
401 const std::vector<std::string>& getExtFuncAnnotations(const Function* fun);
402
403 // Does (F) have some annotation?
404 bool hasExtFuncAnnotation(const Function* fun, const std::string& funcAnnotation);
405
406 // Does (F) have a static var X (unavailable to us) that its return points to?
407 bool has_static(const Function *F);
408
409 // Does (F) have a memcpy_like operation?
410 bool is_memcpy(const Function *F);
411
412 // Does (F) have a memset_like operation?
413 bool is_memset(const Function *F);
414
415 // Does (F) allocate a new object and return it?
416 bool is_alloc(const Function *F);
417
418 // Does (F) allocate a new object and assign it to one of its arguments?
419 bool is_arg_alloc(const Function *F);
420
421 // Does (F) allocate a new stack object and return it?
422 bool is_alloc_stack_ret(const Function *F);
423
424 // Get the position of argument which holds the new object
426
427 // Does (F) reallocate a new object?
428 bool is_realloc(const Function *F);
429
430 // Should (F) be considered "external" (either not defined in the program
431 // or a user-defined version of a known alloc or no-op)?
432 bool is_ext(const Function *F);
433
434 // Set the annotation of (F)
435 void setExtFuncAnnotations(const Function* fun, const std::vector<std::string>& funcAnnotations);
436
437private:
438 inline void addFunctionSet(const Function* svfFunc)
439 {
440 funSet.push_back(svfFunc);
441 }
442
443 inline void setFunExitBB(const Function* fun, const BasicBlock* bb)
444 {
445 funToExitBB[fun] = bb;
446 }
447
448 inline void setFunRealDefFun(const Function* fun, const Function* realDefFun)
449 {
450 funToRealDefFun[fun] = realDefFun;
451 }
453 SVFType* addSVFTypeInfo(const Type* t);
455 StInfo* collectTypeInfo(const Type* ty);
462
463 std::vector<const Function*> getLLVMGlobalFunctions(const GlobalVariable* global);
464
465 void loadModules(const std::vector<std::string>& moduleNameVec);
466 // Loads ExtAPI bitcode file; uses LLVMContext made while loading module bitcode files or from Module
467 void loadExtAPIModules();
468 void addSVFMain();
469
471
473 void buildFunToFunMap();
476 void prePassSchedule();
477 void buildSymbolTable() const;
479
482 {
483 CSToCallNodeMapTy::const_iterator it = CSToCallNodeMap.find(cs);
484 if (it == CSToCallNodeMap.end())
485 return nullptr;
486 return it->second;
487 }
488
491 {
492 CSToRetNodeMapTy::const_iterator it = CSToRetNodeMap.find(cs);
493 if (it == CSToRetNodeMap.end())
494 return nullptr;
495 return it->second;
496 }
497
499 {
500 InstToBlockNodeMapTy::const_iterator it = InstToBlockNodeMap.find(inst);
501 if (it == InstToBlockNodeMap.end())
502 return nullptr;
503 return it->second;
504 }
505
508 {
509 FunToFunEntryNodeMapTy::const_iterator it = FunToFunEntryNodeMap.find(fun);
510 if (it == FunToFunEntryNodeMap.end())
511 return nullptr;
512 return it->second;
513 }
514
517 {
518 FunToFunExitNodeMapTy::const_iterator it = FunToFunExitNodeMap.find(fun);
519 if (it == FunToFunExitNodeMap.end())
520 return nullptr;
521 return it->second;
522 }
523};
524
525} // End namespace SVF
526
527#endif /* INCLUDE_SVF_FE_LLVMMODULE_H_ */
const cJSON *const b
Definition cJSON.h:255
const char *const name
Definition cJSON.h:264
SVFBasicBlock * addBasicBlock(const std::string &bbname)
std::string getExtBcPath()
Definition ExtAPI.cpp:120
static ExtAPI * getExtAPI()
Definition ExtAPI.cpp:43
BasicBlockGraph * getBasicBlockGraph()
bool hasValueNode(const Value *V)
NodeID getValueNode(const Value *V)
IntraICFGNode * getIntraBlock(const Instruction *inst)
Definition LLVMModule.h:498
LLVMType2SVFTypeMap LLVMType2SVFType
Definition LLVMModule.h:99
ValueToIDMapTy & valSyms()
Definition LLVMModule.h:205
bool is_alloc_stack_ret(const Function *F)
std::vector< const Function * > getLLVMGlobalFunctions(const GlobalVariable *global)
bool hasGlobalRep(const GlobalVariable *val) const
Global to rep.
Definition LLVMModule.h:351
LLVMBB2SVFBBMap LLVMBB2SVFBB
Definition LLVMModule.h:98
FunToIDMapTy & retSyms()
Definition LLVMModule.h:272
FunExitICFGNode * getFunExitBlock(const Function *fun)
Get/Add a function exit node.
Definition LLVMModule.h:516
void buildSymbolTable() const
std::vector< const Function * > FunctionSetType
Definition LLVMModule.h:50
Module * getMainLLVMModule() const
Definition LLVMModule.h:364
const FunObjVar * getFunObjVar(const Function *fun) const
Definition LLVMModule.h:265
ValueToIDMapTy valSymMap
map a value to its sym id
Definition LLVMModule.h:112
Map< const Function *, FunObjVar * > LLVMFun2FunObjVarMap
Definition LLVMModule.h:55
StInfo * collectTypeInfo(const Type *ty)
Collect a type info.
InstToBlockNodeMapTy InstToBlockNodeMap
map a basic block to its ICFGNode
Definition LLVMModule.h:106
static LLVMModuleSet * getLLVMModuleSet()
Definition LLVMModule.h:129
void setFunRealDefFun(const Function *fun, const Function *realDefFun)
Definition LLVMModule.h:448
GlobalVariable * getGlobalRep(const GlobalVariable *val) const
Definition LLVMModule.h:357
SVFBasicBlock * getSVFBasicBlock(const BasicBlock *bb)
Definition LLVMModule.h:296
Map< const Function *, FunEntryICFGNode * > FunToFunEntryNodeMapTy
Definition LLVMModule.h:65
void loadModules(const std::vector< std::string > &moduleNameVec)
u32_t getObjNodeNum() const
Definition LLVMModule.h:200
std::vector< const Function * > FunctionSet
Definition LLVMModule.h:74
ObjTypeInference * typeInference
Definition LLVMModule.h:101
static void releaseLLVMModuleSet()
Definition LLVMModule.h:136
static void preProcessBCs(std::vector< std::string > &moduleNameVec)
static bool preProcessed
Definition LLVMModule.h:80
Map< const Function *, const BasicBlock * > FunToExitBBMap
Definition LLVMModule.h:75
Module & getModuleRef(u32_t idx) const
Definition LLVMModule.h:165
FunEntryICFGNode * getFunEntryICFGNode(const Function *fun)
Add a function entry node.
Definition LLVMModule.h:335
void addInstructionMap(const Instruction *inst, CallICFGNode *svfInst)
Definition LLVMModule.h:237
Map< const Type *, StInfo * > Type2TypeInfoMap
Definition LLVMModule.h:59
void addInstructionMap(const Instruction *inst, RetICFGNode *svfInst)
Definition LLVMModule.h:242
Map< const BasicBlock *, SVFBasicBlock * > LLVMBB2SVFBBMap
Definition LLVMModule.h:56
bool hasLLVMValue(const SVFValue *value) const
Definition LLVMModule.h:253
DominatorTree & getDomTree(const Function *fun)
FunToRealDefFunMap funToRealDefFun
Definition LLVMModule.h:119
void addToSVFVar2LLVMValueMap(const Value *val, SVFValue *svfBaseNode)
Map< const Function *, const Function * > FunToRealDefFunMap
Definition LLVMModule.h:76
OrderedMap< const Value *, NodeID > ValueToIDMapTy
Definition LLVMModule.h:70
LLVMFun2FunObjVarMap LLVMFun2FunObjVar
Map an LLVM Function to an SVF Funobjvar.
Definition LLVMModule.h:97
RetICFGNode * getRetBlock(const Instruction *cs)
Get/Add a return node.
Definition LLVMModule.h:490
std::unique_ptr< LLVMContext > owned_ctx
Definition LLVMModule.h:82
FunToIDMapTy varargSymMap
vararg map
Definition LLVMModule.h:115
void createSVFDataStructure()
Map< const GlobalVariable *, GlobalVariable * > GlobalDefToRepMapTy
Definition LLVMModule.h:53
StInfo * collectSimpleTypeInfo(const Type *T)
Collect simple type (non-aggregate) info.
bool is_alloc(const Function *F)
Map< const Instruction *, RetICFGNode * > CSToRetNodeMapTy
Definition LLVMModule.h:63
Map< const Instruction *, IntraICFGNode * > InstToBlockNodeMapTy
Definition LLVMModule.h:64
const Value * getLLVMValue(const SVFValue *value) const
Definition LLVMModule.h:258
const std::vector< std::string > & getExtFuncAnnotations(const Function *fun)
SVFType * getSVFType(const Type *T)
Get or create SVFType and typeinfo.
void setFunExitBB(const Function *fun, const BasicBlock *bb)
Definition LLVMModule.h:443
const Type * getLLVMType(const SVFType *T) const
Get LLVM Type.
NodeID getReturnNode(const Function *func) const
Definition LLVMModule.h:282
Map< const Function *, FunExitICFGNode * > FunToFunExitNodeMapTy
Definition LLVMModule.h:66
void setExtFuncAnnotations(const Function *fun, const std::vector< std::string > &funcAnnotations)
bool hasICFGNode(const Instruction *inst)
CallICFGNode * getCallBlock(const Instruction *cs)
Get/Add a call node.
Definition LLVMModule.h:481
ValueToIDMapTy objSymMap
map a obj reference to its sym id
Definition LLVMModule.h:113
Map< const SVFValue *, const Value * > SVFBaseNode2LLVMValueMap
Definition LLVMModule.h:57
OrderedMap< const Function *, NodeID > FunToIDMapTy
Definition LLVMModule.h:72
ICFGNode * getICFGNode(const Instruction *inst)
Get a basic block ICFGNode.
const Function * getFunction(const std::string &name)
Get the corresponding Function based on its name.
Definition LLVMModule.h:304
NodeID getVarargNode(const Function *func) const
Definition LLVMModule.h:289
CallICFGNode * getCallICFGNode(const Instruction *cs)
get a call node
Fun2AnnoMap ExtFun2Annotations
Record annotations of function in extapi.bc.
Definition LLVMModule.h:89
bool is_arg_alloc(const Function *F)
Map< const Function *, std::vector< std::string > > func2Annotations
Definition LLVMModule.h:92
const BasicBlock * getFunExitBB(const Function *fun) const
Definition LLVMModule.h:176
NodeID getObjectNode(const Value *V)
Module * getModule(u32_t idx) const
Definition LLVMModule.h:160
RetICFGNode * getRetICFGNode(const Instruction *cs)
get a return node
void prePassSchedule()
Invoke llvm passes to modify module.
bool has_static(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:104
s32_t get_alloc_arg_pos(const Function *F)
void addFunctionSet(const Function *svfFunc)
Definition LLVMModule.h:438
FunToIDMapTy returnSymMap
return map
Definition LLVMModule.h:114
const std::vector< std::reference_wrapper< Module > > & getLLVMModules() const
Definition LLVMModule.h:155
LLVMContext & getContext() const
Definition LLVMModule.h:379
bool is_ext(const Function *F)
Map< const Function *, DominatorTree > FunToDominatorTree
Definition LLVMModule.h:110
CSToRetNodeMapTy CSToRetNodeMap
map a callsite to its RetICFGNode
Definition LLVMModule.h:105
static void buildSVFModule(Module &mod)
SVFBaseNode2LLVMValueMap SVFBaseNode2LLVMValue
Definition LLVMModule.h:103
FunExitICFGNode * getFunExitICFGNode(const Function *fun)
Add a function exit node.
Definition LLVMModule.h:342
FunToExitBBMap funToExitBB
Definition LLVMModule.h:118
StInfo * collectArrayInfo(const ArrayType *T)
Collect the array info.
LLVMModuleSet()
Constructor.
void buildGlobalDefToRepMap()
SVFType * addSVFTypeInfo(const Type *t)
Create SVFTypes.
FunctionSet funSet
Definition LLVMModule.h:117
FunEntryICFGNode * getFunEntryBlock(const Function *fun)
Get/Add a function entry node.
Definition LLVMModule.h:507
u32_t getValueNodeNum() const
Definition LLVMModule.h:195
Map< const Instruction *, CallICFGNode * > CSToCallNodeMapTy
Definition LLVMModule.h:62
void addInstructionMap(const Instruction *inst, IntraICFGNode *svfInst)
Definition LLVMModule.h:247
FunctionSetType ExtFuncsVec
Record some "sse_" function declarations used in other ext function definition, e....
Definition LLVMModule.h:87
bool hasExtFuncAnnotation(const Function *fun, const std::string &funcAnnotation)
FunToFunEntryNodeMapTy FunToFunEntryNodeMap
map a function to its FunExitICFGNode
Definition LLVMModule.h:107
const Function * getRealDefFun(const Function *fun) const
Definition LLVMModule.h:183
static LLVMModuleSet * llvmModuleSet
Definition LLVMModule.h:79
IntraICFGNode * getIntraICFGNode(const Instruction *inst)
get a intra node
std::vector< std::reference_wrapper< Module > > modules
Definition LLVMModule.h:84
Map< const Function *, const Function * > FunDeclToDefMapTy
Definition LLVMModule.h:51
ValueToIDMapTy & objSyms()
Definition LLVMModule.h:210
std::string getExtFuncAnnotation(const Function *fun, const std::string &funcAnnotation)
FunToFunExitNodeMapTy FunToFunExitNodeMap
map a function to its FunEntryICFGNode
Definition LLVMModule.h:108
bool empty() const
Definition LLVMModule.h:385
Map< std::string, std::vector< std::string > > Fun2AnnoMap
Definition LLVMModule.h:60
u32_t getModuleNum() const
Definition LLVMModule.h:150
GlobalDefToRepMapTy GlobalDefToRepMap
Global definition to a rep definition map.
Definition LLVMModule.h:95
std::vector< std::unique_ptr< Module > > owned_modules
Definition LLVMModule.h:83
void addBasicBlock(FunObjVar *fun, const BasicBlock *bb)
Definition LLVMModule.h:230
bool is_memset(const Function *F)
FunToIDMapTy & varargSyms()
Definition LLVMModule.h:277
const FunctionSet & getFunctionSet() const
Definition LLVMModule.h:190
Map< const Type *, SVFType * > LLVMType2SVFTypeMap
Definition LLVMModule.h:58
Type2TypeInfoMap Type2TypeInfo
Definition LLVMModule.h:100
Map< const Function *, FunctionSetType > FunDefToDeclsMapTy
Definition LLVMModule.h:52
void dumpModulesToFile(const std::string &suffix)
void collectExtFunAnnotations(const Module *mod)
bool is_memcpy(const Function *F)
ObjTypeInference * getTypeInference()
bool is_realloc(const Function *F)
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
u32_t NodeID
Definition GeneralType.h:56
llvm::Function Function
Definition BasicTypes.h:85
llvm::Instruction Instruction
Definition BasicTypes.h:87
llvm::Value Value
LLVM Basic classes.
Definition BasicTypes.h:82
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:74
signed s32_t
Definition GeneralType.h:48
llvm::Module Module
Definition BasicTypes.h:84
unsigned u32_t
Definition GeneralType.h:47
llvm::DominatorTree DominatorTree
LLVM Dominators.
Definition BasicTypes.h:133
llvm::LLVMContext LLVMContext
Definition BasicTypes.h:70