-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtracer_decode.cpp
396 lines (357 loc) · 11.2 KB
/
tracer_decode.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
#include "tracer_decode.h"
#include <stdio.h>
#include <assert.h>
#include "tracerlib.h"
#define STATIC_CAST(x,y) ((x) (y))
VOID disassemblyToBuffInternal(char * decodeBuffer, VOID *ip, const xed_decoded_inst_t *ins);
// Do no tracing for these instruction categories
bool isIgnoredInstruction(const xed_category_enum_t cat)
{
if(cat == XED_CATEGORY_NOP)
return true;
if(cat == XED_CATEGORY_CALL)
return true;
if(cat == XED_CATEGORY_SYSCALL)
return true;
if(cat == XED_CATEGORY_WIDENOP)
return true;
return false;
}
// Instructions only move data and do no computation
bool isMoveOnlyInstruction(const xed_category_enum_t cat)
{
if(cat == XED_CATEGORY_DATAXFER)
return true;
return false;
}
// Instruction uses x87
bool isX87Instruction(const xed_category_enum_t cat)
{
return(cat == XED_CATEGORY_X87_ALU);
}
bool isIgnoredX87Instruction(const xed_iclass_enum_t iclass)
{
if(iclass == XED_ICLASS_FNSTCW)
return true;
if(iclass == XED_ICLASS_FNCLEX)
return true;
if(iclass == XED_ICLASS_FLDCW)
return true;
return false;
}
// Do not trace dependencies through these registers
bool isIgnoredRegister(xed_reg_enum_t r)
{
if(xed_reg_class(r) == XED_REG_CLASS_CR)
return true;
if(xed_reg_class(r) == XED_REG_CLASS_DR)
return true;
if(xed_reg_class(r) == XED_REG_CLASS_FLAGS)
return true;
if(xed_reg_class(r) == XED_REG_CLASS_SR)
return true;
if(xed_reg_class(r) == XED_REG_CLASS_INVALID)
return true;
if(xed_reg_class(r) == XED_REG_CLASS_IP)
return true;
if(xed_reg_class(r) == XED_REG_CLASS_MXCSR)
return true;
if(r == XED_REG_RSP)
return true;
if(r == XED_REG_STACKPUSH)
return true;
if(r == XED_REG_STACKPOP)
return true;
return false;
}
// Instructions that clear all dependencies
bool isZeroingInstruction(xed_decoded_inst_t *ins)
{
if(xed_decoded_inst_get_iclass(ins) == XED_ICLASS_XOR || xed_decoded_inst_get_iclass(ins) == XED_ICLASS_PXOR)
if(
xed_decoded_inst_get_reg(ins,xed_operand_name(xed_inst_operand(xed_decoded_inst_inst(ins),0))) ==
xed_decoded_inst_get_reg(ins,xed_operand_name(xed_inst_operand(xed_decoded_inst_inst(ins),1)))
)
return true;
return false;
}
// Decode instruction at ip saving needed items to the instructionLocations map
instructionType decodeInstructionData(ADDRINT ip, unordered_map<ADDRINT,instructionLocationsData > &instructionLocations)
{
const xed_inst_t *xedins;
xed_decoded_inst_t ins;
instructionType insType = NORMAL_INS_TYPE;
instructionLocations[(ADDRINT)ip].ip = (ADDRINT)ip;
xed_state_t dstate;
xed_state_zero(&dstate);
xed_state_init2(&dstate,XED_MACHINE_MODE_LONG_64,XED_ADDRESS_WIDTH_64b);
xed_decoded_inst_zero_set_mode(&(ins), &dstate);
xed_decode(&(ins),STATIC_CAST(const xed_uint8_t*,ip),15);
xedins = xed_decoded_inst_inst(&(ins));
xed_category_enum_t cat = xed_inst_category(xedins);
if(isIgnoredInstruction(cat))
insType = IGNORED_INS_TYPE;
if(isMoveOnlyInstruction(cat))
insType = MOVEONLY_INS_TYPE;
if(isX87Instruction(cat))
{
if(isIgnoredX87Instruction(xed_decoded_inst_get_iclass(&ins)))
insType = IGNORED_INS_TYPE;
else
insType = X87_INS_TYPE;
}
int numOperands = xed_decoded_inst_noperands(&(ins));
int numMemOps = xed_decoded_inst_number_of_memory_operands(&ins);
if(!isZeroingInstruction(&(ins)))
{
for(int i = 0; i < numOperands; i++)
{
const xed_operand_t *curOp = xed_inst_operand(xedins, i);
if(xed_operand_read(curOp) )
{
xed_operand_enum_t op_name = xed_operand_name(xed_inst_operand(xed_decoded_inst_inst(&(ins)),i));
xed_reg_enum_t r = xed_decoded_inst_get_reg(&(ins), op_name);
if(r)
{
if(!isIgnoredRegister(r))
{
if(xed_reg_class(r) == XED_REG_CLASS_GPR)
{
instructionLocations[(ADDRINT)ip].registers_read.push_back(xed_get_largest_enclosing_register(r));
}
else if(xed_gpr_reg_class(r) == XED_REG_CLASS_XMM)
{
// read from YMM register
instructionLocations[(ADDRINT)ip].registers_read.push_back((xed_reg_enum_t)(r+16));
}
else
{
instructionLocations[(ADDRINT)ip].registers_read.push_back(r);
}
}
}
}
}
for(int i = 0; i < numMemOps; i++)
{
// if( !xed_decoded_inst_mem_read(&ins,i) && !xed_decoded_inst_mem_written(&ins,i))
{
xed_reg_enum_t r = xed_decoded_inst_get_seg_reg(&ins,i);
if(r != XED_REG_INVALID)
{
if(!isIgnoredRegister(r))
{
if(xed_reg_class(r) == XED_REG_CLASS_GPR)
{
instructionLocations[(ADDRINT)ip].registers_read.push_back(xed_get_largest_enclosing_register(r));
}
else if(xed_gpr_reg_class(r) == XED_REG_CLASS_XMM)
{
// read from YMM register
instructionLocations[(ADDRINT)ip].registers_read.push_back((xed_reg_enum_t)(r+16));
}
else
{
instructionLocations[(ADDRINT)ip].registers_read.push_back(r);
}
}
}
r = xed_decoded_inst_get_base_reg(&ins,i);
if(r != XED_REG_INVALID)
{
if(!isIgnoredRegister(r))
{
if(xed_reg_class(r) == XED_REG_CLASS_GPR)
{
instructionLocations[(ADDRINT)ip].registers_read.push_back(xed_get_largest_enclosing_register(r));
}
else if(xed_gpr_reg_class(r) == XED_REG_CLASS_XMM)
{
// read from YMM register
instructionLocations[(ADDRINT)ip].registers_read.push_back((xed_reg_enum_t)(r+16));
}
else
{
instructionLocations[(ADDRINT)ip].registers_read.push_back(r);
}
}
}
r = xed_decoded_inst_get_index_reg(&ins,i);
if(r != XED_REG_INVALID)
{
if(!isIgnoredRegister(r))
{
if(xed_reg_class(r) == XED_REG_CLASS_GPR)
{
instructionLocations[(ADDRINT)ip].registers_read.push_back(xed_get_largest_enclosing_register(r));
}
else if(xed_gpr_reg_class(r) == XED_REG_CLASS_XMM)
{
// read from YMM register
instructionLocations[(ADDRINT)ip].registers_read.push_back((xed_reg_enum_t)(r+16));
}
else
{
instructionLocations[(ADDRINT)ip].registers_read.push_back(r);
}
}
}
}
}
}
for(int i = 0; i < numOperands; i++)
{
const xed_operand_t *curOp = xed_inst_operand(xedins, i);
if(xed_operand_written(curOp) )
{
xed_operand_enum_t op_name = xed_operand_name(xed_inst_operand(xed_decoded_inst_inst(&(ins)),i));
xed_reg_enum_t r = xed_decoded_inst_get_reg(&(ins), op_name);
if(r)
{
if(!isIgnoredRegister(r))
{
if(xed_reg_class(r) == XED_REG_CLASS_GPR)
{
instructionLocations[(ADDRINT)ip].registers_written.push_back(xed_get_largest_enclosing_register(r));
}
else if(xed_gpr_reg_class(r) == XED_REG_CLASS_XMM)
{
// store in YMM register
instructionLocations[(ADDRINT)ip].registers_written.push_back((xed_reg_enum_t)(r+16));
}
else
{
instructionLocations[(ADDRINT)ip].registers_written.push_back(r);
}
}
}
}
}
PIN_LockClient();
PIN_GetSourceLocation(ip, &(debugData[ip].col_number), &(debugData[ip].line_number), &(debugData[ip].file_name));
PIN_UnlockClient();
return insType;
}
// Standard form of instruction tracing for debugging
#ifdef NOSHAODWCACHE
void instructionTracing(VOID * ip, VOID * addr, long int value, const char *called_from, FILE *out, ShadowMemoryNoCache &shadowMemory, ShadowRegisters ®isters)
#else
void instructionTracing(VOID * ip, VOID * addr, long int value, const char *called_from, FILE *out, ShadowMemory &shadowMemory, ShadowRegisters ®isters)
#endif
{
xed_state_t dstate;
xed_state_zero(&dstate);
xed_state_init2(&dstate,XED_MACHINE_MODE_LONG_64,XED_ADDRESS_WIDTH_64b);
xed_decoded_inst_t ins;
xed_decoded_inst_zero_set_mode(&ins, &dstate);
xed_decode(&ins,STATIC_CAST(const xed_uint8_t*,ip),15);
const xed_inst_t *xedins = xed_decoded_inst_inst(&ins);
int numOperands = xed_decoded_inst_noperands(&ins);
int numMemOps = xed_decoded_inst_number_of_memory_operands(&ins);
INT32 source_column = 0;
INT32 source_line = 0;
string source_file;
PIN_LockClient();
PIN_GetSourceLocation((ADDRINT)ip, &source_column, &source_line, &source_file);
PIN_UnlockClient();
fprintf(out, "%p:%s\t\tOps(%d)", ip, debugData[(ADDRINT)ip].instruction.c_str(),numOperands);
for(int i = 0; i < numOperands; i++)
{
const xed_operand_t *curOp = xed_inst_operand(xedins, i);
fprintf(out, "%d:", (i+1));
if(xed_operand_read(curOp) )
{
xed_operand_enum_t op_name = xed_operand_name(xed_inst_operand(xed_decoded_inst_inst(&ins),i));
if(xed_decoded_inst_get_reg(&ins, xed_operand_name(curOp)))
{
xed_reg_enum_t r = xed_decoded_inst_get_reg(&ins, op_name);
fprintf(out,"r:%s=%ld ",xed_reg_enum_t2str(r),registers.readReg(r));
if(r != XED_REG_CR0)
value = max(registers.readReg(r),value);
}
else
{
fprintf(out,"r:not a regitster (%s)",xed_operand_enum_t2str(op_name));
}
}
if(xed_operand_written(curOp) )
{
xed_operand_enum_t op_name = xed_operand_name(xed_inst_operand(xed_decoded_inst_inst(&ins),i));
if(xed_decoded_inst_get_reg(&ins, xed_operand_name(curOp)))
{
xed_reg_enum_t r = xed_decoded_inst_get_reg(&ins, op_name);
fprintf(out,"w:%s=%ld ",xed_reg_enum_t2str(r),registers.readReg(r));
if(r != XED_REG_CR0)
value = max(registers.readReg(r),value);
}
else
{
fprintf(out, "w:not a register (%s)",xed_operand_enum_t2str(op_name));
}
}
if(!xed_operand_written(curOp) && !xed_operand_read(curOp))
{
fprintf(out,"not read or written ");
}
}
for(int i = 0; i < numMemOps; i++)
{
// if( !xed_decoded_inst_mem_read(&ins,i) && !xed_decoded_inst_mem_written(&ins,i))
{
fprintf(out, "mem op(");
xed_reg_enum_t seg = xed_decoded_inst_get_seg_reg(&ins,i);
xed_reg_enum_t base = xed_decoded_inst_get_base_reg(&ins,i);
xed_reg_enum_t index = xed_decoded_inst_get_index_reg(&ins,i);
if (seg != XED_REG_INVALID)
{
fprintf(out, "seg=%s ",xed_reg_enum_t2str(seg));
}
if (base != XED_REG_INVALID)
{
fprintf(out, "base=%s ",xed_reg_enum_t2str(base));
}
if (index != XED_REG_INVALID)
{
fprintf(out, "index=%s ",xed_reg_enum_t2str(index));
}
fprintf(out, ")");
}
}
if(addr != NULL)
fprintf(out, "addr=%p ",addr);
fprintf(out, "value=%ld\n",value);
}
// Do not use if debugData is available.
VOID disassemblyToBuffInternal(char * decodeBuffer, VOID *ip, const xed_decoded_inst_t *ins)
{
xed_print_info_t pi;
xed_init_print_info(&pi);
pi.p = ins;
pi.blen = 1024;
pi.buf = decodeBuffer;
pi.context = 0;
pi.disassembly_callback = 0;
pi.runtime_address = STATIC_CAST(xed_uint64_t,ip);
//pi.syntax = syntax
pi.format_options_valid = 0; // use defaults
pi.buf[0]=0; //allow use of strcat
int ok = xed_format_generic(&pi);
if (!ok)
{
pi.blen = xed_strncpy(pi.buf,"Error disassembling ",pi.blen);
pi.blen = xed_strncat(pi.buf,
xed_syntax_enum_t2str(pi.syntax),
pi.blen);
pi.blen = xed_strncat(pi.buf," syntax.",pi.blen);
}
}
const char *getInstCategoryString(VOID *ip)
{
xed_state_t dstate;
xed_state_zero(&dstate);
xed_state_init2(&dstate,XED_MACHINE_MODE_LONG_64,XED_ADDRESS_WIDTH_64b);
xed_decoded_inst_t ins;
xed_decoded_inst_zero_set_mode(&ins, &dstate);
xed_decode(&ins,STATIC_CAST(const xed_uint8_t*,ip),15);
return xed_category_enum_t2str(xed_inst_category(xed_decoded_inst_inst(&(ins))));
}