-
Notifications
You must be signed in to change notification settings - Fork 442
/
Copy pathConsG.h
402 lines (362 loc) · 12.3 KB
/
ConsG.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
//===- ConsG.h -- Constraint graph representation-----------------------------//
//
// 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 Affero 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 Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
//===----------------------------------------------------------------------===//
/*
* ConstraintGraph.h
*
* Created on: Oct 14, 2013
* Author: Yulei Sui
*/
#ifndef CONSG_H_
#define CONSG_H_
#include "Graphs/ConsGEdge.h"
#include "Graphs/ConsGNode.h"
namespace SVF
{
/*!
* Constraint graph for Andersen's analysis
* ConstraintNodes are same as PAGNodes
* ConstraintEdges are self-defined edges (initialized with ConstraintEdges)
*/
class ConstraintGraph : public GenericGraph<ConstraintNode,ConstraintEdge>
{
public:
typedef OrderedMap<NodeID, ConstraintNode *> ConstraintNodeIDToNodeMapTy;
typedef ConstraintEdge::ConstraintEdgeSetTy::iterator ConstraintNodeIter;
typedef Map<NodeID, NodeID> NodeToRepMap;
typedef Map<NodeID, NodeBS> NodeToSubsMap;
typedef FIFOWorkList<NodeID> WorkList;
protected:
SVFIR* pag;
NodeToRepMap nodeToRepMap;
NodeToSubsMap nodeToSubsMap;
WorkList nodesToBeCollapsed;
EdgeID edgeIndex;
ConstraintEdge::ConstraintEdgeSetTy AddrCGEdgeSet;
ConstraintEdge::ConstraintEdgeSetTy directEdgeSet;
ConstraintEdge::ConstraintEdgeSetTy LoadCGEdgeSet;
ConstraintEdge::ConstraintEdgeSetTy StoreCGEdgeSet;
void buildCG();
void destroy();
void clearSolitaries(); // remove nodes that are neither pointers nor connected with any edge
SVFStmt::SVFStmtSetTy& getPAGEdgeSet(SVFStmt::PEDGEK kind)
{
return pag->getPTASVFStmtSet(kind);
}
/// Wrappers used internally, not expose to Andersen Pass
//@{
inline NodeID getReturnNode(const FunObjVar* value) const
{
return pag->getReturnNode(value);
}
inline NodeID getVarargNode(const FunObjVar* value) const
{
return pag->getVarargNode(value);
}
//@}
public:
/// Constructor
ConstraintGraph(SVFIR* p): pag(p), edgeIndex(0)
{
buildCG();
}
/// Destructor
virtual ~ConstraintGraph()
{
destroy();
}
/// Get/add/remove constraint node
//@{
inline ConstraintNode* getConstraintNode(NodeID id) const
{
id = sccRepNode(id);
return getGNode(id);
}
inline void addConstraintNode(ConstraintNode* node, NodeID id)
{
addGNode(id,node);
}
inline bool hasConstraintNode(NodeID id) const
{
id = sccRepNode(id);
return hasGNode(id);
}
inline void removeConstraintNode(ConstraintNode* node)
{
removeGNode(node);
}
//@}
//// Return true if this edge exits
inline bool hasEdge(ConstraintNode* src, ConstraintNode* dst, ConstraintEdge::ConstraintEdgeK kind)
{
ConstraintEdge edge(src,dst,kind);
if(kind == ConstraintEdge::Copy ||
kind == ConstraintEdge::NormalGep || kind == ConstraintEdge::VariantGep)
return directEdgeSet.find(&edge) != directEdgeSet.end();
else if(kind == ConstraintEdge::Addr)
return AddrCGEdgeSet.find(&edge) != AddrCGEdgeSet.end();
else if(kind == ConstraintEdge::Store)
return StoreCGEdgeSet.find(&edge) != StoreCGEdgeSet.end();
else if(kind == ConstraintEdge::Load)
return LoadCGEdgeSet.find(&edge) != LoadCGEdgeSet.end();
else
assert(false && "no other kind!");
return false;
}
/// Get an edge via its src and dst nodes and kind
inline ConstraintEdge* getEdge(ConstraintNode* src, ConstraintNode* dst, ConstraintEdge::ConstraintEdgeK kind)
{
ConstraintEdge edge(src,dst,kind);
if(kind == ConstraintEdge::Copy || kind == ConstraintEdge::NormalGep || kind == ConstraintEdge::VariantGep)
{
auto eit = directEdgeSet.find(&edge);
return *eit;
}
else if(kind == ConstraintEdge::Addr)
{
auto eit = AddrCGEdgeSet.find(&edge);
return *eit;
}
else if(kind == ConstraintEdge::Store)
{
auto eit = StoreCGEdgeSet.find(&edge);
return *eit;
}
else if(kind == ConstraintEdge::Load)
{
auto eit = LoadCGEdgeSet.find(&edge);
return *eit;
}
else
{
assert(false && "no other kind!");
return nullptr;
}
}
///Add a SVFIR edge into Edge map
//@{
/// Add Address edge
AddrCGEdge* addAddrCGEdge(NodeID src, NodeID dst);
/// Add Copy edge
CopyCGEdge* addCopyCGEdge(NodeID src, NodeID dst);
/// Add Gep edge
NormalGepCGEdge* addNormalGepCGEdge(NodeID src, NodeID dst, const AccessPath& ap);
VariantGepCGEdge* addVariantGepCGEdge(NodeID src, NodeID dst);
/// Add Load edge
LoadCGEdge* addLoadCGEdge(NodeID src, NodeID dst);
/// Add Store edge
StoreCGEdge* addStoreCGEdge(NodeID src, NodeID dst);
//@}
///Get SVFIR edge
//@{
/// Get Address edges
inline ConstraintEdge::ConstraintEdgeSetTy& getAddrCGEdges()
{
return AddrCGEdgeSet;
}
/// Get Copy/call/ret/gep edges
inline ConstraintEdge::ConstraintEdgeSetTy& getDirectCGEdges()
{
return directEdgeSet;
}
/// Get Load edges
inline ConstraintEdge::ConstraintEdgeSetTy& getLoadCGEdges()
{
return LoadCGEdgeSet;
}
/// Get Store edges
inline ConstraintEdge::ConstraintEdgeSetTy& getStoreCGEdges()
{
return StoreCGEdgeSet;
}
//@}
/// Used for cycle elimination
//@{
/// Remove edge from old dst target, change edge dst id and add modified edge into new dst
void reTargetDstOfEdge(ConstraintEdge* edge, ConstraintNode* newDstNode);
/// Remove edge from old src target, change edge dst id and add modified edge into new src
void reTargetSrcOfEdge(ConstraintEdge* edge, ConstraintNode* newSrcNode);
/// Remove addr edge from their src and dst edge sets
void removeAddrEdge(AddrCGEdge* edge);
/// Remove direct edge from their src and dst edge sets
void removeDirectEdge(ConstraintEdge* edge);
/// Remove load edge from their src and dst edge sets
void removeLoadEdge(LoadCGEdge* edge);
/// Remove store edge from their src and dst edge sets
void removeStoreEdge(StoreCGEdge* edge);
//@}
/// SCC rep/sub nodes methods
//@{
inline NodeID sccRepNode(NodeID id) const
{
NodeToRepMap::const_iterator it = nodeToRepMap.find(id);
if(it==nodeToRepMap.end())
return id;
else
return it->second;
}
inline NodeBS& sccSubNodes(NodeID id)
{
nodeToSubsMap[id].set(id);
return nodeToSubsMap[id];
}
inline void setRep(NodeID node, NodeID rep)
{
nodeToRepMap[node] = rep;
}
inline void setSubs(NodeID node, NodeBS& subs)
{
nodeToSubsMap[node] |= subs;
}
inline void resetSubs(NodeID node)
{
nodeToSubsMap.erase(node);
}
inline NodeBS& getSubs(NodeID node)
{
return nodeToSubsMap[node];
}
inline NodeID getRep(NodeID node)
{
return nodeToRepMap[node];
}
inline void resetRep(NodeID node)
{
nodeToRepMap.erase(node);
}
//@}
/// Move incoming direct edges of a sub node which is outside the SCC to its rep node
/// Remove incoming direct edges of a sub node which is inside the SCC from its rep node
/// Return TRUE if there's a gep edge inside this SCC (PWC).
bool moveInEdgesToRepNode(ConstraintNode*node, ConstraintNode* rep );
/// Move outgoing direct edges of a sub node which is outside the SCC to its rep node
/// Remove outgoing direct edges of sub node which is inside the SCC from its rep node
/// Return TRUE if there's a gep edge inside this SCC (PWC).
bool moveOutEdgesToRepNode(ConstraintNode*node, ConstraintNode* rep );
/// Move incoming/outgoing direct edges of a sub node to its rep node
/// Return TRUE if there's a gep edge inside this SCC (PWC).
inline bool moveEdgesToRepNode(ConstraintNode*node, ConstraintNode* rep )
{
bool gepIn = moveInEdgesToRepNode(node, rep);
bool gepOut = moveOutEdgesToRepNode(node, rep);
return (gepIn || gepOut);
}
/// Check if a given edge is a NormalGepCGEdge with 0 offset.
inline bool isZeroOffsettedGepCGEdge(ConstraintEdge *edge) const
{
if (NormalGepCGEdge *normalGepCGEdge = SVFUtil::dyn_cast<NormalGepCGEdge>(edge))
if (0 == normalGepCGEdge->getConstantFieldIdx())
return true;
return false;
}
/// Wrappers for invoking SVFIR methods
//@{
inline const SVFIR::CallSiteToFunPtrMap& getIndirectCallsites() const
{
return pag->getIndirectCallsites();
}
inline NodeID getBlackHoleNode()
{
return pag->getBlackHoleNode();
}
inline bool isBlkObjOrConstantObj(NodeID id)
{
return pag->isBlkObjOrConstantObj(id);
}
inline NodeBS& getAllFieldsObjVars(NodeID id)
{
return pag->getAllFieldsObjVars(id);
}
inline NodeID getBaseObjVar(NodeID id)
{
return pag->getBaseObjVar(id);
}
inline bool isSingleFieldObj(NodeID id) const
{
const BaseObjVar* baseObj = pag->getBaseObject(id);
return (baseObj->getMaxFieldOffsetLimit() == 1);
}
/// Get a field of a memory object
inline NodeID getGepObjVar(NodeID id, const APOffset& apOffset)
{
NodeID gep = pag->getGepObjVar(id, apOffset);
/// Create a node when it is (1) not exist on graph and (2) not merged
if(sccRepNode(gep)==gep && hasConstraintNode(gep)==false)
addConstraintNode(new ConstraintNode(gep),gep);
return gep;
}
/// Get a field-insensitive node of a memory object
inline NodeID getFIObjVar(NodeID id)
{
NodeID fi = pag->getFIObjVar(id);
/// The fi obj in PAG must be either an existing node or merged to another rep node in ConsG
assert((hasConstraintNode(fi) || sccRepNode(fi) != fi) && "non-existing fi obj??");
return fi;
}
//@}
/// Check/Set PWC (positive weight cycle) flag
//@{
inline bool isPWCNode(NodeID nodeId)
{
return getConstraintNode(nodeId)->isPWCNode();
}
inline void setPWCNode(NodeID nodeId)
{
getConstraintNode(nodeId)->setPWCNode();
}
//@}
/// Add/get nodes to be collapsed
//@{
inline bool hasNodesToBeCollapsed() const
{
return (!nodesToBeCollapsed.empty());
}
inline void addNodeToBeCollapsed(NodeID id)
{
nodesToBeCollapsed.push(id);
}
inline NodeID getNextCollapseNode()
{
return nodesToBeCollapsed.pop();
}
//@}
/// Dump graph into dot file
void dump(std::string name);
/// Print CG into terminal
void print();
/// View graph from the debugger.
void view();
};
/* !
* GenericGraphTraits specializations for the generic graph algorithms.
* Provide graph traits for traversing from a constraint node using standard graph traversals.
*/
template<> struct GenericGraphTraits<SVF::ConstraintNode*> : public GenericGraphTraits<SVF::GenericNode<SVF::ConstraintNode,SVF::ConstraintEdge>* >
{
};
/// Inverse GenericGraphTraits specializations for Value flow node, it is used for inverse traversal.
template<>
struct GenericGraphTraits<Inverse<SVF::ConstraintNode *> > : public GenericGraphTraits<Inverse<SVF::GenericNode<SVF::ConstraintNode,SVF::ConstraintEdge>* > >
{
};
template<> struct GenericGraphTraits<SVF::ConstraintGraph*> : public GenericGraphTraits<SVF::GenericGraph<SVF::ConstraintNode,SVF::ConstraintEdge>* >
{
typedef SVF::ConstraintNode *NodeRef;
};
} // End namespace SVF
#endif /* CONSG_H_ */