/
heap_trace.cpp
294 lines (243 loc) · 6.9 KB
/
heap_trace.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
#include "pin.H"
#include<iostream>
#include<fstream>
#include<string.h>
#include<map>
/* ========================================================================================= */
/* Names of Malloc and Free */
/* ========================================================================================= */
#define MALLOC "malloc"
#define CALLOC "calloc"
#define FREE "free"
#define FGETS "fgets"
#define READ "read"
#define MEMCPY "memcpy"
#define STRNCPY "strncpy"
/* ========================================================================================= */
/* Global Variables */
/* ========================================================================================= */
std::ofstream TraceFile;
std::map<VOID *,INT32> in_use;
std::map<VOID *,INT32> freed;
INT32 malloc_size=0;
/* ========================================================================================= */
/* Commandline switches */
/* ========================================================================================= */
KNOB<string> KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool",
"o", "heap_trace.out", "specify trace file name");
/* ========================================================================================= */
/* Print Help */
/* ========================================================================================= */
INT32 Usage()
{
cerr <<
"This tool produces a trace of calls to gets and fgets.\n"
"\n";
cerr << KNOB_BASE::StringKnobSummary();
cerr << endl;
return -1;
}
/* ===================================================================== */
bool isMain(ADDRINT ret)
{
PIN_LockClient();
IMG im = IMG_FindByAddress(ret);
PIN_UnlockClient();
int inMain = IMG_Valid(im) ? IMG_IsMainExecutable(im) : 0;
return inMain;
}
VOID malloc_before(INT32 size)
{
malloc_size = size;
}
VOID malloc_after(VOID *chunk)
{
TraceFile << "malloc(" << malloc_size << ") returns " << chunk << endl;
if ( in_use.count(chunk) )
{
cout << "\n[+][+]Chunk "<< chunk <<" allocated more than once" << endl;
}
in_use[chunk] = malloc_size;
}
VOID calloc_before(INT32 size)
{
malloc_size = size;
}
VOID calloc_after(VOID *chunk)
{
TraceFile << "calloc(" << malloc_size << ") returns " << chunk << endl;
if ( in_use.count( chunk))
{
cout << "\n[+][+]Chunk "<< chunk <<" allocated more than once" << endl;
}
in_use[chunk] = malloc_size;
}
VOID free_chunk(VOID *chunk)
{
TraceFile << "free(" << chunk << ")" << endl;
if ( freed.count(chunk) )
{
cout << "\n[+][+]Chunk " << chunk << " freed more than once" << endl;
}
freed[chunk] = in_use[chunk];
in_use.erase(chunk);
}
VOID check_write(VOID *buffer, INT32 size)
{
map<VOID *,INT32>::iterator it;
if ( freed.count(buffer) )
{
cout << "\n[+][+]Reading input to freed chunk " << buffer << endl;
if ( freed[buffer] < size )
{
cout << "\n[+][+]Overflow while writing " << size << " bytes to ";
cout << buffer << endl;
}
}
else
{
for ( it = freed.begin(); it != freed.end(); it++)
{
if ( buffer > it->first && (int *)buffer < (int *)it->first + it->second )
{
cout << "\n[+][+]Reading input to freed chunk " << buffer << endl;
if ( ( (int *) it->first + it->second - (int *) buffer ) < size )
{
cout << "\n[+][+]Overflow while writing " << size;
cout << " bytes to " << it->first<< endl;
}
}
break;
}
}
if ( in_use.count(buffer) )
{
if ( in_use[buffer] < size )
{
cout << "\n[+][+]Overflow while writing " << size << " bytes to ";
cout << buffer << " of size " << in_use[buffer] << endl;
return;
}
return;
}
else
{
for ( it = in_use.begin(); it!=in_use.end(); it++)
{
if ( buffer > it->first && (int *) buffer < (int *) it->first + it->second )
{
if ( ( (int *) it->first + it->second - (int *) buffer ) < size )
{
cout << "\n[+][+]Overflow while writing " << size;
cout << " bytes to " << it->first<< endl;
}
return;
}
}
}
}
VOID fgets_call(VOID *buffer, INT32 size)
{
TraceFile << "fgets(" << buffer << " , " << size << " , stdin);" << endl;
check_write(buffer,size);
}
VOID read_call(VOID *buffer, INT32 size)
{
TraceFile << "read(0" << buffer << " , " << size << ")" << endl;
check_write(buffer,size);
}
VOID memcpy_call(VOID *buffer, INT32 size)
{
TraceFile << "memcpy(" << buffer << " , " << size << ")" << endl;
check_write(buffer,size);
}
VOID strncpy_call(VOID *buffer, INT32 size)
{
TraceFile << "strncpy(" << buffer << " , " << size << ")" << endl;
check_write(buffer,size);
}
/* ===================================================================== */
VOID Image(IMG img,VOID *v)
{
RTN rtn = RTN_FindByName(img, MALLOC);
if (RTN_Valid(rtn))
{
RTN_Open(rtn);
RTN_InsertCall(rtn,IPOINT_BEFORE,(AFUNPTR)malloc_before,
IARG_G_ARG0_CALLEE,IARG_END);
RTN_InsertCall(rtn,IPOINT_AFTER,(AFUNPTR)malloc_after,IARG_G_RESULT0,IARG_END);
RTN_Close(rtn);
}
rtn = RTN_FindByName(img, CALLOC);
if (RTN_Valid(rtn))
{
RTN_Open(rtn);
RTN_InsertCall(rtn,IPOINT_BEFORE,(AFUNPTR)calloc_before,
IARG_G_ARG0_CALLEE,IARG_END);
RTN_InsertCall(rtn,IPOINT_AFTER,(AFUNPTR)calloc_after,IARG_G_RESULT0,IARG_END);
RTN_Close(rtn);
}
rtn = RTN_FindByName(img,FREE);
if (RTN_Valid(rtn))
{
RTN_Open(rtn);
RTN_InsertCall(rtn,IPOINT_BEFORE,(AFUNPTR)free_chunk,IARG_G_ARG0_CALLEE,IARG_END);
RTN_Close(rtn);
}
rtn = RTN_FindByName(img,FGETS);
if (RTN_Valid(rtn) )
{
RTN_Open(rtn);
RTN_InsertCall(rtn,IPOINT_BEFORE,(AFUNPTR)fgets_call,IARG_G_ARG0_CALLEE,
IARG_G_ARG1_CALLEE,IARG_END);
RTN_Close(rtn);
}
rtn = RTN_FindByName(img,READ);
if (RTN_Valid(rtn) )
{
RTN_Open(rtn);
RTN_InsertCall(rtn,IPOINT_BEFORE,(AFUNPTR)read_call,IARG_G_ARG1_CALLEE,
IARG_G_ARG2_CALLEE,IARG_END);
RTN_Close(rtn);
}
rtn = RTN_FindByName(img,MEMCPY);
if (RTN_Valid(rtn))
{
RTN_Open(rtn);
RTN_InsertCall(rtn,IPOINT_BEFORE,(AFUNPTR)memcpy_call,IARG_G_ARG1_CALLEE,
IARG_G_ARG3_CALLEE,IARG_END);
RTN_Close(rtn);
}
rtn = RTN_FindByName(img,STRNCPY);
if(RTN_Valid(rtn))
{
RTN_Open(rtn);
RTN_InsertCall(rtn,IPOINT_BEFORE,(AFUNPTR)strncpy_call,IARG_G_ARG1_CALLEE,
IARG_G_ARG3_CALLEE,IARG_END);
RTN_Close(rtn);
}
}
/* ===================================================================== */
VOID Fini(INT32 code, VOID *v)
{
TraceFile.close();
}
/* ===================================================================== */
int main(int argc, char *argv[])
{
PIN_InitSymbols();
if ( PIN_Init(argc,argv) )
{
return Usage();
}
TraceFile.open(KnobOutputFile.Value().c_str() );
TraceFile << hex;
TraceFile.setf(ios::showbase);
cout << hex;
cout.setf(ios::showbase);
IMG_AddInstrumentFunction(Image, 0);
PIN_AddFiniFunction(Fini, 0);
PIN_StartProgram();
return 0;
}
/* ===================================================================== */