-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcall.cpp
53 lines (39 loc) · 1.38 KB
/
call.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
#include "call.h"
#include <llvm/IR/CallSite.h>
#include <llvm/IR/Constants.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/Instruction.h>
#include <llvm/IR/BasicBlock.h>
#include <llvm/IR/Function.h>
#include <llvm/IR/Instructions.h>
#include <llvm/IR/InstIterator.h>
#include <llvm/IR/Module.h>
#include <llvm/IRReader/IRReader.h>
#include <llvm/Support/raw_ostream.h>
#include <unordered_set>
using namespace llvm;
const bool DEBUG = false;
typedef std::unordered_set<Function*> FunctionSetTy;
bool mayCall(Function *src, Function *tgt, Function *ign, FunctionSetTy& analyzed) {
if (analyzed.find(src) != analyzed.end()) return false;
analyzed.insert(src);
if (src == tgt || src == ign) return false; // do not analyze tgt further
for(Function::iterator bb = src->begin(), bbe = src->end(); bb != bbe; ++bb) {
for(BasicBlock::iterator in = bb->begin(), ine = bb->end(); in != ine; ++in) {
Value *v = &*in;
CallSite cs(v);
if (!cs) continue;
Function *curtgt = cs.getCalledFunction();
if (!curtgt) continue;
if (curtgt == tgt || mayCall(curtgt, tgt, ign, analyzed)) {
errs() << " " << curtgt->getName() << " calls " << tgt->getName() << "\n";
return true;
}
}
}
return false;
}
bool mayCall(Function *src, Function *tgt, Function *ign) {
FunctionSetTy analyzed;
return mayCall(src, tgt, ign, analyzed);
}