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 CallGraph;
42
47class ICFG : public GenericICFGTy
48{
49 friend class ICFGBuilder;
50 friend class ICFGSimplification;
51
52public:
53
56 typedef ICFGNodeIDToNodeMapTy::iterator iterator;
57 typedef ICFGNodeIDToNodeMapTy::const_iterator const_iterator;
58
61 typedef std::vector<const SVFLoop *> SVFLoopVec;
63
65
66private:
71
72public:
74 ICFG();
75
77 ~ICFG() override;
78
80 inline ICFGNode* getICFGNode(NodeID id) const
81 {
82 return getGNode(id);
83 }
84
86 inline bool hasICFGNode(NodeID id) const
87 {
88 return hasGNode(id);
89 }
90
92
97
99 ICFGEdge* getICFGEdge(const ICFGNode* src, const ICFGNode* dst, ICFGEdge::ICFGEdgeK kind);
100
102 void dump(const std::string& file, bool simple = false);
103
105 void view();
106
108 void updateCallGraph(CallGraph* callgraph);
109
111 inline bool isInLoop(const ICFGNode *node)
112 {
113 auto it = icfgNodeToSVFLoopVec.find(node);
114 return it != icfgNodeToSVFLoopVec.end();
115 }
116
118 inline void addNodeToSVFLoop(const ICFGNode *node, const SVFLoop* loop)
119 {
120 icfgNodeToSVFLoopVec[node].push_back(loop);
121 }
122
124 inline SVFLoopVec& getSVFLoops(const ICFGNode *node)
125 {
126 auto it = icfgNodeToSVFLoopVec.find(node);
127 assert(it != icfgNodeToSVFLoopVec.end() && "node not in loop");
128 return it->second;
129 }
130
132 {
134 }
135
136protected:
138
144
146 {
147 edge->getDstNode()->removeIncomingEdge(edge);
148 edge->getSrcNode()->removeOutgoingEdge(edge);
149 delete edge;
150 }
151
153 inline void removeICFGNode(ICFGNode* node)
154 {
155 removeGNode(node);
156 }
157
160 {
161 const FunObjVar* srcfun = srcNode->getFun();
162 const FunObjVar* dstfun = dstNode->getFun();
163 if(srcfun != nullptr && dstfun != nullptr)
164 {
165 assert((srcfun == dstfun) && "src and dst nodes of an intra edge should in the same function!" );
166 }
167 }
168
169 virtual inline IntraICFGNode* addIntraICFGNode(const SVFBasicBlock* bb, bool isRet)
170 {
172 new IntraICFGNode(totalICFGNode++, bb, isRet);
174 return intraIcfgNode;
175 }
176
178 const SVFBasicBlock* bb, const SVFType* ty,
179 const FunObjVar* calledFunc, bool isVararg, bool isvcall,
180 s32_t vcallIdx, const std::string& funNameOfVcall)
181 {
182
184 new CallICFGNode(totalICFGNode++, bb, ty, calledFunc, isVararg,
185 isvcall, vcallIdx, funNameOfVcall);
187 return callICFGNode;
188 }
189
191 {
195 return retICFGNode;
196 }
197
204
211
213 virtual inline void addICFGNode(ICFGNode* node)
214 {
215 addGNode(node->getId(),node);
216 }
217
218public:
221
222
223
225
227
229 {
230 return globalBlockNode;
231 }
233
234private:
237 {
238 bool added1 = edge->getDstNode()->addIncomingEdge(edge);
239 bool added2 = edge->getSrcNode()->addOutgoingEdge(edge);
240 bool all_added = added1 && added2;
241 assert(all_added && "ICFGEdge not added?");
242 return all_added;
243 }
244
247 {
248 FunToFunEntryNodeMapTy::const_iterator it = FunToFunEntryNodeMap.find(fun);
249 if (it == FunToFunEntryNodeMap.end())
250 return nullptr;
251 return it->second;
252 }
253
256 {
257 FunToFunExitNodeMapTy::const_iterator it = FunToFunExitNodeMap.find(fun);
258 if (it == FunToFunExitNodeMap.end())
259 return nullptr;
260 return it->second;
261 }
262
263};
264
265} // End namespace SVF
266
267namespace SVF
268{
269/* !
270 * GenericGraphTraits specializations for generic graph algorithms.
271 * Provide graph traits for traversing from a constraint node using standard graph traversals.
272 */
273template<> struct GenericGraphTraits<SVF::ICFGNode*> : public GenericGraphTraits<SVF::GenericNode<SVF::ICFGNode,SVF::ICFGEdge>* >
274{
275};
276
278template<>
279struct GenericGraphTraits<Inverse<SVF::ICFGNode *> > : public GenericGraphTraits<Inverse<SVF::GenericNode<SVF::ICFGNode,SVF::ICFGEdge>* > >
280{
281};
282
283template<> struct GenericGraphTraits<SVF::ICFG*> : public GenericGraphTraits<SVF::GenericGraph<SVF::ICFGNode,SVF::ICFGEdge>* >
284{
286};
287
288} // End namespace llvm
289
290#endif /* INCLUDE_UTIL_ICFG_H_ */
void setRetICFGNode(const RetICFGNode *r)
Return callsite.
Definition ICFGNode.h:436
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:88
virtual FunEntryICFGNode * addFunEntryICFGNode(const FunObjVar *svfFunc)
Definition ICFG.h:198
virtual CallICFGNode * addCallICFGNode(const SVFBasicBlock *bb, const SVFType *ty, const FunObjVar *calledFunc, bool isVararg, bool isvcall, s32_t vcallIdx, const std::string &funNameOfVcall)
Definition ICFG.h:177
void view()
View graph from the debugger.
Definition ICFG.cpp:419
virtual void addICFGNode(ICFGNode *node)
Add a ICFG node.
Definition ICFG.h:213
bool hasICFGNode(NodeID id) const
Whether has the ICFGNode.
Definition ICFG.h:86
ICFGEdge::ICFGEdgeSetTy ICFGEdgeSetTy
Definition ICFG.h:55
ICFGEdge * hasThreadICFGEdge(ICFGNode *src, ICFGNode *dst, ICFGEdge::ICFGEdgeK kind)
Definition ICFG.cpp:293
const ICFGNodeToSVFLoopVec & getIcfgNodeToSVFLoopVec() const
Definition ICFG.h:131
void removeICFGNode(ICFGNode *node)
Remove a ICFGNode.
Definition ICFG.h:153
Map< const FunObjVar *, FunExitICFGNode * > FunToFunExitNodeMapTy
Definition ICFG.h:60
Map< const ICFGNode *, SVFLoopVec > ICFGNodeToSVFLoopVec
Definition ICFG.h:62
bool addICFGEdge(ICFGEdge *edge)
Add ICFG edge, only used by addIntraEdge, addCallEdge, addRetEdge etc.
Definition ICFG.h:236
friend class ICFGSimplification
Definition ICFG.h:50
ICFGNodeIDToNodeMapTy::iterator iterator
Definition ICFG.h:56
virtual RetICFGNode * addRetICFGNode(CallICFGNode *call)
Definition ICFG.h:190
void removeICFGEdge(ICFGEdge *edge)
Remove a ICFG edge.
Definition ICFG.h:145
ICFGEdge * getICFGEdge(const ICFGNode *src, const ICFGNode *dst, ICFGEdge::ICFGEdgeK kind)
Get a SVFG edge according to src and dst.
Definition ICFG.cpp:311
NodeID totalICFGNode
Definition ICFG.h:64
Map< const FunObjVar *, FunEntryICFGNode * > FunToFunEntryNodeMapTy
Definition ICFG.h:59
ICFGNode * getICFGNode(NodeID id) const
Get a ICFG node.
Definition ICFG.h:80
ICFG()
Constructor.
Definition ICFG.cpp:218
FunToFunEntryNodeMapTy FunToFunEntryNodeMap
map a function to its FunExitICFGNode
Definition ICFG.h:67
virtual IntraICFGNode * addIntraICFGNode(const SVFBasicBlock *bb, bool isRet)
Definition ICFG.h:169
void checkIntraEdgeParents(const ICFGNode *srcNode, const ICFGNode *dstNode)
sanitize Intra edges, verify that both nodes belong to the same function.
Definition ICFG.h:159
ICFGEdge * addCallEdge(ICFGNode *srcNode, ICFGNode *dstNode)
Definition ICFG.cpp:374
ICFGNodeToSVFLoopVec icfgNodeToSVFLoopVec
map ICFG node to the SVF loops where it resides
Definition ICFG.h:70
FunExitICFGNode * getFunExitICFGNode(const FunObjVar *fun)
Add a function exit node.
Definition ICFG.cpp:249
ICFGEdge * hasInterICFGEdge(ICFGNode *src, ICFGNode *dst, ICFGEdge::ICFGEdgeK kind)
Definition ICFG.cpp:276
void addNodeToSVFLoop(const ICFGNode *node, const SVFLoop *loop)
Insert (node, loop) to icfgNodeToSVFLoopVec.
Definition ICFG.h:118
GlobalICFGNode * globalBlockNode
unique basic block for all globals
Definition ICFG.h:69
ICFGNodeIDToNodeMapTy::const_iterator const_iterator
Definition ICFG.h:57
void dump(const std::string &file, bool simple=false)
Dump graph into dot file.
Definition ICFG.cpp:411
virtual FunExitICFGNode * addFunExitICFGNode(const FunObjVar *svfFunc)
Definition ICFG.h:205
ICFGEdge * addRetEdge(ICFGNode *srcNode, ICFGNode *dstNode)
Definition ICFG.cpp:392
void updateCallGraph(CallGraph *callgraph)
update ICFG for indirect calls
Definition ICFG.cpp:427
ICFGEdge * hasIntraICFGEdge(ICFGNode *src, ICFGNode *dst, ICFGEdge::ICFGEdgeK kind)
Whether we has a SVFG edge.
Definition ICFG.cpp:259
FunToFunExitNodeMapTy FunToFunExitNodeMap
map a function to its FunEntryICFGNode
Definition ICFG.h:68
ICFGEdge * addConditionalIntraEdge(ICFGNode *srcNode, ICFGNode *dstNode, s64_t branchCondVal)
Definition ICFG.cpp:352
OrderedMap< NodeID, ICFGNode * > ICFGNodeIDToNodeMapTy
Definition ICFG.h:54
FunExitICFGNode * getFunExitBlock(const FunObjVar *fun)
Get/Add a function exit node.
Definition ICFG.h:255
std::vector< const SVFLoop * > SVFLoopVec
Definition ICFG.h:61
ICFGEdge * addIntraEdge(ICFGNode *srcNode, ICFGNode *dstNode)
Add intraprocedural and interprocedural control-flow edges.
Definition ICFG.cpp:333
FunEntryICFGNode * getFunEntryICFGNode(const FunObjVar *fun)
Add a function entry node.
Definition ICFG.cpp:242
bool isInLoop(const ICFGNode *node)
Whether node is in a loop.
Definition ICFG.h:111
~ICFG() override
Destructor.
Definition ICFG.cpp:222
GlobalICFGNode * getGlobalICFGNode() const
Definition ICFG.h:228
FunEntryICFGNode * getFunEntryBlock(const FunObjVar *fun)
Get/Add a function entry node.
Definition ICFG.h:246
SVFLoopVec & getSVFLoops(const ICFGNode *node)
Get loops where a node resides.
Definition ICFG.h:124
NodeID getId() const
Get ID.
Definition SVFValue.h:160
for isBitcode
Definition BasicTypes.h:68
u32_t NodeID
Definition GeneralType.h:56
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:74
signed s32_t
Definition GeneralType.h:48
signed long long s64_t
Definition GeneralType.h:50
GenericGraph< ICFGNode, ICFGEdge > GenericICFGTy
Definition ICFG.h:46