Debian Codegraph @

suite/cstest/src/capstone_test.c

raw symbols SCIP
1 /* Capstone testing regression */
2 /* By Do Minh Tuan <[email protected]>, 02-2019 */
3
4
5 #include "capstone_test.h"
6
7 char *(*function)(csh *, cs_mode, cs_insn*) = NULL;
8
9 void test_single_MC(csh *handle, int mc_mode, char *line)
10 {
11 char **list_part, **list_byte;
12 int size_part, size_byte;
13 int i, count;
14 unsigned char *code;
15 cs_insn *insn;
16 char tmp[MAXMEM], tmp_mc[MAXMEM], origin[MAXMEM], tmp_noreg[MAXMEM];
17 char **offset_opcode;
18 int size_offset_opcode;
19 unsigned long offset;
20 char *p;
21
22 list_part = split(line, " = ", &size_part);
23 offset_opcode = split(list_part[0], ": ", &size_offset_opcode);
24 if (size_offset_opcode > 1) {
25 offset = (unsigned int)strtol(offset_opcode[0], NULL, 16);
26 list_byte = split(offset_opcode[1], ",", &size_byte);
27 } else {
28 offset = 0;
29 list_byte = split(offset_opcode[0], ",", &size_byte);
30 }
31
32 code = (unsigned char *)malloc(size_byte * sizeof(char));
33 for (i = 0; i < size_byte; ++i) {
34 code[i] = (unsigned char)strtol(list_byte[i], NULL, 16);
35 }
36
37 count = cs_disasm(*handle, code, size_byte, offset, 0, &insn);
38 if (count == 0) {
39 fprintf(stderr, "[ ERROR ] --- %s --- Failed to disassemble given code!\n", list_part[0]);
40 free_strs(list_part, size_part);
41 free_strs(offset_opcode, size_offset_opcode);
42 free_strs(list_byte, size_byte);
43 free(code);
44 _fail(__FILE__, __LINE__);
45 }
46 if (count > 1) {
47 fprintf(stderr, "[ ERROR ] --- %s --- Multiple instructions(%d) disassembling doesn't support!\n", list_part[0], count);
48 free_strs(list_part, size_part);
49 free_strs(offset_opcode, size_offset_opcode);
50 free_strs(list_byte, size_byte);
51 free(code);
52 _fail(__FILE__, __LINE__);
53 }
54
55 for (p = list_part[1]; *p; ++p) *p = tolower(*p);
56 for (p = list_part[1]; *p; ++p)
57 if (*p == '\t') *p = ' ';
58 trim_str(list_part[1]);
59 strcpy(tmp_mc, list_part[1]);
60 replace_hex(tmp_mc);
61 replace_negative(tmp_mc, mc_mode);
62
63 strcpy(tmp, insn[0].mnemonic);
64 if (strlen(insn[0].op_str) > 0) {
65 tmp[strlen(insn[0].mnemonic)] = ' ';
66 strcpy(tmp + strlen(insn[0].mnemonic) + 1, insn[0].op_str);
67 }
68
69 trim_str(tmp);
70 strcpy(origin, tmp);
71 replace_hex(tmp);
72 replace_negative(tmp, mc_mode);
73
74 if (cs_option(*handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_NOREGNAME) == CS_ERR_OK) {
75 cs_disasm(*handle, code, size_byte, offset, 0, &insn);
76 strcpy(tmp_noreg, insn[0].mnemonic);
77 if (strlen(insn[0].op_str) > 0) {
78 tmp_noreg[strlen(insn[0].mnemonic)] = ' ';
79 strcpy(tmp_noreg + strlen(insn[0].mnemonic) + 1, insn[0].op_str);
80 }
81
82 trim_str(tmp_noreg);
83 replace_hex(tmp_noreg);
84 replace_negative(tmp_noreg, mc_mode);
85
86 if (strcmp(tmp, tmp_mc) && strcmp(tmp_noreg, tmp_mc)) {
87 fprintf(stderr, "[ ERROR ] --- %s --- \"%s\" != \"%s\" ( \"%s\" != \"%s\" and \"%s\" != \"%s\" )\n", list_part[0], origin, list_part[1], tmp, tmp_mc, tmp_noreg, tmp_mc);
88 free_strs(list_part, size_part);
89 free_strs(offset_opcode, size_offset_opcode);
90 free_strs(list_byte, size_byte);
91 free(code);
92 cs_free(insn, count);
93 _fail(__FILE__, __LINE__);
94 }
95
96 cs_option(*handle, CS_OPT_SYNTAX, 0);
97
98 } else if (strcmp(tmp, tmp_mc)) {
99 fprintf(stderr, "[ ERROR ] --- %s --- \"%s\" != \"%s\" ( \"%s\" != \"%s\" )\n", list_part[0], origin, list_part[1], tmp, tmp_mc);
100 free_strs(list_part, size_part);
101 free_strs(offset_opcode, size_offset_opcode);
102 free_strs(list_byte, size_byte);
103 free(code);
104 cs_free(insn, count);
105 _fail(__FILE__, __LINE__);
106 }
107
108 free_strs(list_part, size_part);
109 free_strs(offset_opcode, size_offset_opcode);
110 free_strs(list_byte, size_byte);
111 free(code);
112 cs_free(insn, count);
113 }
114
115 int get_value(single_dict d[], unsigned int size, const char *str)
116 {
117 int i;
118
119 for (i = 0; i < size; ++i)
120 if (!strcmp(d[i].str, str))
121 return d[i].value;
122 return -1;
123 }
124
125 int get_index(double_dict d[], unsigned int size, const char *s)
126 {
127 int i;
128
129 for (i = 0; i < size; ++i) {
130 if (!strcmp(s, d[i].str))
131 return i;
132 }
133 return -1;
134 }
135
136 int set_function(int arch)
137 {
138 switch(arch) {
139 case CS_ARCH_ARM:
140 function = get_detail_arm;
141 break;
142 case CS_ARCH_ARM64:
143 function = get_detail_arm64;
144 break;
145 case CS_ARCH_MIPS:
146 function = get_detail_mips;
147 break;
148 case CS_ARCH_PPC:
149 function = get_detail_ppc;
150 break;
151 case CS_ARCH_SPARC:
152 function = get_detail_sparc;
153 break;
154 case CS_ARCH_SYSZ:
155 function = get_detail_sysz;
156 break;
157 case CS_ARCH_X86:
158 function = get_detail_x86;
159 break;
160 case CS_ARCH_XCORE:
161 function = get_detail_xcore;
162 break;
163 case CS_ARCH_M68K:
164 function = get_detail_m68k;
165 break;
166 case CS_ARCH_M680X:
167 function = get_detail_m680x;
168 break;
169 case CS_ARCH_EVM:
170 function = get_detail_evm;
171 break;
172 case CS_ARCH_MOS65XX:
173 function = get_detail_mos65xx;
174 break;
175 case CS_ARCH_TMS320C64X:
176 function = get_detail_tms320c64x;
177 break;
178 case CS_ARCH_BPF:
179 function = get_detail_bpf;
180 break;
181 case CS_ARCH_RISCV:
182 function = get_detail_riscv;
183 break;
184 case CS_ARCH_TRICORE:
185 function = get_detail_tricore;
186 break;
187 default:
188 return -1;
189 }
190 return 0;
191 }
192
193 void test_single_issue(csh *handle, cs_mode mode, char *line, int detail)
194 {
195 char **list_part, **list_byte, **list_part_issue_result;
196 int size_part, size_byte, size_part_issue_result;
197 int i, count, j;
198 unsigned char *code;
199 cs_insn *insn;
200 char *cs_result, *tmp, *p;
201 char **offset_opcode;
202 int size_offset_opcode;
203 unsigned long offset;
204
205 cs_result = (char *)malloc(sizeof(char));
206 cs_result[0] = '\0';
207
208 list_part = split(line, " == ", &size_part);
209
210 offset_opcode = split(list_part[0], ": ", &size_offset_opcode);
211 if (size_offset_opcode > 1) {
212 offset = (unsigned int)strtol(offset_opcode[0], NULL, 16);
213 list_byte = split(offset_opcode[1], ",", &size_byte);
214 } else {
215 offset = 0;
216 list_byte = split(offset_opcode[0], ",", &size_byte);
217 }
218
219 code = (unsigned char *)malloc(sizeof(char) * size_byte);
220 for (i = 0; i < size_byte; ++i) {
221 code[i] = (unsigned char)strtol(list_byte[i], NULL, 16);
222 }
223
224 count = cs_disasm(*handle, code, size_byte, offset, 0, &insn);
225 for (i = 0; i < count; ++i) {
226 tmp = (char *)malloc(strlen(insn[i].mnemonic) + strlen(insn[i].op_str) + 100);
227 strcpy(tmp, insn[i].mnemonic);
228 if (strlen(insn[i].op_str) > 0) {
229 tmp[strlen(insn[i].mnemonic)] = ' ';
230 strcpy(tmp + strlen(insn[i].mnemonic) + 1, insn[i].op_str);
231 }
232 add_str(&cs_result, "%s", tmp);
233 free(tmp);
234 }
235
236 if (detail == 1) {
237 tmp = (*function)(handle, mode, insn);
238 add_str(&cs_result, "%s", tmp);
239 free(tmp);
240
241 if (insn->detail->groups_count) {
242 add_str(&cs_result, " ; Groups: ");
243 for (j = 0; j < insn->detail->groups_count; j++) {
244 add_str(&cs_result, "%s ", cs_group_name(*handle, insn->detail->groups[j]));
245 }
246 }
247 }
248
249 trim_str(cs_result);
250 add_str(&cs_result, " ;");
251 // list_part_cs_result = split(cs_result, " ; ", &size_part_cs_result);
252 for (p = list_part[1]; *p; ++p) if (*p == '\t') *p = ' ';
253 list_part_issue_result = split(list_part[1], " ; ", &size_part_issue_result);
254
255 for (i = 0; i < size_part_issue_result; ++i) {
256 trim_str(list_part_issue_result[i]);
257 char *tmptmp = (char *)malloc(sizeof(char));
258 tmptmp[0] = '\0';
259 add_str(&tmptmp, "%s", list_part_issue_result[i]);
260 add_str(&tmptmp, " ;");
261
262 if ((strstr(cs_result, tmptmp)) == NULL) {
263 fprintf(stderr, "[ ERROR ] --- %s --- \"%s\" not in \"%s\"\n", list_part[0], list_part_issue_result[i], cs_result);
264 cs_free(insn, count);
265 free_strs(list_part, size_part);
266 free_strs(list_byte, size_byte);
267 free(cs_result);
268 // free_strs(list_part_cs_result, size_part_cs_result);
269 free_strs(list_part_issue_result, size_part_issue_result);
270 _fail(__FILE__, __LINE__);
271 }
272 free(tmptmp);
273 }
274
275 cs_free(insn, count);
276 free_strs(list_part, size_part);
277 free_strs(list_byte, size_byte);
278 free(cs_result);
279 // free_strs(list_part_cs_result, size_part_cs_result);
280 free_strs(list_part_issue_result, size_part_issue_result);
281 }