Static Value-Flow Analysis
SrcSnkSolver.h
Go to the documentation of this file.
1 //===- SrcSnkSolver.h -- CFL reachability solver---------------------------------//
2 //
3 // SVF: Static Value-Flow Analysis
4 //
5 // Copyright (C) <2013-> <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  * SrcSnkSolver.h
25  *
26  * Created on: Apr 1, 2014
27  * Author: Yulei Sui
28  */
29 
30 #ifndef CFLSOLVER_H_
31 #define CFLSOLVER_H_
32 
33 #include "Util/WorkList.h"
34 #include "Util/DPItem.h"
35 
36 namespace SVF
37 {
38 
39 /*
40  * Generic CFL solver for demand-driven analysis based on different graphs (e.g. SVFIR, VFG, ThreadVFG)
41  * Extend this class for sophisticated CFL-reachability resolution (e.g. field, flow, path)
42  */
43 template<class GraphType, class DPIm = DPItem>
45 {
46 
47 public:
50  typedef typename GTraits::NodeType GNODE;
51  typedef typename GTraits::EdgeType GEDGE;
52  typedef typename GTraits::nodes_iterator node_iterator;
53  typedef typename GTraits::ChildIteratorType child_iterator;
54 
57  typedef typename InvGTraits::ChildIteratorType inv_child_iterator;
58 
61 
62 protected:
63 
65  SrcSnkSolver(): _graph(nullptr)
66  {
67  }
69  virtual ~SrcSnkSolver()
70  {
71  }
73 
74  const inline GraphType graph() const
75  {
76  return _graph;
77  }
78  inline void setGraph(GraphType g)
79  {
80  _graph = g;
81  }
83 
84  inline GNODE* getNode(NodeID id) const
85  {
86  return _graph->getGNode(id);
87  }
88  virtual inline NodeID getNodeIDFromItem(const DPIm& item) const
89  {
90  return item.getCurNodeID();
91  }
93  virtual void forwardTraverse(DPIm& it)
94  {
95  pushIntoWorklist(it);
96 
97  while (!isWorklistEmpty())
98  {
99  DPIm item = popFromWorklist();
101 
103  child_iterator EI = GTraits::child_begin(v);
104  child_iterator EE = GTraits::child_end(v);
105  for (; EI != EE; ++EI)
106  {
107  FWProcessOutgoingEdge(item,*(EI.getCurrent()) );
108  }
109  }
110  }
112  virtual void backwardTraverse(DPIm& it)
113  {
114  pushIntoWorklist(it);
115 
116  while (!isWorklistEmpty())
117  {
118  DPIm item = popFromWorklist();
120 
122  inv_child_iterator EI = InvGTraits::child_begin(v);
123  inv_child_iterator EE = InvGTraits::child_end(v);
124  for (; EI != EE; ++EI)
125  {
126  BWProcessIncomingEdge(item,*(EI.getCurrent()) );
127  }
128  }
129  }
131 
132  virtual void FWProcessCurNode(const DPIm&)
133  {
134  }
135  virtual void BWProcessCurNode(const DPIm&)
136  {
137  }
139 
141  virtual void FWProcessOutgoingEdge(const DPIm& item, GEDGE* edge)
142  {
143  DPIm newItem(item);
144  newItem.setCurNodeID(edge->getDstID());
145  pushIntoWorklist(newItem);
146  }
147  virtual void BWProcessIncomingEdge(const DPIm& item, GEDGE* edge)
148  {
149  DPIm newItem(item);
150  newItem.setCurNodeID(edge->getSrcID());
151  pushIntoWorklist(newItem);
152  }
154 
156  inline DPIm popFromWorklist()
157  {
158  return worklist.pop();
159  }
160  inline bool pushIntoWorklist(DPIm& item)
161  {
162  return worklist.push(item);
163  }
164  inline bool isWorklistEmpty()
165  {
166  return worklist.empty();
167  }
168  inline bool isInWorklist(DPIm& item)
169  {
170  return worklist.find(item);
171  }
173 
174 private:
175 
177  GraphType _graph;
178 
181 
182 };
183 
184 } // End namespace SVF
185 
186 #endif /* CFLSOLVER_H_ */
cJSON * item
Definition: cJSON.h:222
bool push(const Data &data)
Definition: WorkList.h:165
bool empty() const
Definition: WorkList.h:146
bool find(const Data &data) const
Definition: WorkList.h:157
GTraits::nodes_iterator node_iterator
Definition: SrcSnkSolver.h:52
GNODE * getNode(NodeID id) const
Definition: SrcSnkSolver.h:84
FIFOWorkList< DPIm > WorkList
Define worklist.
Definition: SrcSnkSolver.h:60
bool isInWorklist(DPIm &item)
Definition: SrcSnkSolver.h:168
virtual ~SrcSnkSolver()
Destructor.
Definition: SrcSnkSolver.h:69
bool pushIntoWorklist(DPIm &item)
Definition: SrcSnkSolver.h:160
virtual void BWProcessCurNode(const DPIm &)
Definition: SrcSnkSolver.h:135
void setGraph(GraphType g)
Definition: SrcSnkSolver.h:78
GTraits::EdgeType GEDGE
Definition: SrcSnkSolver.h:51
GTraits::ChildIteratorType child_iterator
Definition: SrcSnkSolver.h:53
SVF::GenericGraphTraits< SVF::Inverse< GNODE * > > InvGTraits
Define inverse GTraits and note iterator.
Definition: SrcSnkSolver.h:56
GraphType _graph
Graph.
Definition: SrcSnkSolver.h:177
SrcSnkSolver()
Constructor.
Definition: SrcSnkSolver.h:65
virtual void backwardTraverse(DPIm &it)
CFL forward traverse solve.
Definition: SrcSnkSolver.h:112
virtual void BWProcessIncomingEdge(const DPIm &item, GEDGE *edge)
Definition: SrcSnkSolver.h:147
virtual void FWProcessOutgoingEdge(const DPIm &item, GEDGE *edge)
Propagation for the solving, to be implemented in the child class.
Definition: SrcSnkSolver.h:141
SVF::GenericGraphTraits< GraphType > GTraits
Define the GTraits and node iterator.
Definition: SrcSnkSolver.h:49
DPIm popFromWorklist()
Worklist operations.
Definition: SrcSnkSolver.h:156
GTraits::NodeType GNODE
Definition: SrcSnkSolver.h:50
virtual void forwardTraverse(DPIm &it)
CFL forward traverse solve.
Definition: SrcSnkSolver.h:93
virtual void FWProcessCurNode(const DPIm &)
Process the DP item.
Definition: SrcSnkSolver.h:132
virtual NodeID getNodeIDFromItem(const DPIm &item) const
Definition: SrcSnkSolver.h:88
InvGTraits::ChildIteratorType inv_child_iterator
Definition: SrcSnkSolver.h:57
const GraphType graph() const
Get/Set graph methods.
Definition: SrcSnkSolver.h:74
WorkList worklist
Worklist for resolution.
Definition: SrcSnkSolver.h:180
for isBitcode
Definition: BasicTypes.h:68
u32_t NodeID
Definition: GeneralType.h:55