-
Notifications
You must be signed in to change notification settings - Fork 1
/
DLCallGraph.h
131 lines (96 loc) · 4.52 KB
/
DLCallGraph.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
#include "tainter.h"
struct DLCallGraphNode{
Function * fun_ptr;
uint calls;
CallBase* callbase_inst;
/*
* TODO:
* CalledFunctionsVector = std::vector< CallRecord >
* using llvm::CallGraphNode::CallRecord = std::pair<Optional<WeakTrackingVH>, CallGraphNode *>
*/
vector<Function *> called_func;
vector<Function *> caller_funcs;
DLCallGraphNode(Function * f, uint c, CallBase* ci){
fun_ptr = f;
calls = c;
callbase_inst = ci;
}
};
struct DLCallGraph{
DLCallGraphNode * enter;
set<DLCallGraphNode *> allDLCallGraphNodes;
vector<Function *> * getCallers(Function * f){
for(set<DLCallGraphNode *>::iterator I = this->allDLCallGraphNodes.begin(); I != this->allDLCallGraphNodes.end(); I++){
if ((*I)->fun_ptr == f)
return &(*I)->caller_funcs;
}
llvm::outs() << "NOT FOUND: " << f->getName().str() << " in the DLCallGraph via `getCallers`.\n";
return nullptr;
}
DLCallGraph(){}
~DLCallGraph(){}
DLCallGraph(CallGraph *CG){
uint remaining_calls = 0;
DLCallGraphNode * cur_parent = nullptr;
queue<DLCallGraphNode *> tobe_parent;
bool checkA = true;
for (auto IT = bf_begin(CG), EI = bf_end(CG); IT != EI; IT++) {
if (Function *F = IT->getFunction()) {
llvm::outs() << "Visiting function: " << getOriginalName(F->getName().str()) << ", callees: " << F->size() << "\n";
//cur_parent = IT;
for (CallGraphNode::iterator calledNode_IT = IT->begin(); calledNode_IT != IT->end(); calledNode_IT++){
if(calledNode_IT->second->getFunction()->hasExternalLinkage()){
continue;
} else {
if(CallBase* cb = llvm::dyn_cast<CallBase>(calledNode_IT->first)) {
DLCallGraphNode* newnode = new DLCallGraphNode(calledNode_IT->second->getFunction(), calledNode_IT->second->size(), cb);
cur_parent->called_func.push_back(newnode->fun_ptr);
if(cur_parent->fun_ptr) // when cur_parent == this->enter, cur_parent->fun_ptr is nullptr
newnode->caller_funcs.push_back(cur_parent->fun_ptr);
this->allDLCallGraphNodes.insert(newnode);
} else {
llvm::outs() << "STRAGE. NOT a CallBase for the calledNode_IT->first" <<getClassType(calledNode_IT->first) << "\n";
}
}
}
} else {
llvm::outs() << "Visiting function: nullptr. This is usually the root node of the CallGraph.\n";
this->enter = new DLCallGraphNode(nullptr, IT->size(), nullptr);
cur_parent = this->enter;
continue;
}
/*
if (!checkA) {
llvm::outs() << "This should be very wrong, since CallGraph traversing is not done but tobe_parent is empty.\n";
}
// only this branch in first iteration.
if (this->enter == nullptr){
this->enter = new DLCallGraphNode(IT->getFunction(), IT->size());
this->allDLCallGraphNodes.insert(this->enter);
cur_parent = this->enter;
remaining_calls = cur_parent->calls;
continue;
}
// the last sub-parent-son-tree has all been visited.
if (remaining_calls == 0){
if (tobe_parent.empty()) {
checkA = false;
llvm::outs() << "This should be the last node when building DLCallGraph\n";
continue;
}
cur_parent = tobe_parent.front();
tobe_parent.pop();
remaining_calls = cur_parent->calls;
} else {
DLCallGraphNode * cur = new DLCallGraphNode(IT->getFunction(), IT->size());
this->allDLCallGraphNodes.insert(cur);
tobe_parent.push(cur);
// Double-linked.
cur->caller_funcs.push_back(cur_parent->fun_ptr);
cur_parent->called_func.push_back(cur->fun_ptr);
remaining_calls--;
}
*/
}
}
};