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
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);
135 const SVFVar* op2);
136
137public:
143
144 virtual ~IRGraph();
145
146
148 inline bool isBuiltFromFile()
149 {
150 return fromFile;
151 }
152
154 // @{
155 static inline bool isBlkPtr(NodeID id)
156 {
157 return (id == BlkPtr);
158 }
159 static inline bool isNullPtr(NodeID id)
160 {
161 return (id == NullPtr);
162 }
163 static inline bool isBlkObj(NodeID id)
164 {
165 return (id == BlackHole);
166 }
167 static inline bool isConstantSym(NodeID id)
168 {
169 return (id == ConstantObj);
170 }
171 static inline bool isBlkObjOrConstantObj(NodeID id)
172 {
173 return (isBlkObj(id) || isConstantSym(id));
174 }
175
176 inline NodeID blkPtrSymID() const
177 {
178 return BlkPtr;
179 }
180
181 inline NodeID nullPtrSymID() const
182 {
183 return NullPtr;
184 }
185
186 inline NodeID constantSymID() const
187 {
188 return ConstantObj;
189 }
190
191 inline NodeID blackholeSymID() const
192 {
193 return BlackHole;
194 }
195
197
198 inline u32_t getTotalSymNum() const
199 {
200 return totalSymNum;
201 }
202 inline u32_t getMaxStructSize() const
203 {
204 return maxStSize;
205 }
207
209
211 {
212 return objTypeInfoMap;
213 }
214
216 {
217 return objTypeInfoMap;
218 }
219
221 {
222 return returnFunObjSymMap;
223 }
224
226 {
227 return varargFunObjSymMap;
228 }
229
231
233 {
234 IDToTypeInfoMapTy::const_iterator iter = objTypeInfoMap.find(id);
235 assert(iter!=objTypeInfoMap.end() && "obj type info not found");
236 return iter->second;
237 }
238
240 NodeID getReturnNode(const FunObjVar*func) const;
241
243 NodeID getVarargNode(const FunObjVar*func) const;
244
246 {
247 return blackholeSymID();
248 }
249 inline NodeID getConstantNode() const
250 {
251 return constantSymID();
252 }
253 inline NodeID getBlkPtr() const
254 {
255 return blkPtrSymID();
256 }
257 inline NodeID getNullPtr() const
258 {
259 return nullPtrSymID();
260 }
261
263
265
267
268 inline const SVFTypeSet& getSVFTypes() const
269 {
270 return svfTypes;
271 }
272
273 inline const Set<const StInfo*>& getStInfos() const
274 {
275 return stInfos;
276 }
278
279 virtual APOffset getModulusOffset(const BaseObjVar* baseObj, const APOffset& apOffset);
281
282
283 const StInfo* getTypeInfo(const SVFType* T) const;
284 inline bool hasSVFTypeInfo(const SVFType* T)
285 {
286 return svfTypes.find(T) != svfTypes.end();
287 }
288
291
293
306
308 void printFlattenFields(const SVFType* type);
309
310
312 {
314 }
319
320 inline u32_t getPAGNodeNum() const
321 {
322 return nodeNum;
323 }
324 inline u32_t getPAGEdgeNum() const
325 {
326 return edgeNum;
327 }
328 inline u32_t getPTAPAGEdgeNum() const
329 {
330 return totalPTAPAGEdge;
331 }
333 inline std::string getGraphName() const
334 {
335 return "SVFIR";
336 }
337
339
341 void dump(std::string name);
342
344 void view();
345
346
347
350
353
354 inline void addTypeInfo(const SVFType* ty)
355 {
356 bool inserted = svfTypes.insert(ty).second;
357 if(!inserted)
358 assert(false && "this type info has been added before");
359 }
360
361 inline void addStInfo(StInfo* stInfo)
362 {
363 stInfos.insert(stInfo);
364 }
365
366protected:
367
369 const std::vector<const SVFType*>& getFlattenFieldTypes(const SVFStructType *T);
370};
371
372}
373
374namespace SVF
375{
376
377/* !
378 * GenericGraphTraits specializations of SVFIR to be used for the generic graph algorithms.
379 * Provide graph traits for traversing from a SVFIR node using standard graph traversals.
380 */
381template<> struct GenericGraphTraits<SVF::SVFVar*> : public GenericGraphTraits<SVF::GenericNode<SVF::SVFVar,SVF::SVFStmt>* >
382{
383};
384
386template<> struct GenericGraphTraits<Inverse<SVF::SVFVar *> > : public GenericGraphTraits<Inverse<SVF::GenericNode<SVF::SVFVar,SVF::SVFStmt>* > >
387{
388};
389
390template<> struct GenericGraphTraits<SVF::IRGraph*> : public GenericGraphTraits<SVF::GenericGraph<SVF::SVFVar,SVF::SVFStmt>* >
391{
393};
394
395} // End namespace llvm
396#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:186
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:273
u32_t getNodeNumAfterPAGBuild() const
Definition IRGraph.h:311
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:202
const SVFType * maxStruct
The struct type with the most fields.
Definition IRGraph.h:349
SVFStmt * hasLabeledEdge(SVFVar *src, SVFVar *dst, SVFStmt::PEDGEK kind, const ICFGNode *cs)
Definition IRGraph.cpp:296
void printFlattenFields(const SVFType *type)
Debug method.
Definition IRGraph.cpp:73
void dump(std::string name)
Dump SVFIR.
Definition IRGraph.cpp:310
virtual ~IRGraph()
Definition IRGraph.cpp:54
Set< const StInfo * > stInfos
(owned) All StInfo
Definition IRGraph.h:96
u32_t getObjectNodeNum()
Definition IRGraph.cpp:337
Set< const SVFStmt * > SVFStmtSet
Definition IRGraph.h:104
NodeID addNode(SVFVar *node)
Add a node into the graph.
Definition IRGraph.h:116
void addStInfo(StInfo *stInfo)
Definition IRGraph.h:361
FunObjVarToIDMapTy & retFunObjSyms()
Definition IRGraph.h:220
IRGraph(bool buildFromFile)
Definition IRGraph.h:138
static bool isNullPtr(NodeID id)
Definition IRGraph.h:159
void dumpSymTable()
void view()
View graph from the debugger.
Definition IRGraph.cpp:318
NodeID getBlkPtr() const
Definition IRGraph.h:253
NodeID blkPtrSymID() const
Definition IRGraph.h:176
NodeID getBlackHoleNode() const
Definition IRGraph.h:245
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:107
NodeID getNullPtr() const
Definition IRGraph.h:257
bool isBuiltFromFile()
Whether this SVFIR built from a txt file.
Definition IRGraph.h:148
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:181
void addTypeInfo(const SVFType *ty)
Definition IRGraph.h:354
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:231
u32_t getTotalSymNum() const
Statistics.
Definition IRGraph.h:198
u32_t getPTAPAGEdgeNum() const
Definition IRGraph.h:328
SYMTYPE
Symbol types.
Definition IRGraph.h:60
static bool isBlkObj(NodeID id)
Definition IRGraph.h:163
std::string getGraphName() const
Return graph name.
Definition IRGraph.h:333
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:60
const SVFType * getOriginalElemType(const SVFType *baseType, u32_t origId) const
Definition IRGraph.cpp:139
u32_t valVarNum
Definition IRGraph.h:112
void setNodeNumAfterPAGBuild(u32_t num)
Definition IRGraph.h:315
static bool isBlkObjOrConstantObj(NodeID id)
Definition IRGraph.h:171
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:253
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:215
NodeID nodeNumAfterPAGBuild
initial node number after building SVFIR, excluding later added nodes, e.g., gepobj nodes
Definition IRGraph.h:110
u32_t getPAGEdgeNum() const
Definition IRGraph.h:324
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:67
static bool isConstantSym(NodeID id)
Definition IRGraph.h:167
NodeID blackholeSymID() const
Definition IRGraph.h:191
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:352
ObjTypeInfo * getObjTypeInfo(NodeID id) const
Definition IRGraph.h:232
u32_t getValueNodeNum()
Definition IRGraph.cpp:324
NodeID getConstantNode() const
Definition IRGraph.h:249
SVFTypeSet svfTypes
Definition IRGraph.h:93
const StInfo * getTypeInfo(const SVFType *T) const
Get struct info.
Definition IRGraph.cpp:242
static bool isBlkPtr(NodeID id)
special value
Definition IRGraph.h:155
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:177
OrderedMap< NodeID, ObjTypeInfo * > IDToTypeInfoMapTy
various maps defined
Definition IRGraph.h:74
FunObjVarToIDMapTy & varargFunObjSyms()
Definition IRGraph.h:225
u32_t getPAGNodeNum() const
Definition IRGraph.h:320
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:210
bool hasSVFTypeInfo(const SVFType *T)
Definition IRGraph.h:284
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