Static Value-Flow Analysis
Loading...
Searching...
No Matches
ICFG.h
Go to the documentation of this file.
1//===- ICFG.h ----------------------------------------------------------------//
2//
3// SVF: Static Value-Flow Analysis
4//
5// Copyright (C) <2013-2018> <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 * ICFG.h
25 *
26 * Created on: 11 Sep. 2018
27 * Author: Yulei
28 */
29
30#ifndef INCLUDE_UTIL_ICFG_H_
31#define INCLUDE_UTIL_ICFG_H_
32
33#include "Graphs/ICFGNode.h"
34#include "Graphs/ICFGEdge.h"
35#include "Util/WorkList.h"
36#include "MemoryModel/SVFLoop.h"
37
38namespace SVF
39{
40
41class PTACallGraph;
42
47class ICFG : public GenericICFGTy
48{
49 friend class ICFGBuilder;
50 friend class SVFIRWriter;
51 friend class SVFIRReader;
52 friend class ICFGSimplification;
53
54public:
55
58 typedef ICFGNodeIDToNodeMapTy::iterator iterator;
59 typedef ICFGNodeIDToNodeMapTy::const_iterator const_iterator;
60
63 typedef std::vector<const SVFLoop *> SVFLoopVec;
65
67
68private:
73
76
77
78public:
80 ICFG();
81
83 ~ICFG() override;
84
86 inline ICFGNode* getICFGNode(NodeID id) const
87 {
88 return getGNode(id);
89 }
90
92 inline bool hasICFGNode(NodeID id) const
93 {
94 return hasGNode(id);
95 }
96
98
103
105 ICFGEdge* getICFGEdge(const ICFGNode* src, const ICFGNode* dst, ICFGEdge::ICFGEdgeK kind);
106
108 void dump(const std::string& file, bool simple = false);
109
111 void view();
112
114 void updateCallGraph(PTACallGraph* callgraph);
115
117 inline bool isInLoop(const ICFGNode *node)
118 {
119 auto it = icfgNodeToSVFLoopVec.find(node);
120 return it != icfgNodeToSVFLoopVec.end();
121 }
122
124 inline void addNodeToSVFLoop(const ICFGNode *node, const SVFLoop* loop)
125 {
126 icfgNodeToSVFLoopVec[node].push_back(loop);
127 }
128
130 inline SVFLoopVec& getSVFLoops(const ICFGNode *node)
131 {
132 auto it = icfgNodeToSVFLoopVec.find(node);
133 assert(it != icfgNodeToSVFLoopVec.end() && "node not in loop");
134 return it->second;
135 }
136
138 {
140 }
141
142protected:
144
150
152 {
153 edge->getDstNode()->removeIncomingEdge(edge);
154 edge->getSrcNode()->removeOutgoingEdge(edge);
155 delete edge;
156 }
157
159 inline void removeICFGNode(ICFGNode* node)
160 {
161 removeGNode(node);
162 }
163
166 {
167 const SVFFunction* srcfun = srcNode->getFun();
168 const SVFFunction* dstfun = dstNode->getFun();
169 if(srcfun != nullptr && dstfun != nullptr)
170 {
171 assert((srcfun == dstfun) && "src and dst nodes of an intra edge should in the same function!" );
172 }
173 }
174
175 virtual inline IntraICFGNode* addIntraICFGNode(const SVFBasicBlock* bb, bool isRet)
176 {
178 new IntraICFGNode(totalICFGNode++, bb, isRet);
180 return intraIcfgNode;
181 }
182
184 const SVFBasicBlock* bb, const SVFType* ty,
185 const SVFFunction* calledFunc, bool isVararg, bool isvcall,
186 s32_t vcallIdx, const std::string& funNameOfVcall)
187 {
188
190 new CallICFGNode(totalICFGNode++, bb, ty, calledFunc, isVararg,
191 isvcall, vcallIdx, funNameOfVcall);
193 return callICFGNode;
194 }
195
197 {
201 return retICFGNode;
202 }
203
210
217
219 virtual inline void addICFGNode(ICFGNode* node)
220 {
221 addGNode(node->getId(),node);
222 _repNode[node] = node;
223 _subNodes[node].push_back(node);
224 }
225
226public:
229
230
231
233
235
237 {
238 return globalBlockNode;
239 }
240
241 const std::vector<const ICFGNode*>& getSubNodes(const ICFGNode* node) const
242 {
243 return _subNodes.at(node);
244 }
245
246 const ICFGNode* getRepNode(const ICFGNode* node) const
247 {
248 return _repNode.at(node);
249 }
250
251
252 void updateSubAndRep(const ICFGNode* rep, const ICFGNode* sub)
253 {
254 addSubNode(rep, sub);
255 updateRepNode(rep, sub);
256 }
258
259private:
261 void addSubNode(const ICFGNode* rep, const ICFGNode* sub)
262 {
263 std::vector<const ICFGNode*>& subNodes = _subNodes[sub];
264 if(std::find(subNodes.begin(), subNodes.end(), rep) == subNodes.end())
265 {
266 subNodes.push_back(rep);
267 }
268 }
269
271 void updateRepNode(const ICFGNode* rep, const ICFGNode* sub)
272 {
273 _repNode[rep] = sub;
274 }
275
278 {
279 bool added1 = edge->getDstNode()->addIncomingEdge(edge);
280 bool added2 = edge->getSrcNode()->addOutgoingEdge(edge);
281 bool all_added = added1 && added2;
282 assert(all_added && "ICFGEdge not added?");
283 return all_added;
284 }
285
288 {
289 FunToFunEntryNodeMapTy::const_iterator it = FunToFunEntryNodeMap.find(fun);
290 if (it == FunToFunEntryNodeMap.end())
291 return nullptr;
292 return it->second;
293 }
294
297 {
298 FunToFunExitNodeMapTy::const_iterator it = FunToFunExitNodeMap.find(fun);
299 if (it == FunToFunExitNodeMap.end())
300 return nullptr;
301 return it->second;
302 }
303
304};
305
306} // End namespace SVF
307
308namespace SVF
309{
310/* !
311 * GenericGraphTraits specializations for generic graph algorithms.
312 * Provide graph traits for traversing from a constraint node using standard graph traversals.
313 */
314template<> struct GenericGraphTraits<SVF::ICFGNode*> : public GenericGraphTraits<SVF::GenericNode<SVF::ICFGNode,SVF::ICFGEdge>* >
315{
316};
317
319template<>
320struct GenericGraphTraits<Inverse<SVF::ICFGNode *> > : public GenericGraphTraits<Inverse<SVF::GenericNode<SVF::ICFGNode,SVF::ICFGEdge>* > >
321{
322};
323
324template<> struct GenericGraphTraits<SVF::ICFG*> : public GenericGraphTraits<SVF::GenericGraph<SVF::ICFGNode,SVF::ICFGEdge>* >
325{
327};
328
329} // End namespace llvm
330
331#endif /* INCLUDE_UTIL_ICFG_H_ */
void setRetICFGNode(const RetICFGNode *r)
Return callsite.
Definition ICFGNode.h:464
void addGNode(NodeID id, NodeType *node)
Add a Node.
void removeGNode(NodeType *node)
Delete a node.
bool hasGNode(NodeID id) const
Has a node.
NodeType * getGNode(NodeID id) const
Get a node.
GenericNode< ICFGNode, ICFGEdge >::GEdgeSetTy ICFGEdgeSetTy
Definition ICFGEdge.h:90
void view()
View graph from the debugger.
Definition ICFG.cpp:411
FunEntryICFGNode * getFunEntryICFGNode(const SVFFunction *fun)
Add a function entry node.
Definition ICFG.cpp:234
const ICFGNode * getRepNode(const ICFGNode *node) const
Definition ICFG.h:246
virtual void addICFGNode(ICFGNode *node)
Add a ICFG node.
Definition ICFG.h:219
bool hasICFGNode(NodeID id) const
Whether has the ICFGNode.
Definition ICFG.h:92
ICFGEdge::ICFGEdgeSetTy ICFGEdgeSetTy
Definition ICFG.h:57
Map< const SVFFunction *, FunExitICFGNode * > FunToFunExitNodeMapTy
Definition ICFG.h:62
ICFGEdge * hasThreadICFGEdge(ICFGNode *src, ICFGNode *dst, ICFGEdge::ICFGEdgeK kind)
Definition ICFG.cpp:285
FunEntryICFGNode * getFunEntryBlock(const SVFFunction *fun)
Get/Add a function entry node.
Definition ICFG.h:287
FunExitICFGNode * getFunExitBlock(const SVFFunction *fun)
Get/Add a function exit node.
Definition ICFG.h:296
const ICFGNodeToSVFLoopVec & getIcfgNodeToSVFLoopVec() const
Definition ICFG.h:137
void removeICFGNode(ICFGNode *node)
Remove a ICFGNode.
Definition ICFG.h:159
Map< const ICFGNode *, SVFLoopVec > ICFGNodeToSVFLoopVec
Definition ICFG.h:64
Map< const SVFFunction *, FunEntryICFGNode * > FunToFunEntryNodeMapTy
Definition ICFG.h:61
bool addICFGEdge(ICFGEdge *edge)
Add ICFG edge, only used by addIntraEdge, addCallEdge, addRetEdge etc.
Definition ICFG.h:277
friend class ICFGSimplification
Definition ICFG.h:52
ICFGNodeIDToNodeMapTy::iterator iterator
Definition ICFG.h:58
virtual RetICFGNode * addRetICFGNode(CallICFGNode *call)
Definition ICFG.h:196
virtual FunEntryICFGNode * addFunEntryICFGNode(const SVFFunction *svfFunc)
Definition ICFG.h:204
void removeICFGEdge(ICFGEdge *edge)
Remove a ICFG edge.
Definition ICFG.h:151
virtual CallICFGNode * addCallICFGNode(const SVFBasicBlock *bb, const SVFType *ty, const SVFFunction *calledFunc, bool isVararg, bool isvcall, s32_t vcallIdx, const std::string &funNameOfVcall)
Definition ICFG.h:183
void updateCallGraph(PTACallGraph *callgraph)
update ICFG for indirect calls
Definition ICFG.cpp:419
ICFGEdge * getICFGEdge(const ICFGNode *src, const ICFGNode *dst, ICFGEdge::ICFGEdgeK kind)
Get a SVFG edge according to src and dst.
Definition ICFG.cpp:303
NodeID totalICFGNode
Definition ICFG.h:66
ICFGNode * getICFGNode(NodeID id) const
Get a ICFG node.
Definition ICFG.h:86
ICFG()
Constructor.
Definition ICFG.cpp:210
FunToFunEntryNodeMapTy FunToFunEntryNodeMap
map a function to its FunExitICFGNode
Definition ICFG.h:69
virtual IntraICFGNode * addIntraICFGNode(const SVFBasicBlock *bb, bool isRet)
Definition ICFG.h:175
void checkIntraEdgeParents(const ICFGNode *srcNode, const ICFGNode *dstNode)
sanitize Intra edges, verify that both nodes belong to the same function.
Definition ICFG.h:165
Map< const ICFGNode *, const ICFGNode * > _repNode
map a subnode to its representative node(1st node of basicblock)
Definition ICFG.h:75
ICFGEdge * addCallEdge(ICFGNode *srcNode, ICFGNode *dstNode)
Definition ICFG.cpp:366
ICFGNodeToSVFLoopVec icfgNodeToSVFLoopVec
map ICFG node to the SVF loops where it resides
Definition ICFG.h:72
ICFGEdge * hasInterICFGEdge(ICFGNode *src, ICFGNode *dst, ICFGEdge::ICFGEdgeK kind)
Definition ICFG.cpp:268
void addNodeToSVFLoop(const ICFGNode *node, const SVFLoop *loop)
Insert (node, loop) to icfgNodeToSVFLoopVec.
Definition ICFG.h:124
GlobalICFGNode * globalBlockNode
unique basic block for all globals
Definition ICFG.h:71
ICFGNodeIDToNodeMapTy::const_iterator const_iterator
Definition ICFG.h:59
void dump(const std::string &file, bool simple=false)
Dump graph into dot file.
Definition ICFG.cpp:403
ICFGEdge * addRetEdge(ICFGNode *srcNode, ICFGNode *dstNode)
Definition ICFG.cpp:384
const std::vector< const ICFGNode * > & getSubNodes(const ICFGNode *node) const
Definition ICFG.h:241
ICFGEdge * hasIntraICFGEdge(ICFGNode *src, ICFGNode *dst, ICFGEdge::ICFGEdgeK kind)
Whether we has a SVFG edge.
Definition ICFG.cpp:251
FunToFunExitNodeMapTy FunToFunExitNodeMap
map a function to its FunEntryICFGNode
Definition ICFG.h:70
ICFGEdge * addConditionalIntraEdge(ICFGNode *srcNode, ICFGNode *dstNode, s64_t branchCondVal)
Definition ICFG.cpp:344
OrderedMap< NodeID, ICFGNode * > ICFGNodeIDToNodeMapTy
Definition ICFG.h:56
void updateSubAndRep(const ICFGNode *rep, const ICFGNode *sub)
Definition ICFG.h:252
Map< const ICFGNode *, std::vector< const ICFGNode * > > _subNodes
map a node(1st node of basicblock) to its subnodes
Definition ICFG.h:74
std::vector< const SVFLoop * > SVFLoopVec
Definition ICFG.h:63
virtual FunExitICFGNode * addFunExitICFGNode(const SVFFunction *svfFunc)
Definition ICFG.h:211
ICFGEdge * addIntraEdge(ICFGNode *srcNode, ICFGNode *dstNode)
Add intraprocedural and interprocedural control-flow edges.
Definition ICFG.cpp:325
bool isInLoop(const ICFGNode *node)
Whether node is in a loop.
Definition ICFG.h:117
~ICFG() override
Destructor.
Definition ICFG.cpp:214
void updateRepNode(const ICFGNode *rep, const ICFGNode *sub)
when ICFG is simplified, some node would be removed, this map records the removed node to its rep nod...
Definition ICFG.h:271
void addSubNode(const ICFGNode *rep, const ICFGNode *sub)
when ICFG is simplified, SubNode would merge repNode, then update the map
Definition ICFG.h:261
GlobalICFGNode * getGlobalICFGNode() const
Definition ICFG.h:236
FunExitICFGNode * getFunExitICFGNode(const SVFFunction *fun)
Add a function exit node.
Definition ICFG.cpp:241
SVFLoopVec & getSVFLoops(const ICFGNode *node)
Get loops where a node resides.
Definition ICFG.h:130
NodeID getId() const
Get ID.
for isBitcode
Definition BasicTypes.h:68
u32_t NodeID
Definition GeneralType.h:55
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:74
signed s32_t
Definition GeneralType.h:47
signed long long s64_t
Definition GeneralType.h:49
GenericGraph< ICFGNode, ICFGEdge > GenericICFGTy
Definition ICFG.h:46