-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathaccess_instrument.cpp
More file actions
726 lines (607 loc) · 24.7 KB
/
access_instrument.cpp
File metadata and controls
726 lines (607 loc) · 24.7 KB
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
#include "Util.hpp"
#include "llvm/IR/Module.h"
#include "llvm/Pass.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Value.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/Support/Dwarf.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/DebugInfo.h"
#include "llvm/ADT/APInt.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
#include <llvm/IRReader/IRReader.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/Support/SourceMgr.h>
#include <llvm/ADT/SmallVector.h>
#include "json11.hpp"
#include "IdMap.hpp"
#include "AllocDefs.hpp"
#include "MetadataCrawler.hpp"
#include "LogFunctionManager.hpp"
#include "InstrumentationFilter.hpp"
#include <iostream>
#include <fstream>
#include <sstream>
#include <map>
#include <set>
#include <vector>
#define TRACELINE() cerr << __LINE__ << endl;
#define CHECK_TAG(x, tag) (((x) & (tag)) == (tag))
//#define INST_ALLOC_ONLY 1
#define DEBUG_PRINT 1
using namespace llvm;
using namespace std;
enum fn_events {
FN_BEGIN, FN_END
};
namespace {
struct AccessInstrumentationPass : public ModulePass {
static char ID;
IdMap srcmap;
IdMap typemap;
IdMap varmap;
IdMap fnmap;
AllocDefManager adm;
MetadataCrawler mdc;
LogFunctionManager lfm;
InstrumentationFilter insfilt;
set<Value *> argLogSet;
Function *currentFunction;
typedef struct _SourceLoc {
int fileId;
int line;
int col;
} SourceLoc;
SourceLoc getSourceLoc(Instruction *i) {
SourceLoc retval;
int line = -1;
int col = -1;
int fileid = -1;
StringRef file("");
StringRef dir("");
if (MDNode *N = i->getMetadata("dbg")) { // Here I is an LLVM instruction
DILocation loc(N); // DILocation is in DebugInfo.h
line = loc.getLineNumber();
col = loc.getColumnNumber();
file = loc.getFilename();
dir = loc.getDirectory();
fileid = srcmap.getId(dir.str() + "/" + file.str());
#ifdef DEBUG_PRINT
cerr << dir.str() << " " << file.str() << ":" << line << ":" << col << endl;
#endif
}
retval.fileId = fileid;
retval.line = line;
retval.col = col;
return retval;
}
Function * loadExternalFunction(Module *m, Module *extM, const char *name) {
Function *fn = extM->getFunction(name);
FunctionType *ft = fn->getFunctionType();
Function *newFn = Function::Create(ft, Function::ExternalWeakLinkage, name, m);
return newFn;
}
AccessInstrumentationPass() : ModulePass(ID), srcmap("map_sources.json"), typemap("map_types.json"), varmap("map_variables.json"), fnmap("map_functions.json") {}
Constant *getConstantFromInt(int val, Type *t) {
APInt ai(32, val, true);
return Constant::getIntegerValue(t, ai);
}
string getGEPType(GetElementPtrInst *gep) {
Value *v_src = gep->getOperand(0);
Type *t_src = v_src->getType();
string prefix;
string end;
if (GetElementPtrInst *gepv = dyn_cast<GetElementPtrInst>(v_src)) {
prefix.assign(getGEPType(gepv));
}
if (t_src->isPointerTy()) {
t_src = t_src->getPointerElementType();
if (t_src->isStructTy()) {
if (StructType *st = (StructType *)(t_src)) {
if (st->isLiteral()) {
// This seems to be a bug in llvm. hasName() will return false, but sometimes it's possible to get the name, and sometimes it breaks :(
//end.assign("");
end.assign("literal");
} else {
end.assign(t_src->getStructName().str());
}
}
}
}
if (end.find("class.") == 0) {
end.erase(0, 6);
}
if (end.find("struct.") == 0) {
end.erase(0, 7);
}
if (!prefix.empty()) {
return prefix + std::string(".") + end;
} else {
return end;
}
}
string getValueType(Value *v) {
std::string t_string;
llvm::raw_string_ostream rso(t_string);
v->getType()->print(rso);
std::string result(rso.str());
if (result.substr(0,7).compare("%struct") == 0) {
result = result.substr(8);
}
return result;
}
StringRef getGEPVarName(GetElementPtrInst *gep) {
unsigned long fieldIndex;
if (gep->getNumOperands() < 3) {
fieldIndex = -1;
} else {
Value *fiV = gep->getOperand(2);
if (ConstantInt *ciV = dyn_cast<ConstantInt> (fiV)) {
fieldIndex = ciV->getSExtValue();
}
}
Value *v_src = gep->getOperand(0);
Type *t_src = v_src->getType();
string parent;
if (t_src->isPointerTy()) {
t_src = t_src->getPointerElementType();
if (t_src->isStructTy()) {
if (StructType *st = (StructType*)(t_src)) {
if (st->isLiteral()) {
parent.assign("literal");
} else {
parent.assign(t_src->getStructName().str());
}
}
}
}
if (parent.find("class.") == 0) {
parent.erase(0, 6);
}
if (parent.find("struct.") == 0) {
parent.erase(0, 7);
}
if (parent.find("::") != string::npos) {
int dblcolon = parent.find("::");
parent.erase(0, dblcolon+2);
}
string fieldName = mdc.getFieldName(parent, fieldIndex);
parent.append(".");
parent.append(fieldName);
if ((parent.length() > 5) && (parent.substr(parent.length()-5,
parent.length()).compare(".addr") == 0)) {
parent.erase(parent.length()-5, parent.length());
}
return parent;
}
string getVarName(Value *v) {
if (v->getName().str().compare("this.addr") == 0) {
string thisclass(getValueType(v));
if (thisclass.find("%class.") == 0) {
thisclass.erase(0, 7);
}
if (thisclass.find("%struct.") == 0) {
thisclass.erase(0, 8);
}
while (thisclass.find("*") != string::npos) {
thisclass.erase(thisclass.find("*") ,1);
}
thisclass.append(".this");
return thisclass;
}
if (GetElementPtrInst *gepv = dyn_cast<GetElementPtrInst>(v)) {
return getGEPVarName(gepv);
}
string base("");
if (dyn_cast<GlobalVariable>(v)) {
base.assign("<global>.");
} else {
//return "<function_local>";
base = demangle(currentFunction->getName().str().c_str());
if (base.find("(") == string::npos) {
base.append("()");
} else {
base.erase(base.find("("), base.size()-1);
base.append("()");
}
base.append(".");
}
if (v->getName().str().size() == 0) {
base.append("<NA>");
}
base.append(v->getName().str());
if ((base.length() > 5) && (base.substr(base.length()-5, base.length()).compare(".addr") == 0)) {
base.erase(base.length()-5, base.length());
}
return base;
}
void instrumentAccess(Instruction *si, char accessType) {
int line = -1;
int col = -1;
int fileid = -1;
int tid = -1;
int varid = -1;
StringRef file("");
StringRef dir("");
Function *afunc;
if (MDNode *N = si->getMetadata("dbg")) {
DILocation loc(N);
line = loc.getLineNumber();
col = loc.getColumnNumber();
file = loc.getFilename();
dir = loc.getDirectory();
fileid = srcmap.getId(dir.str() + "/" + file.str());
#ifdef DEBUG_PRINT
cerr << dir.str() << " " << file.str() << ":" << line << ":" << col << endl;
#endif
}
IRBuilder<> Builder(si);
std::vector<Value *> args;
// for (auto op = si->op_begin(); op != si->op_end(); op++) {
{
Value *accessedValue;
if ((accessType == 'w') || (accessType == 'a')) {
accessedValue = si->op_begin()->get();
} else {
accessedValue = Builder.CreateLoad(si->op_begin()->get());
}
#ifdef DEBUG_PRINT
{
std::string s;
llvm::raw_string_ostream rso(s);
si->print(rso);
cerr << "Instruction: " << s << endl;
}
#endif
afunc = lfm.getLogFunction(accessedValue, si->getParent()->getParent());
if (afunc != NULL) {
#ifdef DEBUG_PRINT
cerr << afunc->getName().str() << endl;
#endif
auto op = si->op_end() - 1; // destination
Value *v = op->get();
string fullType(getValueType(v));
string varName(getVarName(v));
#ifdef DEBUG_PRINT
cerr << "DEBUG Var name: " << varName << " Insn: ";
si->dump();
cerr << endl;
#endif
tid = typemap.getId(fullType);
varid = varmap.getId(varName);
Value *destPtrVal = op->get();
Type *destType = afunc->getFunctionType()->getParamType(0);
#ifdef DEBUG_PRINT
cerr << "\t" << op->get()->getName().data() << endl;
#endif
Value *address = Builder.CreateCast(Instruction::BitCast, destPtrVal, destType);
args.push_back(address);
Value *castValue;
Type *valueType = afunc->getFunctionType()->getParamType(1);
if (accessedValue->getType()->isFloatingPointTy()) {
castValue = Builder.CreateFPCast(accessedValue, valueType);
} else if (accessedValue->getType()->getScalarSizeInBits() == 1) {
castValue = Builder.CreateZExtOrBitCast(accessedValue, valueType);
} else {
if (Constant *c = dyn_cast<Constant>(accessedValue)) {
castValue = ConstantExpr::getBitCast(c, valueType);
} else {
castValue = Builder.CreateBitCast(accessedValue, valueType);
}
}
if (castValue->getType() != valueType) {
cerr << "BAD CAST" << endl;
}
args.push_back(castValue);
args.push_back(getConstantFromInt(accessType, afunc->getFunctionType()->getParamType(2)));
args.push_back(getConstantFromInt(fileid, afunc->getFunctionType()->getParamType(3)));
args.push_back(getConstantFromInt(line, afunc->getFunctionType()->getParamType(4)));
args.push_back(getConstantFromInt(col, afunc->getFunctionType()->getParamType(5)));
args.push_back(getConstantFromInt(tid, afunc->getFunctionType()->getParamType(6)));
args.push_back(getConstantFromInt(varid, afunc->getFunctionType()->getParamType(7)));
Builder.CreateCall(afunc, args);
}
}
}
void instrumentAlloc(CallInst *ci) {
Function *fn = ci->getCalledFunction();
if (fn == NULL) {
return;
}
#ifdef DEBUG_PRINT
{
std::string s;
llvm::raw_string_ostream rso(s);
ci->print(rso);
cerr << "Instruction: " << s << endl;
}
#endif
if (AllocDefinition *adef = adm.getAllocDef(fn)) {
#ifdef DEBUG_PRINT
cerr << "=========================================" << endl;
cerr << "alloc call: " << adef->name << " " << adef->sizeIdx << " " << adef->numIdx << " " << adef->addrIdx << endl;
cerr << "=========================================" << endl;
#endif
BasicBlock::iterator insertionPoint = ci;
insertionPoint++;
Instruction *nextInst = insertionPoint;
string allocType;
if (BitCastInst *bci = dyn_cast<BitCastInst>(nextInst)) {
allocType = getValueType(bci);
} else {
allocType = "void";
}
int typeId = typemap.getId(allocType);
SourceLoc srcLoc = getSourceLoc(ci);
IRBuilder<> Builder(insertionPoint);
std::vector<Value *> args;
Type *addrType = lfm.allocLogFunc->getFunctionType()->getParamType(0);
Value *addrVal = ci; // get actual value
Value *castAddr = Builder.CreateBitCast(addrVal, addrType);
Type *sizeType = lfm.allocLogFunc->getFunctionType()->getParamType(1);
Value *size = ci->getArgOperand(adef->sizeIdx);
Type *numType = lfm.allocLogFunc->getFunctionType()->getParamType(2);
Value *num;
if (adef->numIdx != -1) {
num = ci->getArgOperand(adef->numIdx);
} else {
num = getConstantFromInt(1, numType);
}
#ifdef DEBUG_PRINT
printType(sizeType);
printType(size->getType());
#endif
Value *castSize = Builder.CreateZExtOrBitCast(size, sizeType);
Value *castNum = Builder.CreateZExtOrBitCast(num, numType);
args.push_back(castAddr);
args.push_back(castSize);
args.push_back(castNum);
args.push_back(getConstantFromInt(typeId, lfm.allocLogFunc->getFunctionType()->getParamType(2)));
args.push_back(getConstantFromInt(srcLoc.fileId, lfm.allocLogFunc->getFunctionType()->getParamType(3)));
args.push_back(getConstantFromInt(srcLoc.line, lfm.allocLogFunc->getFunctionType()->getParamType(4)));
args.push_back(getConstantFromInt(srcLoc.col, lfm.allocLogFunc->getFunctionType()->getParamType(5)));
Builder.CreateCall(lfm.allocLogFunc, args);
}
}
void instrumentFnEvent(Instruction *i, int event) {
IRBuilder<> Builder(i);
std::vector<Value *> args;
Function *f = i->getParent()->getParent();
Function *lfunc;
if (f->getName().str().substr(0, 8).compare("_GLOBAL_") == 0) {
if (event == FN_BEGIN) {
lfunc = lfm.initLogFunc;
} else {
lfunc = lfm.exitLogFunc;
}
} else
if (f->getName().str().compare("main") == 0) {
if (event == FN_BEGIN) {
lfunc = lfm.initLogFunc;
} else {
lfunc = lfm.exitLogFunc;
}
} else {
#ifdef INST_ALLOC_ONLY
return;
#endif
if (event == FN_BEGIN) {
lfunc = lfm.fnBeginLogFunc;
} else {
lfunc = lfm.fnEndLogFunc;
}
}
int fnid = fnmap.getId(demangle(f->getName().str().c_str()));
args.push_back(getConstantFromInt(fnid, lfunc->getFunctionType()->getParamType(0)));
Builder.CreateCall(lfunc, args);
}
void instrumentExit(CallInst *ci) {
IRBuilder<> Builder(ci);
std::vector<Value *> args;
Function *f = ci->getCalledFunction();
if (f == NULL) {
return;
}
if (f->getName().str().compare("exit") == 0) {
args.push_back(getConstantFromInt(-1, lfm.exitLogFunc->getFunctionType()->getParamType(0)));
Builder.CreateCall(lfm.exitLogFunc, args);
}
}
bool checkAndConsumeArg(StoreInst *si) {
Value *argCandidate = si->op_begin()->get();
#ifdef DEBUG_PRINT
cerr << "========================================" << endl;
cerr << "Check and consume" << endl;
cerr << "========================================" << endl;
si->dump(); cerr << endl;
cerr << " Candidate " << hex << argCandidate << endl;
for (auto it = argLogSet.begin(); it != argLogSet.end(); it++) {
cerr << hex << *it << endl;
}
cerr << dec;
cerr << "========================================" << endl;
#endif
if (argLogSet.count(argCandidate) != 0) {
argLogSet.erase(argCandidate);
#ifdef DEBUG_PRINT
cerr << "ARGLOG HIT" << endl;
#endif
return true;
}
return false;
}
void queueAndInjectArgsToLog(Function *f) {
if (f->empty()) { return; }
string fname = f->getName().str();
Instruction *first = f->getEntryBlock().getFirstInsertionPt();
IRBuilder<> ArgLoadBuilder(first);
int idx = 0;
for (auto it = f->arg_begin(); it != f->arg_end(); it++) {
if (insfilt.checkFunctionArgFilter(fname, idx)) {
argLogSet.insert(it);
Value *destPtr = ArgLoadBuilder.CreateAlloca(it->getType());
Value *storeInst = ArgLoadBuilder.CreateStore(it, destPtr);
(void)storeInst;
}
idx++;
}
}
virtual bool runOnModule(Module &m) {
insfilt.loadFilterDataEnv();
if (!insfilt.checkFileFilter(m.getModuleIdentifier())) {
return false;
}
adm.loadAllocDefs();
lfm.loadFunctions(&m);
cerr << "Generating class field maps... ";
mdc.crawlModule(m);
cerr << "done!" << endl;
for (Function &f : m) {
if (!lfm.isLogFunction(&f)) { // TODO: remove and test, should work
//string fname = demangle(f.getName().str().c_str());
size_t fnSize;
fn_size_metrics metric = insfilt.getFunctionSizeMetric();
if (metric == FN_SIZE_IR) {
fnSize = f.size();
} else if (metric == FN_SIZE_LOC) {
fnSize = getFunctionLOCSize(f);
} else { /* FN_SIZE_PATH */
fnSize = longestPathSize(f);
/* If the function has loops and the loop
* check is on, we want to keep functions
* with loops regardless of their size.
* Adjust the size of the function to
* a very large number to reflect that
* setting.
*/
if (checkFunctionLoops(f) &&
insfilt.loopCheckEnabled()) {
fnSize = (size_t) ((uint32_t)~0);
#ifdef DEBUG_PRINT
cerr << "Loop check enabled: " <<
"size set to " << fnSize
<< endl;
#endif
}
}
#ifdef DEBUG_PRINT
cerr << f.getName().str() << ": " << fnSize
<< ", metric is ";
if (metric == FN_SIZE_LOC)
cerr << "LOC";
else if (metric == FN_SIZE_IR)
cerr << "IR";
else
cerr << "LOC_PATH";
cerr << endl;
#endif
if (!insfilt.checkFunctionSize(f.getName().str(), fnSize))
continue;
bool accessFilter = insfilt.checkFunctionFilter(
f.getName().str(), "access");
bool functionFilter = insfilt.checkFunctionFilter(
f.getName().str(), "function");
bool allocFilter = insfilt.checkFunctionFilter(
f.getName().str(), "alloc");
#ifdef DEBUG_PRINT
cerr << "functionFilter for " << f.getName().str() << " is "
<< functionFilter << ".";
if (functionFilter)
cerr << "Instrumenting...";
else
cerr << "Not instrumenting...";
cerr << endl;
#endif
currentFunction = &f;
//TODO: fill this with functionality:
if (functionFilter) {
queueAndInjectArgsToLog(&f);
}
if (!f.empty()) {
BasicBlock &entryBlock = f.getEntryBlock();
Instruction *first = entryBlock.getFirstInsertionPt();
if ((functionFilter) || (f.getName().str().compare("main") == 0)) {
instrumentFnEvent(first, FN_BEGIN);
}
}
for (BasicBlock &b : f) {
{
TerminatorInst *ti = b.getTerminator();
if (ReturnInst *ri = dyn_cast<ReturnInst>(ti)) {
if ((functionFilter) || (f.getName().str().compare("main") == 0)) {
instrumentFnEvent(ri, FN_END);
}
}
}
for (Instruction &i : b) {
#ifdef DEBUG_PRINT
cerr << "Ins: " << i.getOpcodeName() << endl;
#endif
#ifndef INST_ALLOC_ONLY
bool isArg;
if (StoreInst *si = dyn_cast<StoreInst>(&i)) {
/* First check for argument instrumentation,
* and remember the result.
*/
if ( (functionFilter &&
(isArg = checkAndConsumeArg(si)))
|| (accessFilter)) {
/* These guys get read once at the
* start of the function.
*/
if (isArg) {
instrumentAccess(si, 'a');
} else {
instrumentAccess(si, 'w');
}
}
}
if (LoadInst *li = dyn_cast<LoadInst>(&i)) {
if (accessFilter) {
instrumentAccess(li, 'r');
}
}
#endif
if (CallInst *ci = dyn_cast<CallInst>(&i)) {
if (allocFilter) {
instrumentAlloc(ci);
}
if ((functionFilter) || (f.getName().str().compare("main") == 0)) {
instrumentExit(ci);
}
}
}
}
}
}
srcmap.saveMap();
typemap.saveMap();
varmap.saveMap();
fnmap.saveMap();
return true;
}
};
static void registerAccessInstrumentationPass(const llvm::PassManagerBuilder &,
llvm::legacy::PassManagerBase &PM) {
PM.add(new AccessInstrumentationPass());
}
static llvm::RegisterStandardPasses
//RegisterMyPass(llvm::PassManagerBuilder::EP_EarlyAsPossible,
RegisterMyPass(llvm::PassManagerBuilder::EP_EnabledOnOptLevel0,
registerAccessInstrumentationPass);
/*
static llvm::RegisterStandardPasses
RegisterMyPassOx(llvm::PassManagerBuilder::EP_OptimizerLast,
registerAccessInstrumentationPass);
*/
static llvm::RegisterStandardPasses
RegisterMyPassOx(llvm::PassManagerBuilder::EP_ModuleOptimizerEarly,
registerAccessInstrumentationPass);
}
char AccessInstrumentationPass::ID = 0;
static RegisterPass<AccessInstrumentationPass> X("accessinst", "Memory access instrumentation", false, false);