-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
492 lines (328 loc) · 12.9 KB
/
main.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
/*
============================================================================
IHPP: An Intraprocedural Hot Path Profiler
Author: Vladislav K. Valtchev ([email protected])
The kSlabForest and kCCF data structures,
the forest join operation, the forest inverseK operation
and the traceObject function (and only these ones),
are based on the theoretical research of:
Giorgio Ausiello ([email protected])
Camil Demetrescu ([email protected])
Irene Finocchi ([email protected])
Donatella Firmani ([email protected])
Sapienza University of Rome
[ Universita' degli studi di Roma "La Sapienza" ]
============================================================================
Licence: LGPL (see LICENCE.txt)
*/
#include <iostream>
#include "config.h"
#include "debug.h"
#include "ihpp.h"
#include "benchmark.h"
#include "output.h"
#include "tracingFuncs.h"
using namespace std;
GlobalContext *globalSharedContext = nullptr;
void insInstrumentation(RTN rtn, INS ins) {
if (INS_IsRet(ins) && !FUNC_IS_TEXT(RTN_Address(rtn))) {
INS_InsertPredicatedCall(ins,
IPOINT_BEFORE, (AFUNPTR)funcMode_ret,
IARG_CALL_ORDER,
CALL_ORDER_FIRST,
IARG_END);
}
#if ENABLE_INS_TRACING
if (!globalSharedContext->options.insTracing)
return;
if (INS_IsControlFlow(ins)) {
if (INS_IsDirectControlFlow(ins)) {
ADDRINT targetAddr = INS_DirectControlFlowTargetAddress(ins);
RTN r;
PIN_LockClient();
{
r = RTN_FindByAddress(targetAddr);
}
PIN_UnlockClient();
if (!RTN_Valid(r)) {
return;
}
INS_InsertPredicatedCall(ins,
IPOINT_BEFORE, (AFUNPTR)branchOrCall,
IARG_CALL_ORDER,
CALL_ORDER_FIRST,
IARG_PTR, RTN_Address(rtn),
IARG_PTR, targetAddr,
IARG_PTR, RTN_Address(r),
IARG_PTR, INS_Category(ins),
IARG_END);
} else {
INS_InsertPredicatedCall(ins,
IPOINT_BEFORE, (AFUNPTR)indirect_branchOrCall,
IARG_CALL_ORDER,
CALL_ORDER_FIRST,
IARG_PTR, RTN_Address(rtn),
IARG_BRANCH_TARGET_ADDR,
IARG_PTR, INS_Category(ins),
IARG_END);
}
} else {
INS_InsertPredicatedCall(ins,
IPOINT_BEFORE, (AFUNPTR)singleInstruction,
IARG_CALL_ORDER,
CALL_ORDER_FIRST,
IARG_FAST_ANALYSIS_CALL,
IARG_PTR, RTN_Address(rtn),
IARG_END);
}
#endif
}
void BlockTraceInstrumentation(TRACE trace, void *)
{
RTN rtn;
string file;
INT32 row,col;
BasicBlock *bb;
ADDRINT blockPtr,funcAddr;
map<ADDRINT,BasicBlock*>::iterator it;
GlobalContext *ctx = globalSharedContext;
for (BBL bbl = TRACE_BblHead(trace); BBL_Valid(bbl); bbl = BBL_Next(bbl))
{
PIN_LockClient();
blockPtr = BBL_Address(bbl);
PIN_GetSourceLocation(blockPtr, &col, &row, 0);
rtn = RTN_FindByAddress(blockPtr);
if (!RTN_Valid(rtn)) {
PIN_UnlockClient();
continue;
}
funcAddr = RTN_Address(rtn);
if (!ctx->hasToTrace(funcAddr)) {
PIN_UnlockClient();
continue;
}
it = ctx->allBlocks.find(blockPtr);
if (it == ctx->allBlocks.end()) {
INS ins = BBL_InsTail(bbl);
ADDRINT lastAddr = INS_Address(ins);
assert(ctx->allFuncs.find(funcAddr) != ctx->allFuncs.end());
bb = new BasicBlock(blockPtr, ctx->allFuncs.find(funcAddr)->second, lastAddr, row, col);
ctx->allBlocks[blockPtr]=bb;
} else {
bb = it->second;
}
if (ctx->WorkingMode() == WM_InterProcMode)
BBL_InsertCall(bbl,
IPOINT_BEFORE, AFUNPTR(interModeBlockTrace),
IARG_PTR, bb,
IARG_END);
if (ctx->WorkingMode() == WM_IntraMode)
BBL_InsertCall(bbl,
IPOINT_BEFORE, AFUNPTR(intraModeBlockTrace),
IARG_CALL_ORDER, CALL_ORDER_LAST,
IARG_PTR, bb,
#if ENABLE_KEEP_STACK_PTR
IARG_REG_VALUE, REG_STACK_PTR,
#endif
IARG_END);
PIN_UnlockClient();
}
}
static void imageload_specialfunc(ADDRINT& funcAddr, const string& funcName)
{
#if defined(_WIN32)
if (funcName == "_NLG_Notify")
globalSharedContext->spAttrs._NLG_Notify_addr = funcAddr;
else if (funcName == "_NLG_Notify1")
globalSharedContext->spAttrs._NLG_Notify1_addr = funcAddr;
else if (funcName == "__NLG_Dispatch")
globalSharedContext->spAttrs.__NLG_Dispatch_addr = funcAddr;
else if (funcName == "__tmainCRTStartup")
globalSharedContext->spAttrs.__tmainCRTStartup_addr = funcAddr;
else if (funcName == "wWinMain")
globalSharedContext->spAttrs.wWinMain_addr = funcAddr;
else if (funcName == "unnamedImageEntryPoint")
globalSharedContext->spAttrs.unnamedImageEntryPoint_addr = funcAddr;
#endif
if (funcName == "main")
globalSharedContext->spAttrs.main_addr = funcAddr;
else if (funcName == ".text")
globalSharedContext->spAttrs.text_addr = funcAddr;
}
void imageLoad_doInsInstrumentation(IMG &img, RTN &rtn, FunctionObj *fc) {
GlobalContext *ctx = globalSharedContext;
RTN_Open(rtn);
for( INS ins = RTN_InsHead(rtn); INS_Valid(ins); ins = INS_Next(ins) ) {
if (ctx->WorkingMode() != WM_InterProcMode)
insInstrumentation(rtn, ins);
if (!ctx->options.blocksDisasm && !ctx->options.funcsDisasm)
continue;
const string& ins_text = INS_Disassemble(ins);
if (!INS_IsDirectControlFlow(ins)) {
fc->instructions[INS_Address(ins)] = insInfo(ins_text, 0, 0);
continue;
}
ADDRINT addr = INS_DirectControlFlowTargetAddress(ins);
RTN r = RTN_FindByAddress(addr);
if (!RTN_Valid(r)) {
fc->instructions[INS_Address(ins)] = insInfo(ins_text, addr, 0);
continue;
}
const string& rname = RTN_Name(r);
insInfo insData = insInfo((ins_text), addr, RTN_Address(r));
RTN r2 = RTN_FindByName(img, rname.c_str());
if (!RTN_Valid(r2) && !FUNC_IS_TEXT(RTN_Address(r))) {
insData.externFuncName = duplicate_string(rname);
fc->instructions[INS_Address(ins)] = insData;
continue;
}
fc->instructions[INS_Address(ins)] = insData;
}
RTN_Close(rtn);
}
void ImageLoad(IMG img, void *)
{
RTN rtn2;
GlobalContext *ctx = globalSharedContext;
FunctionObj *fc;
map<ADDRINT, FunctionObj*>::iterator it;
const bool mainImage = IMG_IsMainExecutable(img);
dbg_imgload_imgname();
rtn2 = RTN_FindByName(img, "exit");
if (RTN_Valid(rtn2)) {
globalSharedContext->spAttrs.exit_addr = RTN_Address(rtn2);
}
if (!mainImage)
return;
for (SEC sec = IMG_SecHead(img); SEC_Valid(sec); sec = SEC_Next(sec)) {
dbg_imgload_sectionname();
for (RTN rtn = SEC_RtnHead(sec); RTN_Valid(rtn); rtn = RTN_Next(rtn)) {
ADDRINT funcAddr = RTN_Address(rtn);
const string& funcName = RTN_Name(rtn);
string fileName;
PIN_GetSourceLocation(funcAddr, 0, 0, &fileName);
imageload_specialfunc(funcAddr, funcName);
#ifdef __unix__
if (SEC_Name(RTN_Sec(rtn)) == ".plt")
continue;
#endif
assert(ctx->allFuncs.find(funcAddr) == ctx->allFuncs.end());
fc = new FunctionObj(funcAddr, funcName, fileName);
ctx->allFuncs[funcAddr]=fc;
const bool trace = ctx->hasToTraceByName(funcName, funcAddr);
dbg_imgload_funcname();
if (trace || FUNC_IS_TEXT_N(funcName)) {
imageLoad_doInsInstrumentation(img, rtn, fc);
}
if (!trace)
continue;
ctx->funcAddrsToTrace.insert(funcAddr);
if (ctx->WorkingMode() != WM_InterProcMode)
{
RTN_Open(rtn);
RTN_InsertCall(rtn, IPOINT_BEFORE, AFUNPTR(FunctionObjTrace),
IARG_CALL_ORDER, CALL_ORDER_FIRST,
IARG_PTR, (ADDRINT)fc,
#if ENABLE_KEEP_STACK_PTR
IARG_REG_VALUE, REG_STACK_PTR,
#endif
IARG_END);
RTN_Close(rtn);
}
}
}
if (ctx->options.startFuncName != "--") {
rtn2 = RTN_FindByName(img, ctx->options.startFuncName.c_str());
if (!RTN_Valid(rtn2)) {
cerr << "Error: start tracing function '"
<< ctx->options.startFuncName
<< "' not found.\n";
PIN_ExitProcess(1);
}
ctx->startFuncAddr = RTN_Address(rtn2);
}
if (ctx->options.stopFuncName != "--") {
rtn2 = RTN_FindByName(img, ctx->options.stopFuncName.c_str());
if (!RTN_Valid(rtn2)) {
cerr << "Error: stop tracing function '"
<< ctx->options.stopFuncName
<< "' not found.\n";
PIN_ExitProcess(1);
}
ctx->stopFuncAddr = RTN_Address(rtn2);
}
#if defined(_WIN32)
if (!ctx->spAttrs.text_addr && ctx->spAttrs.unnamedImageEntryPoint_addr)
ctx->spAttrs.text_addr=ctx->spAttrs.unnamedImageEntryPoint_addr;
#endif
}
void ThreadStart(THREADID tid, CONTEXT* ctxt, INT32 flags, VOID* v)
{
globalSharedContext->createThreadContext(tid);
}
void ThreadFini(THREADID tid, const CONTEXT* ctxt, INT32 code, VOID* v)
{
globalSharedContext->destroyThreadContext(tid);
}
/* ===================================================================== */
/* Main */
/* ===================================================================== */
int main(int argc, char ** argv) {
PIN_InitSymbols();
if (PIN_Init(argc, argv)) {
optionsClass::showHelp();
return 0;
}
if (!optionsClass::checkOptions())
return 0;
optionsClass options;
options.initFromGlobalOptions();
globalSharedContext = new GlobalContext(
optionsClass::getGlobalWM(),
optionsClass::getGlobalKVal(),
options
);
if (!options.tracingFuncList.empty()) {
vector<string> funcs = splitString(options.tracingFuncList, ',');
if (funcs.empty()) {
cerr << "Invalid function list: the argument of -funcs option\n";
cerr << "must be a list of comma-seperated function names.\n";
return 0;
}
for (const string& f: funcs) {
globalSharedContext->funcsToTrace.insert(f);
}
}
globalSharedContext->OutFile.open(optionsClass::getOutfileName());
if (globalSharedContext->WorkingMode() != WM_FuncMode) {
TRACE_AddInstrumentFunction(BlockTraceInstrumentation, 0);
}
IMG_AddInstrumentFunction(ImageLoad, 0);
PIN_AddFiniFunction(Fini, 0);
if (options.kinf) {
if (!options.unrollRec)
traceObject = traceObject_kinf_roll;
else
traceObject = traceObject_kinf_no_roll;
} else {
if (!options.unrollRec)
traceObject = traceObject_classic_roll;
else
traceObject = traceObject_classic_no_roll;
}
//add a fake __root__ function
if (globalSharedContext->funcsToTrace.size()) {
FunctionObj *f = new FunctionObj((ADDRINT)-1, "__root__", "");
globalSharedContext->allFuncs[f->getKey()] = f;
globalSharedContext->funcAddrsToTrace.insert(f->functionAddress());
if (globalSharedContext->WorkingMode() != WM_FuncMode) {
BasicBlock *bb = new BasicBlock(f->functionAddress(), f, f->functionAddress(), 0, 0);
globalSharedContext->allBlocks[bb->blockAddress()] = bb;
}
}
PIN_AddThreadStartFunction(ThreadStart, NULL);
PIN_AddThreadFiniFunction(ThreadFini, NULL);
globalSharedContext->timer = getMilliseconds();
PIN_StartProgram();
return 0;
}