Static Value-Flow Analysis
WPASolver.h
Go to the documentation of this file.
1 //===- WPASolver.h -- Generic WPA solver--------------------------------------//
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 /*
25  * WPASolver.h
26  *
27  * Created on: Oct 25, 2013
28  * Author: Yulei Sui
29  */
30 
31 #ifndef GRAPHSOLVER_H_
32 #define GRAPHSOLVER_H_
33 
34 #include "Util/WorkList.h"
35 
36 namespace SVF
37 {
38 
39 /*
40  * Generic graph solver for whole program pointer analysis
41  */
42 template<class GraphType>
43 class WPASolver
44 {
45 
46 public:
49  typedef typename GTraits::NodeRef GNODE;
50  typedef typename GTraits::EdgeType GEDGE;
51  typedef typename GTraits::ChildIteratorType child_iterator;
52 
54 
56 
57 protected:
58 
61  {
62  }
64  virtual ~WPASolver() = default;
65 
67  inline SCC* getSCCDetector() const
68  {
69  return scc.get();
70  }
71 
73 
74  const inline GraphType graph()
75  {
76  return _graph;
77  }
78  inline void setGraph(GraphType g)
79  {
80  _graph = g;
81  scc = std::make_unique<SCC>(_graph);
82  }
84 
86  virtual inline NodeStack& SCCDetect()
87  {
88  getSCCDetector()->find();
89  return getSCCDetector()->topoNodeStack();
90  }
91  virtual inline NodeStack& SCCDetect(NodeSet& candidates)
92  {
93  getSCCDetector()->find(candidates);
94  return getSCCDetector()->topoNodeStack();
95  }
96 
97  virtual inline void initWorklist()
98  {
99  NodeStack& nodeStack = SCCDetect();
100  while (!nodeStack.empty())
101  {
102  NodeID nodeId = nodeStack.top();
103  nodeStack.pop();
104  pushIntoWorklist(nodeId);
105  }
106  }
107 
108  virtual inline void solveWorklist()
109  {
110  while (!isWorklistEmpty())
111  {
112  NodeID nodeId = popFromWorklist();
113  // Keep solving until workList is empty.
114  processNode(nodeId);
115  collapseFields();
116  }
117  }
118 
120 
121  virtual inline void processNode(NodeID) {}
124  virtual void collapseFields() {}
127  virtual void propagate(GNODE* v)
128  {
129  child_iterator EI = GTraits::direct_child_begin(*v);
130  child_iterator EE = GTraits::direct_child_end(*v);
131  for (; EI != EE; ++EI)
132  {
133  if (propFromSrcToDst(*(EI.getCurrent())))
135  }
136  }
138  virtual bool propFromSrcToDst(GEDGE*)
139  {
140  return false;
141  }
143 
144  virtual NodeID sccRepNode(NodeID id) const
145  {
146  return getSCCDetector()->repNode(id);
147  }
148 
150 
152  {
153  return sccRepNode(worklist.pop());
154  }
155 
156  virtual inline void pushIntoWorklist(NodeID id)
157  {
158  worklist.push(sccRepNode(id));
159  }
160  inline bool isWorklistEmpty()
161  {
162  return worklist.empty();
163  }
164  inline bool isInWorklist(NodeID id)
165  {
166  return worklist.find(id);
167  }
169 
171  bool reanalyze;
174 
175 
177  inline GNODE* Node(NodeID id)
178  {
179  return GTraits::getNode(_graph, id);
180  }
181 
183  inline NodeID Node_Index(GNODE node)
184  {
185  return GTraits::getNodeID(node);
186  }
187 
188 protected:
190  GraphType _graph;
191 
193  std::unique_ptr<SCC> scc;
194 
197 
198 public:
201 };
202 
203 } // End namespace SVF
204 
205 #endif /* GRAPHSOLVER_H_ */
#define false
Definition: cJSON.cpp:70
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
void find(void)
Definition: SCC.h:308
NodeID repNode(NodeID n) const
get the rep node if not found return itself
Definition: SCC.h:139
GNodeStack & topoNodeStack()
Definition: SCC.h:128
NodeID Node_Index(GNODE node)
Get node ID.
Definition: WPASolver.h:183
bool isInWorklist(NodeID id)
Definition: WPASolver.h:164
WPASolver()
Constructor.
Definition: WPASolver.h:60
SCCDetection< GraphType > SCC
Definition: WPASolver.h:53
virtual NodeStack & SCCDetect(NodeSet &candidates)
Definition: WPASolver.h:91
WorkList worklist
Worklist for resolution.
Definition: WPASolver.h:196
std::unique_ptr< SCC > scc
SCC.
Definition: WPASolver.h:193
NodeID popFromWorklist()
Worklist operations.
Definition: WPASolver.h:151
GTraits::NodeRef GNODE
Definition: WPASolver.h:49
SCC * getSCCDetector() const
Get SCC detector.
Definition: WPASolver.h:67
virtual NodeID sccRepNode(NodeID id) const
Definition: WPASolver.h:144
virtual void pushIntoWorklist(NodeID id)
Definition: WPASolver.h:156
GTraits::ChildIteratorType child_iterator
Definition: WPASolver.h:51
virtual void propagate(GNODE *v)
Definition: WPASolver.h:127
GNODE * Node(NodeID id)
Get node on the graph.
Definition: WPASolver.h:177
FIFOWorkList< NodeID > WorkList
Definition: WPASolver.h:55
SVF::GenericGraphTraits< GraphType > GTraits
Define the GTraits and node iterator for printing.
Definition: WPASolver.h:48
virtual void initWorklist()
Definition: WPASolver.h:97
virtual void collapseFields()
collapse positive weight cycles of a graph
Definition: WPASolver.h:124
GraphType _graph
Graph.
Definition: WPASolver.h:190
virtual bool propFromSrcToDst(GEDGE *)
Propagate information from source to destination node, to be implemented in the child class.
Definition: WPASolver.h:138
bool isWorklistEmpty()
Definition: WPASolver.h:160
virtual ~WPASolver()=default
Destructor.
void setGraph(GraphType g)
Definition: WPASolver.h:78
u32_t iterationForPrintStat
print out statistics for i-th iteration
Definition: WPASolver.h:173
virtual NodeStack & SCCDetect()
SCC detection.
Definition: WPASolver.h:86
u32_t numOfIteration
num of iterations during constraint solving
Definition: WPASolver.h:200
GTraits::EdgeType GEDGE
Definition: WPASolver.h:50
virtual void processNode(NodeID)
Following methods are to be implemented in child class, in order to achieve a fully worked PTA.
Definition: WPASolver.h:122
const GraphType graph()
Get/Set graph methods.
Definition: WPASolver.h:74
bool reanalyze
Reanalyze if any constraint value changed.
Definition: WPASolver.h:171
virtual void solveWorklist()
Definition: WPASolver.h:108
for isBitcode
Definition: BasicTypes.h:68
std::stack< NodeID > NodeStack
Definition: GeneralType.h:118
Set< NodeID > NodeSet
Definition: GeneralType.h:113
u32_t NodeID
Definition: GeneralType.h:55
unsigned u32_t
Definition: GeneralType.h:46
typename GraphType::UnknownGraphTypeError NodeRef
Definition: GraphTraits.h:80