Static Value-Flow Analysis
Loading...
Searching...
No Matches
CFLSolver.h
Go to the documentation of this file.
1//===----- CFLSolver.h -- Context-free language 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 * CFLSolver.h
25 *
26 * Created on: March 5, 2022
27 * Author: Yulei Suiļ¼Œ Yuxiang Lei
28 */
29
30#ifndef INCLUDE_CFL_CFLSolver_H_
31#define INCLUDE_CFL_CFLSolver_H_
32
33#include "Graphs/CFLGraph.h"
34#include "CFL/CFGrammar.h"
35#include "Util/WorkList.h"
36
37using namespace std;
38
39namespace SVF
40{
42
44{
45
46public:
51
52 static double numOfChecks;
53
55 {
56 }
57
58 virtual ~CFLSolver()
59 {
60 delete graph;
61 delete grammar;
62 }
63
65 virtual void initialize();
66
68 virtual void processCFLEdge(const CFLEdge* Y_edge);
69
71 virtual void solve();
72
74 inline const CFLGraph* getGraph() const
75 {
76 return graph;
77 }
78
80 inline const CFGrammar* getGrammar() const
81 {
82 return grammar;
83 }
84 virtual inline bool pushIntoWorklist(const CFLEdge* item)
85 {
86 return worklist.push(item);
87 }
88 virtual inline bool isWorklistEmpty()
89 {
90 return worklist.empty();
91 }
92
93protected:
95
96 inline const CFLEdge* popFromWorklist()
97 {
98 return worklist.pop();
99 }
100
101 inline bool isInWorklist(const CFLEdge* item)
102 {
103 return worklist.find(item);
104 }
106
107protected:
112
113};
114
116class POCRSolver : public CFLSolver
117{
118public:
119 typedef std::map<const Label, NodeBS> TypeMap; // Label with SparseBitVector of NodeID
120 typedef std::unordered_map<NodeID, TypeMap> DataMap; // Each Node has a TypeMap
121 typedef typename DataMap::iterator iterator; // iterator for each node
122 typedef typename DataMap::const_iterator const_iterator;
123
124protected:
125 DataMap succMap; // succ map for nodes contains Label: Edgeset
126 DataMap predMap; // pred map for nodes contains Label: edgeset
127 const NodeBS emptyData; // ??
129 // union/add data
131 inline bool addPred(const NodeID key, const NodeID src, const Label ty)
132 {
133 return predMap[key][ty].test_and_set(src);
134 };
135
136 inline bool addSucc(const NodeID key, const NodeID dst, const Label ty)
137 {
138 return succMap[key][ty].test_and_set(dst);
139 };
140
141 inline bool addPreds(const NodeID key, const NodeBS& data, const Label ty)
142 {
143 if (data.empty())
144 return false;
145 return predMap[key][ty] |= data; // union of sparsebitvector (add to LHS)
146 }
147
148 inline bool addSuccs(const NodeID key, const NodeBS& data, const Label ty)
149 {
150 if (data.empty())
151 return false;
152 return succMap[key][ty] |= data; // // union of sparsebitvector (add to LHS)
153 }
155public:
156
157 virtual void clear()
158 {
159 succMap.clear();
160 predMap.clear();
161 }
162
163 inline const_iterator begin() const
164 {
165 return succMap.begin();
166 }
167
168 inline const_iterator end() const
169 {
170 return succMap.end();
171 }
172
174 {
175 return succMap.begin();
176 }
177
178 inline iterator end()
179 {
180 return succMap.end();
181 }
182
184 {
185 return succMap;
186 }
187
189 {
190 return predMap;
191 }
192
194 {
195 return succMap[key];
196 }
197
199 {
200 return predMap[key];
201 }
202
203 inline NodeBS& getSuccs(const NodeID key, const Label ty)
204 {
205 return succMap[key][ty];
206 }
207
208 inline NodeBS& getPreds(const NodeID key, const Label ty)
209 {
210 return predMap[key][ty];
211 }
212
213 // Alias data operations
215 inline bool addEdge(const NodeID src, const NodeID dst, const Label ty)
216 {
217 addSucc(src, dst, ty);
218 return addPred(dst, src, ty);
219 }
220
222 inline NodeBS addEdges(const NodeID src, const NodeBS& dstData, const Label ty)
223 {
225 if (addSuccs(src, dstData, ty))
226 {
227 for (const NodeID datum: dstData)
228 if (addPred(datum, src, ty))
230 }
231 return newDsts;
232 }
233
235 inline NodeBS addEdges(const NodeBS& srcData, const NodeID dst, const Label ty)
236 {
238 if (addPreds(dst, srcData, ty))
239 {
240 for (const NodeID datum: srcData)
241 if (addSucc(datum, dst, ty))
243 }
244 return newSrcs;
245 }
246
248 inline bool hasEdge(const NodeID src, const NodeID dst, const Label ty)
249 {
250 const_iterator iter1 = succMap.find(src);
251 if (iter1 == succMap.end())
252 return false;
253
254 auto iter2 = iter1->second.find(ty);
255 if (iter2 == iter1->second.end())
256 return false;
257
258 return iter2->second.test(dst);
259 }
260
261 /* This is a dataset version, to be modified to a cflData version */
262 inline void clearEdges(const NodeID key)
263 {
264 succMap[key].clear();
265 predMap[key].clear();
266 }
268
270 {
271 buildCFLData();
272 }
274 virtual ~POCRSolver()
275 {
276 }
277
279 virtual void processCFLEdge(const CFLEdge* Y_edge);
280
282 virtual void buildCFLData();
283
284 virtual void initialize();
285};
295{
296//Hybrid
297//{@
298public:
299 struct TreeNode
300 {
302 std::unordered_set<TreeNode*> children;
303
306
308 {
309 }
310
311 inline bool operator==(const TreeNode& rhs) const
312 {
313 return id == rhs.id;
314 }
315
316 inline bool operator<(const TreeNode& rhs) const
317 {
318 return id < rhs.id;
319 }
320 };
321
322public:
323 Map<NodeID, std::unordered_map<NodeID, TreeNode*>> indMap; // indMap[v][u] points to node v in tree(u)
324
325 bool hasInd_h(NodeID src, NodeID dst);
326
328 TreeNode* addInd_h(NodeID src, NodeID dst);
329
332 {
333 return indMap[dst][src];
334 }
335
338 {
339 u->children.insert(v);
340 }
341
342 void addArc_h(NodeID src, NodeID dst);
343
344 void meld_h(NodeID x, TreeNode* uNode, TreeNode* vNode);
346public:
352 {
353 for (auto iter1: indMap)
354 {
355 for (auto iter2: iter1.second)
356 {
357 delete iter2.second;
358 iter2.second = NULL;
359 }
360 }
361 }
362
364 virtual void processCFLEdge(const CFLEdge* Y_edge);
365
366 virtual void initialize();
367
368public:
369 void addArc(NodeID src, NodeID dst);
370 void meld(NodeID x, TreeNode* uNode, TreeNode* vNode);
371};
372}
373
374#endif /* INCLUDE_CFL_CFLSolver_H_*/
cJSON * item
Definition cJSON.h:222
bool isInWorklist(const CFLEdge *item)
Definition CFLSolver.h:101
FIFOWorkList< const CFLEdge * > WorkList
Define worklist.
Definition CFLSolver.h:48
WorkList worklist
Worklist for resolution.
Definition CFLSolver.h:111
virtual void solve()
Start solving.
const CFLGraph * getGraph() const
Return CFL Graph.
Definition CFLSolver.h:74
static double numOfChecks
Definition CFLSolver.h:52
virtual void processCFLEdge(const CFLEdge *Y_edge)
Process CFLEdge.
Definition CFLSolver.cpp:62
virtual void initialize()
Initialize worklist.
Definition CFLSolver.cpp:36
CFGrammar::Production Production
Definition CFLSolver.h:49
CFGrammar::Symbol Symbol
Definition CFLSolver.h:50
CFGrammar * grammar
Definition CFLSolver.h:109
virtual ~CFLSolver()
Definition CFLSolver.h:58
const CFGrammar * getGrammar() const
Return CFL Grammar.
Definition CFLSolver.h:80
virtual bool pushIntoWorklist(const CFLEdge *item)
Definition CFLSolver.h:84
virtual bool isWorklistEmpty()
Definition CFLSolver.h:88
const CFLEdge * popFromWorklist()
Worklist operations.
Definition CFLSolver.h:96
CFLGraph * graph
Definition CFLSolver.h:108
CFLSolver(CFLGraph *_graph, CFGrammar *_grammar)
Definition CFLSolver.h:54
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
std::vector< Symbol > Production
Definition CFGrammar.h:156
Solver Utilize Hybrid Representation of Graph.
Definition CFLSolver.h:295
TreeNode * getNode_h(NodeID src, NodeID dst)
Get the node dst in tree(src)
Definition CFLSolver.h:331
void insertEdge_h(TreeNode *u, TreeNode *v)
add v into desc(x) as a child of u
Definition CFLSolver.h:337
virtual void initialize()
Initialize worklist.
POCRHybridSolver(CFLGraph *_graph, CFGrammar *_grammar)
Definition CFLSolver.h:347
void meld(NodeID x, TreeNode *uNode, TreeNode *vNode)
void meld_h(NodeID x, TreeNode *uNode, TreeNode *vNode)
bool hasInd_h(NodeID src, NodeID dst)
void addArc_h(NodeID src, NodeID dst)
void addArc(NodeID src, NodeID dst)
TreeNode * addInd_h(NodeID src, NodeID dst)
Add a node dst to tree(src)
Map< NodeID, std::unordered_map< NodeID, TreeNode * > > indMap
Definition CFLSolver.h:323
virtual ~POCRHybridSolver()
Destructor.
Definition CFLSolver.h:351
virtual void processCFLEdge(const CFLEdge *Y_edge)
Process CFLEdge.
Solver Utilize CFLData.
Definition CFLSolver.h:117
iterator end()
Definition CFLSolver.h:178
DataMap::const_iterator const_iterator
Definition CFLSolver.h:122
void clearEdges(const NodeID key)
Definition CFLSolver.h:262
TypeMap & getPredMap(const NodeID key)
Definition CFLSolver.h:198
DataMap predMap
Definition CFLSolver.h:126
iterator begin()
Definition CFLSolver.h:173
bool addPred(const NodeID key, const NodeID src, const Label ty)
Definition CFLSolver.h:131
bool hasEdge(const NodeID src, const NodeID dst, const Label ty)
find src -> find src[ty] -> find dst in set
Definition CFLSolver.h:248
virtual ~POCRSolver()
Destructor.
Definition CFLSolver.h:274
DataMap::iterator iterator
Definition CFLSolver.h:121
DataMap & getPredMap()
Definition CFLSolver.h:188
virtual void clear()
Definition CFLSolver.h:157
virtual void processCFLEdge(const CFLEdge *Y_edge)
Process CFLEdge.
TypeMap & getSuccMap(const NodeID key)
Definition CFLSolver.h:193
NodeBS & getSuccs(const NodeID key, const Label ty)
Definition CFLSolver.h:203
NodeBS addEdges(const NodeID src, const NodeBS &dstData, const Label ty)
add edges and return the set of added edges (dst) for src
Definition CFLSolver.h:222
const_iterator begin() const
Definition CFLSolver.h:163
bool addSucc(const NodeID key, const NodeID dst, const Label ty)
Definition CFLSolver.h:136
const NodeBS emptyData
Definition CFLSolver.h:127
DataMap succMap
Definition CFLSolver.h:125
NodeBS addEdges(const NodeBS &srcData, const NodeID dst, const Label ty)
add edges and return the set of added edges (src) for dst
Definition CFLSolver.h:235
std::map< const Label, NodeBS > TypeMap
Definition CFLSolver.h:119
POCRSolver(CFLGraph *_graph, CFGrammar *_grammar)
Definition CFLSolver.h:269
NodeBS & getPreds(const NodeID key, const Label ty)
Definition CFLSolver.h:208
bool addEdge(const NodeID src, const NodeID dst, const Label ty)
Definition CFLSolver.h:215
std::unordered_map< NodeID, TypeMap > DataMap
Definition CFLSolver.h:120
DataMap & getSuccMap()
Definition CFLSolver.h:183
bool addPreds(const NodeID key, const NodeBS &data, const Label ty)
Definition CFLSolver.h:141
const_iterator end() const
Definition CFLSolver.h:168
bool addSuccs(const NodeID key, const NodeBS &data, const Label ty)
Definition CFLSolver.h:148
virtual void initialize()
Initialize worklist.
virtual void buildCFLData()
Init CFLData.
void set(unsigned Idx)
#define NULL
Definition extapi.c:2
for isBitcode
Definition BasicTypes.h:68
GrammarBase::Symbol Label
Definition CFLSolver.h:41
u32_t NodeID
Definition GeneralType.h:55
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:74
bool operator==(const TreeNode &rhs) const
Definition CFLSolver.h:311
bool operator<(const TreeNode &rhs) const
Definition CFLSolver.h:316
std::unordered_set< TreeNode * > children
Definition CFLSolver.h:302