-
Notifications
You must be signed in to change notification settings - Fork 2
/
Utils.cpp
155 lines (138 loc) · 3.32 KB
/
Utils.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
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/DebugLoc.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/raw_ostream.h"
#include "Utils.h"
using namespace llvm;
using namespace std;
string getName(Value *value, bool shortName)
{
string tmp;
raw_string_ostream ss(tmp);
ss << *value;
string longName = ss.str();
if (!shortName)
{
return longName;
}
// Some Instructions contain '='
size_t idx = longName.find(" = ");
if (idx != string::npos)
{
string shortName = longName.substr(0, idx);
return shortName;
}
else
{
return longName;
}
}
bool overlap(int64_t start1, int64_t end1, int64_t start2, int64_t end2)
{
if (end1 == -1)
{
end1 = start1 + 10000; // TODO
}
if (end2 == -1)
{
end2 = start2 + 10000; // TODO
}
return start1 < end2 && start2 < end1;
}
string trimName(string name)
{
size_t nd = name.rfind("."), t = 0;
if (nd != std::string::npos)
{
std::string suffix = name.substr(nd + 1);
try
{
std::stoi(suffix, &t);
}
catch (...)
{
t = 0;
}
if (t >= suffix.size())
{
name.erase(nd);
}
}
return name;
}
vector<Instruction *> getAllInstsAtSrcLine(llvm::Function *F, string filename, uint32_t line)
{
vector<Instruction *> insts;
for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
llvm::DebugLoc dbgloc = (*I).getDebugLoc();
// The instruction has DebugLoc
if(dbgloc)
{
string fn = dbgloc->getFilename().str();
uint32_t ln = dbgloc->getLine();
// match file name and line
if(fn == filename && ln == line)
{
insts.push_back(&(*I));
}
}
}
return insts;
}
tuple<string, uint32_t> getInstFileAndLine(llvm::Instruction *I)
{
llvm::DebugLoc dbgloc = I->getDebugLoc();
if (dbgloc)
{
string fn = dbgloc->getFilename().str();
uint32_t ln = dbgloc->getLine();
return make_tuple(fn, ln);
}
return make_tuple(string(), 0);
}
vector<string> tokenize(string s, string del)
{
vector<string> splits;
int start = 0;
int end = s.find(del);
while (end != -1)
{
splits.push_back(s.substr(start, end - start));
start = end + del.size();
end = s.find(del, start);
}
splits.push_back(s.substr(start, end - start));
return splits;
}
vector<unsigned> findAllOccurrences(string str, char c)
{
vector<unsigned> occurrences;
for (unsigned i = 0; i < str.size(); i++)
{
if (str[i] == c)
{
occurrences.push_back(i);
}
}
return occurrences;
}
bool storesSensinfo(llvm::StoreInst *storeInst)
{
Value *value = storeInst->getValueOperand();
if (isa<ConstantPointerNull>(value)) // nullptr is not sensitive info
{
return false;
}
return value->getType()->isPointerTy();
}
Function *getCalledFunction(CallInst *CI)
{
Function *calledFunc = CI->getCalledFunction();
if(!calledFunc)
{
Value *calledVal = CI->getCalledOperand();
calledFunc = dyn_cast<Function>(calledVal->stripPointerCasts());
}
return calledFunc;
}