Static Value-Flow Analysis
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"
36 #include "Util/NodeIDAllocator.h"
37 #include "Util/SVFUtil.h"
38 #include "Graphs/ICFG.h"
39 
40 namespace SVF
41 {
42 typedef SVFVar PAGNode;
43 typedef SVFStmt PAGEdge;
44 
45 /*
46  * Graph representation of SVF IR.
47  * It can be seen as a program assignment graph (PAG).
48  */
49 class IRGraph : public GenericGraph<SVFVar, SVFStmt>
50 {
51  friend class SVFIRWriter;
52  friend class SVFIRReader;
53 
54 public:
57 
58 protected:
61  bool fromFile;
66 
68  inline NodeID addNode(SVFVar* node, NodeID i)
69  {
70  addGNode(i,node);
71  return i;
72  }
74  bool addEdge(SVFVar* src, SVFVar* dst, SVFStmt* edge);
75 
82  const ICFGNode* cs);
86  const SVFVar* op2);
87 
89  inline void mapValueToEdge(const SVFValue* V, SVFStmt *edge)
90  {
91  auto inserted = valueToEdgeMap.emplace(V, SVFStmtSet{edge});
92  if (!inserted.second)
93  {
94  inserted.first->second.emplace(edge);
95  }
96  }
98  inline const MemObj* getMemObj(const SVFValue* val) const
99  {
100  return symInfo->getObj(symInfo->getObjSym(val));
101  }
102 
103 public:
104  IRGraph(bool buildFromFile)
105  : fromFile(buildFromFile), nodeNumAfterPAGBuild(0), totalPTAPAGEdge(0)
106  {
108  // insert dummy value if a correct value cannot be found
109  valueToEdgeMap[nullptr] = SVFStmtSet();
110  }
111 
112  virtual ~IRGraph();
113 
115  {
116  return symInfo;
117  }
119  inline bool isBuiltFromFile()
120  {
121  return fromFile;
122  }
124  inline const SVFStmtSet& getValueEdges(const SVFValue* V)
125  {
126  auto it = valueToEdgeMap.find(V);
127  if (it == valueToEdgeMap.end())
128  {
129  //special empty set
130  return valueToEdgeMap.at(nullptr);
131  }
132  return it->second;
133  }
134 
137  inline NodeID getValueNode(const SVFValue* V)
138  {
139  return symInfo->getValSym(V);
140  }
141  inline bool hasValueNode(const SVFValue* V)
142  {
143  return symInfo->hasValSym(V);
144  }
147  inline NodeID getObjectNode(const SVFValue* V)
148  {
149  return symInfo->getObjSym(V);
150  }
152  inline NodeID getReturnNode(const SVFFunction* func) const
153  {
154  return symInfo->getRetSym(func);
155  }
157  inline NodeID getVarargNode(const SVFFunction* func) const
158  {
159  return symInfo->getVarargSym(func);
160  }
161  inline NodeID getBlackHoleNode() const
162  {
163  return symInfo->blackholeSymID();
164  }
165  inline NodeID getConstantNode() const
166  {
167  return symInfo->constantSymID();
168  }
169  inline NodeID getBlkPtr() const
170  {
171  return symInfo->blkPtrSymID();
172  }
173  inline NodeID getNullPtr() const
174  {
175  return symInfo->nullPtrSymID();
176  }
177  inline const MemObj* getBlackHoleObj() const
178  {
179  return symInfo->getBlkObj();
180  }
181  inline const MemObj* getConstantObj() const
182  {
183  return symInfo->getConstantObj();
184  }
185 
186  inline u32_t getValueNodeNum() const
187  {
188  return symInfo->valSyms().size();
189  }
190  inline u32_t getObjectNodeNum() const
191  {
192  return symInfo->idToObjMap().size();
193  }
195  {
196  return nodeNumAfterPAGBuild;
197  }
199  {
200  nodeNumAfterPAGBuild = num;
201  }
202 
203  inline u32_t getPAGNodeNum() const
204  {
205  return nodeNum;
206  }
207  inline u32_t getPAGEdgeNum() const
208  {
209  return edgeNum;
210  }
211  inline u32_t getPTAPAGEdgeNum() const
212  {
213  return totalPTAPAGEdge;
214  }
216  inline std::string getGraphName() const
217  {
218  return "SVFIR";
219  }
220 
222  void dump(std::string name);
223 
225  void view();
226 };
227 
228 }
229 
230 namespace SVF
231 {
232 
233 /* !
234  * GenericGraphTraits specializations of SVFIR to be used for the generic graph algorithms.
235  * Provide graph traits for traversing from a SVFIR node using standard graph traversals.
236  */
237 template<> struct GenericGraphTraits<SVF::SVFVar*> : public GenericGraphTraits<SVF::GenericNode<SVF::SVFVar,SVF::SVFStmt>* >
238 {
239 };
240 
242 template<> struct GenericGraphTraits<Inverse<SVF::SVFVar *> > : public GenericGraphTraits<Inverse<SVF::GenericNode<SVF::SVFVar,SVF::SVFStmt>* > >
243 {
244 };
245 
246 template<> struct GenericGraphTraits<SVF::IRGraph*> : public GenericGraphTraits<SVF::GenericGraph<SVF::SVFVar,SVF::SVFStmt>* >
247 {
249 };
250 
251 } // End namespace llvm
252 #endif /* IRGRAPH_H_ */
const char *const name
Definition: cJSON.h:264
const char *const string
Definition: cJSON.h:172
void addGNode(NodeID id, NodeType *node)
Add a Node.
Definition: GenericGraph.h:646
u32_t edgeNum
total num of node
Definition: GenericGraph.h:702
u32_t nodeNum
total num of edge
Definition: GenericGraph.h:703
u32_t getValueNodeNum() const
Definition: IRGraph.h:186
u32_t getNodeNumAfterPAGBuild() const
Definition: IRGraph.h:194
ValueToEdgeMap valueToEdgeMap
Map SVFValues (e.g., ICFGNodes) to all corresponding PAGEdges.
Definition: IRGraph.h:64
SVFStmt * hasLabeledEdge(SVFVar *src, SVFVar *dst, SVFStmt::PEDGEK kind, const ICFGNode *cs)
Definition: IRGraph.cpp:88
void dump(std::string name)
Dump SVFIR.
Definition: IRGraph.cpp:102
virtual ~IRGraph()
Definition: IRGraph.cpp:36
Set< const SVFStmt * > SVFStmtSet
Definition: IRGraph.h:55
IRGraph(bool buildFromFile)
Definition: IRGraph.h:104
void view()
View graph from the debugger.
Definition: IRGraph.cpp:110
NodeID getBlkPtr() const
Definition: IRGraph.h:169
NodeID getBlackHoleNode() const
Definition: IRGraph.h:161
SVFStmt::KindToSVFStmtMapTy KindToSVFStmtSetMap
SVFIR edge map containing all PAGEdges.
Definition: IRGraph.h:59
NodeID getNullPtr() const
Definition: IRGraph.h:173
bool hasValueNode(const SVFValue *V)
Definition: IRGraph.h:141
bool isBuiltFromFile()
Whether this SVFIR built from a txt file.
Definition: IRGraph.h:119
NodeID getValueNode(const SVFValue *V)
Definition: IRGraph.h:137
void mapValueToEdge(const SVFValue *V, SVFStmt *edge)
Map a value to a set of edges.
Definition: IRGraph.h:89
u32_t getPTAPAGEdgeNum() const
Definition: IRGraph.h:211
std::string getGraphName() const
Return graph name.
Definition: IRGraph.h:216
void setNodeNumAfterPAGBuild(u32_t num)
Definition: IRGraph.h:198
const SVFStmtSet & getValueEdges(const SVFValue *V)
Get all SVFIR Edges that corresponds to an LLVM value.
Definition: IRGraph.h:124
Map< const SVFValue *, SVFStmtSet > ValueToEdgeMap
Definition: IRGraph.h:56
SymbolTableInfo * symInfo
Definition: IRGraph.h:65
NodeID addNode(SVFVar *node, NodeID i)
Add a node into the graph.
Definition: IRGraph.h:68
bool addEdge(SVFVar *src, SVFVar *dst, SVFStmt *edge)
Add an edge into the graph.
Definition: IRGraph.cpp:45
bool fromFile
Whether the SVFIR is built according to user specified data from a txt file.
Definition: IRGraph.h:61
NodeID nodeNumAfterPAGBuild
initial node number after building SVFIR, excluding later added nodes, e.g., gepobj nodes
Definition: IRGraph.h:62
u32_t getPAGEdgeNum() const
Definition: IRGraph.h:207
u32_t getObjectNodeNum() const
Definition: IRGraph.h:190
NodeID getVarargNode(const SVFFunction *func) const
getVarargNode - Return the unique node representing the variadic argument of a variadic function.
Definition: IRGraph.h:157
NodeID getReturnNode(const SVFFunction *func) const
GetReturnNode - Return the unique node representing the return value of a function.
Definition: IRGraph.h:152
u32_t totalPTAPAGEdge
Definition: IRGraph.h:63
SymbolTableInfo * getSymbolInfo() const
Definition: IRGraph.h:114
const MemObj * getBlackHoleObj() const
Definition: IRGraph.h:177
NodeID getObjectNode(const SVFValue *V)
Definition: IRGraph.h:147
SVFStmt * hasNonlabeledEdge(SVFVar *src, SVFVar *dst, SVFStmt::PEDGEK kind)
Definition: IRGraph.cpp:60
NodeID getConstantNode() const
Definition: IRGraph.h:165
const MemObj * getConstantObj() const
Definition: IRGraph.h:181
const MemObj * getMemObj(const SVFValue *val) const
get MemObj according to LLVM value
Definition: IRGraph.h:98
SVFStmt::KindToSVFStmtMapTy KindToPTASVFStmtSetMap
SVFIR edge map containing only pointer-related edges, i.e., both LHS and RHS are of pointer type.
Definition: IRGraph.h:60
u32_t getPAGNodeNum() const
Definition: IRGraph.h:203
PAGEdgeToSetMapTy KindToSVFStmtMapTy
SymID blackholeSymID() const
SymID blkPtrSymID() const
static SymbolTableInfo * SymbolInfo()
Singleton design here to make sure we only have one instance during any analysis.
MemObj * getObj(SymID id) const
MemObj * getConstantObj() const
SymID getVarargSym(const SVFFunction *val) const
SymID getValSym(const SVFValue *val)
Get different kinds of syms.
SymID getObjSym(const SVFValue *val) const
ValueToIDMapTy & valSyms()
Get different kinds of syms maps.
IDToMemMapTy & idToObjMap()
SymID constantSymID() const
SymID nullPtrSymID() const
MemObj * getBlkObj() const
bool hasValSym(const SVFValue *val)
SymID getRetSym(const SVFFunction *val) const
for isBitcode
Definition: BasicTypes.h:68
u32_t NodeID
Definition: GeneralType.h:55
SVFVar PAGNode
Definition: IRGraph.h:42
std::unordered_map< Key, Value, Hash, KeyEqual, Allocator > Map
Definition: GeneralType.h:101
unsigned u32_t
Definition: GeneralType.h:46
SVFStmt PAGEdge
Definition: IRGraph.h:43
std::unordered_set< Key, Hash, KeyEqual, Allocator > Set
Definition: GeneralType.h:96