Static Value-Flow Analysis
Loading...
Searching...
No Matches
IRGraph.h
Go to the documentation of this file.
1//===- SVFIR.h -- SVF IR Graph or PAG (Program Assignment Graph)--------------//
2//
3// SVF: Static Value-Flow Analysis
4//
5// Copyright (C) <2013-2017> <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 * IRGraph.h
25 *
26 * Created on: Nov 1, 2013
27 * Author: Yulei Sui
28 */
29
30
31#ifndef IRGRAPH_H_
32#define IRGRAPH_H_
33
34#include "Graphs/GenericGraph.h"
35#include "SVFIR/SVFStatements.h"
36#include "SVFIR/SVFVariables.h"
37
38namespace SVF
39{
42
43class ICFGNode;
44class ObjTypeInfo;
45
46/*
47 * Graph representation of SVF IR.
48 * It can be seen as a program assignment graph (PAG).
49 */
50class IRGraph : public GenericGraph<SVFVar, SVFStmt>
51{
52 friend class SVFIRBuilder;
53 friend class SymbolTableBuilder;
54 friend class GraphDBClient;
55
56public:
57
70
72 //{@
75
78
82
83private:
87
94
97
100
101 void destorySymTable();
102
103public:
105
106protected:
109 bool fromFile;
114
116 inline NodeID addNode(SVFVar* node)
117 {
118 assert(node && "cannot add a null node");
119 addGNode(node->getId(),node);
120 return node->getId();
121 }
123 bool addEdge(SVFVar* src, SVFVar* dst, SVFStmt* edge);
124
131 const ICFGNode* cs);
136 const SVFVar* op2);
137
138public:
144
145 virtual ~IRGraph();
146
147
149 inline bool isBuiltFromFile()
150 {
151 return fromFile;
152 }
153
155 // @{
156 static inline bool isBlkPtr(NodeID id)
157 {
158 return (id == BlkPtr);
159 }
160 static inline bool isNullPtr(NodeID id)
161 {
162 return (id == NullPtr);
163 }
164 static inline bool isBlkObj(NodeID id)
165 {
166 return (id == BlackHole);
167 }
168 static inline bool isConstantSym(NodeID id)
169 {
170 return (id == ConstantObj);
171 }
172 static inline bool isBlkObjOrConstantObj(NodeID id)
173 {
174 return (isBlkObj(id) || isConstantSym(id));
175 }
176
177 inline NodeID blkPtrSymID() const
178 {
179 return BlkPtr;
180 }
181
182 inline NodeID nullPtrSymID() const
183 {
184 return NullPtr;
185 }
186
187 inline NodeID constantSymID() const
188 {
189 return ConstantObj;
190 }
191
192 inline NodeID blackholeSymID() const
193 {
194 return BlackHole;
195 }
196
198
199 inline u32_t getTotalSymNum() const
200 {
201 return totalSymNum;
202 }
203 inline u32_t getMaxStructSize() const
204 {
205 return maxStSize;
206 }
208
210
212 {
213 return objTypeInfoMap;
214 }
215
217 {
218 return objTypeInfoMap;
219 }
220
222 {
223 return returnFunObjSymMap;
224 }
225
227 {
228 return varargFunObjSymMap;
229 }
230
232
234 {
235 IDToTypeInfoMapTy::const_iterator iter = objTypeInfoMap.find(id);
236 assert(iter!=objTypeInfoMap.end() && "obj type info not found");
237 return iter->second;
238 }
239
241 NodeID getReturnNode(const FunObjVar*func) const;
242
244 NodeID getVarargNode(const FunObjVar*func) const;
245
247 {
248 return blackholeSymID();
249 }
250 inline NodeID getConstantNode() const
251 {
252 return constantSymID();
253 }
254 inline NodeID getBlkPtr() const
255 {
256 return blkPtrSymID();
257 }
258 inline NodeID getNullPtr() const
259 {
260 return nullPtrSymID();
261 }
263
265
267
268 inline const SVFTypeSet& getSVFTypes() const
269 {
270 return svfTypes;
271 }
272
273 inline const SVFType* getSVFType(u32_t id) const
274 {
275 for(const SVFType* type : svfTypes)
276 {
277 if(type->getId() == id)
278 return type;
279 }
280 return nullptr;
281 }
282 inline const Set<const StInfo*>& getStInfos() const
283 {
284 return stInfos;
285 }
287
288 virtual APOffset getModulusOffset(const BaseObjVar* baseObj, const APOffset& apOffset);
290
291
292 const StInfo* getTypeInfo(const SVFType* T) const;
293 inline bool hasSVFTypeInfo(const SVFType* T)
294 {
295 return svfTypes.find(T) != svfTypes.end();
296 }
297
300
302
315
317 void printFlattenFields(const SVFType* type);
318
319
321 {
323 }
328
329 inline u32_t getSVFVarNum() const
330 {
331 return nodeNum;
332 }
333 inline u32_t getSVFStmtNum() const
334 {
335 return edgeNum;
336 }
337 inline u32_t getPTAPAGEdgeNum() const
338 {
339 return totalPTAPAGEdge;
340 }
342 inline std::string getGraphName() const
343 {
344 return "SVFIR";
345 }
346
348
350 void dump(std::string name);
351
353 void view();
354
355
356
359
362
363 inline void addTypeInfo(const SVFType* ty)
364 {
365 bool inserted = svfTypes.insert(ty).second;
366 if(!inserted)
367 assert(false && "this type info has been added before");
368 }
369
370 inline void addStInfo(StInfo* stInfo)
371 {
372 stInfo->setStinfoId(stInfos.size());
373 stInfos.insert(stInfo);
374 }
375
376protected:
377
379 const std::vector<const SVFType*>& getFlattenFieldTypes(const SVFStructType *T);
380};
381
382}
383
384namespace SVF
385{
386
387/* !
388 * GenericGraphTraits specializations of SVFIR to be used for the generic graph algorithms.
389 * Provide graph traits for traversing from a SVFIR node using standard graph traversals.
390 */
391template<> struct GenericGraphTraits<SVF::SVFVar*> : public GenericGraphTraits<SVF::GenericNode<SVF::SVFVar,SVF::SVFStmt>* >
392{
393};
394
396template<> struct GenericGraphTraits<Inverse<SVF::SVFVar *> > : public GenericGraphTraits<Inverse<SVF::GenericNode<SVF::SVFVar,SVF::SVFStmt>* > >
397{
398};
399
400template<> struct GenericGraphTraits<SVF::IRGraph*> : public GenericGraphTraits<SVF::GenericGraph<SVF::SVFVar,SVF::SVFStmt>* >
401{
403};
404
405} // End namespace llvm
406#endif /* IRGRAPH_H_ */
newitem type
Definition cJSON.cpp:2739
const char *const name
Definition cJSON.h:264
void addGNode(NodeID id, NodeType *node)
Add a Node.
NodeID constantSymID() const
Definition IRGraph.h:187
u32_t getFlattenedElemIdx(const SVFType *T, u32_t origId)
Flattened element idx of an array or struct by considering stride.
Definition IRGraph.cpp:148
const Set< const StInfo * > & getStInfos() const
Definition IRGraph.h:282
u32_t getNodeNumAfterPAGBuild() const
Definition IRGraph.h:320
u32_t getNumOfFlattenElements(const SVFType *T)
Definition IRGraph.cpp:173
u32_t getSVFVarNum() const
Definition IRGraph.h:329
const std::vector< const SVFType * > & getFlattenFieldTypes(const SVFStructType *T)
Return the flattened field type for struct type only.
Definition IRGraph.cpp:122
void destorySymTable()
Definition IRGraph.cpp:40
u32_t getMaxStructSize() const
Definition IRGraph.h:203
const SVFType * maxStruct
The struct type with the most fields.
Definition IRGraph.h:358
SVFStmt * hasLabeledEdge(SVFVar *src, SVFVar *dst, SVFStmt::PEDGEK kind, const ICFGNode *cs)
Definition IRGraph.cpp:295
SVFStmt * hasEdge(SVFStmt *edge, SVFStmt::PEDGEK kind)
Definition IRGraph.cpp:306
void printFlattenFields(const SVFType *type)
Debug method.
Definition IRGraph.cpp:77
void dump(std::string name)
Dump SVFIR.
Definition IRGraph.cpp:320
virtual ~IRGraph()
Definition IRGraph.cpp:58
Set< const StInfo * > stInfos
(owned) All StInfo
Definition IRGraph.h:96
u32_t getObjectNodeNum()
Definition IRGraph.cpp:347
Set< const SVFStmt * > SVFStmtSet
Definition IRGraph.h:104
NodeID addNode(SVFVar *node)
Add a node into the graph.
Definition IRGraph.h:116
u32_t getSVFStmtNum() const
Definition IRGraph.h:333
void addStInfo(StInfo *stInfo)
Definition IRGraph.h:370
FunObjVarToIDMapTy & retFunObjSyms()
Definition IRGraph.h:221
IRGraph(bool buildFromFile)
Definition IRGraph.h:139
static bool isNullPtr(NodeID id)
Definition IRGraph.h:160
void dumpSymTable()
void view()
View graph from the debugger.
Definition IRGraph.cpp:328
NodeID getBlkPtr() const
Definition IRGraph.h:254
NodeID blkPtrSymID() const
Definition IRGraph.h:177
NodeID getBlackHoleNode() const
Definition IRGraph.h:246
virtual APOffset getModulusOffset(const BaseObjVar *baseObj, const APOffset &apOffset)
Given an offset from a Gep Instruction, return it modulus offset by considering memory layout.
Definition IRGraph.cpp:192
SVFStmt::KindToSVFStmtMapTy KindToSVFStmtSetMap
SVFIR edge map containing all PAGEdges.
Definition IRGraph.h:107
NodeID getNullPtr() const
Definition IRGraph.h:258
bool isBuiltFromFile()
Whether this SVFIR built from a txt file.
Definition IRGraph.h:149
const SVFType * getFlatternedElemType(const SVFType *baseType, u32_t flatten_idx)
Return the type of a flattened element given a flattened index.
Definition IRGraph.cpp:127
NodeID nullPtrSymID() const
Definition IRGraph.h:182
void addTypeInfo(const SVFType *ty)
Definition IRGraph.h:363
const SVFTypeSet & getSVFTypes() const
Constant reader that won't change the state of the symbol table.
Definition IRGraph.h:268
ObjTypeInfo * createObjTypeInfo(const SVFType *type)
Create an objectInfo based on LLVM type (value is null, and type could be null, representing a dummy ...
Definition IRGraph.cpp:235
u32_t getTotalSymNum() const
Statistics.
Definition IRGraph.h:199
u32_t getPTAPAGEdgeNum() const
Definition IRGraph.h:337
SYMTYPE
Symbol types.
Definition IRGraph.h:60
static bool isBlkObj(NodeID id)
Definition IRGraph.h:164
std::string getGraphName() const
Return graph name.
Definition IRGraph.h:342
FunObjVarToIDMapTy varargFunObjSymMap
vararg map
Definition IRGraph.h:85
NodeID getReturnNode(const FunObjVar *func) const
GetReturnNode - Return the unique node representing the return value of a function.
Definition IRGraph.cpp:64
const SVFType * getOriginalElemType(const SVFType *baseType, u32_t origId) const
Definition IRGraph.cpp:143
u32_t valVarNum
Definition IRGraph.h:112
void setNodeNumAfterPAGBuild(u32_t num)
Definition IRGraph.h:324
static bool isBlkObjOrConstantObj(NodeID id)
Definition IRGraph.h:172
IDToTypeInfoMapTy objTypeInfoMap
map a memory sym id to its obj
Definition IRGraph.h:86
bool addEdge(SVFVar *src, SVFVar *dst, SVFStmt *edge)
Add an edge into the graph.
Definition IRGraph.cpp:257
bool fromFile
Whether the SVFIR is built according to user specified data from a txt file.
Definition IRGraph.h:109
const IDToTypeInfoMapTy & idToObjTypeInfoMap() const
Definition IRGraph.h:216
NodeID nodeNumAfterPAGBuild
initial node number after building SVFIR, excluding later added nodes, e.g., gepobj nodes
Definition IRGraph.h:110
OrderedMap< const FunObjVar *, NodeID > FunObjVarToIDMapTy
function to sym id map
Definition IRGraph.h:77
NodeID totalSymNum
total number of symbols
Definition IRGraph.h:99
u32_t objVarNum
Definition IRGraph.h:113
u32_t totalPTAPAGEdge
Definition IRGraph.h:111
NodeID getVarargNode(const FunObjVar *func) const
getVarargNode - Return the unique node representing the variadic argument of a variadic function.
Definition IRGraph.cpp:71
static bool isConstantSym(NodeID id)
Definition IRGraph.h:168
const SVFType * getSVFType(u32_t id) const
Definition IRGraph.h:273
friend class GraphDBClient
Definition IRGraph.h:54
NodeID blackholeSymID() const
Definition IRGraph.h:192
SVFStmt * hasNonlabeledEdge(SVFVar *src, SVFVar *dst, SVFStmt::PEDGEK kind)
Definition IRGraph.cpp:272
u32_t maxStSize
The number of fields in max_struct.
Definition IRGraph.h:361
ObjTypeInfo * getObjTypeInfo(NodeID id) const
Definition IRGraph.h:233
u32_t getValueNodeNum()
Definition IRGraph.cpp:334
NodeID getConstantNode() const
Definition IRGraph.h:250
SVFTypeSet svfTypes
Definition IRGraph.h:93
const StInfo * getTypeInfo(const SVFType *T) const
Get struct info.
Definition IRGraph.cpp:246
static bool isBlkPtr(NodeID id)
special value
Definition IRGraph.h:156
FunObjVarToIDMapTy returnFunObjSymMap
return map
Definition IRGraph.h:84
SVFStmt::KindToSVFStmtMapTy KindToPTASVFStmtSetMap
SVFIR edge map containing only pointer-related edges, i.e., both LHS and RHS are of pointer type.
Definition IRGraph.h:108
const ObjTypeInfo * createDummyObjTypeInfo(NodeID symId, const SVFType *type)
Definition IRGraph.cpp:181
OrderedMap< NodeID, ObjTypeInfo * > IDToTypeInfoMapTy
various maps defined
Definition IRGraph.h:74
FunObjVarToIDMapTy & varargFunObjSyms()
Definition IRGraph.h:226
Set< const SVFType * > SVFTypeSet
struct type to struct info map
Definition IRGraph.h:80
IDToTypeInfoMapTy & idToObjTypeInfoMap()
Get different kinds of syms maps.
Definition IRGraph.h:211
bool hasSVFTypeInfo(const SVFType *T)
Definition IRGraph.h:293
PAGEdgeToSetMapTy KindToSVFStmtMapTy
NodeID getId() const
Get ID.
Definition SVFValue.h:158
for isBitcode
Definition BasicTypes.h:70
u32_t NodeID
Definition GeneralType.h:76
s64_t APOffset
Definition GeneralType.h:80
SVFVar PAGNode
Definition IRGraph.h:40
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:76
unsigned u32_t
Definition GeneralType.h:67
SVFStmt PAGEdge
Definition IRGraph.h:41