-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathplugin.cc
325 lines (293 loc) · 14.1 KB
/
plugin.cc
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
#include <unordered_map>
#include <vector>
#include "llvm/IR/CFG.h"
#include "llvm/IR/CallSite.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Instructions.h"
#include "llvm/Pass.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/JSON.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
using namespace llvm;
static cl::opt<std::string>
VisFilename("fvisfuzz-export",
cl::desc("Specify output filename for VisFuzz"),
cl::value_desc("filename"));
namespace {
struct VisBasicBlock {
unsigned Line;
unsigned LineEnd;
unsigned Function;
std::vector<unsigned> Successors;
VisBasicBlock(BasicBlock &BB, unsigned Function)
: Line(-1u), LineEnd(0), Function(Function) {
for (auto &Inst : BB) {
if (auto Loc = Inst.getDebugLoc()) {
if (Loc.getLine() == 0)
continue;
Line = std::min(Line, Loc.getLine());
LineEnd = std::max(LineEnd, Loc.getLine());
}
}
// Restore to 0 when debug info is not available
if (Line == -1u)
Line = 0;
}
};
struct VisFunction {
StringRef Name;
StringRef Filename;
unsigned Line;
unsigned LineEnd;
unsigned EntryBB;
unsigned BBSum;
std::vector<unsigned> Calls;
std::vector<unsigned> Refs;
VisFunction(Function &F)
: Line(0), LineEnd(0), EntryBB(-1u), BBSum(0u) {
Name = F.getName();
auto Subprog = F.getSubprogram();
if (Subprog) {
Name = Subprog->getName();
Filename = Subprog->getFilename();
Line = Subprog->getLine();
}
for (auto &BB : F) {
auto end = VisBasicBlock(BB, 0).LineEnd;
LineEnd = std::max(end, LineEnd);
}
}
};
class VisFuzz : public FunctionPass {
public:
static char ID;
VisFuzz() : FunctionPass(ID) {}
~VisFuzz() {
vector<unsigned> visited(BasicBlocks.size());
for (int i = 0; i < Functions.size(); i++) {
if (Functions[i].EntryBB != -1u) {
std::fill(visited.begin(), visited.end(), 0);
Functions[i].BBSum = CalculateBBSum(
visited, Functions[i].EntryBB);
}
}
auto FunctionEntries = json::Array();
auto FunctionInserter = std::back_inserter(FunctionEntries);
std::transform(Functions.begin(), Functions.end(),
FunctionInserter, [](VisFunction &F) {
auto CallEntries = json::Array();
auto CallEntryInserter =
std::back_inserter(CallEntries);
std::copy(F.Calls.begin(), F.Calls.end(),
CallEntryInserter);
auto RefEntries = json::Array();
auto RefEntryInserter =
std::back_inserter(RefEntries);
std::copy(F.Refs.begin(), F.Refs.end(),
RefEntryInserter);
auto FEntry = json::Object();
FEntry["name"] = F.Name;
FEntry["block_sum"] = F.BBSum;
FEntry["calls"] = std::move(CallEntries);
FEntry["refs"] = std::move(RefEntries);
if (F.Line != 0)
FEntry["line"] = F.Line;
if (F.LineEnd != 0)
FEntry["line_end"] = F.LineEnd;
if (F.EntryBB != -1u)
FEntry["entry_block"] =
std::move(F.EntryBB);
if (!F.Filename.empty())
FEntry["filename"] = F.Filename;
return json::Value(std::move(FEntry));
});
auto BBEntries = json::Array();
auto BBEntryInserter = std::back_inserter(BBEntries);
std::transform(BasicBlocks.begin(), BasicBlocks.end(),
BBEntryInserter, [](VisBasicBlock &BB) {
auto SuccessorEntries = json::Array();
auto SuccessorEntryInserter =
std::back_inserter(SuccessorEntries);
std::copy(BB.Successors.begin(),
BB.Successors.end(),
SuccessorEntryInserter);
auto BBEntry = json::Object();
if (BB.Line != 0)
BBEntry["line"] = BB.Line;
if (BB.LineEnd != 0)
BBEntry["line_end"] = BB.LineEnd;
if (BB.Function != -1u)
BBEntry["function"] =
BB.Function;
BBEntry["successors"] =
std::move(SuccessorEntries);
return BBEntry;
});
auto RootObject = json::Object();
RootObject["basic_blocks"] = std::move(BBEntries);
RootObject["functions"] = std::move(FunctionEntries);
auto Json = json::Value(std::move(RootObject));
if (VisFilename.empty()) {
errs() << formatv("{0:2}", Json) << '\n';
} else {
auto ec = std::error_code();
raw_fd_ostream file(VisFilename, ec);
if (file.has_error()) {
errs() << "Can't open file " << VisFilename;
return;
}
file << Json;
}
}
private:
std::vector<VisFunction> Functions;
std::vector<VisBasicBlock> BasicBlocks;
std::unordered_map<BasicBlock *, unsigned> BasicBlockMap;
std::unordered_map<Function *, unsigned> FunctionMap;
GlobalVariable *AFLMapPtr;
unsigned NoSanitize;
unsigned GenerateFunctionSummary(Function &F) {
auto it = FunctionMap.find(&F);
if (it == FunctionMap.end()) {
auto n = Functions.size();
Functions.push_back(VisFunction(F));
FunctionMap[&F] = n;
return n;
} else {
return it->second;
}
}
unsigned CalculateBBSum(vector<unsigned> &visited, unsigned BB) {
unsigned count = 0;
visited[BB] = 1;
queue<unsigned> q;
q.push(BB);
while (!q.empty()) {
auto nowBB = q.front();
q.pop();
count++;
for (auto i : BasicBlocks[nowBB].Successors) {
if (!visited[i]) {
q.push(i);
visited[i] = 1;
}
}
}
return count;
}
void GenerateCalls(Function &F, unsigned ThisIndex) {
for (auto &BB : F) {
for (auto &I : BB.instructionsWithoutDebug()) {
if (auto CS = CallSite(&I)) {
// Direct call: save to
// "Calls"
if (auto Callee =
CS.getCalledFunction()) {
auto ThatIndex =
GenerateFunctionSummary(
*Callee);
Functions[ThisIndex]
.Calls.push_back(ThatIndex);
}
} else {
// Involved in operands: save
// to "Refs"
for (auto Op : I.operand_values()) {
if (auto *Callee =
dyn_cast<Function>(
Op)) {
auto ThatIndex =
GenerateFunctionSummary(
*Callee);
Functions[ThisIndex]
.Refs.push_back(
ThatIndex);
}
}
}
}
}
}
unsigned GenerateCFG(Function &F, unsigned ThisIndex) {
auto &EntryBB = F.getEntryBlock();
auto EntryBBIndex = BasicBlocks.size();
BasicBlocks.emplace_back(EntryBB, ThisIndex);
auto BBPtrs = std::vector<BasicBlock *>{&EntryBB};
BasicBlockMap[&EntryBB] = EntryBBIndex;
for (auto I = 0u; I < BBPtrs.size(); ++I) {
auto BB = BBPtrs[I];
auto BBIndex = BasicBlockMap[BB];
for (auto Successor : successors(BB)) {
auto It = BasicBlockMap.find(Successor);
auto SuccessorIndex = -1u;
if (It == BasicBlockMap.end()) {
SuccessorIndex = BasicBlocks.size();
BBPtrs.push_back(Successor);
BasicBlocks.emplace_back(*Successor,
ThisIndex);
BasicBlockMap[Successor] =
SuccessorIndex;
} else {
SuccessorIndex = It->second;
}
BasicBlocks[BBIndex].Successors.emplace_back(
SuccessorIndex);
}
}
return EntryBBIndex;
}
void SetNoSanitize(Instruction *I) {
auto &C = I->getContext();
I->setMetadata(NoSanitize, MDNode::get(C, None));
}
void Instrument(Function &F) {
auto &C = F.getContext();
auto Int16Ty = IntegerType::getInt16Ty(C);
auto Int32Ty = IntegerType::getInt32Ty(C);
for (auto &BB : F) {
auto BBIndex = BasicBlockMap[&BB];
auto IRB = IRBuilder<>(&(*BB.getFirstInsertionPt()));
auto MapPtr = IRB.CreateLoad(AFLMapPtr);
auto MapPtrIdx = IRB.CreateGEP(
MapPtr, ConstantInt::get(Int32Ty, BBIndex));
auto Counter = IRB.CreateLoad(MapPtrIdx);
auto Incr = IRB.CreateAdd(Counter,
ConstantInt::get(Int16Ty, 1));
SetNoSanitize(Counter);
auto Store = IRB.CreateStore(Incr, MapPtrIdx);
SetNoSanitize(Store);
}
}
protected:
bool doInitialization(Module &M) override {
auto &C = M.getContext();
auto Int16Ty = IntegerType::getInt16Ty(C);
if (!AFLMapPtr)
delete AFLMapPtr;
AFLMapPtr = new GlobalVariable(
M, PointerType::get(Int16Ty, 0), false,
GlobalValue::ExternalLinkage, 0, "__afl_area_ptr");
NoSanitize = C.getMDKindID("nosanitize");
return false;
}
bool runOnFunction(Function &F) override {
auto I = GenerateFunctionSummary(F);
GenerateCalls(F, I);
auto EntryBB = GenerateCFG(F, I);
Functions[I].EntryBB = EntryBB;
Instrument(F);
return true;
}
};
} // namespace
char VisFuzz::ID = 0;
static RegisterPass<VisFuzz> X("visfuzz", "VisFuzz Pass",
false /* Only looks at CFG */,
false /* Analysis Pass */);