-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulate.cc
More file actions
430 lines (385 loc) · 14.1 KB
/
simulate.cc
File metadata and controls
430 lines (385 loc) · 14.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
#include <capstone/capstone.h>
#include <glib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "ideal.hh"
#include "exagear.hh"
#include "rosetta.hh"
#include "latx.hh"
#include "qemu.hh"
#include "zen2.hh"
#include "haswell.hh"
#include "icelake.hh"
#include "instrument.hh"
enum BT_TYPE {
BT_IDEAL,
BT_EXAGEAR,
BT_ROSETTA,
BT_LATX,
BT_QEMU,
BT_ZEN2,
BT_HASWELL,
BT_ICELAKE,
};
BT_TYPE bt_type = BT_IDEAL;
BT *bt;
typedef struct {
uint64_t count;
uint64_t inflt_sum;
GSList *pccnts;
bool unknown;
unsigned int id;
} InstCount;
InstCount inststat[X86_INS_ENDING + MY_GRP_ENDING] = {0};
const char *mygrp_name(csh cs_handle, unsigned int id) {
if (id < X86_INS_ENDING) {
return cs_insn_name(cs_handle, id);
} else {
unsigned int grp = id - X86_INS_ENDING;
return mygrpsinfo[grp].name;
}
}
FILE *file;
csh cs_handle;
static GHashTable *tbstat;
static bool verbose;
static void init(void) {
// init capstone
struct target_info {
const char *name;
cs_arch arch;
cs_mode mode;
};
struct target_info all_archs[] = {
{ "aarch64", CS_ARCH_ARM64, (cs_mode)(CS_MODE_LITTLE_ENDIAN) },
{ "mips64el", CS_ARCH_MIPS, (cs_mode)(CS_MODE_MIPS64|CS_MODE_LITTLE_ENDIAN) },
{ "mips64", CS_ARCH_MIPS, (cs_mode)(CS_MODE_MIPS64|CS_MODE_BIG_ENDIAN) },
{ "i386", CS_ARCH_X86, (cs_mode)(CS_MODE_32) },
{ "x86_64", CS_ARCH_X86, (cs_mode)(CS_MODE_64) },
{ "riscv32", CS_ARCH_RISCV, (cs_mode)(CS_MODE_RISCV32|CS_MODE_RISCVC) },
{ "riscv64", CS_ARCH_RISCV, (cs_mode)(CS_MODE_RISCV64|CS_MODE_RISCVC) },
{ NULL }
};
char target_name[10];
if (EOF == fscanf(file, "%s\n", target_name)) {
perror("fscanf"); exit(-1);
}
struct target_info *target;
cs_err err;
for (int i = 0; all_archs[i].name; i++) {
if (!strcmp(all_archs[i].name, target_name)) {
target = &all_archs[i];
err = cs_open(all_archs[i].arch, all_archs[i].mode, &cs_handle);
if (!err) {
cs_option(cs_handle, CS_OPT_DETAIL, CS_OPT_ON);
} else {
fprintf(stderr, "pcstat: csopen fail, %s\n", cs_strerror(err));
abort();
}
break;
}
}
cs_option(cs_handle, CS_OPT_DETAIL, CS_OPT_ON);
// init hashtable
tbstat = g_hash_table_new(NULL, g_direct_equal);
// init inststat
for (int i=0; i<X86_INS_ENDING+MY_GRP_ENDING; i++)
inststat[i].id = i;
// init BT instance
switch (bt_type) {
case BT_IDEAL:
bt = new Ideal();
break;
case BT_EXAGEAR:
bt = new Exagear();
break;
case BT_ROSETTA:
bt = new Rosetta();
break;
case BT_LATX:
bt = new Latx();
break;
case BT_QEMU:
bt = new Qemu();
break;
case BT_ZEN2:
bt = new Zen2();
break;
case BT_HASWELL:
bt = new Haswell();
break;
case BT_ICELAKE:
bt = new Icelake();
break;
}
}
static void analyse_output(void) {
GHashTableIter iter;
gpointer key, value;
GHashTable *pcstat = g_hash_table_new(NULL, g_direct_equal);
// build pcstat from tbstat and get sum
// calculate opt
int64_t overall_inflt_sum = 0;
uint64_t sum = 0;
g_hash_table_iter_init(&iter, tbstat);
while (g_hash_table_iter_next(&iter, &key, &value)) {
uint64_t hash = (uint64_t)key;
TBCount *tbcnt = (TBCount *)value;
overall_inflt_sum += bt->opt(tbcnt);
overall_inflt_sum += bt->pessi(tbcnt);
for (int i = 0; i < tbcnt->n_insns; i++) {
uint64_t vaddr = tbcnt->cs_insns[i]->address;
PCCount *pccnt =
(PCCount *)g_hash_table_lookup(pcstat, (gconstpointer)vaddr);
if (pccnt) {
pccnt->count += tbcnt->count;
} else {
pccnt = g_new0(PCCount, 1);
pccnt->vaddr = vaddr;
pccnt->count = tbcnt->count;
pccnt->insn = tbcnt->cs_insns[i];
g_hash_table_insert(pcstat, (gpointer)vaddr, (gpointer)pccnt);
}
}
}
g_hash_table_iter_init(&iter, tbstat);
while (g_hash_table_iter_next(&iter, &key, &value)) {
uint64_t hash = (uint64_t)key;
TBCount *tbcnt = (TBCount *)value;
sum += tbcnt->count * tbcnt->n_insns;
}
// build inststat
g_hash_table_iter_init(&iter, pcstat);
while (g_hash_table_iter_next(&iter, &key, &value)) {
PCCount *pccnt = (PCCount *)value;
unsigned int id = pccnt->insn->id;
InstCount *instcnt = &inststat[id];
instcnt->count += pccnt->count;
BaseExtra base_extra = bt->inflt(pccnt);
if (base_extra.first == -1) {
base_extra.first = 1;
instcnt->unknown = true;
}
pccnt->inflt = base_extra.first + base_extra.second;
instcnt->inflt_sum += pccnt->inflt * pccnt->count;
instcnt->pccnts = g_slist_prepend(instcnt->pccnts, pccnt);
}
// map insts to my group
for (int grp=MY_GRP_BEGINING; grp<MY_GRP_ENDING; grp++) {
MyGroupInfo *grpinfo = &mygrpsinfo[grp];
for (int i=0; i<grpinfo->n; i++) {
unsigned int id = grpinfo->ids[i];
InstCount *ic_src = &inststat[id];
InstCount *ic_dst = &inststat[X86_INS_ENDING + grp];
ic_dst->count += ic_src->count;
ic_dst->inflt_sum += ic_src->inflt_sum;
ic_dst->pccnts = g_slist_concat(ic_dst->pccnts, ic_src->pccnts);
ic_dst->unknown |= ic_src->unknown;
ic_src->count = 0;
ic_src->inflt_sum = 0;
ic_src->pccnts = NULL;
ic_src->unknown = false;
}
}
// sort inststat as instseq, calc inflt_sum by the way
GSequence *instseq = g_sequence_new(NULL);
for (unsigned int id=0; id<X86_INS_ENDING+MY_GRP_ENDING; id++) {
InstCount *instcnt = &inststat[id];
if (instcnt->count) {
overall_inflt_sum += instcnt->inflt_sum;
g_sequence_insert_sorted(instseq, (gpointer)instcnt,
[](gconstpointer a, gconstpointer b, gpointer _) {
InstCount *ica = (InstCount *)a;
InstCount *icb = (InstCount *)b;
/* uint64_t excessa = ica->inflt_sum > ica->count ? */
/* ica->inflt_sum - ica->count : 0; */
/* uint64_t excessb = icb->inflt_sum > icb->count ? */
/* icb->inflt_sum - icb->count : 0; */
/* if (excessa < excessb) return -1; */
/* else if (excessa == excessb) return 0; */
/* else return 1; */
if (ica->inflt_sum < icb->inflt_sum) return -1;
else if (ica->inflt_sum == icb->inflt_sum) return 0;
else return 1;
},
NULL
);
// sort pccnts by descending
instcnt->pccnts = g_slist_sort(instcnt->pccnts,
[](gconstpointer a, gconstpointer b) {
PCCount *pcca = (PCCount *)a;
PCCount *pccb = (PCCount *)b;
uint64_t excessa = pcca->inflt > 1 ?
(pcca->inflt - 1) * pcca->count : 0;
uint64_t excessb = pccb->inflt > 1 ?
(pccb->inflt - 1) * pccb->count : 0;
if (excessa < excessb) return 1;
else if (excessa == excessb) return 0;
else return -1;
}
);
}
}
double overall_inflt = 1.0 * overall_inflt_sum / sum;
// print instseq (sorted inststat)
GString *report = g_string_new(NULL);
for (GSequenceIter *seqi = g_sequence_get_begin_iter(instseq);
!g_sequence_iter_is_end(seqi);
seqi = g_sequence_iter_next(seqi)) {
InstCount *instcnt = (InstCount *)g_sequence_get(seqi);
unsigned int id = instcnt->id;
if (instcnt->count) {
double inflt = 1.0 * instcnt->inflt_sum / instcnt->count;
double freq_percent = 100.0 * instcnt->count / sum;
if (instcnt->unknown)
g_string_append_printf(report, "Unknown inst %s\n", mygrp_name(cs_handle, id));
g_string_append_printf(report, "%s %ld * %.4f %.2f%% %.4f\n",
mygrp_name(cs_handle, id), instcnt->count, inflt, freq_percent,
1.0 * inflt * instcnt->count / sum);
if (verbose) {
for (GSList *ele=instcnt->pccnts; ele; ele=g_slist_next(ele)) {
PCCount *pccnt = (PCCount *)ele->data;
char mcstr[48]; // machine code string
cs_insn *insn = pccnt->insn;
for (int i=0; i<insn->size; i++) {
sprintf(mcstr+i*2, "%02x", insn->bytes[i]);
}
g_string_append_printf(report, " %lx %ld * %ld %.2f%% %s %s %s\n",
pccnt->vaddr,
pccnt->count, pccnt->inflt, 100.0 * pccnt->count / sum,
mcstr, insn->mnemonic, insn->op_str);
g_string_append_printf(report, " ");
for (int i=0; i<insn->detail->x86.op_count; i++) {
cs_x86 *x86 = &insn->detail->x86;
cs_x86_op *op = &x86->operands[i];
if (op->type == X86_OP_MEM) {
g_string_append_printf(report, "disp_%d %lx ",
x86->encoding.disp_size, op->mem.disp
);
}
if (op->type == X86_OP_IMM) {
g_string_append_printf(report, "imm_%d %lx ",
x86->encoding.imm_size, op->imm
);
}
}
g_string_append_printf(report, "\n");
for (size_t i = MY_FEAT_BEGINING; i < MY_FEAT_ENDING; i++) {
if (pccnt->feats[i]) {
g_string_append_printf(report, " %s %x\n",
MyBTFeatStr[i], pccnt->feats[i]);
}
}
}
}
}
}
for (int i=MY_FEAT_BEGINING; i<MY_FEAT_ENDING; i++) {
g_string_append_printf(report, "%s %.4f\n",
MyBTFeatStr[i], 1.0*excess_by_isa_feat[i]/sum);
}
g_string_append_printf(report, "insts %ld inflation %ld %.4f\n",
sum, overall_inflt_sum, overall_inflt);
printf("%s", report->str);
// clean pcstat
g_hash_table_iter_init(&iter, pcstat);
while (g_hash_table_iter_next(&iter, &key, &value)) {
PCCount *pccnt = (PCCount *)value;
g_free(pccnt);
}
g_hash_table_destroy(pcstat);
// clean instseq
g_sequence_free(instseq);
g_string_free(report, TRUE);
}
static void clean(void) {
// clean pcstat and capstone
GHashTableIter iter;
gpointer key, value;
g_hash_table_iter_init(&iter, tbstat);
while (g_hash_table_iter_next(&iter, &key, &value)) {
TBCount *tbcnt = (TBCount *)value;
for (int i = 0; i < tbcnt->n_insns; i++) {
cs_free(tbcnt->cs_insns[i], 1);
}
free(tbcnt->cs_insns);
g_free(tbcnt);
}
g_hash_table_destroy(tbstat);
cs_close(&cs_handle);
// clean BT instance
delete bt;
}
static void read_tbstat() {
while (!feof(file)) {
Instru_TBCount *instru_tbcnt = new Instru_TBCount(file);
TBCount *tbcnt = g_new0(TBCount, 1);
tbcnt->count = instru_tbcnt->count;
tbcnt->vaddr = instru_tbcnt->vaddr;
tbcnt->n_insns = instru_tbcnt->n_insns;
tbcnt->cs_insns = (cs_insn **)malloc(sizeof(cs_insn *) * tbcnt->n_insns);
for (int i=0; i<tbcnt->n_insns; i++) {
Instru_Inst *instru_insn = instru_tbcnt->insns[i];
cs_disasm(cs_handle,
instru_insn->bytes, instru_insn->n_bytes, instru_insn->addr,
1, &(tbcnt->cs_insns[i])
);
}
uint64_t hash = tbcnt->vaddr ^ tbcnt->n_insns;
g_hash_table_insert(tbstat, (gpointer)hash, (gpointer)tbcnt);
delete instru_tbcnt;
}
}
void usage(void) {
printf("Usage: de-flate [-h] [-v] -f <trace file> [-t <bt>]\n");
printf(" -h: print this help\n");
printf(" -v: verbose\n");
printf(" -f <trace file>: the path to the trace file, generated by instrument.so\n");
printf(" -t <bt>: binary translator type: ideal(default), exagear, rosetta, latx, qemu\n");
exit(0);
}
int main(int argc, char **argv) {
{int c; while ((c = getopt(argc, argv, "hvf:t:")) != -1) {
switch (c) {
case 'h':
usage();
break;
case 'v':
verbose = true;
break;
case 'f':
file = fopen(optarg, "r");
if (file == NULL) {
perror("fopen");
exit(-1);
}
break;
case 't':
if (g_strcmp0(optarg, "exagear") == 0) {
bt_type = BT_EXAGEAR;
} else if (g_strcmp0(optarg, "rosetta") == 0) {
bt_type = BT_ROSETTA;
} else if (g_strcmp0(optarg, "latx") == 0) {
bt_type = BT_LATX;
} else if (g_strcmp0(optarg, "qemu") == 0) {
bt_type = BT_QEMU;
} else if (g_strcmp0(optarg, "zen2") == 0) {
bt_type = BT_ZEN2;
} else if (g_strcmp0(optarg, "haswell") == 0) {
bt_type = BT_HASWELL;
} else if (g_strcmp0(optarg, "icelake") == 0) {
bt_type = BT_ICELAKE;
} else {
bt_type = BT_IDEAL;
}
break;
}
}}
if (file == NULL) usage();
init();
read_tbstat();
analyse_output();
clean();
return 0;
}