-
Notifications
You must be signed in to change notification settings - Fork 0
/
FuncPtrAnalyse.cpp
508 lines (440 loc) · 12.8 KB
/
FuncPtrAnalyse.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
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
502
503
504
505
506
507
#include <llvm/Support/raw_ostream.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/IntrinsicInst.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/CFG.h>
#include <sstream>
#include <llvm/IR/DebugInfoMetadata.h>
#include "FuncPtrAnalyse.h"
char FuncPtrPass::ID = 0;
bool FuncPtrPass::runOnModule(Module &M)
{
// errs().write_escaped(M.getName()) << '\n';
while(iterate(M));
printCalls(M);
return false;
}
bool FuncPtrPass::iterate(Module &module)
{
auto oldArgsEnv = argsEnv;
auto oldReturned = returned;
auto oldHeap = globalEnvPerFunc;
auto oldDirty = dirtyEnvPerFunc;
for (Function &func: module.getFunctionList())
{
for (BasicBlock &bb: func)
{
Env in, out;
if (&bb == &func.getEntryBlock())
// If instruction is the 1st instruction of a function, pointer set should come from function arguments
in = passArgs(&func);
else
// pointer set comes from predecessors
in = meet(&bb);
for (const auto &it : globalEnvPerFunc[&func])
envUnion(in, it.second);
out = in;
_currEnv = &out;
for (auto &inst: bb)
{
dispatchInst(inst);
}
if (out != envs[&bb])
{
envs[&bb] = out;
}
}
}
// if changed, this function will be invoke again
return argsEnv != oldArgsEnv || returned != oldReturned || globalEnvPerFunc != oldHeap || dirtyEnvPerFunc != oldDirty;
}
FuncPtrPass::Env FuncPtrPass::meet(BasicBlock *bb)
{
Env in;
for (auto *p : predecessors(bb))
{
for (auto pair : envs[p])
{
setUnion(in[pair.first], pair.second);
}
}
return in;
}
bool FuncPtrPass::dispatchInst(Instruction &inst)
{
if (auto casted = dyn_cast<PHINode>(&inst))
return visitPhiNode(casted);
if (auto casted = dyn_cast<CallInst>(&inst))
return visitCall(casted);
if (auto casted = dyn_cast<AllocaInst>(&inst))
return visitAlloc(casted);
if (auto casted = dyn_cast<GetElementPtrInst>(&inst))
return visitGetElementPtr(casted);
if (auto load = dyn_cast<LoadInst>(&inst))
return visitLoad(load);
if (auto store = dyn_cast<StoreInst>(&inst))
return visitStore(store);
if (auto casted = dyn_cast<ReturnInst>(&inst))
return visitReturn(casted);
if (auto bit = dyn_cast<BitCastInst>(&inst))
return visitBitcast(bit);
return false;
}
bool FuncPtrPass::visitPhiNode(PHINode *phiNode)
{
for (unsigned incoming_index = 0, e = phiNode->getNumIncomingValues(); incoming_index != e; incoming_index++)
{
Value *inVal = phiNode->getIncomingValue(incoming_index);
if (dyn_cast<Function>(inVal))
setUnion(currEnv[phiNode], wrappedPtrSet(inVal));
else
setUnion(currEnv[phiNode], currEnv[inVal]);
}
return false;
}
bool FuncPtrPass::visitCall(CallInst *callInst)
{
if (isLLVMBuiltIn(callInst))
{
if (auto copyInst = dyn_cast<MemCpyInst>(callInst))
return visitMemCopy(copyInst);
else
return false;
}
bool updated = false;
FuncPtrSet possible_func_ptr_set;
if (auto *func = callInst->getCalledFunction())
{
possible_func_ptr_set.insert(callInst->getCalledFunction());
if (func->getName() == "malloc")
{
if (currEnv[callInst].empty())
{
Value *p = createAllocValue(callInst);
currEnv[callInst].insert(p);
updated = true;
}
}
}
else
{
auto called_value = callInst->getCalledValue();
possible_func_ptr_set = currEnv[called_value];
}
Env dirtyEnv;
for (auto value: possible_func_ptr_set)
{
auto func = dyn_cast<Function>(value);
if (!func)
continue;
// update call pointer set with return value set
if (callInst->getType()->isPointerTy())
{
auto updated_call_inst = setUnion(currEnv[callInst], returned[func]);
updated |= updated_call_inst;
}
// function callsites and arguments
unsigned arg_index = 0;
for (auto ¶meter: func->args())
{
if (parameter.getType()->isPointerTy())
{
auto arg = callInst->getOperand(arg_index);
argsEnv[func][callInst][¶meter] = wrappedPtrSet(arg);
}
arg_index++;
}
// Pass current env as callee's env.
auto &myOldProvide = globalEnvPerFunc[func][callInst];
if (myOldProvide != currEnv)
{
myOldProvide.clear();
myOldProvide = currEnv;
}
else
{
// Fetch dirty data from this called function
envUnion(dirtyEnv, dirtyEnvPerFunc[func]);
}
}
for (auto &kv : globalEnvPerFunc)
{
if (possible_func_ptr_set.count(kv.first) == 0 && kv.second.count(callInst) != 0)
{
kv.second[callInst].clear();
}
}
updateEnv(currEnv, dirtyEnv);
return updated;
}
Value *FuncPtrPass::createAllocValue(Instruction *alloc)
{
if (allocated.count(alloc))
{
return allocated[alloc];
}
else
{
char name[20];
Value *v = new AllocaInst(IntegerType::get(alloc->getModule()->getContext(), 32), 10, name);
allocated[alloc] = v;
return v;
}
}
bool FuncPtrPass::visitAlloc(AllocaInst *allocaInst)
{
if (currEnv.count(allocaInst) == 0)
{
Value *p = createAllocValue(allocaInst);
currEnv[p];
currEnv[allocaInst].insert(p);
return true;
}
else
{
return false;
}
}
bool FuncPtrPass::visitGetElementPtr(GetElementPtrInst *getElementPtrInst)
{
bool updated = false;
Value *ptr = getElementPtrInst->getOperand(0);
updated = setUnion(currEnv[getElementPtrInst], currEnv[ptr]);
return updated;
}
bool FuncPtrPass::visitLoad(LoadInst *loadInst)
{
bool updated = false;
Value *src = loadInst->getOperand(0);
for (auto p : currEnv[src])
{
updated = setUnion(currEnv[loadInst], currEnv[p]);
}
return updated;
}
bool FuncPtrPass::visitStore(StoreInst *storeInst)
{
bool updated = false;
Value *src = storeInst->getOperand(0);
Value *dst = storeInst->getOperand(1);
for (auto p : currEnv[dst])
{
currEnv[p] = wrappedPtrSet(src);
}
return updated;
}
bool FuncPtrPass::visitReturn(ReturnInst *returnInst)
{
Function *func = returnInst->getParent()->getParent();
Env heap;
for (const auto &it : globalEnvPerFunc[func])
{
envUnion(heap, it.second);
}
bool hasPointerArgs = false;
for (auto &arg : func->args())
{
if (arg.getType()->isPointerTy())
{
hasPointerArgs = true;
break;
}
}
if (hasPointerArgs)
{
if (dirtyEnvPerFunc[func] != currEnv)
{
dirtyEnvPerFunc[func] = currEnv;
}
}
else
{
dirtyEnvPerFunc[func].clear();
}
// Update return value set.
auto value = returnInst->getReturnValue();
if (value == nullptr || !value->getType()->isPointerTy())
{
return false;
}
return setUnion(returned[func], wrappedPtrSet(value));
}
bool FuncPtrPass::visitBitcast(BitCastInst *bitCastInst)
{
Value *ope = bitCastInst->getOperand(0);
return setUnion(currEnv[bitCastInst], currEnv[ope]);
}
FuncPtrPass::Env FuncPtrPass::passArgs(Function *func)
{
Env in;
auto &argsPerCallSite = argsEnv[func];
for (auto &it : argsPerCallSite)
{
for (auto &arg : func->args())
{
if (arg.getType()->isPointerTy())
{
setUnion(in[&arg], it.second[&arg]);
}
}
}
return in;
}
void FuncPtrPass::envUnion(FuncPtrPass::Env &dst, const FuncPtrPass::Env &src)
{
for (auto &pair : src)
{
setUnion(dst[pair.first], pair.second);
}
}
void FuncPtrPass::updateEnv(FuncPtrPass::Env &dst, const FuncPtrPass::Env &src)
{
// For each key in dst, if src has this key, override the content.
for (auto &pair : dst)
{
if (src.count(pair.first))
{
const auto &set = *src.find(pair.first);
if (set.second != pair.second)
{
dst[pair.first] = set.second;
}
}
}
}
bool FuncPtrPass::visitMemCopy(MemCpyInst *copyInst)
{
auto src = copyInst->getSource();
auto dst = copyInst->getDest();
for (auto d: currEnv[dst])
{
currEnv[d].clear();
for (auto s: currEnv[src])
{
setUnion(currEnv[d], wrappedPtrSet(s));
}
}
return false;
}
bool FuncPtrPass::isFunctionPointer(Type *type)
{
if (!type->isPointerTy()) {
return false;
}
auto pointee = type->getPointerElementType();
return pointee->isFunctionTy();
}
FuncPtrPass::FuncPtrSet FuncPtrPass::wrappedPtrSet(Value *value)
{
if (isa<Function>(value)) {
FuncPtrSet fPtrSet;
fPtrSet.insert(value);
return fPtrSet;
} else {
return currEnv[value];
}
}
bool FuncPtrPass::setUnion(FuncPtrPass::FuncPtrSet &dst,
const FuncPtrPass::FuncPtrSet &src)
{
bool updated = false;
for (const auto it: src) {
bool inserted;
std::tie(std::ignore, inserted) = dst.insert(it);
updated |= inserted;
}
return updated;
}
bool FuncPtrPass::isLLVMBuiltIn(CallInst *callInst)
{
return isa<DbgValueInst>(callInst) ||
isa<DbgDeclareInst>(callInst) ||
isa<MemSetInst>(callInst) ||
isa<MemCpyInst>(callInst);
}
void FuncPtrPass::printEnv(FuncPtrPass::Env &env) {
for (auto &pair : env) {
Value *key = pair.first;
FuncPtrSet &set = pair.second;
if (key->getName() == "") {
llvm::errs() << key->getValueID() << ": ";
}
else {
llvm::errs() << key->getName() << ": ";
}
std::stringstream ss;
std::string sep;
ss << "{ ";
for (auto p : set) {
ss << sep << p->getName().str();
sep = ", ";
}
ss << " }\n";
llvm::errs() << ss.str();
}
}
void FuncPtrPass::printSet(const FuncPtrPass::FuncPtrSet &s) {
std::stringstream ss;
std::string sep = "";
ss << "{ ";
for (auto p : s) {
ss << sep << p->getName().str();
sep = ", "; }
ss << " }";
}
void FuncPtrPass::printCalls(Module &module)
{
unsigned last_line = 0;
bool first_in_current_line = true;
bool first_line = true;
for (auto &function: module.getFunctionList()) {
for (auto &bb : function) {
_currEnv = &envs[&bb];
for (auto &inst : bb) {
unsigned null_count = 0, pointer_count = 0;
if (auto callInst = dyn_cast<CallInst>(&inst)) {
if (isLLVMBuiltIn(callInst)) {
continue;
}
auto calledValue = callInst->getCalledValue();
FuncPtrSet possible_func_ptr_set = wrappedPtrSet(calledValue);
MDNode *metadata = callInst->getMetadata(0);
if (!metadata) {
errs() << "No meta data found for " << *callInst;
continue;
}
DILocation *debugLocation = dyn_cast<DILocation>(metadata);
if (!debugLocation) {
errs() << "No debug location found for " << *callInst;
continue;
}
unsigned current_line = debugLocation->getLine();
if (last_line != current_line) {
first_in_current_line = true;
if (!first_line) {
errs() << "\n";
} else {
first_line = false;
}
errs() << current_line << " : ";
}
for (auto value : possible_func_ptr_set) {
auto func = dyn_cast<Function>(value);
if (!func|| func->getName() == "") {
null_count ++;
continue;
}
pointer_count++;
if (!first_in_current_line) {
errs() << ", ";
} else {
first_in_current_line = false;
}
errs() << func->getName();
}
last_line = current_line;
}
}
}
}
}