1 /* Capstone Disassembler Engine */
2 /* By bughoho <bughoho@gmail.com>, 2015> */
3 
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <time.h>
7 
8 #include <capstone/platform.h>
9 #include <capstone/capstone.h>
10 
11 static void test()
12 {
13 #define X86_CODE32 "\x53\x8B\xDC\x83\xEC\x08\x83\xE4\xF0\x83\xC4\x04\x55\x8B\x6B\x04\x89\x6C\x24\x04\x8B\xEC\x83\xEC\x78\xA1\x90\xA3\x4B\x01\x33\xC5 \
14 \x89\x45\xFC\x8B\x41\x04\x0F\x28\x05\x80\x30\x20\x01\x0F\x29\x45\xD0\x0F\x28\x05\x50\xAB\x1E\x01\x89\x4D\x90\x89\x45\xB8\x0F\x29 \
15 \x45\xE0\x56\x8B\x73\x08\x57\xC7\x06\x00\x00\x00\x00\xC7\x46\x04\x00\x00\x00\x00\xC7\x46\x08\x00\x00\x00\x00\xC7\x46\x0C\x00\x00 \
16 \x00\x00\x85\xC0\x0F\x84\xCB\x01\x00\x00\x33\xFF\x8D\x64\x24\x00\x8B\x01\x8B\x0C\x07\x89\x4D\xBC\x85\xC9\x0F\x84\xA6\x01\x00\x00 \
17 \x8B\x43\x0C\x0F\x10\x00\x0F\x29\x45\xD0\x0F\x10\x40\x10\x0F\x29\x45\xE0\x8B\x01\x8B\x40\x08\xFF\xD0\xF3\x0F\x10\x65\xD0\x8D\x55 \
18 \xD0\xF3\x0F\x10\x55\xD4\xF3\x0F\x10\x6D\xE0\xF3\x0F\x10\x48\x10\xF3\x0F\x10\x00\xF3\x0F\x10\x5D\xE4\xF3\x0F\x59\xCA\x8B\x4D\xBC \
19 \xF3\x0F\x59\xC4\x52\x8D\x55\xC0\x52\xF3\x0F\x58\xC8\xF3\x0F\x11\x4D\xD0\xF3\x0F\x10\x48\x10\xF3\x0F\x10\x00\xF3\x0F\x59\xCB\xF3 \
20 \x0F\x59\xC5\xF3\x0F\x58\xC8\xF3\x0F\x11\x4D\xE0\x0F\x28\xCC\xF3\x0F\x59\x48\x04\xF3\x0F\x10\x40\x14\xF3\x0F\x59\xC2\xF3\x0F\x58 \
21 \xC8\xF3\x0F\x11\x4D\xD4\x0F\x28\xCD\xF3\x0F\x10\x40\x14\xF3\x0F\x59\x48\x04\xC7\x45\xE8\x00\x00\x00\x00\xF3\x0F\x59\xC3\xC7\x45 \
22 \xD8\x00\x00\x00\x00\xF3\x0F\x58\xC8\xF3\x0F\x11\x4D\xE4\xF3\x0F\x59\x60\x0C\xF3\x0F\x59\x50\x1C\xF3\x0F\x58\xE2\xF3\x0F\x58\x65 \
23 \xDC\xF3\x0F\x11\x65\xDC\xF3\x0F\x59\x68\x0C\xF3\x0F\x59\x58\x1C\xF3\x0F\x58\xEB\xF3\x0F\x58\x6D\xEC\xF3\x0F\x11\x6D\xEC\x8B\x01 \
24 \x8B\x80\xF8\x00\x00\x00\xFF\xD0\xF3\x0F\x10\x10\xF3\x0F\x10\x58\x08\x0F\x2F\xD3\xF3\x0F\x10\x40\x04\xF3\x0F\x10\x48\x0C\xF3\x0F \
25 \x11\x55\xA0\xF3\x0F\x11\x45\x94\xF3\x0F\x11\x5D\x98\xF3\x0F\x11\x4D\xBC\x0F\x83\x8E\x00\x00\x00\x0F\x2F\xC1\x0F\x83\x85\x00\x00 \
26 \x00\x8B\xCE\xE8\xE8\xAC\x86\xFF\xF3\x0F\x10\x65\xA0\x84\xC0\x75\x53\xF3\x0F\x10\x06\x0F\x2F\xC4\x77\x03\x0F\x28\xE0\xF3\x0F\x10 \
27 \x5E\x08\xF3\x0F\x10\x45\x98\x0F\x2F\xD8\x77\x03\x0F\x28\xD8\xF3\x0F\x10\x4E\x04\xF3\x0F\x10\x45\x94\x0F\x2F\xC8\x77\x03\x0F\x28 \
28 \xC1\xF3\x0F\x10\x4E\x0C\xF3\x0F\x10\x55\xBC\x0F\x2F\xCA\x77\x03\x0F\x28\xCA\xF3\x0F\x11\x46\x04\xF3\x0F\x11\x5E\x08\xF3\x0F\x11"
29 	/* i'm test on the ubuntu 15.04 vmware,
30 	 * Sorry I haven't linux under the physical environment,
31 	 * so the results may not be accurate.
32 	 *
33 	 * original version output:
34 	 * bug@ubuntu:~/capstone/suite/benchmark$ make
35 	 * cc -c -I../../include test_iter_benchmark.c -o test_iter_benchmark.o
36 	 * cc test_iter_benchmark.o -O3 -Wall -lcapstone -o test_iter_benchmark
37 	 * bug@ubuntu:~/capstone/suite/benchmark$ ./test_iter_benchmark
38 	 * time used:6.017613
39 	 *
40 	 * rebuild:
41 	 *
42 	 * bug@ubuntu:~/capstone$ make clean
43      * bug@ubuntu:~/capstone$ sudo make install
44      * bug@ubuntu:~/capstone$ cd suite/benchmark/
45 	 * bug@ubuntu:~/capstone/suite/benchmark$ make clean
46 	 * bug@ubuntu:~/capstone/suite/benchmark$ make
47 	 *
48 	 * modified version output:
49 	 * bug@ubuntu:~/capstone/suite/benchmark$ ./test_iter_benchmark
50 	 * time used:5.003864
51 	 *
52      * if we don't output format text string,like this:
53 	 * //handle->printer(&mci, &ss, handle->printer_info);  <-----cs.c line 700
54 	 * bug@ubuntu:~/capstone/suite/benchmark$ ./test_iter_benchmark
55 	 * time used:2.059570
56 	 */
57 
58 	csh handle;
59 	uint64_t address;
60 	cs_insn *insn;
61 	int i;
62 	cs_err err;
63 	const uint8_t *code;
64 	size_t size;
65 
66 	err = cs_open(CS_ARCH_X86, CS_MODE_32, &handle);
67 	if (err) {
68 		printf("Failed on cs_open() with error returned: %u\n", err);
69 		return;
70 	}
71 	cs_option(handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_INTEL);
72 	cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);
73 
74 	clock_t start, end;
75 	double  timeUsed;
76 
77 	start = clock();
78 	int maxcount = 10000000;
79 	insn = cs_malloc(handle);
80 	for (i = 0; i < maxcount;) {
81 		code = (const uint8_t *)X86_CODE32;
82 		address = 0x1000;
83 		size = sizeof(X86_CODE32) - 1;
84 		while(cs_disasm_iter(handle, &code, &size, &address, insn)) {
85 			i++;
86 		}
87 	}
88 	cs_free(insn, 1);
89 	cs_close(&handle);
90 	end = clock();
91 	timeUsed = (double)(end - start) / CLOCKS_PER_SEC;
92 	printf("time used:%f\n", timeUsed);
93 }
94 
95 int main()
96 {
97 	test();
98 
99 	return 0;
100 }
101