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