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;
143 }
144
145 // Build an SVF module from a given LLVM Module instance (for use e.g. in a LLVM pass)
146 static void buildSVFModule(Module& mod);
147
148 // Build an SVF module from the bitcode files provided in `moduleNameVec`
149 static void buildSVFModule(const std::vector<std::string>& moduleNameVec);
150
151 static void preProcessBCs(std::vector<std::string>& moduleNameVec);
152
154 {
155 return modules.size();
156 }
157
158 const std::vector<std::reference_wrapper<Module>>& getLLVMModules() const
159 {
160 return modules;
161 }
162
164 {
165 return &getModuleRef(idx);
166 }
167
169 {
170 assert(idx < getModuleNum() && "Out of range.");
171 return modules[idx];
172 }
173
174 // Dump modules to files
175 void dumpModulesToFile(const std::string& suffix);
176
177public:
178
179 inline const BasicBlock* getFunExitBB(const Function* fun) const
180 {
181 auto it = funToExitBB.find(fun);
182 if (it == funToExitBB.end()) return nullptr;
183 else return it->second;
184 }
185
186 inline const Function* getRealDefFun(const Function* fun) const
187 {
188 auto it = funToRealDefFun.find(fun);
189 if (it == funToRealDefFun.end()) return nullptr;
190 else return it->second;
191 }
192
193 inline const FunctionSet& getFunctionSet() const
194 {
195 return funSet;
196 }
197
198 inline u32_t getValueNodeNum() const
199 {
200 return valSymMap.size();
201 }
202
203 inline u32_t getObjNodeNum() const
204 {
205 return objSymMap.size();
206 }
207
209 {
210 return valSymMap;
211 }
212
214 {
215 return objSymMap;
216 }
217
220 NodeID getValueNode(const Value* V);
221
222 bool hasValueNode(const Value* V);
223
226 NodeID getObjectNode(const Value* V);
227
228 void dumpSymTable();
229
230public:
231
232 // create a SVFBasicBlock according to LLVM BasicBlock, then add it to SVFFunction's BasicBlockGraph
233 inline void addBasicBlock(FunObjVar* fun, const BasicBlock* bb)
234 {
235 SVFBasicBlock* svfBB = fun->getBasicBlockGraph()->addBasicBlock(bb->getName().str());
236 LLVMBB2SVFBB[bb] = svfBB;
238 }
239
241 {
242 CSToCallNodeMap[inst] = svfInst;
244 }
246 {
247 CSToRetNodeMap[inst] = svfInst;
249 }
251 {
254 }
255
256 inline bool hasLLVMValue(const SVFValue* value) const
257 {
258 return SVFBaseNode2LLVMValue.find(value) != SVFBaseNode2LLVMValue.end();
259 }
260
261 const Value* getLLVMValue(const SVFValue* value) const
262 {
263 SVFBaseNode2LLVMValueMap ::const_iterator it = SVFBaseNode2LLVMValue.find(value);
264 assert(it != SVFBaseNode2LLVMValue.end() && "can't find corresponding llvm value!");
265 return it->second;
266 }
267
268 inline const FunObjVar* getFunObjVar(const Function* fun) const
269 {
270 LLVMFun2FunObjVarMap::const_iterator it = LLVMFun2FunObjVar.find(fun);
271 assert(it!=LLVMFun2FunObjVar.end() && "SVF Function not found!");
272 return it->second;
273 }
274
276 {
277 return returnSymMap;
278 }
279
281 {
282 return varargSymMap;
283 }
284
286 {
287 FunToIDMapTy::const_iterator iter = returnSymMap.find(func);
288 assert(iter!=returnSymMap.end() && "ret sym not found");
289 return iter->second;
290 }
291
293 {
294 FunToIDMapTy::const_iterator iter = varargSymMap.find(func);
295 assert(iter!=varargSymMap.end() && "vararg sym not found");
296 return iter->second;
297 }
298
300 {
301 LLVMBB2SVFBBMap::const_iterator it = LLVMBB2SVFBB.find(bb);
302 assert(it!=LLVMBB2SVFBB.end() && "SVF BasicBlock not found!");
303 return it->second;
304 }
305
307 inline const Function* getFunction(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 fun;
318 }
319 }
320 return nullptr;
321 }
322
324 const FunObjVar* getFunObjVar(const std::string& name);
325
326 ICFGNode* getICFGNode(const Instruction* inst);
327
328 bool hasICFGNode(const Instruction* inst);
329
336
339 {
341 assert(b && "Function entry not created?");
342 return b;
343 }
346 {
348 assert(b && "Function exit not created?");
349 return b;
350 }
351
352
354 bool hasGlobalRep(const GlobalVariable* val) const
355 {
356 GlobalDefToRepMapTy::const_iterator it = GlobalDefToRepMap.find(val);
357 return it != GlobalDefToRepMap.end();
358 }
359
361 {
362 GlobalDefToRepMapTy::const_iterator it = GlobalDefToRepMap.find(val);
363 assert(it != GlobalDefToRepMap.end() && "has no rep?");
364 return it->second;
365 }
366
368 {
369 assert(!empty() && "empty LLVM module!!");
370 for (size_t i = 0; i < getModuleNum(); ++i)
371 {
372 Module& module = getModuleRef(i);
373 if (module.getName().str() != ExtAPI::getExtAPI()->getExtBcPath())
374 {
375 return &module;
376 }
377 }
378 assert(false && "no main module found!");
379 return nullptr;
380 }
381
383 {
384 assert(!empty() && "empty LLVM module!!");
385 return getMainLLVMModule()->getContext();
386 }
387
388 bool empty() const
389 {
390 return getModuleNum() == 0;
391 }
392
394 SVFType* getSVFType(const Type* T);
396 const Type* getLLVMType(const SVFType* T) const;
397
399
400 DominatorTree& getDomTree(const Function* fun);
401
402 std::string getExtFuncAnnotation(const Function* fun, const std::string& funcAnnotation);
403
404 const std::vector<std::string>& getExtFuncAnnotations(const Function* fun);
405
406 // Does (F) have some annotation?
407 bool hasExtFuncAnnotation(const Function* fun, const std::string& funcAnnotation);
408
409 // Does (F) have a static var X (unavailable to us) that its return points to?
410 bool has_static(const Function *F);
411
412 // Does (F) have a memcpy_like operation?
413 bool is_memcpy(const Function *F);
414
415 // Does (F) have a memset_like operation?
416 bool is_memset(const Function *F);
417
418 // Does (F) allocate a new object and return it?
419 bool is_alloc(const Function *F);
420
421 // Does (F) allocate a new object and assign it to one of its arguments?
422 bool is_arg_alloc(const Function *F);
423
424 // Does (F) allocate a new stack object and return it?
425 bool is_alloc_stack_ret(const Function *F);
426
427 // Get the position of argument which holds the new object
429
430 // Does (F) reallocate a new object?
431 bool is_realloc(const Function *F);
432
433 // Should (F) be considered "external" (either not defined in the program
434 // or a user-defined version of a known alloc or no-op)?
435 bool is_ext(const Function *F);
436
437 // Set the annotation of (F)
438 void setExtFuncAnnotations(const Function* fun, const std::vector<std::string>& funcAnnotations);
439
440private:
441 inline void addFunctionSet(const Function* svfFunc)
442 {
443 funSet.push_back(svfFunc);
444 }
445
446 inline void setFunExitBB(const Function* fun, const BasicBlock* bb)
447 {
448 funToExitBB[fun] = bb;
449 }
450
451 inline void setFunRealDefFun(const Function* fun, const Function* realDefFun)
452 {
453 funToRealDefFun[fun] = realDefFun;
454 }
456 SVFType* addSVFTypeInfo(const Type* t);
458 StInfo* collectTypeInfo(const Type* ty);
465
466 std::vector<const Function*> getLLVMGlobalFunctions(const GlobalVariable* global);
467
468 void loadModules(const std::vector<std::string>& moduleNameVec);
469 // Loads ExtAPI bitcode file; uses LLVMContext made while loading module bitcode files or from Module
470 void loadExtAPIModules();
471 void addSVFMain();
472
474
476 void buildFunToFunMap();
479 void prePassSchedule();
480 void buildSymbolTable() const;
482
485 {
486 CSToCallNodeMapTy::const_iterator it = CSToCallNodeMap.find(cs);
487 if (it == CSToCallNodeMap.end())
488 return nullptr;
489 return it->second;
490 }
491
494 {
495 CSToRetNodeMapTy::const_iterator it = CSToRetNodeMap.find(cs);
496 if (it == CSToRetNodeMap.end())
497 return nullptr;
498 return it->second;
499 }
500
502 {
503 InstToBlockNodeMapTy::const_iterator it = InstToBlockNodeMap.find(inst);
504 if (it == InstToBlockNodeMap.end())
505 return nullptr;
506 return it->second;
507 }
508
511 {
512 FunToFunEntryNodeMapTy::const_iterator it = FunToFunEntryNodeMap.find(fun);
513 if (it == FunToFunEntryNodeMap.end())
514 return nullptr;
515 return it->second;
516 }
517
520 {
521 FunToFunExitNodeMapTy::const_iterator it = FunToFunExitNodeMap.find(fun);
522 if (it == FunToFunExitNodeMap.end())
523 return nullptr;
524 return it->second;
525 }
526};
527
528} // End namespace SVF
529
530#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:501
LLVMType2SVFTypeMap LLVMType2SVFType
Definition LLVMModule.h:101
ValueToIDMapTy & valSyms()
Definition LLVMModule.h:208
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:354
LLVMBB2SVFBBMap LLVMBB2SVFBB
Definition LLVMModule.h:100
FunToIDMapTy & retSyms()
Definition LLVMModule.h:275
FunExitICFGNode * getFunExitBlock(const Function *fun)
Get/Add a function exit node.
Definition LLVMModule.h:519
void buildSymbolTable() const
std::vector< const Function * > FunctionSetType
Definition LLVMModule.h:52
Module * getMainLLVMModule() const
Definition LLVMModule.h:367
const FunObjVar * getFunObjVar(const Function *fun) const
Definition LLVMModule.h:268
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:451
GlobalVariable * getGlobalRep(const GlobalVariable *val) const
Definition LLVMModule.h:360
SVFBasicBlock * getSVFBasicBlock(const BasicBlock *bb)
Definition LLVMModule.h:299
Map< const Function *, FunEntryICFGNode * > FunToFunEntryNodeMapTy
Definition LLVMModule.h:67
void loadModules(const std::vector< std::string > &moduleNameVec)
u32_t getObjNodeNum() const
Definition LLVMModule.h:203
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:168
FunEntryICFGNode * getFunEntryICFGNode(const Function *fun)
Add a function entry node.
Definition LLVMModule.h:338
void addInstructionMap(const Instruction *inst, CallICFGNode *svfInst)
Definition LLVMModule.h:240
Map< const Type *, StInfo * > Type2TypeInfoMap
Definition LLVMModule.h:61
void addInstructionMap(const Instruction *inst, RetICFGNode *svfInst)
Definition LLVMModule.h:245
Map< const BasicBlock *, SVFBasicBlock * > LLVMBB2SVFBBMap
Definition LLVMModule.h:58
bool hasLLVMValue(const SVFValue *value) const
Definition LLVMModule.h:256
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:493
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:261
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:446
const Type * getLLVMType(const SVFType *T) const
Get LLVM Type.
NodeID getReturnNode(const Function *func) const
Definition LLVMModule.h:285
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:484
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:307
NodeID getVarargNode(const Function *func) const
Definition LLVMModule.h:292
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:179
NodeID getObjectNode(const Value *V)
Module * getModule(u32_t idx) const
Definition LLVMModule.h:163
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:441
FunToIDMapTy returnSymMap
return map
Definition LLVMModule.h:116
const std::vector< std::reference_wrapper< Module > > & getLLVMModules() const
Definition LLVMModule.h:158
LLVMContext & getContext() const
Definition LLVMModule.h:382
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:345
FunToExitBBMap funToExitBB
Definition LLVMModule.h:120
StInfo * collectArrayInfo(const ArrayType *T)
Collect the array info.
LLVMModuleSet()
Constructor.
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:510
u32_t getValueNodeNum() const
Definition LLVMModule.h:198
Map< const Instruction *, CallICFGNode * > CSToCallNodeMapTy
Definition LLVMModule.h:64
void addInstructionMap(const Instruction *inst, IntraICFGNode *svfInst)
Definition LLVMModule.h:250
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:186
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:213
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:388
Map< std::string, std::vector< std::string > > Fun2AnnoMap
Definition LLVMModule.h:62
u32_t getModuleNum() const
Definition LLVMModule.h:153
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:233
bool is_memset(const Function *F)
FunToIDMapTy & varargSyms()
Definition LLVMModule.h:280
const FunctionSet & getFunctionSet() const
Definition LLVMModule.h:193
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)
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