svf-lib 1.0.1659 → 1.0.1660
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/SVF-osx/Release-build/svf/libSvfCore.a +0 -0
- package/SVF-osx/Release-build/svf-llvm/libSvfLLVM.a +0 -0
- package/SVF-osx/svf/include/Graphs/CDG.h +463 -0
- package/SVF-osx/svf/include/Graphs/ICFGWrapper.h +725 -0
- package/SVF-osx/svf/include/SVFIR/SVFValue.h +26 -0
- package/SVF-osx/svf/include/Util/CDGBuilder.h +99 -0
- package/package.json +1 -1
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,463 @@
|
|
|
1
|
+
//===- CDG.h -- Control Dependence Graph --------------------------------//
|
|
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
|
+
* CDG.h
|
|
25
|
+
*
|
|
26
|
+
* Created on: Sep 27, 2023
|
|
27
|
+
* Author: Xiao Cheng
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
#ifndef SVF_CONTROLDG_H
|
|
31
|
+
#define SVF_CONTROLDG_H
|
|
32
|
+
|
|
33
|
+
#include "SVFIR/SVFIR.h"
|
|
34
|
+
|
|
35
|
+
namespace SVF
|
|
36
|
+
{
|
|
37
|
+
|
|
38
|
+
class CDGNode;
|
|
39
|
+
|
|
40
|
+
typedef GenericEdge<CDGNode> GenericCDGEdgeTy;
|
|
41
|
+
|
|
42
|
+
class CDGEdge : public GenericCDGEdgeTy
|
|
43
|
+
{
|
|
44
|
+
public:
|
|
45
|
+
typedef std::pair<const SVFValue *, s32_t> BranchCondition;
|
|
46
|
+
|
|
47
|
+
/// Constructor
|
|
48
|
+
CDGEdge(CDGNode *s, CDGNode *d) : GenericCDGEdgeTy(s, d, 0)
|
|
49
|
+
{
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/// Destructor
|
|
53
|
+
~CDGEdge()
|
|
54
|
+
{
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
typedef GenericNode<CDGNode, CDGEdge>::GEdgeSetTy CDGEdgeSetTy;
|
|
58
|
+
typedef CDGEdgeSetTy SVFGEdgeSetTy;
|
|
59
|
+
|
|
60
|
+
virtual const std::string toString() const
|
|
61
|
+
{
|
|
62
|
+
std::string str;
|
|
63
|
+
std::stringstream rawstr(str);
|
|
64
|
+
rawstr << "CDGEdge " << " [";
|
|
65
|
+
rawstr << getDstID() << "<--" << getSrcID() << "\t";
|
|
66
|
+
return rawstr.str();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/// get/set branch condition
|
|
70
|
+
//{@
|
|
71
|
+
const Set<BranchCondition> &getBranchConditions() const
|
|
72
|
+
{
|
|
73
|
+
return brConditions;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
void insertBranchCondition(const SVFValue *pNode, s32_t branchID)
|
|
77
|
+
{
|
|
78
|
+
brConditions.insert(std::make_pair(pNode, branchID));
|
|
79
|
+
}
|
|
80
|
+
//@}
|
|
81
|
+
|
|
82
|
+
private:
|
|
83
|
+
Set<BranchCondition> brConditions;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
typedef GenericNode<CDGNode, CDGEdge> GenericCDGNodeTy;
|
|
87
|
+
|
|
88
|
+
class CDGNode : public GenericCDGNodeTy
|
|
89
|
+
{
|
|
90
|
+
|
|
91
|
+
public:
|
|
92
|
+
|
|
93
|
+
typedef CDGEdge::CDGEdgeSetTy::iterator iterator;
|
|
94
|
+
typedef CDGEdge::CDGEdgeSetTy::const_iterator const_iterator;
|
|
95
|
+
|
|
96
|
+
public:
|
|
97
|
+
/// Constructor
|
|
98
|
+
CDGNode(const ICFGNode *icfgNode) : GenericCDGNodeTy(icfgNode->getId(), 0), _icfgNode(icfgNode)
|
|
99
|
+
{
|
|
100
|
+
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
virtual const std::string toString() const
|
|
104
|
+
{
|
|
105
|
+
std::string str;
|
|
106
|
+
std::stringstream rawstr(str);
|
|
107
|
+
rawstr << getId();
|
|
108
|
+
return rawstr.str();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const ICFGNode *getICFGNode() const
|
|
112
|
+
{
|
|
113
|
+
return _icfgNode;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
private:
|
|
117
|
+
const ICFGNode *_icfgNode;
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
typedef std::vector<std::pair<NodeID, NodeID>> NodePairVector;
|
|
121
|
+
typedef GenericGraph<CDGNode, CDGEdge> GenericCDGTy;
|
|
122
|
+
|
|
123
|
+
class CDG : public GenericCDGTy
|
|
124
|
+
{
|
|
125
|
+
|
|
126
|
+
public:
|
|
127
|
+
|
|
128
|
+
typedef Map<NodeID, CDGNode *> CDGNodeIDToNodeMapTy;
|
|
129
|
+
typedef CDGEdge::CDGEdgeSetTy CDGEdgeSetTy;
|
|
130
|
+
typedef CDGNodeIDToNodeMapTy::iterator iterator;
|
|
131
|
+
typedef CDGNodeIDToNodeMapTy::const_iterator const_iterator;
|
|
132
|
+
typedef std::vector<const ICFGNode *> ICFGNodeVector;
|
|
133
|
+
typedef std::vector<std::pair<const ICFGNode *, const ICFGNode *>> ICFGNodePairVector;
|
|
134
|
+
|
|
135
|
+
private:
|
|
136
|
+
static CDG *controlDg; ///< Singleton pattern here
|
|
137
|
+
/// Constructor
|
|
138
|
+
CDG()
|
|
139
|
+
{
|
|
140
|
+
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
public:
|
|
144
|
+
/// Singleton design here to make sure we only have one instance during any analysis
|
|
145
|
+
//@{
|
|
146
|
+
static inline CDG * getCDG()
|
|
147
|
+
{
|
|
148
|
+
if (controlDg == nullptr)
|
|
149
|
+
{
|
|
150
|
+
controlDg = new CDG();
|
|
151
|
+
}
|
|
152
|
+
return controlDg;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
static void releaseCDG()
|
|
156
|
+
{
|
|
157
|
+
if (controlDg)
|
|
158
|
+
delete controlDg;
|
|
159
|
+
controlDg = nullptr;
|
|
160
|
+
}
|
|
161
|
+
//@}
|
|
162
|
+
|
|
163
|
+
/// Destructor
|
|
164
|
+
virtual ~CDG() {}
|
|
165
|
+
|
|
166
|
+
/// Get a CDG node
|
|
167
|
+
inline CDGNode *getCDGNode(NodeID id) const
|
|
168
|
+
{
|
|
169
|
+
if (!hasCDGNode(id))
|
|
170
|
+
return nullptr;
|
|
171
|
+
return getGNode(id);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/// Whether has the CDGNode
|
|
175
|
+
inline bool hasCDGNode(NodeID id) const
|
|
176
|
+
{
|
|
177
|
+
return hasGNode(id);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/// Whether we has a CDG edge
|
|
181
|
+
bool hasCDGEdge(CDGNode *src, CDGNode *dst)
|
|
182
|
+
{
|
|
183
|
+
CDGEdge edge(src, dst);
|
|
184
|
+
CDGEdge *outEdge = src->hasOutgoingEdge(&edge);
|
|
185
|
+
CDGEdge *inEdge = dst->hasIncomingEdge(&edge);
|
|
186
|
+
if (outEdge && inEdge)
|
|
187
|
+
{
|
|
188
|
+
assert(outEdge == inEdge && "edges not match");
|
|
189
|
+
return true;
|
|
190
|
+
}
|
|
191
|
+
else
|
|
192
|
+
return false;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/// Get a control dependence edge according to src and dst
|
|
196
|
+
CDGEdge *getCDGEdge(const CDGNode *src, const CDGNode *dst)
|
|
197
|
+
{
|
|
198
|
+
CDGEdge *edge = nullptr;
|
|
199
|
+
size_t counter = 0;
|
|
200
|
+
for (CDGEdge::CDGEdgeSetTy::iterator iter = src->OutEdgeBegin();
|
|
201
|
+
iter != src->OutEdgeEnd(); ++iter)
|
|
202
|
+
{
|
|
203
|
+
if ((*iter)->getDstID() == dst->getId())
|
|
204
|
+
{
|
|
205
|
+
counter++;
|
|
206
|
+
edge = (*iter);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
assert(counter <= 1 && "there's more than one edge between two CDG nodes");
|
|
210
|
+
return edge;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/// View graph from the debugger
|
|
214
|
+
void view()
|
|
215
|
+
{
|
|
216
|
+
SVF::ViewGraph(this, "Control Dependence Graph");
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/// Dump graph into dot file
|
|
220
|
+
void dump(const std::string &filename)
|
|
221
|
+
{
|
|
222
|
+
GraphPrinter::WriteGraphToFile(SVFUtil::outs(), filename, this);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
public:
|
|
226
|
+
/// Remove a control dependence edge
|
|
227
|
+
inline void removeCDGEdge(CDGEdge *edge)
|
|
228
|
+
{
|
|
229
|
+
edge->getDstNode()->removeIncomingEdge(edge);
|
|
230
|
+
edge->getSrcNode()->removeOutgoingEdge(edge);
|
|
231
|
+
delete edge;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/// Remove a CDGNode
|
|
235
|
+
inline void removeCDGNode(CDGNode *node)
|
|
236
|
+
{
|
|
237
|
+
std::set<CDGEdge *> temp;
|
|
238
|
+
for (CDGEdge *e: node->getInEdges())
|
|
239
|
+
temp.insert(e);
|
|
240
|
+
for (CDGEdge *e: node->getOutEdges())
|
|
241
|
+
temp.insert(e);
|
|
242
|
+
for (CDGEdge *e: temp)
|
|
243
|
+
{
|
|
244
|
+
removeCDGEdge(e);
|
|
245
|
+
}
|
|
246
|
+
removeGNode(node);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/// Remove node from nodeID
|
|
250
|
+
inline bool removeCDGNode(NodeID id)
|
|
251
|
+
{
|
|
252
|
+
if (hasCDGNode(id))
|
|
253
|
+
{
|
|
254
|
+
removeCDGNode(getCDGNode(id));
|
|
255
|
+
return true;
|
|
256
|
+
}
|
|
257
|
+
return false;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/// Add CDG edge
|
|
261
|
+
inline bool addCDGEdge(CDGEdge *edge)
|
|
262
|
+
{
|
|
263
|
+
bool added1 = edge->getDstNode()->addIncomingEdge(edge);
|
|
264
|
+
bool added2 = edge->getSrcNode()->addOutgoingEdge(edge);
|
|
265
|
+
assert(added1 && added2 && "edge not added??");
|
|
266
|
+
return true;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/// Add a CDG node
|
|
270
|
+
virtual inline void addCDGNode(CDGNode *node)
|
|
271
|
+
{
|
|
272
|
+
addGNode(node->getId(), node);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/// Add CDG nodes from nodeid vector
|
|
276
|
+
inline void addCDGNodesFromVector(ICFGNodeVector nodes)
|
|
277
|
+
{
|
|
278
|
+
for (const ICFGNode *icfgNode: nodes)
|
|
279
|
+
{
|
|
280
|
+
if (!IDToNodeMap.count(icfgNode->getId()))
|
|
281
|
+
{
|
|
282
|
+
addGNode(icfgNode->getId(), new CDGNode(icfgNode));
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/// Add CDG edges from nodeid pair
|
|
288
|
+
void addCDGEdgeFromSrcDst(const ICFGNode *src, const ICFGNode *dst, const SVFValue *pNode, s32_t branchID);
|
|
289
|
+
|
|
290
|
+
};
|
|
291
|
+
} // end namespace SVF
|
|
292
|
+
|
|
293
|
+
namespace SVF
|
|
294
|
+
{
|
|
295
|
+
/* !
|
|
296
|
+
* GenericGraphTraits specializations for generic graph algorithms.
|
|
297
|
+
* Provide graph traits for traversing from a constraint node using standard graph ICFGTraversals.
|
|
298
|
+
*/
|
|
299
|
+
template<>
|
|
300
|
+
struct GenericGraphTraits<SVF::CDGNode *>
|
|
301
|
+
: public GenericGraphTraits<SVF::GenericNode<SVF::CDGNode, SVF::CDGEdge> *>
|
|
302
|
+
{
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
/// Inverse GenericGraphTraits specializations for call graph node, it is used for inverse ICFGTraversal.
|
|
306
|
+
template<>
|
|
307
|
+
struct GenericGraphTraits<Inverse<SVF::CDGNode *> > : public GenericGraphTraits<
|
|
308
|
+
Inverse<SVF::GenericNode<SVF::CDGNode, SVF::CDGEdge> *> >
|
|
309
|
+
{
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
template<>
|
|
313
|
+
struct GenericGraphTraits<SVF::CDG *>
|
|
314
|
+
: public GenericGraphTraits<SVF::GenericGraph<SVF::CDGNode, SVF::CDGEdge> *>
|
|
315
|
+
{
|
|
316
|
+
typedef SVF::CDGNode *NodeRef;
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
template<>
|
|
320
|
+
struct DOTGraphTraits<SVF::CDG *> : public DOTGraphTraits<SVF::PAG *>
|
|
321
|
+
{
|
|
322
|
+
|
|
323
|
+
typedef SVF::CDGNode NodeType;
|
|
324
|
+
|
|
325
|
+
DOTGraphTraits(bool isSimple = false) :
|
|
326
|
+
DOTGraphTraits<SVF::PAG *>(isSimple)
|
|
327
|
+
{
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/// Return name of the graph
|
|
331
|
+
static std::string getGraphName(SVF::CDG *)
|
|
332
|
+
{
|
|
333
|
+
return "Control Dependence Graph";
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
std::string getNodeLabel(NodeType *node, SVF::CDG *graph)
|
|
337
|
+
{
|
|
338
|
+
return getSimpleNodeLabel(node, graph);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/// Return the label of an ICFG node
|
|
342
|
+
static std::string getSimpleNodeLabel(NodeType *node, SVF::CDG *)
|
|
343
|
+
{
|
|
344
|
+
std::string str;
|
|
345
|
+
std::stringstream rawstr(str);
|
|
346
|
+
rawstr << "NodeID: " << node->getId() << "\n";
|
|
347
|
+
const SVF::ICFGNode *icfgNode = node->getICFGNode();
|
|
348
|
+
if (const SVF::IntraICFGNode *bNode = SVF::SVFUtil::dyn_cast<SVF::IntraICFGNode>(icfgNode))
|
|
349
|
+
{
|
|
350
|
+
rawstr << "IntraBlockNode ID: " << bNode->getId() << " \t";
|
|
351
|
+
SVF::PAG::SVFStmtList &edges = SVF::PAG::getPAG()->getPTASVFStmtList(bNode);
|
|
352
|
+
if (edges.empty())
|
|
353
|
+
{
|
|
354
|
+
rawstr << bNode->getInst()->toString() << " \t";
|
|
355
|
+
}
|
|
356
|
+
else
|
|
357
|
+
{
|
|
358
|
+
for (SVF::PAG::SVFStmtList::iterator it = edges.begin(), eit = edges.end(); it != eit; ++it)
|
|
359
|
+
{
|
|
360
|
+
const SVF::PAGEdge *edge = *it;
|
|
361
|
+
rawstr << edge->toString();
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
rawstr << " {fun: " << bNode->getFun()->getName() << "}";
|
|
365
|
+
}
|
|
366
|
+
else if (const SVF::FunEntryICFGNode *entry = SVF::SVFUtil::dyn_cast<SVF::FunEntryICFGNode>(icfgNode))
|
|
367
|
+
{
|
|
368
|
+
rawstr << entry->toString();
|
|
369
|
+
}
|
|
370
|
+
else if (const SVF::FunExitICFGNode *exit = SVF::SVFUtil::dyn_cast<SVF::FunExitICFGNode>(icfgNode))
|
|
371
|
+
{
|
|
372
|
+
rawstr << exit->toString();
|
|
373
|
+
}
|
|
374
|
+
else if (const SVF::CallICFGNode *call = SVF::SVFUtil::dyn_cast<SVF::CallICFGNode>(icfgNode))
|
|
375
|
+
{
|
|
376
|
+
rawstr << call->toString();
|
|
377
|
+
}
|
|
378
|
+
else if (const SVF::RetICFGNode *ret = SVF::SVFUtil::dyn_cast<SVF::RetICFGNode>(icfgNode))
|
|
379
|
+
{
|
|
380
|
+
rawstr << ret->toString();
|
|
381
|
+
}
|
|
382
|
+
else if (const SVF::GlobalICFGNode *glob = SVF::SVFUtil::dyn_cast<SVF::GlobalICFGNode>(icfgNode))
|
|
383
|
+
{
|
|
384
|
+
SVF::PAG::SVFStmtList &edges = SVF::PAG::getPAG()->getPTASVFStmtList(glob);
|
|
385
|
+
for (SVF::PAG::SVFStmtList::iterator it = edges.begin(), eit = edges.end(); it != eit; ++it)
|
|
386
|
+
{
|
|
387
|
+
const SVF::PAGEdge *edge = *it;
|
|
388
|
+
rawstr << edge->toString();
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
else
|
|
392
|
+
assert(false && "what else kinds of nodes do we have??");
|
|
393
|
+
|
|
394
|
+
return rawstr.str();
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
static std::string getNodeAttributes(NodeType *node, SVF::CDG *)
|
|
398
|
+
{
|
|
399
|
+
std::string str;
|
|
400
|
+
std::stringstream rawstr(str);
|
|
401
|
+
const SVF::ICFGNode *icfgNode = node->getICFGNode();
|
|
402
|
+
|
|
403
|
+
if (SVF::SVFUtil::isa<SVF::IntraICFGNode>(icfgNode))
|
|
404
|
+
{
|
|
405
|
+
rawstr << "color=black";
|
|
406
|
+
}
|
|
407
|
+
else if (SVF::SVFUtil::isa<SVF::FunEntryICFGNode>(icfgNode))
|
|
408
|
+
{
|
|
409
|
+
rawstr << "color=yellow";
|
|
410
|
+
}
|
|
411
|
+
else if (SVF::SVFUtil::isa<SVF::FunExitICFGNode>(icfgNode))
|
|
412
|
+
{
|
|
413
|
+
rawstr << "color=green";
|
|
414
|
+
}
|
|
415
|
+
else if (SVF::SVFUtil::isa<SVF::CallICFGNode>(icfgNode))
|
|
416
|
+
{
|
|
417
|
+
rawstr << "color=red";
|
|
418
|
+
}
|
|
419
|
+
else if (SVF::SVFUtil::isa<SVF::RetICFGNode>(icfgNode))
|
|
420
|
+
{
|
|
421
|
+
rawstr << "color=blue";
|
|
422
|
+
}
|
|
423
|
+
else if (SVF::SVFUtil::isa<SVF::GlobalICFGNode>(icfgNode))
|
|
424
|
+
{
|
|
425
|
+
rawstr << "color=purple";
|
|
426
|
+
}
|
|
427
|
+
else
|
|
428
|
+
assert(false && "no such kind of node!!");
|
|
429
|
+
|
|
430
|
+
rawstr << "";
|
|
431
|
+
|
|
432
|
+
return rawstr.str();
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
template<class EdgeIter>
|
|
436
|
+
static std::string getEdgeAttributes(NodeType *, EdgeIter EI, SVF::CDG *)
|
|
437
|
+
{
|
|
438
|
+
SVF::CDGEdge *edge = *(EI.getCurrent());
|
|
439
|
+
assert(edge && "No edge found!!");
|
|
440
|
+
return "style=solid";
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
template<class EdgeIter>
|
|
444
|
+
static std::string getEdgeSourceLabel(NodeType *, EdgeIter EI)
|
|
445
|
+
{
|
|
446
|
+
SVF::CDGEdge *edge = *(EI.getCurrent());
|
|
447
|
+
assert(edge && "No edge found!!");
|
|
448
|
+
|
|
449
|
+
std::string str;
|
|
450
|
+
std::stringstream rawstr(str);
|
|
451
|
+
for (const auto &cond: edge->getBranchConditions())
|
|
452
|
+
{
|
|
453
|
+
rawstr << std::to_string(cond.second) << "|";
|
|
454
|
+
}
|
|
455
|
+
std::string lb = rawstr.str();
|
|
456
|
+
lb.pop_back();
|
|
457
|
+
|
|
458
|
+
return lb;
|
|
459
|
+
}
|
|
460
|
+
};
|
|
461
|
+
|
|
462
|
+
} // End namespace SVF
|
|
463
|
+
#endif //SVF_CONTROLDG_H
|
|
@@ -0,0 +1,725 @@
|
|
|
1
|
+
//===- ICFGWrapper.h -- ICFG Wrapper-----------------------------------------//
|
|
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
|
+
* ICFGWrapper.h
|
|
25
|
+
*
|
|
26
|
+
* Created on: Sep 26, 2023
|
|
27
|
+
* Author: Xiao Cheng
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
#ifndef SVF_ICFGWRAPPER_H
|
|
31
|
+
#define SVF_ICFGWRAPPER_H
|
|
32
|
+
|
|
33
|
+
#include "Graphs/ICFG.h"
|
|
34
|
+
#include "SVFIR/SVFIR.h"
|
|
35
|
+
|
|
36
|
+
namespace SVF
|
|
37
|
+
{
|
|
38
|
+
class ICFGNodeWrapper;
|
|
39
|
+
|
|
40
|
+
typedef GenericEdge<ICFGNodeWrapper> GenericICFGWrapperEdgeTy;
|
|
41
|
+
|
|
42
|
+
class ICFGEdgeWrapper : public GenericICFGWrapperEdgeTy
|
|
43
|
+
{
|
|
44
|
+
public:
|
|
45
|
+
typedef struct equalICFGEdgeWrapper
|
|
46
|
+
{
|
|
47
|
+
bool
|
|
48
|
+
operator()(const ICFGEdgeWrapper *lhs, const ICFGEdgeWrapper *rhs) const
|
|
49
|
+
{
|
|
50
|
+
if (lhs->getSrcID() != rhs->getSrcID())
|
|
51
|
+
return lhs->getSrcID() < rhs->getSrcID();
|
|
52
|
+
else if (lhs->getDstID() != rhs->getDstID())
|
|
53
|
+
return lhs->getDstID() < rhs->getDstID();
|
|
54
|
+
else
|
|
55
|
+
return lhs->getICFGEdge() < rhs->getICFGEdge();
|
|
56
|
+
}
|
|
57
|
+
} equalICFGEdgeWrapper;
|
|
58
|
+
|
|
59
|
+
typedef OrderedSet<ICFGEdgeWrapper *, equalICFGEdgeWrapper> ICFGEdgeWrapperSetTy;
|
|
60
|
+
typedef ICFGEdgeWrapperSetTy::iterator iterator;
|
|
61
|
+
typedef ICFGEdgeWrapperSetTy::const_iterator const_iterator;
|
|
62
|
+
|
|
63
|
+
private:
|
|
64
|
+
ICFGEdge *_icfgEdge;
|
|
65
|
+
|
|
66
|
+
public:
|
|
67
|
+
ICFGEdgeWrapper(ICFGNodeWrapper *src, ICFGNodeWrapper *dst, ICFGEdge *edge) :
|
|
68
|
+
GenericICFGWrapperEdgeTy(src, dst, 0), _icfgEdge(edge)
|
|
69
|
+
{
|
|
70
|
+
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
~ICFGEdgeWrapper() {}
|
|
74
|
+
|
|
75
|
+
virtual const std::string toString() const
|
|
76
|
+
{
|
|
77
|
+
return _icfgEdge->toString();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
inline ICFGEdge *getICFGEdge() const
|
|
81
|
+
{
|
|
82
|
+
return _icfgEdge;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
inline void setICFGEdge(ICFGEdge *edge)
|
|
86
|
+
{
|
|
87
|
+
_icfgEdge = edge;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/// Add the hash function for std::set (we also can overload operator< to implement this)
|
|
91
|
+
// and duplicated elements in the set are not inserted (binary tree comparison)
|
|
92
|
+
//@{
|
|
93
|
+
|
|
94
|
+
virtual inline bool operator==(const ICFGEdgeWrapper *rhs) const
|
|
95
|
+
{
|
|
96
|
+
return (rhs->getSrcID() == this->getSrcID() && rhs->getDstID() == this->getDstID() &&
|
|
97
|
+
rhs->getICFGEdge() == this->getICFGEdge());
|
|
98
|
+
}
|
|
99
|
+
//@}
|
|
100
|
+
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
typedef GenericNode<ICFGNodeWrapper, ICFGEdgeWrapper> GenericICFGNodeWrapperTy;
|
|
104
|
+
|
|
105
|
+
class ICFGNodeWrapper : public GenericICFGNodeWrapperTy
|
|
106
|
+
{
|
|
107
|
+
public:
|
|
108
|
+
typedef ICFGEdgeWrapper::ICFGEdgeWrapperSetTy ICFGEdgeWrapperSetTy;
|
|
109
|
+
typedef ICFGEdgeWrapper::ICFGEdgeWrapperSetTy::iterator iterator;
|
|
110
|
+
typedef ICFGEdgeWrapper::ICFGEdgeWrapperSetTy::const_iterator const_iterator;
|
|
111
|
+
private:
|
|
112
|
+
const ICFGNode *_icfgNode;
|
|
113
|
+
ICFGNodeWrapper *_callICFGNodeWrapper{nullptr};
|
|
114
|
+
ICFGNodeWrapper *_retICFGNodeWrapper{nullptr};
|
|
115
|
+
ICFGEdgeWrapperSetTy InEdges; ///< all incoming edge of this node
|
|
116
|
+
ICFGEdgeWrapperSetTy OutEdges; ///< all outgoing edge of this node
|
|
117
|
+
public:
|
|
118
|
+
ICFGNodeWrapper(const ICFGNode *node) : GenericICFGNodeWrapperTy(node->getId(), 0), _icfgNode(node) {}
|
|
119
|
+
|
|
120
|
+
virtual ~ICFGNodeWrapper()
|
|
121
|
+
{
|
|
122
|
+
for (auto *edge: OutEdges)
|
|
123
|
+
delete edge;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
virtual const std::string toString() const
|
|
127
|
+
{
|
|
128
|
+
return _icfgNode->toString();
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const ICFGNode *getICFGNode() const
|
|
132
|
+
{
|
|
133
|
+
return _icfgNode;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
ICFGNodeWrapper *getCallICFGNodeWrapper() const
|
|
137
|
+
{
|
|
138
|
+
return _callICFGNodeWrapper;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
void setCallICFGNodeWrapper(ICFGNodeWrapper *node)
|
|
142
|
+
{
|
|
143
|
+
_callICFGNodeWrapper = node;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
ICFGNodeWrapper *getRetICFGNodeWrapper() const
|
|
147
|
+
{
|
|
148
|
+
return _retICFGNodeWrapper;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
void setRetICFGNodeWrapper(ICFGNodeWrapper *node)
|
|
152
|
+
{
|
|
153
|
+
_retICFGNodeWrapper = node;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/// Get incoming/outgoing edge set
|
|
157
|
+
///@{
|
|
158
|
+
inline const ICFGEdgeWrapperSetTy &getOutEdges() const
|
|
159
|
+
{
|
|
160
|
+
return OutEdges;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
inline const ICFGEdgeWrapperSetTy &getInEdges() const
|
|
164
|
+
{
|
|
165
|
+
return InEdges;
|
|
166
|
+
}
|
|
167
|
+
///@}
|
|
168
|
+
|
|
169
|
+
/// Has incoming/outgoing edge set
|
|
170
|
+
//@{
|
|
171
|
+
inline bool hasIncomingEdge() const
|
|
172
|
+
{
|
|
173
|
+
return (InEdges.empty() == false);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
inline bool hasOutgoingEdge() const
|
|
177
|
+
{
|
|
178
|
+
return (OutEdges.empty() == false);
|
|
179
|
+
}
|
|
180
|
+
//@}
|
|
181
|
+
|
|
182
|
+
/// iterators
|
|
183
|
+
//@{
|
|
184
|
+
inline iterator OutEdgeBegin()
|
|
185
|
+
{
|
|
186
|
+
return OutEdges.begin();
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
inline iterator OutEdgeEnd()
|
|
190
|
+
{
|
|
191
|
+
return OutEdges.end();
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
inline iterator InEdgeBegin()
|
|
195
|
+
{
|
|
196
|
+
return InEdges.begin();
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
inline iterator InEdgeEnd()
|
|
200
|
+
{
|
|
201
|
+
return InEdges.end();
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
inline const_iterator OutEdgeBegin() const
|
|
205
|
+
{
|
|
206
|
+
return OutEdges.begin();
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
inline const_iterator OutEdgeEnd() const
|
|
210
|
+
{
|
|
211
|
+
return OutEdges.end();
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
inline const_iterator InEdgeBegin() const
|
|
215
|
+
{
|
|
216
|
+
return InEdges.begin();
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
inline const_iterator InEdgeEnd() const
|
|
220
|
+
{
|
|
221
|
+
return InEdges.end();
|
|
222
|
+
}
|
|
223
|
+
//@}
|
|
224
|
+
|
|
225
|
+
/// Iterators used for SCC detection, overwrite it in child class if necessory
|
|
226
|
+
//@{
|
|
227
|
+
virtual inline iterator directOutEdgeBegin()
|
|
228
|
+
{
|
|
229
|
+
return OutEdges.begin();
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
virtual inline iterator directOutEdgeEnd()
|
|
233
|
+
{
|
|
234
|
+
return OutEdges.end();
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
virtual inline iterator directInEdgeBegin()
|
|
238
|
+
{
|
|
239
|
+
return InEdges.begin();
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
virtual inline iterator directInEdgeEnd()
|
|
243
|
+
{
|
|
244
|
+
return InEdges.end();
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
virtual inline const_iterator directOutEdgeBegin() const
|
|
248
|
+
{
|
|
249
|
+
return OutEdges.begin();
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
virtual inline const_iterator directOutEdgeEnd() const
|
|
253
|
+
{
|
|
254
|
+
return OutEdges.end();
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
virtual inline const_iterator directInEdgeBegin() const
|
|
258
|
+
{
|
|
259
|
+
return InEdges.begin();
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
virtual inline const_iterator directInEdgeEnd() const
|
|
263
|
+
{
|
|
264
|
+
return InEdges.end();
|
|
265
|
+
}
|
|
266
|
+
//@}
|
|
267
|
+
|
|
268
|
+
/// Add incoming and outgoing edges
|
|
269
|
+
//@{
|
|
270
|
+
inline bool addIncomingEdge(ICFGEdgeWrapper *inEdge)
|
|
271
|
+
{
|
|
272
|
+
return InEdges.insert(inEdge).second;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
inline bool addOutgoingEdge(ICFGEdgeWrapper *outEdge)
|
|
276
|
+
{
|
|
277
|
+
return OutEdges.insert(outEdge).second;
|
|
278
|
+
}
|
|
279
|
+
//@}
|
|
280
|
+
|
|
281
|
+
/// Remove incoming and outgoing edges
|
|
282
|
+
///@{
|
|
283
|
+
inline u32_t removeIncomingEdge(ICFGEdgeWrapper *edge)
|
|
284
|
+
{
|
|
285
|
+
iterator it = InEdges.find(edge);
|
|
286
|
+
assert(it != InEdges.end() && "can not find in edge in SVFG node");
|
|
287
|
+
return InEdges.erase(edge);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
inline u32_t removeOutgoingEdge(ICFGEdgeWrapper *edge)
|
|
291
|
+
{
|
|
292
|
+
iterator it = OutEdges.find(edge);
|
|
293
|
+
assert(it != OutEdges.end() && "can not find out edge in SVFG node");
|
|
294
|
+
return OutEdges.erase(edge);
|
|
295
|
+
}
|
|
296
|
+
///@}
|
|
297
|
+
|
|
298
|
+
/// Find incoming and outgoing edges
|
|
299
|
+
//@{
|
|
300
|
+
inline ICFGEdgeWrapper *hasIncomingEdge(ICFGEdgeWrapper *edge) const
|
|
301
|
+
{
|
|
302
|
+
const_iterator it = InEdges.find(edge);
|
|
303
|
+
if (it != InEdges.end())
|
|
304
|
+
return *it;
|
|
305
|
+
else
|
|
306
|
+
return nullptr;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
inline ICFGEdgeWrapper *hasOutgoingEdge(ICFGEdgeWrapper *edge) const
|
|
310
|
+
{
|
|
311
|
+
const_iterator it = OutEdges.find(edge);
|
|
312
|
+
if (it != OutEdges.end())
|
|
313
|
+
return *it;
|
|
314
|
+
else
|
|
315
|
+
return nullptr;
|
|
316
|
+
}
|
|
317
|
+
//@}
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
typedef std::vector<std::pair<NodeID, NodeID>> NodePairVector;
|
|
321
|
+
typedef GenericGraph<ICFGNodeWrapper, ICFGEdgeWrapper> GenericICFGWrapperTy;
|
|
322
|
+
|
|
323
|
+
class ICFGWrapper : public GenericICFGWrapperTy
|
|
324
|
+
{
|
|
325
|
+
public:
|
|
326
|
+
|
|
327
|
+
typedef Map<NodeID, ICFGNodeWrapper *> ICFGWrapperNodeIDToNodeMapTy;
|
|
328
|
+
typedef ICFGEdgeWrapper::ICFGEdgeWrapperSetTy ICFGEdgeWrapperSetTy;
|
|
329
|
+
typedef ICFGWrapperNodeIDToNodeMapTy::iterator iterator;
|
|
330
|
+
typedef ICFGWrapperNodeIDToNodeMapTy::const_iterator const_iterator;
|
|
331
|
+
typedef std::vector<const ICFGNodeWrapper *> ICFGNodeWrapperVector;
|
|
332
|
+
typedef std::vector<std::pair<const ICFGNodeWrapper *, const ICFGNodeWrapper *>> ICFGNodeWrapperPairVector;
|
|
333
|
+
typedef Map<const SVFFunction *, const ICFGNodeWrapper *> SVFFuncToICFGNodeWrapperMap;
|
|
334
|
+
private:
|
|
335
|
+
static std::unique_ptr<ICFGWrapper> _icfgWrapper; ///< Singleton pattern here
|
|
336
|
+
SVFFuncToICFGNodeWrapperMap _funcToFunEntry;
|
|
337
|
+
SVFFuncToICFGNodeWrapperMap _funcToFunExit;
|
|
338
|
+
u32_t _edgeWrapperNum; ///< total num of node
|
|
339
|
+
u32_t _nodeWrapperNum; ///< total num of edge
|
|
340
|
+
ICFG *_icfg;
|
|
341
|
+
|
|
342
|
+
/// Constructor
|
|
343
|
+
ICFGWrapper(ICFG *icfg) : _edgeWrapperNum(0), _nodeWrapperNum(0), _icfg(icfg)
|
|
344
|
+
{
|
|
345
|
+
assert(_icfg && "ICFGWrapper constructor cannot accept nullptr of icfg");
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
public:
|
|
349
|
+
/// Singleton design here to make sure we only have one instance during any analysis
|
|
350
|
+
//@{
|
|
351
|
+
static inline const std::unique_ptr<ICFGWrapper> &getICFGWrapper(ICFG *_icfg)
|
|
352
|
+
{
|
|
353
|
+
if (_icfgWrapper == nullptr)
|
|
354
|
+
{
|
|
355
|
+
_icfgWrapper = std::make_unique<ICFGWrapper>(ICFGWrapper(_icfg));
|
|
356
|
+
}
|
|
357
|
+
return _icfgWrapper;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
static inline const std::unique_ptr<ICFGWrapper> &getICFGWrapper()
|
|
361
|
+
{
|
|
362
|
+
assert(_icfgWrapper && "icfg wrapper not init?");
|
|
363
|
+
return _icfgWrapper;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
static void releaseICFGWrapper()
|
|
367
|
+
{
|
|
368
|
+
ICFGWrapper *w = _icfgWrapper.release();
|
|
369
|
+
delete w;
|
|
370
|
+
_icfgWrapper = nullptr;
|
|
371
|
+
}
|
|
372
|
+
//@}
|
|
373
|
+
|
|
374
|
+
/// Destructor
|
|
375
|
+
virtual ~ICFGWrapper() = default;
|
|
376
|
+
|
|
377
|
+
/// Get a ICFG node wrapper
|
|
378
|
+
inline ICFGNodeWrapper *getICFGNodeWrapper(NodeID id) const
|
|
379
|
+
{
|
|
380
|
+
if (!hasICFGNodeWrapper(id))
|
|
381
|
+
return nullptr;
|
|
382
|
+
return getGNode(id);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/// Whether has the ICFGNodeWrapper
|
|
386
|
+
inline bool hasICFGNodeWrapper(NodeID id) const
|
|
387
|
+
{
|
|
388
|
+
return hasGNode(id);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/// Whether we has a ICFG Edge Wrapper
|
|
392
|
+
bool hasICFGEdgeWrapper(ICFGNodeWrapper *src, ICFGNodeWrapper *dst, ICFGEdge *icfgEdge)
|
|
393
|
+
{
|
|
394
|
+
ICFGEdgeWrapper edge(src, dst, icfgEdge);
|
|
395
|
+
ICFGEdgeWrapper *outEdge = src->hasOutgoingEdge(&edge);
|
|
396
|
+
ICFGEdgeWrapper *inEdge = dst->hasIncomingEdge(&edge);
|
|
397
|
+
if (outEdge && inEdge)
|
|
398
|
+
{
|
|
399
|
+
assert(outEdge == inEdge && "edges not match");
|
|
400
|
+
return true;
|
|
401
|
+
}
|
|
402
|
+
else
|
|
403
|
+
return false;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
ICFGEdgeWrapper *hasICFGEdgeWrapper(ICFGNodeWrapper *src, ICFGNodeWrapper *dst)
|
|
407
|
+
{
|
|
408
|
+
for (const auto &e: src->getOutEdges())
|
|
409
|
+
{
|
|
410
|
+
if (e->getDstNode() == dst)
|
|
411
|
+
return e;
|
|
412
|
+
}
|
|
413
|
+
return nullptr;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/// Get a ICFG edge wrapper according to src, dst and icfgEdge
|
|
417
|
+
ICFGEdgeWrapper *
|
|
418
|
+
getICFGEdgeWrapper(const ICFGNodeWrapper *src, const ICFGNodeWrapper *dst, ICFGEdge *icfgEdge)
|
|
419
|
+
{
|
|
420
|
+
ICFGEdgeWrapper *edge = nullptr;
|
|
421
|
+
size_t counter = 0;
|
|
422
|
+
for (ICFGEdgeWrapper::ICFGEdgeWrapperSetTy::iterator iter = src->OutEdgeBegin();
|
|
423
|
+
iter != src->OutEdgeEnd(); ++iter)
|
|
424
|
+
{
|
|
425
|
+
if ((*iter)->getDstID() == dst->getId())
|
|
426
|
+
{
|
|
427
|
+
counter++;
|
|
428
|
+
edge = (*iter);
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
assert(counter <= 1 && "there's more than one edge between two ICFGNodeWrappers");
|
|
432
|
+
return edge;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/// View graph from the debugger
|
|
436
|
+
void view();
|
|
437
|
+
|
|
438
|
+
/// Dump graph into dot file
|
|
439
|
+
void dump(const std::string &filename);
|
|
440
|
+
|
|
441
|
+
/// Remove a ICFGEdgeWrapper
|
|
442
|
+
inline void removeICFGEdgeWrapper(ICFGEdgeWrapper *edge)
|
|
443
|
+
{
|
|
444
|
+
if (edge->getDstNode()->hasIncomingEdge(edge))
|
|
445
|
+
{
|
|
446
|
+
edge->getDstNode()->removeIncomingEdge(edge);
|
|
447
|
+
}
|
|
448
|
+
if (edge->getSrcNode()->hasOutgoingEdge(edge))
|
|
449
|
+
{
|
|
450
|
+
edge->getSrcNode()->removeOutgoingEdge(edge);
|
|
451
|
+
}
|
|
452
|
+
delete edge;
|
|
453
|
+
_edgeWrapperNum--;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
/// Remove a ICFGNodeWrapper
|
|
457
|
+
inline void removeICFGNodeWrapper(ICFGNodeWrapper *node)
|
|
458
|
+
{
|
|
459
|
+
std::set<ICFGEdgeWrapper *> temp;
|
|
460
|
+
for (ICFGEdgeWrapper *e: node->getInEdges())
|
|
461
|
+
temp.insert(e);
|
|
462
|
+
for (ICFGEdgeWrapper *e: node->getOutEdges())
|
|
463
|
+
temp.insert(e);
|
|
464
|
+
for (ICFGEdgeWrapper *e: temp)
|
|
465
|
+
{
|
|
466
|
+
removeICFGEdgeWrapper(e);
|
|
467
|
+
}
|
|
468
|
+
removeGNode(node);
|
|
469
|
+
_nodeWrapperNum--;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/// Remove node from nodeID
|
|
473
|
+
inline bool removeICFGNodeWrapper(NodeID id)
|
|
474
|
+
{
|
|
475
|
+
if (hasICFGNodeWrapper(id))
|
|
476
|
+
{
|
|
477
|
+
removeICFGNodeWrapper(getICFGNodeWrapper(id));
|
|
478
|
+
return true;
|
|
479
|
+
}
|
|
480
|
+
return false;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/// Add ICFGEdgeWrapper
|
|
484
|
+
inline bool addICFGEdgeWrapper(ICFGEdgeWrapper *edge)
|
|
485
|
+
{
|
|
486
|
+
bool added1 = edge->getDstNode()->addIncomingEdge(edge);
|
|
487
|
+
bool added2 = edge->getSrcNode()->addOutgoingEdge(edge);
|
|
488
|
+
assert(added1 && added2 && "edge not added??");
|
|
489
|
+
_edgeWrapperNum++;
|
|
490
|
+
return true;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/// Add a ICFGNodeWrapper
|
|
494
|
+
virtual inline void addICFGNodeWrapper(ICFGNodeWrapper *node)
|
|
495
|
+
{
|
|
496
|
+
addGNode(node->getId(), node);
|
|
497
|
+
_nodeWrapperNum++;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
const ICFGNodeWrapper *getFunEntry(const SVFFunction *func) const
|
|
501
|
+
{
|
|
502
|
+
auto it = _funcToFunEntry.find(func);
|
|
503
|
+
assert(it != _funcToFunEntry.end() && "no entry?");
|
|
504
|
+
return it->second;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
const ICFGNodeWrapper *getFunExit(const SVFFunction *func) const
|
|
508
|
+
{
|
|
509
|
+
auto it = _funcToFunExit.find(func);
|
|
510
|
+
assert(it != _funcToFunExit.end() && "no exit?");
|
|
511
|
+
return it->second;
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
/// Add ICFGEdgeWrappers from nodeid pair
|
|
515
|
+
void addICFGNodeWrapperFromICFGNode(const ICFGNode *src);
|
|
516
|
+
|
|
517
|
+
inline u32_t getNodeWrapperNum() const
|
|
518
|
+
{
|
|
519
|
+
return _nodeWrapperNum;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
inline u32_t getEdgeWrapperNum() const
|
|
523
|
+
{
|
|
524
|
+
return _edgeWrapperNum;
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
};
|
|
528
|
+
|
|
529
|
+
class ICFGWrapperBuilder
|
|
530
|
+
{
|
|
531
|
+
public:
|
|
532
|
+
ICFGWrapperBuilder() {}
|
|
533
|
+
|
|
534
|
+
~ICFGWrapperBuilder() {}
|
|
535
|
+
|
|
536
|
+
void build(ICFG *icfg);
|
|
537
|
+
};
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
namespace SVF
|
|
541
|
+
{
|
|
542
|
+
/* !
|
|
543
|
+
* GenericGraphTraits specializations for generic graph algorithms.
|
|
544
|
+
* Provide graph traits for traversing from a constraint node using standard graph ICFGTraversals.
|
|
545
|
+
*/
|
|
546
|
+
template<>
|
|
547
|
+
struct GenericGraphTraits<SVF::ICFGNodeWrapper *>
|
|
548
|
+
: public GenericGraphTraits<SVF::GenericNode<SVF::ICFGNodeWrapper, SVF::ICFGEdgeWrapper> *>
|
|
549
|
+
{
|
|
550
|
+
};
|
|
551
|
+
|
|
552
|
+
/// Inverse GenericGraphTraits specializations for call graph node, it is used for inverse ICFGTraversal.
|
|
553
|
+
template<>
|
|
554
|
+
struct GenericGraphTraits<Inverse<SVF::ICFGNodeWrapper *> > : public GenericGraphTraits<
|
|
555
|
+
Inverse<SVF::GenericNode<SVF::ICFGNodeWrapper, SVF::ICFGEdgeWrapper> *> >
|
|
556
|
+
{
|
|
557
|
+
};
|
|
558
|
+
|
|
559
|
+
template<>
|
|
560
|
+
struct GenericGraphTraits<SVF::ICFGWrapper *>
|
|
561
|
+
: public GenericGraphTraits<SVF::GenericGraph<SVF::ICFGNodeWrapper, SVF::ICFGEdgeWrapper> *>
|
|
562
|
+
{
|
|
563
|
+
typedef SVF::ICFGNodeWrapper *NodeRef;
|
|
564
|
+
};
|
|
565
|
+
|
|
566
|
+
template<>
|
|
567
|
+
struct DOTGraphTraits<SVF::ICFGWrapper *> : public DOTGraphTraits<SVF::SVFIR *>
|
|
568
|
+
{
|
|
569
|
+
|
|
570
|
+
typedef SVF::ICFGNodeWrapper NodeType;
|
|
571
|
+
|
|
572
|
+
DOTGraphTraits(bool isSimple = false) :
|
|
573
|
+
DOTGraphTraits<SVF::SVFIR *>(isSimple)
|
|
574
|
+
{
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
/// Return name of the graph
|
|
578
|
+
static std::string getGraphName(SVF::ICFGWrapper *)
|
|
579
|
+
{
|
|
580
|
+
return "ICFGWrapper";
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
static bool isNodeHidden(NodeType *node, SVF::ICFGWrapper *graph)
|
|
584
|
+
{
|
|
585
|
+
return false;
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
std::string getNodeLabel(NodeType *node, SVF::ICFGWrapper *graph)
|
|
589
|
+
{
|
|
590
|
+
return getSimpleNodeLabel(node, graph);
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
/// Return the label of an ICFG node
|
|
594
|
+
static std::string getSimpleNodeLabel(NodeType *node, SVF::ICFGWrapper *)
|
|
595
|
+
{
|
|
596
|
+
std::string str;
|
|
597
|
+
std::stringstream rawstr(str);
|
|
598
|
+
rawstr << "NodeID: " << node->getId() << "\n";
|
|
599
|
+
if (const SVF::IntraICFGNode *bNode = SVF::SVFUtil::dyn_cast<SVF::IntraICFGNode>(node->getICFGNode()))
|
|
600
|
+
{
|
|
601
|
+
rawstr << "IntraICFGNode ID: " << bNode->getId() << " \t";
|
|
602
|
+
SVF::SVFIR::SVFStmtList &edges = SVF::SVFIR::getPAG()->getSVFStmtList(bNode);
|
|
603
|
+
if (edges.empty())
|
|
604
|
+
{
|
|
605
|
+
rawstr << bNode->getInst()->toString() << " \t";
|
|
606
|
+
}
|
|
607
|
+
else
|
|
608
|
+
{
|
|
609
|
+
for (SVF::SVFIR::SVFStmtList::iterator it = edges.begin(), eit = edges.end(); it != eit; ++it)
|
|
610
|
+
{
|
|
611
|
+
const SVF::PAGEdge *edge = *it;
|
|
612
|
+
rawstr << edge->toString();
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
rawstr << " {fun: " << bNode->getFun()->getName() << "}";
|
|
616
|
+
}
|
|
617
|
+
else if (const SVF::FunEntryICFGNode *entry = SVF::SVFUtil::dyn_cast<SVF::FunEntryICFGNode>(
|
|
618
|
+
node->getICFGNode()))
|
|
619
|
+
{
|
|
620
|
+
rawstr << entry->toString();
|
|
621
|
+
}
|
|
622
|
+
else if (const SVF::FunExitICFGNode *exit = SVF::SVFUtil::dyn_cast<SVF::FunExitICFGNode>(
|
|
623
|
+
node->getICFGNode()))
|
|
624
|
+
{
|
|
625
|
+
rawstr << exit->toString();
|
|
626
|
+
}
|
|
627
|
+
else if (const SVF::CallICFGNode *call = SVF::SVFUtil::dyn_cast<SVF::CallICFGNode>(node->getICFGNode()))
|
|
628
|
+
{
|
|
629
|
+
rawstr << call->toString();
|
|
630
|
+
}
|
|
631
|
+
else if (const SVF::RetICFGNode *ret = SVF::SVFUtil::dyn_cast<SVF::RetICFGNode>(node->getICFGNode()))
|
|
632
|
+
{
|
|
633
|
+
rawstr << ret->toString();
|
|
634
|
+
}
|
|
635
|
+
else if (const SVF::GlobalICFGNode *glob = SVF::SVFUtil::dyn_cast<SVF::GlobalICFGNode>(
|
|
636
|
+
node->getICFGNode()))
|
|
637
|
+
{
|
|
638
|
+
SVF::SVFIR::SVFStmtList &edges = SVF::SVFIR::getPAG()->getSVFStmtList(glob);
|
|
639
|
+
for (SVF::SVFIR::SVFStmtList::iterator it = edges.begin(), eit = edges.end(); it != eit; ++it)
|
|
640
|
+
{
|
|
641
|
+
const SVF::PAGEdge *edge = *it;
|
|
642
|
+
rawstr << edge->toString();
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
else
|
|
646
|
+
assert(false && "what else kinds of nodes do we have??");
|
|
647
|
+
|
|
648
|
+
return rawstr.str();
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
static std::string getNodeAttributes(NodeType *node, SVF::ICFGWrapper *)
|
|
652
|
+
{
|
|
653
|
+
std::string str;
|
|
654
|
+
std::stringstream rawstr(str);
|
|
655
|
+
|
|
656
|
+
if (SVF::SVFUtil::isa<SVF::IntraICFGNode>(node->getICFGNode()))
|
|
657
|
+
{
|
|
658
|
+
rawstr << "color=black";
|
|
659
|
+
}
|
|
660
|
+
else if (SVF::SVFUtil::isa<SVF::FunEntryICFGNode>(node->getICFGNode()))
|
|
661
|
+
{
|
|
662
|
+
rawstr << "color=yellow";
|
|
663
|
+
}
|
|
664
|
+
else if (SVF::SVFUtil::isa<SVF::FunExitICFGNode>(node->getICFGNode()))
|
|
665
|
+
{
|
|
666
|
+
rawstr << "color=green";
|
|
667
|
+
}
|
|
668
|
+
else if (SVF::SVFUtil::isa<SVF::CallICFGNode>(node->getICFGNode()))
|
|
669
|
+
{
|
|
670
|
+
rawstr << "color=red";
|
|
671
|
+
}
|
|
672
|
+
else if (SVF::SVFUtil::isa<SVF::RetICFGNode>(node->getICFGNode()))
|
|
673
|
+
{
|
|
674
|
+
rawstr << "color=blue";
|
|
675
|
+
}
|
|
676
|
+
else if (SVF::SVFUtil::isa<SVF::GlobalICFGNode>(node->getICFGNode()))
|
|
677
|
+
{
|
|
678
|
+
rawstr << "color=purple";
|
|
679
|
+
}
|
|
680
|
+
else
|
|
681
|
+
assert(false && "no such kind of node!!");
|
|
682
|
+
|
|
683
|
+
rawstr << "";
|
|
684
|
+
|
|
685
|
+
return rawstr.str();
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
template<class EdgeIter>
|
|
689
|
+
static std::string getEdgeAttributes(NodeType *, EdgeIter EI, SVF::ICFGWrapper *)
|
|
690
|
+
{
|
|
691
|
+
SVF::ICFGEdgeWrapper *edge = *(EI.getCurrent());
|
|
692
|
+
assert(edge && "No edge found!!");
|
|
693
|
+
if (!edge->getICFGEdge())
|
|
694
|
+
return "style=solid";
|
|
695
|
+
if (SVF::SVFUtil::isa<SVF::CallCFGEdge>(edge->getICFGEdge()))
|
|
696
|
+
return "style=solid,color=red";
|
|
697
|
+
else if (SVF::SVFUtil::isa<SVF::RetCFGEdge>(edge->getICFGEdge()))
|
|
698
|
+
return "style=solid,color=blue";
|
|
699
|
+
else
|
|
700
|
+
return "style=solid";
|
|
701
|
+
return "";
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
template<class EdgeIter>
|
|
705
|
+
static std::string getEdgeSourceLabel(NodeType *, EdgeIter EI)
|
|
706
|
+
{
|
|
707
|
+
SVF::ICFGEdgeWrapper *edge = *(EI.getCurrent());
|
|
708
|
+
assert(edge && "No edge found!!");
|
|
709
|
+
|
|
710
|
+
std::string str;
|
|
711
|
+
std::stringstream rawstr(str);
|
|
712
|
+
if (!edge->getICFGEdge())
|
|
713
|
+
return rawstr.str();
|
|
714
|
+
if (SVF::CallCFGEdge *dirCall = SVF::SVFUtil::dyn_cast<SVF::CallCFGEdge>(edge->getICFGEdge()))
|
|
715
|
+
rawstr << dirCall->getCallSite();
|
|
716
|
+
else if (SVF::RetCFGEdge *dirRet = SVF::SVFUtil::dyn_cast<SVF::RetCFGEdge>(edge->getICFGEdge()))
|
|
717
|
+
rawstr << dirRet->getCallSite();
|
|
718
|
+
|
|
719
|
+
return rawstr.str();
|
|
720
|
+
}
|
|
721
|
+
};
|
|
722
|
+
|
|
723
|
+
} // End namespace SVF
|
|
724
|
+
|
|
725
|
+
#endif //SVF_ICFGWRAPPER_H
|
|
@@ -61,6 +61,9 @@ private:
|
|
|
61
61
|
Map<const SVFBasicBlock*,BBSet> pdtBBsMap; ///< map a BasicBlock to BasicBlocks it PostDominates
|
|
62
62
|
Map<const SVFBasicBlock*,BBSet> dfBBsMap; ///< map a BasicBlock to its Dominate Frontier BasicBlocks
|
|
63
63
|
Map<const SVFBasicBlock*, LoopBBs> bb2LoopMap; ///< map a BasicBlock (if it is in a loop) to all the BasicBlocks in this loop
|
|
64
|
+
Map<const SVFBasicBlock*, u32_t> bb2PdomLevel; ///< map a BasicBlock to its level in pdom tree, used in findNearestCommonPDominator
|
|
65
|
+
Map<const SVFBasicBlock*, const SVFBasicBlock*> bb2PIdom; ///< map a BasicBlock to its immediate dominator in pdom tree, used in findNearestCommonPDominator
|
|
66
|
+
|
|
64
67
|
public:
|
|
65
68
|
SVFLoopAndDomInfo()
|
|
66
69
|
{
|
|
@@ -111,6 +114,26 @@ public:
|
|
|
111
114
|
return pdtBBsMap;
|
|
112
115
|
}
|
|
113
116
|
|
|
117
|
+
inline const Map<const SVFBasicBlock*,u32_t>& getBBPDomLevel() const
|
|
118
|
+
{
|
|
119
|
+
return bb2PdomLevel;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
inline Map<const SVFBasicBlock*,u32_t>& getBBPDomLevel()
|
|
123
|
+
{
|
|
124
|
+
return bb2PdomLevel;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
inline const Map<const SVFBasicBlock*,const SVFBasicBlock*>& getBB2PIdom() const
|
|
128
|
+
{
|
|
129
|
+
return bb2PIdom;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
inline Map<const SVFBasicBlock*,const SVFBasicBlock*>& getBB2PIdom()
|
|
133
|
+
{
|
|
134
|
+
return bb2PIdom;
|
|
135
|
+
}
|
|
136
|
+
|
|
114
137
|
inline Map<const SVFBasicBlock*,BBSet>& getDomTreeMap()
|
|
115
138
|
{
|
|
116
139
|
return dtBBsMap;
|
|
@@ -144,6 +167,9 @@ public:
|
|
|
144
167
|
bool dominate(const SVFBasicBlock* bbKey, const SVFBasicBlock* bbValue) const;
|
|
145
168
|
|
|
146
169
|
bool postDominate(const SVFBasicBlock* bbKey, const SVFBasicBlock* bbValue) const;
|
|
170
|
+
|
|
171
|
+
/// find nearest common post dominator of two basic blocks
|
|
172
|
+
const SVFBasicBlock *findNearestCommonPDominator(const SVFBasicBlock *A, const SVFBasicBlock *B) const;
|
|
147
173
|
};
|
|
148
174
|
|
|
149
175
|
class SVFValue
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
//===----- CDGBuilder.h -- Control Dependence Graph Builder -------------//
|
|
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
|
+
* CDGBuilder.h
|
|
25
|
+
*
|
|
26
|
+
* Created on: Sep 27, 2023
|
|
27
|
+
* Author: Xiao Cheng
|
|
28
|
+
*/
|
|
29
|
+
#ifndef SVF_CDGBUILDER_H
|
|
30
|
+
#define SVF_CDGBUILDER_H
|
|
31
|
+
|
|
32
|
+
#include "Graphs/CDG.h"
|
|
33
|
+
#include "SVFIR/SVFValue.h"
|
|
34
|
+
|
|
35
|
+
// control dependence builder
|
|
36
|
+
namespace SVF
|
|
37
|
+
{
|
|
38
|
+
class CDGBuilder
|
|
39
|
+
{
|
|
40
|
+
public:
|
|
41
|
+
|
|
42
|
+
/// constructor
|
|
43
|
+
CDGBuilder() : _controlDG(CDG::getCDG())
|
|
44
|
+
{
|
|
45
|
+
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/// destructor
|
|
49
|
+
~CDGBuilder()
|
|
50
|
+
{
|
|
51
|
+
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/// start here
|
|
55
|
+
void build();
|
|
56
|
+
|
|
57
|
+
/// build control dependence for each function
|
|
58
|
+
void buildControlDependence(const SVFModule *svfgModule);
|
|
59
|
+
|
|
60
|
+
/// build map at icfg node level
|
|
61
|
+
void buildICFGNodeControlMap();
|
|
62
|
+
|
|
63
|
+
private:
|
|
64
|
+
|
|
65
|
+
/// extract basic block edges to be processed
|
|
66
|
+
static void
|
|
67
|
+
extractBBS(const SVFFunction *func,
|
|
68
|
+
Map<const SVFBasicBlock *, std::vector<const SVFBasicBlock *>> &res);
|
|
69
|
+
|
|
70
|
+
/// extract nodes between two nodes in pdom tree
|
|
71
|
+
void
|
|
72
|
+
extractNodesBetweenPdomNodes(const SVFBasicBlock *succ, const SVFBasicBlock *LCA,
|
|
73
|
+
std::vector<const SVFBasicBlock *> &tgtNodes);
|
|
74
|
+
|
|
75
|
+
void dfsNodesBetweenPdomNodes(const SVFBasicBlock *cur,
|
|
76
|
+
const SVFBasicBlock *tgt,
|
|
77
|
+
std::vector<const SVFBasicBlock *> &path,
|
|
78
|
+
std::vector<const SVFBasicBlock *> &tgtNodes,
|
|
79
|
+
SVFLoopAndDomInfo *ld);
|
|
80
|
+
|
|
81
|
+
s64_t getBBSuccessorBranchID(const SVFBasicBlock *BB, const SVFBasicBlock *Succ);
|
|
82
|
+
|
|
83
|
+
/// update map
|
|
84
|
+
inline void updateMap(const SVFBasicBlock *pred, const SVFBasicBlock *bb, s32_t pos)
|
|
85
|
+
{
|
|
86
|
+
_svfcontrolMap[pred][bb].insert(pos);
|
|
87
|
+
_svfdependentOnMap[bb][pred].insert(pos);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
private:
|
|
91
|
+
CDG *_controlDG;
|
|
92
|
+
Map<const SVFBasicBlock *, Map<const SVFBasicBlock *, Set<s32_t>>> _svfcontrolMap; ///< map a basicblock to its controlling BBs (position, set of BBs)
|
|
93
|
+
Map<const SVFBasicBlock *, Map<const SVFBasicBlock *, Set<s32_t>>> _svfdependentOnMap; ///< map a basicblock to its dependent on BBs (position, set of BBs)
|
|
94
|
+
Map<const ICFGNode *, Map<const ICFGNode *, Set<s32_t>>> _nodeControlMap; ///< map an ICFG node to its controlling ICFG nodes (position, set of Nodes)
|
|
95
|
+
Map<const ICFGNode *, Map<const ICFGNode *, Set<s32_t>>> _nodeDependentOnMap; ///< map an ICFG node to its dependent on ICFG nodes (position, set of Nodes)
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
#endif //SVF_CDGBUILDER_H
|