Static Value-Flow Analysis
Loading...
Searching...
No Matches
CallGraph.cpp
Go to the documentation of this file.
1//===- CallGraph.cpp -- Call graph used internally in SVF------------------//
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/*
25 * CallGraph.cpp
26 *
27 * Created on: Nov 7, 2013
28 * Author: Yulei Sui
29 */
30
31#include "Graphs/CallGraph.h"
32#include "SVFIR/SVFIR.h"
33#include "SVFIR/SVFModule.h"
34#include "Util/SVFUtil.h"
35#include <sstream>
36
37using namespace SVF;
38using namespace SVFUtil;
39
40
42
44{
45 assert(call->getCalledFunction() && "not a direct callsite??");
46 directCalls.insert(call);
47}
49
50const std::string CallGraphEdge::toString() const
51{
52 std::string str;
53 std::stringstream rawstr(str);
54 rawstr << "CallICFGNode ID: " << getEdgeKindWithoutMask();
55 rawstr << "direct call";
56 rawstr << "[" << getDstID() << "<--" << getSrcID() << "]\t";
57 return rawstr.str();
58}
59
60const std::string CallGraphNode::toString() const
61{
62 std::string str;
63 std::stringstream rawstr(str);
64 rawstr << "CallGraphNode ID: " << getId() << " {fun: " << fun->getName() << "}";
65 return rawstr.str();
66}
67
68
74
75
80{
81}
82
87{
89 CallGraphNode*callGraphNode = new CallGraphNode(id, fun);
90 addGNode(id, callGraphNode);
91 funToCallGraphNodeMap[callGraphNode->getFunction()] = callGraphNode;
93}
94
99 CallGraphNode* dst,
100 const CallICFGNode* callIcfgNode) const
101{
105 if (outEdge && inEdge)
106 {
107 assert(outEdge == inEdge && "edges not match");
108 return outEdge;
109 }
110 else
111 return nullptr;
112}
113
132
136void CallGraph::dump(const std::string& filename)
137{
139}
140
142{
143 SVF::ViewGraph(this, "Call Graph");
144}
145
147{
148 for (const auto& item : *this)
149 {
150 if (item.second->getName() == name)
151 return item.second;
152 }
153 return nullptr;
154}
155
156namespace SVF
157{
158
162template<>
164{
165
168 DOTGraphTraits(bool isSimple = false) :
169 DefaultDOTGraphTraits(isSimple)
170 {
171 }
172
174 static std::string getGraphName(CallGraph*)
175 {
176 return "Call Graph";
177 }
179 static std::string getNodeLabel(CallGraphNode*node, CallGraph*)
180 {
181 return node->toString();
182 }
183
184 static std::string getNodeAttributes(CallGraphNode*node, CallGraph*)
185 {
186 const SVFFunction* fun = node->getFunction();
187 if (!SVFUtil::isExtCall(fun))
188 {
189 return "shape=box";
190 }
191 else
192 return "shape=Mrecord";
193 }
194
195 template<class EdgeIter>
197 CallGraph*)
198 {
199
200 //TODO: mark indirect call of Fork with different color
201 CallGraphEdge* edge = *(EI.getCurrent());
202 assert(edge && "No edge found!!");
203
204 std::string color = "color=black";
205 return color;
206 }
207
208 template<class EdgeIter>
210 {
211 CallGraphEdge* edge = *(EI.getCurrent());
212 assert(edge && "No edge found!!");
213
214 std::string str;
215 std::stringstream rawstr(str);
216 rawstr << edge->getEdgeKindWithoutMask();
217
218 return rawstr.str();
219 }
220};
221} // End namespace llvm
const char *const name
Definition cJSON.h:264
cJSON * item
Definition cJSON.h:222
void addDirectCallSite(const CallICFGNode *call)
Add direct callsite.
Definition CallGraph.cpp:43
virtual const std::string toString() const
Definition CallGraph.cpp:50
CallInstSet directCalls
Definition CallGraph.h:58
const SVFFunction * fun
Definition CallGraph.h:117
virtual const std::string toString() const
Definition CallGraph.cpp:60
const SVFFunction * getFunction() const
Get function of this call node.
Definition CallGraph.h:131
CallInstToCallGraphEdgesMap callinstToCallGraphEdgesMap
Map a call instruction to its corresponding call edges.
Definition CallGraph.h:184
CallGraph()
Constructor.
Definition CallGraph.cpp:70
void addCallGraphNode(const SVFFunction *fun)
Definition CallGraph.cpp:86
void addEdge(CallGraphEdge *edge)
Add call graph edge.
Definition CallGraph.h:192
NodeID callGraphNodeNum
Definition CallGraph.h:186
void destroy()
Clean up memory.
Definition CallGraph.cpp:79
FunToCallGraphNodeMap funToCallGraphNodeMap
Call Graph node map.
Definition CallGraph.h:183
void view()
View the graph from the debugger.
void dump(const std::string &filename)
Dump the graph.
CallGraphEdge * hasGraphEdge(CallGraphNode *src, CallGraphNode *dst, const CallICFGNode *callIcfgNode) const
Whether we have already created this call graph edge.
Definition CallGraph.cpp:98
void addDirectCallGraphEdge(const CallICFGNode *call, const SVFFunction *callerFun, const SVFFunction *calleeFun)
Add direct call edges.
const CallGraphNode * getCallGraphNode(const std::string &name)
const SVFFunction * getCalledFunction() const
Definition ICFGNode.h:518
GEdgeKind getEdgeKindWithoutMask() const
NodeID getDstID() const
NodeID getSrcID() const
get methods of the components
void addGNode(NodeID id, NodeType *node)
Add a Node.
bool hasIncomingEdge() const
Has incoming/outgoing edge set.
bool hasOutgoingEdge() const
GEdgeSetTy::iterator iterator
static void WriteGraphToFile(SVF::OutStream &O, const std::string &GraphName, const GraphType &GT, bool simple=false)
NodeID getId() const
Get ID.
const std::string & getName() const
Definition SVFValue.h:243
bool isExtCall(const SVFFunction *fun)
Definition SVFUtil.h:278
std::ostream & outs()
Overwrite llvm::outs()
Definition SVFUtil.h:50
for isBitcode
Definition BasicTypes.h:68
u32_t NodeID
Definition GeneralType.h:55
void ViewGraph(const GraphType &G, const std::string &name, bool ShortNames=false, GraphProgram::Name Program=GraphProgram::DOT)
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:74
DOTGraphTraits(bool isSimple=false)
static std::string getNodeAttributes(CallGraphNode *node, CallGraph *)
static std::string getGraphName(CallGraph *)
Return name of the graph.
static std::string getNodeLabel(CallGraphNode *node, CallGraph *)
Return function name;.
static std::string getEdgeSourceLabel(NodeType *, EdgeIter EI)
static std::string getEdgeAttributes(CallGraphNode *, EdgeIter EI, CallGraph *)
NodeType::iterator ChildIteratorType