Debian Codegraph @

tests/test_mips.c

raw symbols SCIP
1 /* Capstone Disassembler Engine */cxx . . $ `<file>/tests/test_mips.c`/
2 /* By Nguyen Anh Quynh <[email protected]>, 2013-2019 */
3
4 #include <stdio.h>
5 #include <stdlib.h>
6
7 #include <capstone/platform.h>cxx . . $ `<file>/include/capstone/platform.h`/
8 #include <capstone/capstone.h>cxx . . $ `<file>/include/capstone/capstone.h`/
9
10 struct platformcxx . . $ platform# {
11 cs_archcxx . . $ cs_arch# archcxx . . $ platform#arch.;
12 cs_modecxx . . $ cs_mode# modecxx . . $ platform#mode.;
13 unsigned char *codecxx . . $ platform#code.;
14 size_t sizecxx . . $ platform#size.;
15 const char *commentcxx . . $ platform#comment.;
16 };
17
18 static cshcxx . . $ csh# handlecxx . . $ handle.;
19
20 static void print_string_hexcxx . . $ print_string_hex(24a4beb24605093f).(const char *comment, unsigned char *str, size_t len)
21 {
22 unsigned char *c;
23
24 printfcxx . . $ printf(581fddd72a8db8fc).("%s", comment);
25 for (c = str; c < str + len; c++) {
26 printfcxx . . $ printf(581fddd72a8db8fc).("0x%02x ", *c & 0xff);
27 }
28
29 printfcxx . . $ printf(581fddd72a8db8fc).("\n");
30 }
31
32 static void print_insn_detailcxx . . $ print_insn_detail(ecef60df25d96a55).(cs_insncxx . . $ cs_insn# *ins)
33 {
34 int i;
35 cs_mipscxx . . $ cs_mips# *mips;
36
37 // detail can be NULL on "data" instruction if SKIPDATA option is turned ON
38 if (ins->detailcxx . . $ cs_insn#detail. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!)
39 return;
40
41 mips = &(ins->detailcxx . . $ cs_insn#detail.->mipscxx . . $ cs_detail#$anonymous_type_0#mips.);
42 if (mips->op_countcxx . . $ cs_mips#op_count.)
43 printfcxx . . $ printf(581fddd72a8db8fc).("\top_count: %u\n", mips->op_countcxx . . $ cs_mips#op_count.);
44
45 for (i = 0; i < mips->op_countcxx . . $ cs_mips#op_count.; i++) {
46 cs_mips_opcxx . . $ cs_mips_op# *op = &(mips->operandscxx . . $ cs_mips#operands.[i]);
47 switch((int)op->typecxx . . $ cs_mips_op#type.) {
48 default:
49 break;
50 case MIPS_OP_REGcxx . . $ MIPS_OP_REG.:
51 printfcxx . . $ printf(581fddd72a8db8fc).("\t\toperands[%u].type: REG = %s\n", i, cs_reg_namecxx . . $ cs_reg_name(efd1348d9ae5172e).(handlecxx . . $ handle., op->regcxx . . $ cs_mips_op#$anonymous_type_0#reg.));
52 break;
53 case MIPS_OP_IMMcxx . . $ MIPS_OP_IMM.:
54 printfcxx . . $ printf(581fddd72a8db8fc).("\t\toperands[%u].type: IMM = 0x%" PRIx64cxx . . $ `<external>/15933738808807975373/inttypes.h:126:10`! "\n", i, op->immcxx . . $ cs_mips_op#$anonymous_type_0#imm.);
55 break;
56 case MIPS_OP_MEMcxx . . $ MIPS_OP_MEM.:
57 printfcxx . . $ printf(581fddd72a8db8fc).("\t\toperands[%u].type: MEM\n", i);
58 if (op->memcxx . . $ cs_mips_op#$anonymous_type_0#mem..basecxx . . $ mips_op_mem#base. != MIPS_REG_INVALIDcxx . . $ MIPS_REG_INVALID.)
59 printfcxx . . $ printf(581fddd72a8db8fc).("\t\t\toperands[%u].mem.base: REG = %s\n",
60 i, cs_reg_namecxx . . $ cs_reg_name(efd1348d9ae5172e).(handlecxx . . $ handle., op->memcxx . . $ cs_mips_op#$anonymous_type_0#mem..basecxx . . $ mips_op_mem#base.));
61 if (op->memcxx . . $ cs_mips_op#$anonymous_type_0#mem..dispcxx . . $ mips_op_mem#disp. != 0)
62 printfcxx . . $ printf(581fddd72a8db8fc).("\t\t\toperands[%u].mem.disp: 0x%" PRIx64cxx . . $ `<external>/15933738808807975373/inttypes.h:126:10`! "\n", i, op->memcxx . . $ cs_mips_op#$anonymous_type_0#mem..dispcxx . . $ mips_op_mem#disp.);
63
64 break;
65 }
66
67 }
68
69 printfcxx . . $ printf(581fddd72a8db8fc).("\n");
70 }
71
72 static void testcxx . . $ test(d878ad36e69247c7).()
73 {
74 #define MIPS_CODEcxx . . $ `tests/test_mips.c:74:9`! "\x0C\x10\x00\x97\x00\x00\x00\x00\x24\x02\x00\x0c\x8f\xa2\x00\x00\x34\x21\x34\x56"
75 #define MIPS_CODE2cxx . . $ `tests/test_mips.c:75:9`! "\x56\x34\x21\x34\xc2\x17\x01\x00"
76 #define MIPS_32R6Mcxx . . $ `tests/test_mips.c:76:9`! "\x00\x07\x00\x07\x00\x11\x93\x7c\x01\x8c\x8b\x7c\x00\xc7\x48\xd0"
77 #define MIPS_32R6cxx . . $ `tests/test_mips.c:77:9`! "\xec\x80\x00\x19\x7c\x43\x22\xa0"
78 #define MIPS_64SDcxx . . $ `tests/test_mips.c:78:9`! "\x70\x00\xb2\xff"
79
80 struct platformcxx . . $ platform# platforms[] = {
81 {
82 CS_ARCH_MIPScxx . . $ CS_ARCH_MIPS.,
83 (cs_modecxx . . $ cs_mode#)(CS_MODE_MIPS32cxx . . $ CS_MODE_MIPS32. | CS_MODE_BIG_ENDIANcxx . . $ CS_MODE_BIG_ENDIAN.),
84 (unsigned char *)MIPS_CODEcxx . . $ `tests/test_mips.c:74:9`!,
85 sizeof(MIPS_CODEcxx . . $ `tests/test_mips.c:74:9`!) - 1,
86 "MIPS-32 (Big-endian)"
87 },
88 {
89 CS_ARCH_MIPScxx . . $ CS_ARCH_MIPS.,
90 (cs_modecxx . . $ cs_mode#)(CS_MODE_MIPS64cxx . . $ CS_MODE_MIPS64. | CS_MODE_LITTLE_ENDIANcxx . . $ CS_MODE_LITTLE_ENDIAN.),
91 (unsigned char *)MIPS_CODE2cxx . . $ `tests/test_mips.c:75:9`!,
92 sizeof(MIPS_CODE2cxx . . $ `tests/test_mips.c:75:9`!) - 1,
93 "MIPS-64-EL (Little-endian)"
94 },
95 {
96 CS_ARCH_MIPScxx . . $ CS_ARCH_MIPS.,
97 (cs_modecxx . . $ cs_mode#)(CS_MODE_MIPS32R6cxx . . $ CS_MODE_MIPS32R6. | CS_MODE_MICROcxx . . $ CS_MODE_MICRO. | CS_MODE_BIG_ENDIANcxx . . $ CS_MODE_BIG_ENDIAN.),
98 (unsigned char*)MIPS_32R6Mcxx . . $ `tests/test_mips.c:76:9`!,
99 sizeof(MIPS_32R6Mcxx . . $ `tests/test_mips.c:76:9`!) - 1,
100 "MIPS-32R6 | Micro (Big-endian)"
101 },
102 {
103 CS_ARCH_MIPScxx . . $ CS_ARCH_MIPS.,
104 (cs_modecxx . . $ cs_mode#)(CS_MODE_MIPS32R6cxx . . $ CS_MODE_MIPS32R6. | CS_MODE_BIG_ENDIANcxx . . $ CS_MODE_BIG_ENDIAN.),
105 (unsigned char*)MIPS_32R6cxx . . $ `tests/test_mips.c:77:9`!,
106 sizeof(MIPS_32R6cxx . . $ `tests/test_mips.c:77:9`!) - 1,
107 "MIPS-32R6 (Big-endian)"
108 },
109 {
110 CS_ARCH_MIPScxx . . $ CS_ARCH_MIPS.,
111 (cs_modecxx . . $ cs_mode#)(CS_MODE_MIPS64cxx . . $ CS_MODE_MIPS64. | CS_MODE_MIPS2cxx . . $ CS_MODE_MIPS2. | CS_MODE_LITTLE_ENDIANcxx . . $ CS_MODE_LITTLE_ENDIAN.),
112 (unsigned char *)MIPS_64SDcxx . . $ `tests/test_mips.c:78:9`!,
113 sizeof(MIPS_64SDcxx . . $ `tests/test_mips.c:78:9`!) - 1,
114 "MIPS-64-EL + Mips II (Little-endian)"
115 },
116 {
117 CS_ARCH_MIPScxx . . $ CS_ARCH_MIPS.,
118 (cs_modecxx . . $ cs_mode#)(CS_MODE_MIPS64cxx . . $ CS_MODE_MIPS64. | CS_MODE_LITTLE_ENDIANcxx . . $ CS_MODE_LITTLE_ENDIAN.),
119 (unsigned char *)MIPS_64SDcxx . . $ `tests/test_mips.c:78:9`!,
120 sizeof(MIPS_64SDcxx . . $ `tests/test_mips.c:78:9`!) - 1,
121 "MIPS-64-EL (Little-endian)"
122 },
123 };
124
125 uint64_tcxx . . $ uint64_t# address = 0x1000;
126 cs_insncxx . . $ cs_insn# *insn;
127 int i;
128 size_t count;
129
130 for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
131 cs_errcxx . . $ cs_err# err = cs_opencxx . . $ cs_open(b2cb849d8ee082cd).(platforms[i].archcxx . . $ platform#arch., platforms[i].modecxx . . $ platform#mode., &handlecxx . . $ handle.);
132 if (err) {
133 printfcxx . . $ printf(581fddd72a8db8fc).("Failed on cs_open() with error returned: %u\n", err);
134 abortcxx . . $ abort(8df32a62017fbbf6).();
135 }
136
137 cs_optioncxx . . $ cs_option(a321a010700ba3d2).(handlecxx . . $ handle., CS_OPT_DETAILcxx . . $ CS_OPT_DETAIL., CS_OPT_ONcxx . . $ CS_OPT_ON.);
138
139 count = cs_disasmcxx . . $ cs_disasm(d552efbd9b22be5).(handlecxx . . $ handle., platforms[i].codecxx . . $ platform#code., platforms[i].sizecxx . . $ platform#size., address, 0, &insn);
140 if (count) {
141 size_t j;
142
143 printfcxx . . $ printf(581fddd72a8db8fc).("****************\n");
144 printfcxx . . $ printf(581fddd72a8db8fc).("Platform: %s\n", platforms[i].commentcxx . . $ platform#comment.);
145 print_string_hexcxx . . $ print_string_hex(24a4beb24605093f).("Code:", platforms[i].codecxx . . $ platform#code., platforms[i].sizecxx . . $ platform#size.);
146 printfcxx . . $ printf(581fddd72a8db8fc).("Disasm:\n");
147
148 for (j = 0; j < count; j++) {
149 printfcxx . . $ printf(581fddd72a8db8fc).("0x%" PRIx64cxx . . $ `<external>/15933738808807975373/inttypes.h:126:10`! ":\t%s\t%s\n", insn[j].addresscxx . . $ cs_insn#address., insn[j].mnemoniccxx . . $ cs_insn#mnemonic., insn[j].op_strcxx . . $ cs_insn#op_str.);
150 print_insn_detailcxx . . $ print_insn_detail(ecef60df25d96a55).(&insn[j]);
151 }
152 printfcxx . . $ printf(581fddd72a8db8fc).("0x%" PRIx64cxx . . $ `<external>/15933738808807975373/inttypes.h:126:10`! ":\n", insn[j-1].addresscxx . . $ cs_insn#address. + insn[j-1].sizecxx . . $ cs_insn#size.);
153
154 // free memory allocated by cs_disasm()
155 cs_freecxx . . $ cs_free(d0ae7023b33cf0c8).(insn, count);
156 } else {
157 printfcxx . . $ printf(581fddd72a8db8fc).("****************\n");
158 printfcxx . . $ printf(581fddd72a8db8fc).("Platform: %s\n", platforms[i].commentcxx . . $ platform#comment.);
159 print_string_hexcxx . . $ print_string_hex(24a4beb24605093f).("Code:", platforms[i].codecxx . . $ platform#code., platforms[i].sizecxx . . $ platform#size.);
160 printfcxx . . $ printf(581fddd72a8db8fc).("ERROR: Failed to disasm given code!\n");
161 abortcxx . . $ abort(8df32a62017fbbf6).();
162 }
163
164 printfcxx . . $ printf(581fddd72a8db8fc).("\n");
165
166 cs_closecxx . . $ cs_close(b9ab39fbab147149).(&handlecxx . . $ handle.);
167 }
168 }
169
170 int maincxx . . $ main(d1b40a211a1a4e4c).()
171 {
172 testcxx . . $ test(d878ad36e69247c7).();
173
174 return 0;
175 }