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