-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.h
More file actions
538 lines (503 loc) · 13.1 KB
/
utils.h
File metadata and controls
538 lines (503 loc) · 13.1 KB
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
#ifndef __UTILS_H__
#define __UTILS_H__
#include "dr_api.h"
// TODO: search for dynamorio interface to obtain element witdh of a SIMD operation
// As the dynamorio did not provide function to obtain SIMD operation width of each element, Zerospy mannually implements with a lookup table
#ifdef ARM_CCTLIB
#ifdef ARM
uint32_t
FloatOperandSizeTable(instr_t *instr, opnd_t opnd)
{
uint size = opnd_size_in_bytes(opnd_get_size(opnd));
if(size!=16 && size!=32 && size!=64) {
// ignore those non-vectorized instructions (not start with OP_v*)
return size;
}
int opc = instr_get_opcode(instr);
switch (opc) {
/* convert instructions */
case OP_vcvt_f32_f16:
case OP_vcvtb_f32_f16:
case OP_vcvtb_f64_f16:
case OP_vcvtt_f32_f16:
case OP_vcvtt_f64_f16:
/* end of convert instructions */
case OP_vrev16_16: // ?
case OP_vrev32_16: // ?
case OP_vrev64_16: // ?
case OP_vtrn_16: // ?
case OP_vtst_16: // ?
case OP_vuzp_16: // ?
case OP_vzip_16: // ?
return 2;
case OP_vabs_f32:
case OP_vacge_f32:
case OP_vacgt_f32:
case OP_vadd_f32:
case OP_vceq_f32:
case OP_vcge_f32:
case OP_vcgt_f32:
case OP_vcle_f32:
case OP_vclt_f32:
case OP_vcmp_f32:
case OP_vcmpe_f32:
/* convert instructions */
case OP_vcvt_f16_f32:
case OP_vcvt_f64_f32:
case OP_vcvt_s16_f32:
case OP_vcvt_s32_f32:
case OP_vcvt_u16_f32:
case OP_vcvt_u32_f32:
case OP_vcvta_s32_f32:
case OP_vcvta_u32_f32:
case OP_vcvtb_f16_f32:
case OP_vcvtm_s32_f32:
case OP_vcvtm_u32_f32:
case OP_vcvtn_s32_f32:
case OP_vcvtn_u32_f32:
case OP_vcvtp_s32_f32:
case OP_vcvtp_u32_f32:
case OP_vcvtr_s32_f32:
case OP_vcvtr_u32_f32:
case OP_vcvtt_f16_f32:
/* end of convert instructions */
case OP_vdiv_f32:
case OP_vfma_f32:
case OP_vfms_f32:
case OP_vfnma_f32:
case OP_vfnms_f32:
case OP_vmax_f32:
case OP_vmaxnm_f32:
case OP_vmin_f32:
case OP_vminnm_f32:
case OP_vmla_f32:
case OP_vmls_f32:
case OP_vmov_f32:
case OP_vmul_f32:
case OP_vneg_f32:
case OP_vnmla_f32:
case OP_vnmls_f32:
case OP_vnmul_f32:
case OP_vpadd_f32:
case OP_vpmax_f32:
case OP_vpmin_f32:
case OP_vrecpe_f32:
case OP_vrecps_f32:
case OP_vrev32_32: // ?
case OP_vrev64_32: // ?
case OP_vrinta_f32_f32: // ?
case OP_vrintm_f32_f32: // ?
case OP_vrintn_f32_f32: // ?
case OP_vrintp_f32_f32: // ?
case OP_vrintr_f32: // ?
case OP_vrintx_f32: // ?
case OP_vrintx_f32_f32: // ?
case OP_vrintz_f32: // ?
case OP_vrintz_f32_f32: // ?
case OP_vrsqrte_f32:
case OP_vrsqrts_f32:
case OP_vsel_eq_f32:
case OP_vsel_ge_f32:
case OP_vsel_gt_f32:
case OP_vsel_vs_f32:
case OP_vsqrt_f32:
case OP_vsub_f32:
case OP_vtrn_32: // ?
case OP_vtst_32: // ?
case OP_vuzp_32: // ?
case OP_vzip_32: // ?
return 4;
case OP_vabs_f64:
case OP_vadd_f64:
case OP_vcmp_f64:
case OP_vcmpe_f64:
/* convert instructions */
case OP_vcvt_f32_f64:
case OP_vcvt_s16_f64:
case OP_vcvt_s32_f64:
case OP_vcvt_u16_f64:
case OP_vcvt_u32_f64:
case OP_vcvta_s32_f64:
case OP_vcvta_u32_f64:
case OP_vcvtb_f16_f64:
case OP_vcvtm_s32_f64:
case OP_vcvtm_u32_f64:
case OP_vcvtn_s32_f64:
case OP_vcvtn_u32_f64:
case OP_vcvtp_s32_f64:
case OP_vcvtp_u32_f64:
case OP_vcvtr_s32_f64:
case OP_vcvtr_u32_f64:
case OP_vcvtt_f16_f64:
/* end of convert instructions */
case OP_vdiv_f64:
case OP_vfma_f64:
case OP_vfms_f64:
case OP_vfnma_f64:
case OP_vfnms_f64:
case OP_vmaxnm_f64:
case OP_vminnm_f64:
case OP_vmla_f64:
case OP_vmls_f64:
case OP_vmov_f64:
case OP_vmul_f64:
case OP_vneg_f64:
case OP_vnmla_f64:
case OP_vnmls_f64:
case OP_vnmul_f64:
case OP_vrinta_f64_f64: // ?
case OP_vrintm_f64_f64: // ?
case OP_vrintn_f64_f64: // ?
case OP_vrintp_f64_f64: // ?
case OP_vrintr_f64: // ?
case OP_vrintx_f64: // ?
case OP_vrintz_f64: // ?
case OP_vsel_eq_f64:
case OP_vsel_ge_f64:
case OP_vsel_gt_f64:
case OP_vsel_vs_f64:
case OP_vsqrt_f64:
case OP_vsub_f64:
return 8;
default: return 0;
}
}
#else
uint32_t
FloatOperandSizeTable(instr_t *instr, opnd_t opnd)
{
uint size = opnd_size_in_bytes(opnd_get_size(opnd));
if(size!=16 && size!=32 && size!=64) {
// ignore those non-vectorized instructions (not start with OP_v*)
return size;
}
opnd_t width = instr_get_src(instr, instr_num_srcs(instr) - 1);
if (!opnd_is_immed_int(width)) return size;
switch (opnd_get_immed_int(width)) {
case VECTOR_ELEM_WIDTH_HALF:
return 2;
case VECTOR_ELEM_WIDTH_SINGLE:
return 4;
case VECTOR_ELEM_WIDTH_DOUBLE:
return 8;
default:
return 0;
}
}
#endif
bool instr_is_ignorable(instr_t *ins) {
int opc = instr_get_opcode(ins);
switch (opc) {
#if defined(AARCH64)
case OP_ld3:
case OP_ldr:
case OP_ld3r:
return true;
#endif
default:
return false;
}
return false;
}
#else
uint32_t
FloatOperandSizeTable(instr_t *instr, opnd_t opnd)
{
uint size = opnd_size_in_bytes(opnd_get_size(opnd));
if(size!=16 && size!=32 && size!=64) {
// ignore those non-vectorized instructions (not start with OP_v*)
return size;
}
int opc = instr_get_opcode(instr);
switch (opc) {
// TODO: packed 128-bit floating-point, treat them as two double floating points
case OP_vinsertf128:
case OP_vextractf128:
case OP_vbroadcastf128:
case OP_vperm2f128:
return 8;
//return 16;
case OP_vmovss:
case OP_vmovups:
case OP_vmovlps:
case OP_vmovsldup:
case OP_vmovhps:
case OP_vmovshdup:
case OP_vmovaps:
case OP_vmovntps:
case OP_unpcklps:
case OP_unpckhps:
case OP_vunpcklps:
case OP_vunpckhps:
case OP_extractps:
case OP_insertps:
case OP_vextractps:
case OP_vinsertps:
case OP_vbroadcastss:
case OP_vpermilps:
case OP_vmaskmovps:
case OP_vshufps:
return 4;
case OP_vmovsd:
case OP_vmovupd:
case OP_vmovlpd:
case OP_vmovddup:
case OP_vmovhpd:
case OP_vmovapd:
case OP_vmovntpd:
case OP_unpcklpd:
case OP_unpckhpd:
case OP_vunpcklpd:
case OP_vunpckhpd:
case OP_vbroadcastsd:
case OP_vpermilpd:
case OP_vmaskmovpd:
case OP_vshufpd:
return 8;
/* Ignore Convert instructions */
/* SSE3/3D-Now!/SSE4 */
case OP_haddps:
case OP_hsubps:
case OP_addsubps:
case OP_femms:
case OP_movntss:
case OP_blendvps:
case OP_roundps:
case OP_roundss:
case OP_blendps:
case OP_dpps:
return 4;
case OP_haddpd:
case OP_hsubpd:
case OP_addsubpd:
case OP_movntsd:
case OP_blendvpd:
case OP_roundpd:
case OP_roundsd:
case OP_blendpd:
case OP_dppd:
return 8;
/* AVX */
case OP_vucomiss:
case OP_vcomiss:
case OP_vmovmskps:
case OP_vsqrtps:
case OP_vsqrtss:
case OP_vrsqrtps:
case OP_vrsqrtss:
case OP_vrcpps:
case OP_vrcpss:
case OP_vandps:
case OP_vandnps:
case OP_vorps:
case OP_vxorps:
case OP_vaddps:
case OP_vaddss:
case OP_vmulps:
case OP_vmulss:
case OP_vsubss:
case OP_vsubps:
case OP_vminps:
case OP_vminss:
case OP_vdivps:
case OP_vdivss:
case OP_vmaxps:
case OP_vmaxss:
case OP_vcmpps:
case OP_vcmpss:
case OP_vhaddps:
case OP_vhsubps:
case OP_vaddsubps:
case OP_vblendvps:
case OP_vroundps:
case OP_vroundss:
case OP_vblendps:
case OP_vdpps:
case OP_vtestps:
return 4;
case OP_vucomisd:
case OP_vcomisd:
case OP_vmovmskpd:
case OP_vsqrtpd:
case OP_vsqrtsd:
case OP_vandpd:
case OP_vandnpd:
case OP_vorpd:
case OP_vxorpd:
case OP_vaddpd:
case OP_vaddsd:
case OP_vmulpd:
case OP_vmulsd:
case OP_vsubpd:
case OP_vsubsd:
case OP_vminpd:
case OP_vminsd:
case OP_vdivpd:
case OP_vdivsd:
case OP_vmaxpd:
case OP_vmaxsd:
case OP_vcmppd:
case OP_vcmpsd:
case OP_vhaddpd:
case OP_vhsubpd:
case OP_vaddsubpd:
case OP_vblendvpd:
case OP_vroundpd:
case OP_vroundsd:
case OP_vblendpd:
case OP_vdppd:
case OP_vtestpd:
return 8;
/* SSE packed instruction */
case OP_addpd:
case OP_mulpd:
return 8;
case OP_addps:
case OP_mulps:
return 4;
/* FMA */
case OP_vfmadd132ps:
case OP_vfmadd213ps:
case OP_vfmadd231ps:
case OP_vfmadd132ss:
case OP_vfmadd213ss:
case OP_vfmadd231ss:
case OP_vfmaddsub132ps:
case OP_vfmaddsub213ps:
case OP_vfmaddsub231ps:
case OP_vfmsubadd132ps:
case OP_vfmsubadd213ps:
case OP_vfmsubadd231ps:
case OP_vfmsub132ps:
case OP_vfmsub213ps:
case OP_vfmsub231ps:
case OP_vfmsub132ss:
case OP_vfmsub213ss:
case OP_vfnmadd132ps:
case OP_vfnmadd213ps:
case OP_vfnmadd231ps:
case OP_vfnmadd132ss:
case OP_vfnmadd213ss:
case OP_vfnmadd231ss:
case OP_vfnmsub213ps:
case OP_vfnmsub132ss:
case OP_vfnmsub213ss:
case OP_vfnmsub231ss:
return 4;
case OP_vfmadd132pd:
case OP_vfmadd213pd:
case OP_vfmadd231pd:
case OP_vfmadd132sd:
case OP_vfmadd213sd:
case OP_vfmadd231sd:
case OP_vfmaddsub132pd:
case OP_vfmaddsub213pd:
case OP_vfmaddsub231pd:
case OP_vfmsubadd132pd:
case OP_vfmsubadd213pd:
case OP_vfmsubadd231pd:
case OP_vfmsub132pd:
case OP_vfmsub213pd:
case OP_vfmsub231pd:
case OP_vfmsub132sd:
case OP_vfmsub213sd:
case OP_vfmsub231ss:
case OP_vfmsub231sd:
case OP_vfnmadd132pd:
case OP_vfnmadd213pd:
case OP_vfnmadd231pd:
case OP_vfnmadd132sd:
case OP_vfnmadd213sd:
case OP_vfnmadd231sd:
case OP_vfnmsub132ps:
case OP_vfnmsub132pd:
case OP_vfnmsub213pd:
case OP_vfnmsub231ps:
case OP_vfnmsub231pd:
case OP_vfnmsub132sd:
case OP_vfnmsub213sd:
case OP_vfnmsub231sd:
return 8;
default: return 0;
}
}
// Currently, we mark x87 control instructions handling FPU states are ignorable (not insterested)
bool instr_is_ignorable(instr_t *ins) {
int opc = instr_get_opcode(ins);
switch (opc) {
case OP_fldenv:
case OP_fldcw:
case OP_fnstenv:
case OP_fnstcw:
case OP_fnsave:
case OP_fnstsw:
case OP_frstor:
return true;
default:
return false;
}
return false;
}
#endif
#endif
#if defined(ARM) || defined(AARCH64)
bool instr_is_floating_self(instr_t* instr) {
int num_srcs = instr_num_srcs(instr);
for(int i=0; i<num_srcs; ++i) {
opnd_t opnd = instr_get_src(instr, i);
if(!opnd_is_memory_reference(opnd)) {
for(int j=0; j<opnd_num_regs_used(opnd); ++j) {
reg_id_t reg_used = opnd_get_reg_used(opnd, j);
// check for whether the register is floating point
if(reg_used>=DR_REG_Q0 && reg_used<=DR_REG_B31) {
return true;
}
}
}
}
int num_dsts = instr_num_dsts(instr);
for(int i=0; i<num_dsts; ++i) {
opnd_t opnd = instr_get_dst(instr, i);
if(!opnd_is_memory_reference(opnd)) {
for(int j=0; j<opnd_num_regs_used(opnd); ++j) {
reg_id_t reg_used = opnd_get_reg_used(opnd, j);
// check for whether the register is floating point
if(reg_used>=DR_REG_Q0 && reg_used<=DR_REG_B31) {
return true;
}
}
}
}
return false;
}
bool instr_is_floating(instr_t* instr) {
bool is_float = instr_is_floating_self(instr);
// if it is memory load operation, check for the defined register usage
if(!is_float && instr_reads_memory(instr)) {
// extract defined register
int num_dsts = instr_num_dsts(instr);
for(int i=0; i<num_dsts; ++i) {
opnd_t opnd = instr_get_dst(instr, i);
// we ignore the non-register dst opnd
if(opnd_is_reg(opnd)) {
reg_id_t reg_def = opnd_get_reg(opnd);
for (instr_t* ins = instr; ins != NULL; ins = instr_get_next(ins)) {
// Here, we aggressively suppose that if reg_def is used in a
// instruction and it is a floating point instruction, this
// instruction should be parsed as floating point reads.
int num_srcs = instr_num_srcs(ins);
for(int j=0; j<num_srcs; ++j) {
opnd_t opnd = instr_get_src(ins, j);
if (opnd_is_reg(opnd) && opnd_get_reg(opnd) == reg_def) {
return instr_is_floating_self(ins);
}
}
}
} else {
dr_fprintf(STDERR, "??? Non register dst operand!\n");
}
}
}
return is_float;
}
#endif