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