-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.cpp
465 lines (412 loc) · 12.1 KB
/
common.cpp
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
#include "common.h"
void debugPrint() {}
void printBasicBlock(BasicBlock *B) {
for (Instruction &Ins : *B) {
errs() << Ins.getOpcodeName() << " " << Ins << "\n";
}
}
void printOperand(Instruction *ins) {
errs() << "Operands of " << *ins << "\n";
for (size_t t = 0; t < ins->getNumOperands(); t++) {
errs() << t << ": " << *(ins->getOperand(t)) << "\n";
}
}
void printStrVector(vector<string> *Vec) {
for (string S : *Vec) {
errs() << S << " ";
}
errs() << "\n";
}
void splitStr(string _Str, string Deli, vector<string> *Res) {
if (_Str == "") {
return;
}
string Str = _Str + Deli;
size_t Pos = Str.find(Deli);
int Step = Deli.size();
while (Pos != Str.npos) {
string Temp = Str.substr(0, Pos);
Res->push_back(Temp);
Str = Str.substr(Pos + Step, Str.size());
Pos = Str.find(Deli);
}
}
void printPath(vector<vector<BasicBlock *> *> *Paths) {
for (vector<BasicBlock *> *Path : *Paths) { // single path
errs() << "--- Path ---\n";
for (BasicBlock *Blk : *Path) {
for (Instruction &Ins : *Blk) {
errs() << Ins << "\n";
}
}
}
}
void printSinglePath(vector<BasicBlock *> *Path) {
errs() << "--- Path ---\n";
for (BasicBlock *Blk : *Path) {
for (Instruction &Ins : *Blk) {
errs() << Ins << "\n";
}
}
}
set<Function *> *collectCallee(Function *F) {
set<Function *> *callee_set = new set<Function *>();
if (callee_set == NULL) {
errs() << "Fail to allocate the memory\n";
return NULL;
}
for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
Instruction &Ins = *I;
if (auto CallIns = dyn_cast<CallInst>(&Ins)) {
Function *callee = CallIns->getCalledFunction();
if (callee != NULL) {
callee_set->insert(callee);
} else {
errs() << "Fail to getCalledFunction in collectCallee at " << Ins
<< "\n";
}
}
}
return callee_set;
}
void clearPath(vector<vector<BasicBlock *> *> *path) {
// <BasicBlock *> will be duplicate
vector<uint64_t> freed_addr;
for (size_t i = 0; i != path->size(); i++) {
vector<BasicBlock *> *ele = (*path)[i];
// first, we clean BasicBlock *
for (auto &e : *ele) {
bool free_flag = true;
for (auto addr : freed_addr) {
if (addr == (uint64_t)e) {
free_flag = false;
// errs() << "do not free " << (uint64_t)e << " again\n";
}
}
if (free_flag) {
// delete e; // COMMENT THIS SINCE (delete LLVMCtx;) free this!!
// errs() << "freed " << (uint64_t)e << "\n";
freed_addr.push_back((uint64_t)e);
}
}
delete ele;
}
delete path;
}
// Do not forget to use clearPath on the return value
vector<vector<BasicBlock *> *> *collectPath(Function *F) {
unrollLoops(F);
vector<vector<BasicBlock *> *> *path = new vector<vector<BasicBlock *> *>();
assert(path);
// construct startup, add single entry block
BasicBlock *entry = &F->getEntryBlock();
if (entry == NULL) {
// errs() << "[-] Fail to find entry block\n";
delete path;
return NULL;
}
vector<BasicBlock *> *p = new vector<BasicBlock *>();
assert(p);
p->push_back(entry);
path->push_back(p);
// start iteration until no path has successors
bool flag = true;
int loop = 0, maxloop = 300000;
while (flag) {
flag = false;
for (size_t idx = 0; idx != path->size(); idx++) {
loop++;
if (loop > maxloop) {
// errs() << "[-] Over max loop constraints\n";
goto ERR;
}
vector<BasicBlock *> *cur_path = (*path)[idx];
// access the last instruction of the last block
BasicBlock *last = cur_path->back();
Instruction *last_ins = last->getTerminator();
if (last_ins == NULL) {
// errs() << "[-] Fail to getTerminator\n";
goto ERR;
}
if (last_ins->getNumSuccessors()) {
// the first successor is added to cur_path directly
if (find(cur_path->begin(), cur_path->end(),
last_ins->getSuccessor(0)) == cur_path->end()) {
// not an existing block
cur_path->push_back(last_ins->getSuccessor(0));
flag = true;
}
for (size_t succ_id = 1; succ_id < last_ins->getNumSuccessors();
succ_id++) {
// whether an existing block in current path
if (find(cur_path->begin(), cur_path->end(),
last_ins->getSuccessor(succ_id)) != cur_path->end()) {
continue;
}
vector<BasicBlock *> *expanded_path = new vector<BasicBlock *>();
assert(expanded_path != NULL);
expanded_path->insert(expanded_path->end(), cur_path->begin(),
cur_path->end() - 1);
expanded_path->push_back(last_ins->getSuccessor(succ_id));
path->push_back(expanded_path);
flag = true;
}
}
}
}
// when the above iteration ends, all paths in <path> have no successors
// except back edges
return path;
ERR:
for (size_t st = 0; st < path->size(); st++) {
vector<BasicBlock *> *Vec = path->at(st);
delete Vec;
}
delete path;
return NULL;
}
vector<BasicBlock *> *getLoopHeaders(Function *F) {
// if (F->isDeclaration())
// return;
vector<BasicBlock *> *Headers = new vector<BasicBlock *>();
assert(Headers);
DominatorTree DT = DominatorTree();
DT.recalculate(*F);
LoopInfo *LI = new LoopInfo();
LI->releaseMemory();
LI->analyze(DT);
// Collect all loops in the function
set<Loop *> LPSet;
for (LoopInfo::iterator i = LI->begin(), e = LI->end(); i != e; ++i) {
Loop *LP = *i;
LPSet.insert(LP);
list<Loop *> LPL;
LPL.push_back(LP);
while (!LPL.empty()) {
LP = LPL.front();
LPL.pop_front();
vector<Loop *> SubLPs = LP->getSubLoops();
for (auto SubLP : SubLPs) {
LPSet.insert(SubLP);
LPL.push_back(SubLP);
}
}
}
for (Loop *LP : LPSet) {
// Get the header,latch block, exiting block of every loop
BasicBlock *HeaderB = LP->getHeader();
Headers->push_back(HeaderB);
}
delete LI;
return Headers;
}
// cut from Crix
void unrollLoops(Function *F) {
if (F->isDeclaration())
return;
DominatorTree DT = DominatorTree();
DT.recalculate(*F);
LoopInfo *LI = new LoopInfo();
LI->releaseMemory();
LI->analyze(DT);
// Collect all loops in the function
set<Loop *> LPSet;
for (LoopInfo::iterator i = LI->begin(), e = LI->end(); i != e; ++i) {
Loop *LP = *i;
LPSet.insert(LP);
list<Loop *> LPL;
LPL.push_back(LP);
while (!LPL.empty()) {
LP = LPL.front();
LPL.pop_front();
vector<Loop *> SubLPs = LP->getSubLoops();
for (auto SubLP : SubLPs) {
LPSet.insert(SubLP);
LPL.push_back(SubLP);
}
}
}
for (Loop *LP : LPSet) {
// Get the header,latch block, exiting block of every loop
BasicBlock *HeaderB = LP->getHeader();
unsigned NumBE = LP->getNumBackEdges();
SmallVector<BasicBlock *, 4> LatchBS;
LP->getLoopLatches(LatchBS);
for (BasicBlock *LatchB : LatchBS) {
if (!HeaderB || !LatchB) {
errs() << "[-] ERROR: Cannot find Header Block or Latch Block\n";
continue;
}
// Two cases:
// 1. Latch Block has only one successor:
// for loop or while loop;
// In this case: set the Successor of Latch Block to the
// successor block (out of loop one) of Header block
// 2. Latch Block has two successor:
// do-while loop:
// In this case: set the Successor of Latch Block to the
// another successor block of Latch block
// get the last instruction in the Latch block
Instruction *TI = LatchB->getTerminator();
// Case 1:
if (LatchB->getSingleSuccessor() != NULL) {
for (succ_iterator sit = succ_begin(HeaderB); sit != succ_end(HeaderB);
++sit) {
BasicBlock *SuccB = *sit;
BasicBlockEdge BBE = BasicBlockEdge(HeaderB, SuccB);
// Header block has two successor,
// one edge dominate Latch block;
// another does not.
if (DT.dominates(BBE, LatchB))
continue;
else {
TI->setSuccessor(0, SuccB);
}
}
}
// Case 2:
else {
for (succ_iterator sit = succ_begin(LatchB); sit != succ_end(LatchB);
++sit) {
BasicBlock *SuccB = *sit;
// There will be two successor blocks, one is header
// we need successor to be another
if (SuccB == HeaderB)
continue;
else {
TI->setSuccessor(0, SuccB);
}
}
}
}
}
delete LI;
}
Function *loadFuncFromFile(LLVMContext *Context, string Path, string Name) {
SMDiagnostic Err;
unique_ptr<Module> M = parseIRFile(Path, Err, *Context);
if (M == NULL) {
errs() << "Error loading file: " << Path << "\n";
return NULL;
}
Module *Module = M.release();
Module::FunctionListType &FuncList = Module->getFunctionList();
for (Function &F : FuncList) {
if (F.isIntrinsic() || F.isDeclaration()) {
continue;
}
if (F.getName() == Name) {
return &F;
}
}
return NULL;
}
Function *loadFuncFromFileList(LLVMContext *Context, string Name,
vector<string> *Bitcodes) {
assert(Context && Bitcodes);
for (string B : *Bitcodes) {
Function *F = loadFuncFromFile(Context, B, Name);
if (F) {
return F;
}
}
return NULL;
}
vector<Function *> *loadAllFuncFromFile(LLVMContext *Context, string Path) {
SMDiagnostic Err;
unique_ptr<Module> M;
try {
M = parseIRFile(Path, Err, *Context);
} catch (exception E) {
M = NULL;
}
if (M == NULL) {
errs() << "Error loading file: " << Path << "\n";
return NULL;
}
Module *Module = M.release();
Module::FunctionListType &FuncList = Module->getFunctionList();
vector<Function *> *AllFunc = new vector<Function *>();
assert(AllFunc);
for (Function &F : FuncList) {
if (F.isIntrinsic() || F.isDeclaration()) {
continue;
}
if (F.getName() == "main") {
continue;
}
AllFunc->push_back(&F);
}
return AllFunc;
}
/* collect and return the references of the instruction */
vector<Value *> *collectRefs(Value *Ins) {
if (isa<ConstantInt>(Ins)) {
return new vector<Value *>();
}
vector<Value *> *Refs = new vector<Value *>();
assert(Refs);
Refs->push_back(Ins);
if (LoadInst *LoadIns = dyn_cast<LoadInst>(Ins)) {
Refs->push_back(LoadIns->getOperand(0));
}
bool Flag = true;
int PosStart = 0, PosEnd = Refs->size();
while (Flag) {
Flag = false;
for (int Idx = PosStart; Idx < PosEnd; Idx++) {
// here we set a max loop constraint
if (Idx > 5000) {
errs() << COLOR_RED CONS_TAB CONS_TAB CONS_TAB
"|- ERROR: over the max loop constraint while collecting "
"refs\n" COLOR_CLOSE;
break;
}
Value *V = Refs->at(Idx);
for (User *U : V->users()) {
if (LoadInst *LoadIns = dyn_cast<LoadInst>(U)) {
// if the loaded element is within Refs
Value *Loaded = LoadIns->getOperand(0);
if (find(Refs->begin() + PosStart, Refs->begin() + PosEnd, Loaded) !=
Refs->begin() + PosEnd) {
Refs->push_back(LoadIns);
Flag = true;
}
}
if (StoreInst *StoreIns = dyn_cast<StoreInst>(U)) {
Value *From = StoreIns->getOperand(0);
if (find(Refs->begin() + PosStart, Refs->begin() + PosEnd, From) !=
Refs->begin() + PosEnd) {
Refs->push_back(StoreIns->getOperand(1));
Flag = true;
}
}
}
}
PosStart = PosEnd;
PosEnd = Refs->size();
}
// remove possible duplicated values
set<Value *> S(Refs->begin(), Refs->end());
Refs->erase(Refs->begin(), Refs->end());
Refs->assign(S.begin(), S.end());
return Refs;
}
string getCmpSym(CmpInst::Predicate P) {
switch (P) {
case CmpInst::ICMP_NE:
return "NE";
case CmpInst::ICMP_EQ:
return "EQ";
case CmpInst::ICMP_SGT:
return "SGT";
case CmpInst::ICMP_SLT:
return "SLT";
case CmpInst::ICMP_SGE:
return "SGE";
case CmpInst::ICMP_SLE:
return "SLE";
default:
assert(0);
}
}