Static Value-Flow Analysis
IRGraph.cpp
Go to the documentation of this file.
1 //===- SVFIR.cpp -- 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  * SVFIR.cpp
25  *
26  * Created on: Nov 1, 2013
27  * Author: Yulei Sui
28  */
29 
30 #include "Graphs/IRGraph.h"
31 #include "Util/Options.h"
32 
33 using namespace SVF;
34 using namespace SVFUtil;
35 
37 {
39  symInfo = nullptr;
40 }
41 
45 bool IRGraph::addEdge(SVFVar* src, SVFVar* dst, SVFStmt* edge)
46 {
47 
49  outs() << "add edge from " << src->getId() << " kind :"
50  << src->getNodeKind() << " to " << dst->getId()
51  << " kind :" << dst->getNodeKind() << "\n");
52  src->addOutEdge(edge);
53  dst->addInEdge(edge);
54  return true;
55 }
56 
61 {
62  SVFStmt edge(src,dst,kind, false);
63  SVFStmt::SVFStmtSetTy::iterator it = KindToSVFStmtSetMap[kind].find(&edge);
64  if (it != KindToSVFStmtSetMap[kind].end())
65  {
66  return *it;
67  }
68  return nullptr;
69 }
70 
75 {
76  SVFStmt edge(src,op1,SVFStmt::makeEdgeFlagWithAddionalOpnd(kind,op2), false);
77  SVFStmt::SVFStmtSetTy::iterator it = KindToSVFStmtSetMap[kind].find(&edge);
78  if (it != KindToSVFStmtSetMap[kind].end())
79  {
80  return *it;
81  }
82  return nullptr;
83 }
84 
89 {
90  SVFStmt edge(src,dst,SVFStmt::makeEdgeFlagWithCallInst(kind,callInst), false);
91  SVFStmt::SVFStmtSetTy::iterator it = KindToSVFStmtSetMap[kind].find(&edge);
92  if (it != KindToSVFStmtSetMap[kind].end())
93  {
94  return *it;
95  }
96  return nullptr;
97 }
98 
103 {
105 }
106 
111 {
112  SVF::ViewGraph(this, "ProgramAssignmentGraph");
113 }
114 
115 
116 namespace SVF
117 {
121 template<>
123 {
124 
125  typedef SVFVar NodeType;
127  DOTGraphTraits(bool isSimple = false) :
128  DefaultDOTGraphTraits(isSimple)
129  {
130  }
131 
134  {
135  return graph->getGraphName();
136  }
137 
140  static bool isNodeHidden(SVFVar *node, IRGraph *)
141  {
142  if (Options::ShowHiddenNode()) return false;
143  else return node->isIsolatedNode();
144  }
145 
149  {
150  std::string str;
151  std::stringstream rawstr(str);
152  // print function info
153  if (node->getFunction())
154  rawstr << "[" << node->getFunction()->getName() << "] ";
155 
156  rawstr << node->toString();
157 
158  return rawstr.str();
159 
160  }
161 
163  {
164  if (SVFUtil::isa<ValVar>(node))
165  {
166  if(SVFUtil::isa<GepValVar>(node))
167  return "shape=hexagon";
168  else if (SVFUtil::isa<DummyValVar>(node))
169  return "shape=diamond";
170  else
171  return "shape=box";
172  }
173  else if (SVFUtil::isa<ObjVar>(node))
174  {
175  if(SVFUtil::isa<GepObjVar>(node))
176  return "shape=doubleoctagon";
177  else if(SVFUtil::isa<FIObjVar>(node))
178  return "shape=box3d";
179  else if (SVFUtil::isa<DummyObjVar>(node))
180  return "shape=tab";
181  else
182  return "shape=component";
183  }
184  else if (SVFUtil::isa<RetPN>(node))
185  {
186  return "shape=Mrecord";
187  }
188  else if (SVFUtil::isa<VarArgPN>(node))
189  {
190  return "shape=octagon";
191  }
192  else
193  {
194  assert(0 && "no such kind!!");
195  }
196  return "";
197  }
198 
199  template<class EdgeIter>
200  static std::string getEdgeAttributes(SVFVar*, EdgeIter EI, IRGraph*)
201  {
202  const SVFStmt* edge = *(EI.getCurrent());
203  assert(edge && "No edge found!!");
204  if (SVFUtil::isa<AddrStmt>(edge))
205  {
206  return "color=green";
207  }
208  else if (SVFUtil::isa<CopyStmt>(edge))
209  {
210  return "color=black";
211  }
212  else if (SVFUtil::isa<GepStmt>(edge))
213  {
214  return "color=purple";
215  }
216  else if (SVFUtil::isa<StoreStmt>(edge))
217  {
218  return "color=blue";
219  }
220  else if (SVFUtil::isa<LoadStmt>(edge))
221  {
222  return "color=red";
223  }
224  else if (SVFUtil::isa<PhiStmt>(edge))
225  {
226  return "color=grey";
227  }
228  else if (SVFUtil::isa<SelectStmt>(edge))
229  {
230  return "color=grey";
231  }
232  else if (SVFUtil::isa<CmpStmt>(edge))
233  {
234  return "color=grey";
235  }
236  else if (SVFUtil::isa<BinaryOPStmt>(edge))
237  {
238  return "color=grey";
239  }
240  else if (SVFUtil::isa<UnaryOPStmt>(edge))
241  {
242  return "color=grey";
243  }
244  else if (SVFUtil::isa<BranchStmt>(edge))
245  {
246  return "color=grey";
247  }
248  else if (SVFUtil::isa<TDForkPE>(edge))
249  {
250  return "color=Turquoise";
251  }
252  else if (SVFUtil::isa<TDJoinPE>(edge))
253  {
254  return "color=Turquoise";
255  }
256  else if (SVFUtil::isa<CallPE>(edge))
257  {
258  return "color=black,style=dashed";
259  }
260  else if (SVFUtil::isa<RetPE>(edge))
261  {
262  return "color=black,style=dotted";
263  }
264 
265  assert(false && "No such kind edge!!");
266  exit(1);
267  }
268 
269  template<class EdgeIter>
270  static std::string getEdgeSourceLabel(SVFVar*, EdgeIter EI)
271  {
272  const SVFStmt* edge = *(EI.getCurrent());
273  assert(edge && "No edge found!!");
274  if(const CallPE* calledge = SVFUtil::dyn_cast<CallPE>(edge))
275  {
276  return calledge->getCallSite()->getSourceLoc();
277  }
278  else if(const RetPE* retedge = SVFUtil::dyn_cast<RetPE>(edge))
279  {
280  return retedge->getCallSite()->getSourceLoc();
281  }
282  return "";
283  }
284 };
285 } // End namespace llvm
#define DBOUT(TYPE, X)
LLVM debug macros, define type of your DBUG model of each pass.
Definition: SVFType.h:484
#define DPAGBuild
Definition: SVFType.h:492
const char *const name
Definition: cJSON.h:264
const char *const string
Definition: cJSON.h:172
GEdgeSetTy::iterator iterator
Definition: GenericGraph.h:405
static void WriteGraphToFile(SVF::OutStream &O, const std::string &GraphName, const GraphType &GT, bool simple=false)
Definition: GraphPrinter.h:56
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
void view()
View graph from the debugger.
Definition: IRGraph.cpp:110
std::string getGraphName() const
Return graph name.
Definition: IRGraph.h:216
bool addEdge(SVFVar *src, SVFVar *dst, SVFStmt *edge)
Add an edge into the graph.
Definition: IRGraph.cpp:45
SVFStmt * hasNonlabeledEdge(SVFVar *src, SVFVar *dst, SVFStmt::PEDGEK kind)
Definition: IRGraph.cpp:60
static const Option< bool > ShowHiddenNode
Definition: Options.h:228
GNodeK getNodeKind() const
Get node kind.
Definition: GenericGraph.h:266
NodeID getId() const
Get ID.
Definition: GenericGraph.h:260
static GEdgeFlag makeEdgeFlagWithAddionalOpnd(GEdgeKind k, const SVFVar *var)
static GEdgeFlag makeEdgeFlagWithCallInst(GEdgeKind k, const ICFGNode *cs)
const std::string & getName() const
Definition: SVFValue.h:243
void addOutEdge(SVFStmt *outEdge)
Definition: SVFVariables.h:223
virtual const SVFFunction * getFunction() const
Return the function that this SVFVar resides in. Return nullptr if it is a global or constantexpr nod...
Definition: SVFVariables.h:122
void addInEdge(SVFStmt *inEdge)
add methods of the components
Definition: SVFVariables.h:216
bool isIsolatedNode() const
Whether this is an isolated node on the SVFIR graph.
virtual const std::string toString() const
static void releaseSymbolInfo()
std::ostream & outs()
Overwrite llvm::outs()
Definition: SVFUtil.h:50
for isBitcode
Definition: BasicTypes.h:68
void ViewGraph(const GraphType &G, const std::string &name, bool ShortNames=false, GraphProgram::Name Program=GraphProgram::DOT)
Definition: GraphWriter.h:371
static std::string getEdgeAttributes(SVFVar *, EdgeIter EI, IRGraph *)
Definition: IRGraph.cpp:200
DOTGraphTraits(bool isSimple=false)
Definition: IRGraph.cpp:127
static std::string getGraphName(IRGraph *graph)
Return name of the graph.
Definition: IRGraph.cpp:133
static std::string getNodeLabel(SVFVar *node, IRGraph *)
Definition: IRGraph.cpp:148
static std::string getNodeAttributes(SVFVar *node, IRGraph *)
Definition: IRGraph.cpp:162
static bool isNodeHidden(SVFVar *node, IRGraph *)
Definition: IRGraph.cpp:140
NodeType::iterator ChildIteratorType
Definition: IRGraph.cpp:126
static std::string getEdgeSourceLabel(SVFVar *, EdgeIter EI)
Definition: IRGraph.cpp:270