forked from SVF-tools/SVF
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCallGraph.cpp
More file actions
501 lines (430 loc) · 14 KB
/
Copy pathCallGraph.cpp
File metadata and controls
501 lines (430 loc) · 14 KB
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
//===- PTACallGraph.cpp -- Call graph used internally in SVF------------------//
//
// 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/>.
//
//===----------------------------------------------------------------------===//
/*
* PTACallGraph.cpp
*
* Created on: Nov 7, 2013
* Author: Yulei Sui
*/
#include "Graphs/CallGraph.h"
#include "SVFIR/SVFIR.h"
#include "Util/Options.h"
#include "Util/SVFUtil.h"
#include <sstream>
#include "Util/Options.h"
using namespace SVF;
using namespace SVFUtil;
CallGraph::CallSiteToIdMap CallGraph::csToIdMap;
CallGraph::IdToCallSiteMap CallGraph::idToCSMap;
CallSiteID CallGraph::totalCallSiteNum=1;
const std::string &CallGraphNode::getName() const
{
return fun->getName();
}
/// Add direct and indirect callsite
//@{
void CallGraphEdge::addDirectCallSite(const CallICFGNode* call)
{
assert(call->getCalledFunction() && "not a direct callsite??");
directCalls.insert(call);
}
void CallGraphEdge::addInDirectCallSite(const CallICFGNode* call)
{
assert((nullptr == call->getCalledFunction() || !SVFUtil::isa<FunValVar>(SVFUtil::getForkedFun(call))) &&
"not an indirect callsite??");
indirectCalls.insert(call);
}
//@}
const std::string CallGraphEdge::toString() const
{
std::string str;
std::stringstream rawstr(str);
rawstr << "CallSite ID: " << getCallSiteID();
if(isDirectCallEdge())
rawstr << "direct call";
else
rawstr << "indirect call";
rawstr << "[" << getDstID() << "<--" << getSrcID() << "]\t";
return rawstr.str();
}
const std::string CallGraphNode::toString() const
{
std::string str;
std::stringstream rawstr(str);
rawstr << "CallGraphNode ID: " << getId() << " {fun: " << fun->getName() << "}";
return rawstr.str();
}
const FunObjVar *CallGraph::getCallerOfCallSite(CallSiteID id) const
{
return getCallSite(id)->getCaller();
}
bool CallGraphNode::isReachableFromProgEntry(Map<NodeID, bool> &reachableFromEntry, NodeBS &visitedNodes) const
{
std::function<bool(const CallGraphNode*)> dfs =
[&reachableFromEntry, &visitedNodes, &dfs](const CallGraphNode *v)
{
NodeID id = v->getId();
if (!visitedNodes.test_and_set(id))
return reachableFromEntry[id];
if (SVFUtil::isProgEntryFunction(v->getFunction()))
return reachableFromEntry[id] = true;
bool result = false;
for (const_iterator it = v->InEdgeBegin(), eit = v->InEdgeEnd(); it != eit; ++it)
{
CallGraphEdge* edge = *it;
result |= dfs(edge->getSrcNode());
if (result)
break;
}
return reachableFromEntry[id] = result;
};
return dfs(this);
}
/// Constructor
CallGraph::CallGraph(CGEK k): kind(k)
{
callGraphNodeNum = 0;
numOfResolvedIndCallEdge = 0;
}
/// Copy constructor
CallGraph::CallGraph(const CallGraph& other)
{
callGraphNodeNum = other.getTotalNodeNum();
numOfResolvedIndCallEdge = 0;
kind = NormCallGraph;
/// copy call graph nodes
for (const auto& item : other)
{
const CallGraphNode* cgn = item.second;
CallGraphNode* callGraphNode = new CallGraphNode(cgn->getId(), cgn->getFunction());
addGNode(cgn->getId(),callGraphNode);
funToCallGraphNodeMap[cgn->getFunction()] = callGraphNode;
}
/// copy edges
for (const auto& item : other.callinstToCallGraphEdgesMap)
{
const CallICFGNode* cs = item.first;
for (const CallGraphEdge* edge : item.second)
{
CallGraphNode* src = getCallGraphNode(edge->getSrcID());
CallGraphNode* dst = getCallGraphNode(edge->getDstID());
CallSiteID csId = addCallSite(cs, dst->getFunction());
CallGraphEdge* newEdge = new CallGraphEdge(src,dst, CallGraphEdge::CallRetEdge,csId);
newEdge->addDirectCallSite(cs);
addEdge(newEdge);
callinstToCallGraphEdgesMap[cs].insert(newEdge);
}
}
}
CallSiteID CallGraph::addCallSite(const CallICFGNode* cs, const FunObjVar* callee, const CallSiteID csid, std::pair<const CallICFGNode*, const FunObjVar*> newCS)
{
csToIdMap.insert(std::make_pair(newCS, csid));
idToCSMap.insert(std::make_pair(csid, newCS));
return csid;
}
/*!
* Memory has been cleaned up at GenericGraph
*/
void CallGraph::destroy()
{
}
/*!
* Whether we have already created this call graph edge
*/
CallGraphEdge* CallGraph::hasGraphEdge(CallGraphNode* src,
CallGraphNode* dst,
CallGraphEdge::CEDGEK kind, CallSiteID csId) const
{
CallGraphEdge edge(src,dst,kind,csId);
return hasGraphEdge(&edge);
}
CallGraphEdge* CallGraph::hasGraphEdge(CallGraphEdge* cgEdge) const
{
CallGraphEdge* outEdge = cgEdge->getSrcNode()->hasOutgoingEdge(cgEdge);
CallGraphEdge* inEdge = cgEdge->getDstNode()->hasIncomingEdge(cgEdge);
if (outEdge && inEdge)
{
assert(outEdge == inEdge && "edges not match");
return outEdge;
}
else
return nullptr;
}
/*!
* get PTACallGraph edge via nodes
*/
CallGraphEdge* CallGraph::getGraphEdge(CallGraphNode* src,
CallGraphNode* dst,
CallGraphEdge::CEDGEK kind, CallSiteID)
{
for (CallGraphEdge::CallGraphEdgeSet::iterator iter = src->OutEdgeBegin();
iter != src->OutEdgeEnd(); ++iter)
{
CallGraphEdge* edge = (*iter);
if (edge->getEdgeKind() == kind && edge->getDstID() == dst->getId())
return edge;
}
return nullptr;
}
/*!
* Add indirect call edge to update call graph
*/
void CallGraph::addIndirectCallGraphEdge(const CallICFGNode* cs,const FunObjVar* callerFun, const FunObjVar* calleeFun)
{
CallGraphNode* caller = getCallGraphNode(callerFun);
CallGraphNode* callee = getCallGraphNode(calleeFun);
numOfResolvedIndCallEdge++;
CallSiteID csId = addCallSite(cs, callee->getFunction());
if(!hasGraphEdge(caller,callee, CallGraphEdge::CallRetEdge,csId))
{
CallGraphEdge* edge = new CallGraphEdge(caller,callee, CallGraphEdge::CallRetEdge, csId);
edge->addInDirectCallSite(cs);
addEdge(edge);
callinstToCallGraphEdgesMap[cs].insert(edge);
}
}
/*!
* Get all callsite invoking this callee
*/
void CallGraph::getAllCallSitesInvokingCallee(const FunObjVar* callee, CallGraphEdge::CallInstSet& csSet)
{
CallGraphNode* callGraphNode = getCallGraphNode(callee);
for(CallGraphNode::iterator it = callGraphNode->InEdgeBegin(), eit = callGraphNode->InEdgeEnd();
it!=eit; ++it)
{
for(CallGraphEdge::CallInstSet::const_iterator cit = (*it)->directCallsBegin(),
ecit = (*it)->directCallsEnd(); cit!=ecit; ++cit)
{
csSet.insert((*cit));
}
for(CallGraphEdge::CallInstSet::const_iterator cit = (*it)->indirectCallsBegin(),
ecit = (*it)->indirectCallsEnd(); cit!=ecit; ++cit)
{
csSet.insert((*cit));
}
}
}
/*!
* Get direct callsite invoking this callee
*/
void CallGraph::getDirCallSitesInvokingCallee(const FunObjVar* callee, CallGraphEdge::CallInstSet& csSet)
{
CallGraphNode* callGraphNode = getCallGraphNode(callee);
for(CallGraphNode::iterator it = callGraphNode->InEdgeBegin(), eit = callGraphNode->InEdgeEnd();
it!=eit; ++it)
{
for(CallGraphEdge::CallInstSet::const_iterator cit = (*it)->directCallsBegin(),
ecit = (*it)->directCallsEnd(); cit!=ecit; ++cit)
{
csSet.insert((*cit));
}
}
}
/*!
* Get indirect callsite invoking this callee
*/
void CallGraph::getIndCallSitesInvokingCallee(const FunObjVar* callee, CallGraphEdge::CallInstSet& csSet)
{
CallGraphNode* callGraphNode = getCallGraphNode(callee);
for(CallGraphNode::iterator it = callGraphNode->InEdgeBegin(), eit = callGraphNode->InEdgeEnd();
it!=eit; ++it)
{
for(CallGraphEdge::CallInstSet::const_iterator cit = (*it)->indirectCallsBegin(),
ecit = (*it)->indirectCallsEnd(); cit!=ecit; ++cit)
{
csSet.insert((*cit));
}
}
}
/*!
* Issue a warning if the function which has indirect call sites can not be reached from program entry.
*/
void CallGraph::verifyCallGraph()
{
if (Options::DisableWarn())
return;
Map<NodeID, bool> reachableFromEntry;
NodeBS visitedNodes;
CallEdgeMap::const_iterator it = indirectCallMap.begin();
CallEdgeMap::const_iterator eit = indirectCallMap.end();
for (; it != eit; ++it)
{
const FunctionSet& targets = it->second;
if (targets.empty() == false)
{
const CallICFGNode* cs = it->first;
const FunObjVar* func = cs->getCaller();
if (getCallGraphNode(func)->
isReachableFromProgEntry(reachableFromEntry, visitedNodes) == false)
writeWrnMsg(func->getName() + " has indirect call site but not reachable from main");
}
}
}
/*!
* Whether its reachable between two functions
*/
bool CallGraph::isReachableBetweenFunctions(const FunObjVar* srcFn, const FunObjVar* dstFn) const
{
CallGraphNode* dstNode = getCallGraphNode(dstFn);
std::stack<const CallGraphNode*> nodeStack;
NodeBS visitedNodes;
nodeStack.push(dstNode);
visitedNodes.set(dstNode->getId());
while (nodeStack.empty() == false)
{
CallGraphNode* node = const_cast<CallGraphNode*>(nodeStack.top());
nodeStack.pop();
if (node->getFunction() == srcFn)
return true;
for (CallGraphEdgeConstIter it = node->InEdgeBegin(), eit = node->InEdgeEnd(); it != eit; ++it)
{
CallGraphEdge* edge = *it;
if (visitedNodes.test_and_set(edge->getSrcID()))
nodeStack.push(edge->getSrcNode());
}
}
return false;
}
/*!
* Dump call graph into dot file
*/
void CallGraph::dump(const std::string& filename)
{
GraphPrinter::WriteGraphToFile(outs(), filename, this);
}
void CallGraph::view()
{
SVF::ViewGraph(this, "Call Graph");
}
/*!
* Add call graph node
*/
void CallGraph::addCallGraphNode(const FunObjVar* fun)
{
NodeID id = callGraphNodeNum;
CallGraphNode*callGraphNode = new CallGraphNode(id, fun);
addCallGraphNode(callGraphNode);
}
void CallGraph::addCallGraphNode(CallGraphNode* cgNode)
{
addGNode(cgNode->getId(), cgNode);
funToCallGraphNodeMap[cgNode->getFunction()] = cgNode;
callGraphNodeNum++;
}
const CallGraphNode* CallGraph::getCallGraphNode(const std::string& name) const
{
for (const auto& item : *this)
{
if (item.second->getName() == name)
return item.second;
}
return nullptr;
}
/*!
* Add direct call edges
*/
void CallGraph::addDirectCallGraphEdge(const CallICFGNode* cs,const FunObjVar* callerFun, const FunObjVar* calleeFun)
{
CallGraphNode* caller = getCallGraphNode(callerFun);
CallGraphNode* callee = getCallGraphNode(calleeFun);
CallSiteID csId = addCallSite(cs, calleeFun);
if(!hasGraphEdge(caller,callee, CallGraphEdge::CallRetEdge, csId))
{
CallGraphEdge* edge = new CallGraphEdge(caller,callee, CallGraphEdge::CallRetEdge, csId);
edge->addDirectCallSite(cs);
addDirectCallGraphEdge(edge);
callinstToCallGraphEdgesMap[cs].insert(edge);
}
}
void CallGraph::addDirectCallGraphEdge(CallGraphEdge* cgEdge)
{
addEdge(cgEdge);
}
namespace SVF
{
/*!
* Write value flow graph into dot file for debugging
*/
template<>
struct DOTGraphTraits<CallGraph*> : public DefaultDOTGraphTraits
{
typedef CallGraphNode NodeType;
typedef NodeType::iterator ChildIteratorType;
DOTGraphTraits(bool isSimple = false) :
DefaultDOTGraphTraits(isSimple)
{
}
/// Return name of the graph
static std::string getGraphName(CallGraph*)
{
return "Call Graph";
}
/// Return function name;
static std::string getNodeLabel(CallGraphNode*node, CallGraph*)
{
return node->toString();
}
static std::string getNodeAttributes(CallGraphNode*node, CallGraph*)
{
const FunObjVar* fun = node->getFunction();
if (!SVFUtil::isExtCall(fun))
{
return "shape=record";
}
else
return "shape=Mrecord";
}
template<class EdgeIter>
static std::string getEdgeAttributes(CallGraphNode*, EdgeIter EI,
CallGraph*)
{
//TODO: mark indirect call of Fork with different color
CallGraphEdge* edge = *(EI.getCurrent());
assert(edge && "No edge found!!");
std::string color;
if (edge->getEdgeKind() == CallGraphEdge::TDJoinEdge)
{
color = "color=green";
}
else if (edge->getEdgeKind() == CallGraphEdge::TDForkEdge)
{
color = "color=blue";
}
else
{
color = "color=black";
}
if (0 != edge->getIndirectCalls().size())
{
color = "color=red";
}
return color;
}
template<class EdgeIter>
static std::string getEdgeSourceLabel(NodeType*, EdgeIter EI)
{
CallGraphEdge* edge = *(EI.getCurrent());
assert(edge && "No edge found!!");
std::string str;
std::stringstream rawstr(str);
rawstr << edge->getCallSiteID();
return rawstr.str();
}
};
} // End namespace llvm