-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmultrix.cpp
309 lines (245 loc) · 9.03 KB
/
multrix.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
#include "pin.H"
#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <sstream>
#include <map>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
/* Function prototypes */
VOID ProcedureCall(UINT32 ip);
/* global counter for memory access pattern */
int memory_access_count = 0;
bool first_memory_access = false;
VOID* first_written_rbp;
INT64 rbp_int64;
int total_main_ins=0;
/* 2-D Boolean array of instructions vs. instructions */
int **ndm;
/* memoryWriteMap */
/* key => memory address written */
/* value => vector of writing instructions */ /* ( sorted by default ? ) */
std::map < VOID*, vector<int> > memoryWriteMap;
/* dependencyMap */
/* key => instruction */
/* value => instructions that key depends on */ /* doubt --> is it one instruction or array of ins ? */
std::map < int, vector<int> > dependencyMap;
FILE *fp;
INT32 Usage()
{
cerr << "This tool identifies the functions of a program and gets" << endl;
cerr << " data dependencies in them." << endl;
cerr << endl << KNOB_BASE::StringKnobSummary() << endl;
return -1;
}
VOID RecordMemRead(VOID *ip, VOID *addr, VOID *rbp, int ins_count)
{
std::cout << "Instruction " << ip << " reads from memory " << addr << std::endl;
/*for(UINT32 write_ins = 0; write_ins < memoryWriteMap[addr].size(); write_ins++ )
{
if(ip > memoryWriteMap[addr][write_ins])
{
// std::cout << ip << " > " << memoryWriteMap[addr][write_ins] << std::endl;
}
}*/
if(memoryWriteMap.count(addr) > 0) // check if key exists
{
// dependencyMap[ins_count] = memoryWriteMap[addr].back();
/* Add only unique instructions to the dependency vector for an ins */
/* remove the if block to demo loop identification idea */
if(std::find(dependencyMap[ins_count].begin(), dependencyMap[ins_count].end(), memoryWriteMap[addr].back()) == dependencyMap[ins_count].end())
{
dependencyMap[ins_count].push_back(memoryWriteMap[addr].back());
}
}
/* Code to convert hex to decimal and find offset from rbp*/
INT64 x;
std::stringstream ss;
ss << std::hex << addr;
ss >> x;
std::cout << x << std::endl;
fprintf(fp, "%d %ld\r\n", memory_access_count++, rbp_int64-x);
}
VOID RecordMemWrite(VOID *ip, VOID *addr, int ins_count)
{
/* Record the first write, make the flag true and make it base pointer value*/
if(!first_memory_access)
{
std::stringstream ss1;
first_written_rbp = addr;
ss1 << std::hex << first_written_rbp;
ss1 >> rbp_int64;
first_memory_access = true;
}
/* Code for adding WAW dependency i.e. if two instructions are writing to */
/* same address the second depends on first for sequential logic */
if(memoryWriteMap.count(addr) > 0)
{
std::cout << "Instruction " << ins_count << " depends on " << memoryWriteMap[addr].back() << std::endl;
// dependencyMap[ins_count] = memoryWriteMap[addr].back();
/* Add only unique instructions to the dependency vector for an ins */
/* remove the if block to demo loop identification idea */
if(std::find(dependencyMap[ins_count].begin(), dependencyMap[ins_count].end(), memoryWriteMap[addr].back()) == dependencyMap[ins_count].end())
{
dependencyMap[ins_count].push_back(memoryWriteMap[addr].back());
}
}
std::cout << "Instruction " << ip << " writes to memory " << addr << std::endl;
memoryWriteMap[addr].push_back(ins_count);
/* Code to convert hex to decimal and find offset from rbp */
INT64 x;
std::stringstream ss;
ss << std::hex << addr;
ss >> x;
fprintf(fp, "%d %ld\r\n", memory_access_count++, rbp_int64-x);
}
VOID MainInstruction(INS ins)
{
std::cout << total_main_ins+1 << ". "<<INS_Disassemble(ins) << std::endl;
++total_main_ins;
/* Checking for user defined procedure call instructions */
if(INS_IsProcedureCall(ins))
{
std::string call_ins = INS_Disassemble(ins);
std::string buf;
std::stringstream ss(call_ins);
std::vector<std::string> tokens;
while(ss >> buf)
tokens.push_back(buf);
/* Code to convert string to hex */
UINT32 proc_ip;
std::stringstream temp_ss;
temp_ss << std::hex << tokens[1];
temp_ss >> proc_ip;
/* stub to instrument the function and check it for dependency analysis */
INS_InsertPredicatedCall(
ins, IPOINT_BEFORE, (AFUNPTR)ProcedureCall,
IARG_UINT32,proc_ip,
IARG_END);
}
UINT32 memOperands = INS_MemoryOperandCount(ins);
for(UINT32 memOp = 0; memOp < memOperands; memOp++)
{
/* Memory Read Instruction */
if (INS_MemoryOperandIsRead(ins, memOp))
{
// std::cout << INS_Disassemble(ins) << std::endl;
INS_InsertPredicatedCall(
ins, IPOINT_BEFORE, (AFUNPTR)RecordMemRead,
IARG_INST_PTR,
IARG_MEMORYOP_EA, memOp,
IARG_REG_VALUE,REG_SEG_FS_BASE,
IARG_UINT32, (UINT32)total_main_ins,
IARG_END);
}
/* Memory Write Instruction */
if (INS_MemoryOperandIsWritten(ins, memOp))
{
// std::cout << INS_Disassemble(ins) << std::endl;
INS_InsertPredicatedCall(
ins, IPOINT_BEFORE, (AFUNPTR)RecordMemWrite,
IARG_INST_PTR,
IARG_MEMORYOP_EA, memOp,
IARG_UINT32, (UINT32)total_main_ins,
IARG_END);
}
}
}
VOID ProcedureCall(UINT32 ip)
{
std::cout << "********User defined procedure ********\n";
std::string proc_name = RTN_FindNameByAddress(ip);
std::cout << proc_name << std::endl;
/* To prevent the error : Function RTN_FindByAddress called without holding lock */
PIN_LockClient();
RTN rtn = RTN_FindByAddress(ip);
if (RTN_Valid(rtn) && proc_name != ".plt") // ".plt" are inbuilt functions (like printf). No need to analyse
{
RTN_Open(rtn);
for(INS ins = RTN_InsHead(rtn); INS_Valid(ins); ins = INS_Next(ins))
{
MainInstruction(ins);
}
RTN_Close(rtn);
}
/* Release the lock */
PIN_UnlockClient();
}
VOID Routine(RTN rtn, VOID *v)
{
/* Analysing only the main function of a program(start point) */
if (RTN_Valid(rtn) && RTN_Name(rtn)=="main")
{
RTN_Open(rtn);
for(INS ins = RTN_InsHead(rtn); INS_Valid(ins); ins = INS_Next(ins))
{
MainInstruction(ins);
}
RTN_Close(rtn);
}
}
VOID Fini(INT32 code, VOID *v)
{
std::cout << "I am Fini" << std::endl;
std::cout << "First Written RBP: " << first_written_rbp << std::endl;
std::cout << rbp_int64 << std::endl;
/* code to check contents of memoryWriteMap */
for(std::map< VOID*,vector<int> >::iterator iter=memoryWriteMap.begin();iter!=memoryWriteMap.end();iter++)
{
std::cout << "Memory Address : " << iter->first << std::endl;
std::cout << "Writing Instructions" << std::endl;
/* is sorting of vector of instructions needed here ? */
for(UINT32 i=0; i < (iter->second).size(); i++)
{
std::cout << (iter->second)[i] << std::endl;
}
}
/* code to check contents of dependencyMap */
/* Construction on Node Dependency Matrix */
ndm = (int **)malloc(total_main_ins * sizeof(int *));
for (int i = 0; i < total_main_ins; i++)
ndm[i] = (int *)malloc(total_main_ins * sizeof(int));
std::cout << "Dependency map size " << dependencyMap.size() << std::endl;
for(std::map< int,vector<int> >::iterator iter=dependencyMap.begin();iter!=dependencyMap.end();iter++)
{
// ndm[iter->first-1][iter->second-1] = 1;
std::cout << iter->first << "'s dependency vector size is " << (iter->second).size() << std::endl;
for(UINT32 i=0; i < (iter->second).size(); i++)
{
ndm[iter->first-1][(iter->second)[i]-1] = 1;
std::cout << iter->first <<" depends on " << (iter->second)[i] << std::endl;
}
}
std::cout << "Total main instructions: " << total_main_ins << std::endl;
std::cout << "\n\n ************** Final NDM ************** \n\n ";
for(int i=0; i < total_main_ins; i++)
std::cout << i+1 << " ";
std::cout << endl;
for(int i=0; i < total_main_ins; i++)
{
if(i<9)
std::cout << i+1 << " ";
else
std::cout << i+1 << " ";
for(int j=0; j < total_main_ins; j++)
{
if(j<9)
std::cout << ndm[i][j] << " ";
else
std::cout << ndm[i][j] << " ";
}
std::cout << std::endl;
}
}
int main( int argc, char *argv[] )
{
PIN_InitSymbols();
fp = fopen("mem_access.dat","w");
if (PIN_Init(argc, argv)) return Usage();
RTN_AddInstrumentFunction(Routine, 0 );
PIN_AddFiniFunction(Fini, 0);
PIN_StartProgram();
return 0;
}