-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tracer.cpp
364 lines (322 loc) · 10.6 KB
/
Tracer.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
#include "pin.H"
#include <iostream>
#include <fstream>
#include <map>
#include <unistd.h>
#include <sys/stat.h>
using std::cerr;
using std::endl;
using std::string;
using std::map;
/* ================================================================== */
// Global variables
/* ================================================================== */
UINT64 insCount = 0;
UINT64 count = 0;
UINT64 readCount = 0;
UINT64 writeCount = 0;
BOOL flag = false;
UINT64 mallocCount = 0;
UINT64 callocCount = 0;
UINT64 freeCount = 0;
UINT64 targetSize = 0;
UINT64 minAddress = 0;
UINT64 maxAddress = 0;
string bin;
VOID* loadedAddress;
VOID* codeAddress;
map<VOID*, string> disassembleCode;
string outputDir = "./output";
string infoFile = "./output/info.log";
std::ostream* info = nullptr;
string traceFile = "./output/trace.log";
std::ostream* trace = nullptr;
string backtraceFile = "./output/backtrace.log";
std::ostream* backtrace = nullptr;
string memoryTraceFile = "./output/memoryTrace.log";
std::ostream* memoryTrace = nullptr;
string heapTraceFile = "./output/heapTrace.log";
std::ostream* heapTrace = nullptr;
VOID init()
{
if (access(outputDir.c_str(), 0) == -1) {
mkdir(outputDir.c_str(), S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH);
}
info = new std::ofstream(infoFile.c_str(), std::ios_base::out);
trace = new std::ofstream(traceFile.c_str(), std::ios_base::out);
backtrace = new std::ofstream(backtraceFile.c_str(), std::ios_base::out);
memoryTrace = new std::ofstream(memoryTraceFile.c_str(), std::ios_base::out);
heapTrace = new std::ofstream(heapTraceFile.c_str(), std::ios_base::out);
}
/* ===================================================================== */
/* Names of malloc and free */
/* ===================================================================== */
#if defined(TARGET_MAC)
#define MALLOC "_malloc"
#define FREE "_free"
#else
#define MALLOC "malloc"
#define CALLOC "calloc"
#define FREE "free"
#endif
/* ===================================================================== */
// Utilities
/* ===================================================================== */
INT32 Usage()
{
cerr << KNOB_BASE::StringKnobSummary() << endl;
return -1;
}
BOOL CheckDisassembleCode(string disassemble)
{
string passCode[] = {"jmp qword ptr [rip+", "ret"};
int length = sizeof(passCode)/sizeof(passCode[0]);
for (int i=0; i < length; i++) {
if (disassemble.rfind(passCode[i], 0) == 0) {
return false;
}
}
return true;
}
/* ===================================================================== */
// Analysis routines
/* ===================================================================== */
VOID RecordArg1(CHAR* name, ADDRINT arg)
{
if (flag) {
*heapTrace << "0x0" << endl;
}
if (!strcmp(name, FREE)) {
flag = false;
freeCount++;
count++;
*heapTrace << insCount << "\t" << count << "\t" << name << " [" << (VOID*)arg << "] 0x0" << endl;
} else if (!strcmp(name, MALLOC)) {
flag = true;
mallocCount++;
count ++;
targetSize = arg;
*heapTrace << insCount << "\t" << count << "\t" << name << " [" << arg << "] ";
}
}
VOID RecordArg2(CHAR* name, ADDRINT arg1, ADDRINT arg2)
{
if (flag) {
*heapTrace << "0x0" << endl;
}
flag = true;
count ++;
callocCount++;
targetSize = arg1 * arg2;
*heapTrace << insCount << "\t" << count << "\t" << name << " [" << arg1 << "," << arg2 << "] ";
}
VOID RecordRet(ADDRINT ret)
{
flag = false;
*heapTrace << (VOID*)ret << endl;
if (minAddress != 0) {
if (ret < minAddress) {
minAddress = ret;
}
} else {
minAddress = ret;
}
if (maxAddress != 0) {
if (ret + targetSize > maxAddress) {
maxAddress = ret + targetSize;
}
} else {
maxAddress = ret + targetSize;
}
}
VOID Image(IMG img, VOID* v)
{
if (IMG_IsMainExecutable(img)) {
bin = IMG_Name(img);
loadedAddress = (VOID*)IMG_StartAddress(img);
codeAddress = (VOID*)IMG_LowAddress(img);
*info << "fileName: \'" << bin << "\'" << endl;
*info << "loadedAddress: " << loadedAddress << endl;
*info << "codeAddress: " << codeAddress << endl;
}
// malloc
RTN mallocRtn = RTN_FindByName(img, MALLOC);
if (RTN_Valid(mallocRtn)) {
RTN_Open(mallocRtn);
RTN_InsertCall(
mallocRtn, IPOINT_BEFORE,
(AFUNPTR)RecordArg1,
IARG_ADDRINT, MALLOC,
IARG_FUNCARG_ENTRYPOINT_VALUE, 0,
IARG_END
);
RTN_InsertCall(
mallocRtn, IPOINT_AFTER,
(AFUNPTR)RecordRet,
IARG_FUNCRET_EXITPOINT_VALUE,
IARG_END
);
RTN_Close(mallocRtn);
}
// calloc
RTN callocRtn = RTN_FindByName(img, CALLOC);
if (RTN_Valid(callocRtn)) {
RTN_Open(callocRtn);
RTN_InsertCall(
callocRtn, IPOINT_BEFORE,
(AFUNPTR)RecordArg2,
IARG_ADDRINT, CALLOC,
IARG_FUNCARG_ENTRYPOINT_VALUE, 0,
IARG_FUNCARG_ENTRYPOINT_VALUE, 1,
IARG_END
);
RTN_InsertCall(
callocRtn, IPOINT_AFTER,
(AFUNPTR)RecordRet,
IARG_FUNCRET_EXITPOINT_VALUE,
IARG_END
);
RTN_Close(callocRtn);
}
// free()
RTN freeRtn = RTN_FindByName(img, FREE);
if (RTN_Valid(freeRtn)) {
RTN_Open(freeRtn);
RTN_InsertCall(
freeRtn, IPOINT_BEFORE,
(AFUNPTR)RecordArg1,
IARG_ADDRINT, FREE,
IARG_FUNCARG_ENTRYPOINT_VALUE, 0,
IARG_END
);
RTN_Close(freeRtn);
}
}
VOID RecordIns(VOID* address, const CONTEXT* ctxt)
{
insCount += 1;
*trace << insCount << "\t" << address << ": " << disassembleCode[address] << endl;
if (CheckDisassembleCode(disassembleCode[address])) {
void *buf[128];
PIN_LockClient();
PIN_Backtrace(ctxt, buf, sizeof(buf) / sizeof(buf[0]));
*backtrace << insCount << "\t" << "[";
for (int i=0; i<128; i++) {
if (buf[i] == 0) {
break;
}
if (i != 0) {
*backtrace << ",";
}
*backtrace << (VOID*) VoidStar2Addrint(buf[i]);
}
*backtrace << "]" << endl;
PIN_UnlockClient();
}
}
VOID RecordMemRead(VOID* address, VOID* targetAddress, UINT32 size)
{
if ((UINT64)targetAddress >= minAddress && (UINT64)targetAddress <= maxAddress) {
readCount += 1;
count ++;
*memoryTrace << insCount << "\t" << count << "\t" << address << " R " << targetAddress << " " << size << endl;
}
}
VOID RecordMemWrite(VOID* address, VOID* targetAddress, UINT32 size)
{
if ((UINT64)targetAddress >= minAddress && (UINT64)targetAddress <= maxAddress) {
writeCount += 1;
count ++;
*memoryTrace << insCount << "\t" << count << "\t" << address << " W " << targetAddress << " " << size << endl;
}
}
VOID Trace(INS ins, VOID* v)
{
// Stake insertion for read and write instructions, not just for the main program.
UINT32 memOperands = INS_MemoryOperandCount(ins);
// Iterate over each memory operand of the instruction.
for (UINT32 memOp = 0; memOp < memOperands; memOp++) {
// Read
if (INS_MemoryOperandIsRead(ins, memOp)) {
INS_InsertPredicatedCall(
ins, IPOINT_BEFORE,
(AFUNPTR)RecordMemRead,
IARG_INST_PTR,
IARG_MEMORYOP_EA, memOp,
IARG_MEMORYREAD_SIZE,
IARG_END
);
}
// Write
if (INS_MemoryOperandIsWritten(ins, memOp)) {
INS_InsertPredicatedCall(
ins, IPOINT_BEFORE,
(AFUNPTR)RecordMemWrite,
IARG_INST_PTR,
IARG_MEMORYOP_EA, memOp,
IARG_MEMORYWRITE_SIZE,
IARG_END
);
}
}
PIN_LockClient();
IMG img = IMG_FindByAddress(INS_Address(ins));
PIN_UnlockClient();
if (!IMG_Valid(img) || !IMG_IsMainExecutable(img)) {
return;
}
VOID* address = (VOID*) INS_Address(ins);
string insDisassemble = INS_Disassemble(ins);
disassembleCode[address] = insDisassemble;
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)RecordIns, IARG_INST_PTR, IARG_CONST_CONTEXT, IARG_END);
}
VOID Fini(INT32 code, VOID* v)
{
cerr << endl;
cerr << "===============================================" << endl;
cerr << "Tracer analysis results: " << endl;
cerr << "FileName: " << bin << endl;
cerr << "Loaded address: " << loadedAddress << endl;
cerr << "Code address: " << codeAddress << endl;
cerr << "Number of instructions: " << insCount << endl;
cerr << "Number of read memory instructions: " << readCount << endl;
cerr << "Number of write memory instructions: " << writeCount << endl;
cerr << "Number of malloc: " << mallocCount << endl;
cerr << "Number of calloc: " << callocCount << endl;
cerr << "Number of free: " << freeCount << endl;
cerr << "===============================================" << endl;
*info << "insCount: " << insCount << endl;
*info << "readCount: " << readCount << endl;
*info << "writeCount: " << writeCount << endl;
*info << "mallocCount: " << mallocCount << endl;
*info << "callocCount: " << callocCount << endl;
*info << "freeCount: " << freeCount << endl;
}
int main(int argc, char* argv[])
{
init();
// Initialize PIN library. Print help message if -h(elp) is specified
// in the command line or the command line is invalid
if (PIN_Init(argc, argv))
{
return Usage();
}
PIN_InitSymbols();
// Register function to be called when image load
IMG_AddInstrumentFunction(Image, 0);
// Register function to be called to instrument Instrumention
INS_AddInstrumentFunction(Trace, 0);
// Register function to be called when the application exits
PIN_AddFiniFunction(Fini, 0);
cerr << "===============================================" << endl;
cerr << "This application is instrumented by Tracer" << endl;
cerr << "Info File: " << infoFile << endl;
cerr << "Trace File: " << traceFile << endl;
cerr << "Backtrace File: " << backtraceFile << endl;
cerr << "Memory Trace File: " << memoryTraceFile << endl;
cerr << "Heap Trace File: " << heapTraceFile << endl;
cerr << "===============================================" << endl;
// Start the program, never returns
PIN_StartProgram();
return 0;
}