-
Notifications
You must be signed in to change notification settings - Fork 108
/
bbcounts2.cpp
300 lines (264 loc) · 8.63 KB
/
bbcounts2.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
/*
PIN Tool to find all basic blocks a program executes during fuzzing by VYFuzzer.
Code based on examples from PIN documentation.
*/
#include <pin.H>
#include <stdio.h>
#include <set>
#include <map>
#include <iostream>
#include <unistd.h>
#include <sstream>
#include <string>
#include <vector>
#include <utility>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <cstring>
#define FILEPATH "image.offset"
#define CRASHFILE "crash.bin"
using namespace std;
KNOB<string> KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool",
"o", "bbcount.out", "specify output file name");
KNOB<UINT32> KnobLibC(KNOB_MODE_WRITEONCE, "pintool",
"libc", "0", "if you want to monitor libc BB");
KNOB<UINT32> KnobTimeout(KNOB_MODE_WRITEONCE, "pintool",
"x", "10000", "specify timeout in miliseconds");
KNOB<string> KnobXLibraries(KNOB_MODE_WRITEONCE, "pintool",
"l", "", "specify shared lobraries to be monitored, separated by comma (no spaces)");
static FILE* trace;
static FILE* offsets;
static int ioffset;
static char *offsetmap;
off_t sz;
//static map<long unsigned int, int> bbcount;
//static pair<map<long unsigned int, int>::iterator, bool> ret;
static map<ADDRINT, unsigned int> bbcount;
static pair<map<ADDRINT, unsigned int>::iterator, bool> ret;
PIN_THREAD_UID threadUid;
static vector<pair<ADDRINT,ADDRINT> > allAddr;
static vector<string> libNames;
static FILE* crashFD;
#define LAST_EXECUTED_BB 10
ADDRINT LastExecutedBB[LAST_EXECUTED_BB]={};
UINT32 LastExecutedPosBB=0;
#define LAST_EXECUTED_Rtn 5
ADDRINT LastExecutedRtn[LAST_EXECUTED_Rtn]={};
UINT32 LastExecutedPosRtn=0;
//catching excpetions
//SIGNAL_INTERCEPT_CALLBACK
//EXCEPT_HANDLING_RESULT ExceptionHandling(THREADID tid, EXCEPTION_INFO *pExceptInfo, PHYSICAL_CONTEXT *pPhysCtxt, VOID *v)
//VOID ExceptionHandling(THREADID threadIndex, CONTEXT_CHANGE_REASON reason, const CONTEXT *from, CONTEXT *to, INT32 info, VOID *v)
BOOL ExceptionHandling(THREADID tid, INT32 sig, CONTEXT *ctxt, BOOL hasHandler, const EXCEPTION_INFO *pExceptInfo, VOID *v)
{
UINT32 i;
crashFD=fopen(CRASHFILE,"w");
fprintf(crashFD,"%d",sig);
//fprintf(crashFD,"%p",(void *) PIN_GetExceptionAddress(pExceptInfo));
//fprintf(crashFD,"%p",(void *) PIN_GetContextReg(ctxt,REG_INST_PTR));
for(i=0;i<LAST_EXECUTED_BB;i++)
{
fprintf(crashFD,"%p",(void *)LastExecutedBB[LastExecutedPosBB]);
LastExecutedPosBB = (LastExecutedPosBB+1)%LAST_EXECUTED_BB;
}
for(i=0;i<LAST_EXECUTED_Rtn;i++)
{
fprintf(crashFD,"%p",(void *)LastExecutedRtn[LastExecutedPosRtn]);
LastExecutedPosRtn = (LastExecutedPosRtn+1)%LAST_EXECUTED_Rtn;
}
fclose(crashFD);
return TRUE;
//return EHR_CONTINUE_SEARCH;
}
VOID call_direct(ADDRINT target)
{
LastExecutedRtn[LastExecutedPosRtn]= target;
LastExecutedPosRtn = (LastExecutedPosRtn+1)% LAST_EXECUTED_Rtn;
}
VOID call_indirect(ADDRINT target, BOOL taken)
{
if (!taken) return;
LastExecutedRtn[LastExecutedPosRtn]= target;
LastExecutedPosRtn = (LastExecutedPosRtn+1)% LAST_EXECUTED_Rtn;
}
VOID ImageLoad(IMG img, VOID *v)
{
if(IMG_IsMainExecutable(img))
{
//cout<<"[*]adding main executable addresses at"<<StringFromAddrint(IMG_LoadOffset(img))<<endl;
fprintf(offsets, "Main: %s\n",StringFromAddrint(IMG_LoadOffset(img)).c_str());
fflush(offsets);
allAddr.push_back(std::make_pair(IMG_LowAddress(img), IMG_HighAddress(img)));
}
else
{
//cout<<"[*] "<<IMG_Name(img)<< "is loaded...at offset"<<StringFromAddrint(IMG_LoadOffset(img))<<endl;
// lets check for libc monitoring first
if (KnobLibC.Value() > 0)
{
if (IMG_Name(img).find("libc.")!=std::string::npos)
allAddr.push_back(std::make_pair(IMG_LowAddress(img), IMG_HighAddress(img)));
}
for (vector<string>::iterator it=libNames.begin();it !=libNames.end();++it)
{
if (IMG_Name(img).find(*it)!=std::string::npos)
{
//cout<<"[*] "<<IMG_Name(img)<< "is being added."<<endl;
fprintf(offsets, "%s: %s\n",IMG_Name(img).c_str(),StringFromAddrint(IMG_LoadOffset(img)).c_str());
std::memcpy(offsetmap,StringFromAddrint(IMG_LoadOffset(img)).c_str(),18);
fflush(offsets);
allAddr.push_back(std::make_pair(IMG_LowAddress(img), IMG_HighAddress(img)));
}
}
}
}
BOOL isMonitoredAddress(ADDRINT bb)
{
for(vector<pair<ADDRINT,ADDRINT> >::iterator it=allAddr.begin();it!=allAddr.end();++it)
{
if ((bb >= (*it).first )&&(bb <= (*it).second)) return true;
}
return false;
}
VOID PIN_FAST_ANALYSIS_CALL rememberBlock(ADDRINT bbl)
{
bbcount[bbl]=bbcount[bbl]+1;
}
VOID Trace(TRACE trace, VOID *v)
{
for (BBL bbl = TRACE_BblHead(trace); BBL_Valid(bbl); bbl = BBL_Next(bbl))
{
if(isMonitoredAddress(BBL_Address(bbl)))
{
/* Things related to stack hash/crash fingerprints */
LastExecutedBB[LastExecutedPosBB]= BBL_Address(bbl);
LastExecutedPosBB = (LastExecutedPosBB+1)% LAST_EXECUTED_BB;
INS tail = BBL_InsTail(bbl);
if( INS_IsCall(tail) )
{
if( INS_IsDirectBranchOrCall(tail))
{
ADDRINT target = INS_DirectBranchOrCallTargetAddress(tail);
INS_InsertPredicatedCall(tail, IPOINT_BEFORE, AFUNPTR(call_direct), IARG_ADDRINT, target, IARG_END);
}
else
{
INS_InsertCall(tail, IPOINT_BEFORE, AFUNPTR(call_indirect), IARG_BRANCH_TARGET_ADDR, IARG_BRANCH_TAKEN, IARG_END);
}
}
/* stack hask ends here. */
BBL_InsertCall(bbl, IPOINT_ANYWHERE, AFUNPTR(rememberBlock), IARG_FAST_ANALYSIS_CALL, IARG_ADDRINT, BBL_Address(bbl), IARG_END);
}
}
}
static VOID TimeoutF(VOID * arg)
{
// this function is called in a separate threat to exit the applications after n seconds
//cout<<"[*] In the thread now..."<<endl;
sleep(KnobTimeout.Value());
//cout<<"[*]Going to kill application.."<<endl;
PIN_ExitApplication(0);
/*while(true)
{
if (PIN_IsProcessExiting())
{
PIN_ExitThread(0);
}
}
return 0;*/
//cout<<"[*] Application is killed..Exiting thread now."<<endl;
PIN_ExitThread(0);
}
VOID Fini(INT32 code, VOID *v)
{
/*
set<unsigned int>::iterator i;
for(i = setKnownBlocks.begin(); i != setKnownBlocks.end(); ++i)
{
fprintf(trace, "%p\n", *i);
}
*/
//if(ret.second == true)
map<ADDRINT,unsigned int>::iterator bb;
for (bb=bbcount.begin();bb!=bbcount.end();++bb)
{
fprintf(trace, "%p %u\n", (void *)bb->first, bb->second);
//fflush(trace);
}
fclose(trace);
fclose(offsets);
munmap(offsetmap, 18);
close(ioffset);
}
/* ===================================================================== */
/* Print Help Message */
/* ===================================================================== */
INT32 Usage()
{
cerr << "This tool counts the number of basic block executed with their frequencies" << endl;
cerr << endl << KNOB_BASE::StringKnobSummary() << endl;
return -1;
}
int main(int argc, char * argv[])
{
// Initialize symbol processing
PIN_InitSymbols();
offsets = fopen("imageOffset.txt", "w");
//open file and mmap it to write image load address
ioffset = open(FILEPATH, O_RDWR);
if (ioffset == -1)
{
perror("Error opening file for writing");
exit(0);
}
//sz = lseek(ioffset, 0, SEEK_END);
//printf("file size %ld\n",(long int)sz);
//lseek(ioffset, -sz, SEEK_END);
offsetmap = (char*)mmap(0, 18, PROT_READ | PROT_WRITE, MAP_SHARED, ioffset, 0);
if (offsetmap == MAP_FAILED) {
close(ioffset);
printf("Error mmapping the file");
//perror("Error mmapping the file");
exit(0);
}
PIN_THREAD_UID threadUid;
//THREADID threadId;
if (PIN_Init(argc, argv)) return Usage();
trace = fopen(KnobOutputFile.Value().c_str(), "w");
TRACE_AddInstrumentFunction(Trace, 0);
/* lets add signal intercept for signal 1, 6, and 11. */
INT32 signals[3]={1,6,11};
for (INT32 sig=0;sig<3;sig++)
{
PIN_InterceptSignal(signals[sig], ExceptionHandling, NULL);
PIN_UnblockSignal(sig, TRUE);
}
if (!KnobXLibraries.Value().empty())
{
stringstream libs(KnobXLibraries.Value().c_str());
while(libs.good())
{
string temp;
getline(libs,temp,',');
libNames.push_back(temp);
}
}
// Register ImageLoad to be called when an image is loaded
IMG_AddInstrumentFunction(ImageLoad, 0);
PIN_AddFiniFunction(Fini, 0);
//PIN_AddFiniUnlockedFunction(Fini,0);
//sleep(5);
cout<<"Starting the app now..." << endl;
if (KnobTimeout.Value() > 0)
PIN_SpawnInternalThread(TimeoutF,0,0,&threadUid);
PIN_StartProgram();
//cout << "done.." << endl;
//sleep(10);
//PIN_ExitApplication(0);
return 0;
}
// end