forked from capstone-engine/capstone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_file_benchmark.c
82 lines (73 loc) · 1.82 KB
/
test_file_benchmark.c
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
// Modified from https://github.com/athre0z/disas-bench
#include "./load_bin.inc"
#include <capstone/capstone.h>
int main(int argc, char* argv[])
{
csh handle = 0;
cs_insn *insn = NULL;
int ret = 0;
const uint8_t *code_iter = NULL;
size_t code_len_iter = 0;
uint64_t ip = 0;
size_t num_valid_insns = 0;
size_t num_bad_insn = 0;
size_t round;
if (cs_open(CS_ARCH_X86, CS_MODE_64, &handle) != CS_ERR_OK)
{
fputs("Unable to create Capstone handle\n", stderr);
ret = 1;
goto leave;
}
uint8_t *code = NULL;
size_t code_len = 0, loop_count = 0;
if (!read_file(argc, argv, &code, &code_len, &loop_count))
{
ret = 1;
goto leave;
}
insn = cs_malloc(handle);
if (!insn)
{
fputs("Failed to allocate memory\n", stderr);
ret = 1;
goto leave;
}
clock_t start_time = clock();
for (round = 0; round < loop_count; ++round)
{
code_iter = code;
code_len_iter = code_len;
while (code_len_iter > 0)
{
if (!cs_disasm_iter(
handle,
&code_iter,
&code_len_iter,
&ip,
insn
))
{
++code_iter;
--code_len_iter;
++num_bad_insn;
}
else
{
++num_valid_insns;
}
}
}
clock_t end_time = clock();
printf(
"Disassembled %zu instructions (%zu valid, %zu bad), %.2f ms\n",
num_valid_insns + num_bad_insn,
num_valid_insns,
num_bad_insn,
(double)(end_time - start_time) * 1000.0 / CLOCKS_PER_SEC
);
leave:
if (insn) cs_free(insn, 1);
if (handle) cs_close(&handle);
if (code) free(code);
return ret;
}