-
Notifications
You must be signed in to change notification settings - Fork 427
/
WPASolver.h
212 lines (179 loc) · 5.1 KB
/
WPASolver.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//===- WPASolver.h -- Generic WPA solver--------------------------------------//
//
// SVF: Static Value-Flow Analysis
//
// Copyright (C) <2013-2017> <Yulei Sui>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
//===----------------------------------------------------------------------===//
/*
* WPASolver.h
*
* Created on: Oct 25, 2013
* Author: Yulei Sui
*/
#ifndef GRAPHSOLVER_H_
#define GRAPHSOLVER_H_
#include "Util/WorkList.h"
namespace SVF
{
/*
* Generic graph solver for whole program pointer analysis
*/
template<class GraphType>
class WPASolver
{
public:
///Define the GTraits and node iterator for printing
typedef llvm::GraphTraits<GraphType> GTraits;
typedef typename GTraits::NodeRef GNODE;
typedef typename GTraits::EdgeType GEDGE;
typedef typename GTraits::ChildIteratorType child_iterator;
typedef SCCDetection<GraphType> SCC;
typedef FIFOWorkList<NodeID> WorkList;
protected:
/// Constructor
WPASolver(): reanalyze(false), iterationForPrintStat(1000), _graph(nullptr), scc(nullptr), numOfIteration(0)
{
}
/// Destructor
virtual ~WPASolver()
{
delete scc;
scc = nullptr;
}
/// Get SCC detector
inline SCC* getSCCDetector() const
{
return scc;
}
/// Get/Set graph methods
//@{
const inline GraphType graph()
{
return _graph;
}
inline void setGraph(GraphType g)
{
_graph = g;
if(!scc)
scc = new SCC(_graph);
}
//@}
/// SCC detection
virtual inline NodeStack& SCCDetect()
{
getSCCDetector()->find();
return getSCCDetector()->topoNodeStack();
}
virtual inline NodeStack& SCCDetect(NodeSet& candidates)
{
getSCCDetector()->find(candidates);
return getSCCDetector()->topoNodeStack();
}
virtual inline void initWorklist()
{
NodeStack& nodeStack = SCCDetect();
while (!nodeStack.empty())
{
NodeID nodeId = nodeStack.top();
nodeStack.pop();
pushIntoWorklist(nodeId);
}
}
virtual inline void solveWorklist()
{
while (!isWorklistEmpty())
{
NodeID nodeId = popFromWorklist();
collapsePWCNode(nodeId);
// Keep solving until workList is empty.
processNode(nodeId);
collapseFields();
}
}
/// Following methods are to be implemented in child class, in order to achieve a fully worked PTA
//@{
/// Process each node on the graph, to be implemented in the child class
virtual inline void processNode(NodeID) {}
/// collapse positive weight cycles of a graph
virtual void collapsePWCNode(NodeID) {}
virtual void collapseFields() {};
/// dump statistics
/// Propagation for the solving, to be implemented in the child class
virtual void propagate(GNODE* v)
{
child_iterator EI = GTraits::direct_child_begin(*v);
child_iterator EE = GTraits::direct_child_end(*v);
for (; EI != EE; ++EI)
{
if (propFromSrcToDst(*(EI.getCurrent())))
pushIntoWorklist(Node_Index(*EI));
}
}
/// Propagate information from source to destination node, to be implemented in the child class
virtual bool propFromSrcToDst(GEDGE*)
{
return false;
}
//@}
virtual NodeID sccRepNode(NodeID id) const
{
return getSCCDetector()->repNode(id);
}
/// Worklist operations
//@{
inline NodeID popFromWorklist()
{
return sccRepNode(worklist.pop());
}
virtual inline void pushIntoWorklist(NodeID id)
{
worklist.push(sccRepNode(id));
}
inline bool isWorklistEmpty()
{
return worklist.empty();
}
inline bool isInWorklist(NodeID id)
{
return worklist.find(id);
}
//@}
/// Reanalyze if any constraint value changed
bool reanalyze;
/// print out statistics for i-th iteration
u32_t iterationForPrintStat;
/// Get node on the graph
inline GNODE* Node(NodeID id)
{
return GTraits::getNode(_graph, id);
}
/// Get node ID
inline NodeID Node_Index(GNODE node)
{
return GTraits::getNodeID(node);
}
protected:
/// Graph
GraphType _graph;
/// SCC
SCC* scc;
/// Worklist for resolution
WorkList worklist;
public:
/// num of iterations during constaint solving
u32_t numOfIteration;
};
} // End namespace SVF
#endif /* GRAPHSOLVER_H_ */