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