Debian Codegraph @

suite/arm/test_arm_regression.c

raw symbols SCIP
1 /* Capstone Disassembler Engine */
2 /* By David Hogarty, 2014 */
3
4 // the following must precede stdio (woo, thanks msft)
5 #if defined(_MSC_VER) && _MSC_VER < 1900
6 #define _CRT_SECURE_NO_WARNINGS
7 #define snprintf _snprintf
8 #endif
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 #include <capstone/platform.h>
14 #include <capstone/capstone.h>
15
16 static csh handle;
17
18 struct platform {
19 cs_arch arch;
20 cs_mode mode;
21 unsigned char *code;
22 size_t size;
23 char *comment;
24 int syntax;
25 };
26
27 static char *hex_string(unsigned char *str, size_t len)
28 {
29 // returns a malloced string that has the hex version of the string in it
30 // null if failed to malloc
31 char *hex_out;
32 size_t i;
33
34 hex_out = (char *) malloc(len*2 + 1); // two ascii characters per input character, plus trailing null
35 if (!hex_out) { goto Exit; }
36
37 for (i = 0; i < len; ++i) {
38 snprintf(hex_out + (i * 2), 3, "%02x", str[i]);
39 }
40
41 hex_out[len*2] = 0; // trailing null
42
43 Exit:
44 return hex_out;
45 }
46
47 static void snprint_insn_detail(char * buf, size_t * cur, size_t * left, cs_insn *ins)
48 {
49 #define _this_printf(...) \
50 { \
51 size_t used = 0; \
52 used = snprintf(buf + *cur, *left, __VA_ARGS__); \
53 *left -= used; \
54 *cur += used; \
55 }
56
57 cs_arm *arm;
58 int i;
59
60 // detail can be NULL on "data" instruction if SKIPDATA option is turned ON
61 if (ins->detail == NULL)
62 return;
63
64 arm = &(ins->detail->arm);
65
66 if (arm->op_count)
67 _this_printf("\top_count: %u\n", arm->op_count);
68
69 for (i = 0; i < arm->op_count; i++) {
70 cs_arm_op *op = &(arm->operands[i]);
71 switch((int)op->type) {
72 default:
73 break;
74 case ARM_OP_REG:
75 _this_printf("\t\toperands[%u].type: REG = %s\n", i, cs_reg_name(handle, op->reg));
76 break;
77 case ARM_OP_IMM:
78 _this_printf("\t\toperands[%u].type: IMM = 0x%x\n", i, op->imm);
79 break;
80 case ARM_OP_FP:
81 _this_printf("\t\toperands[%u].type: FP = %f\n", i, op->fp);
82 break;
83 case ARM_OP_MEM:
84 _this_printf("\t\toperands[%u].type: MEM\n", i);
85 if (op->mem.base != ARM_REG_INVALID)
86 _this_printf("\t\t\toperands[%u].mem.base: REG = %s\n",
87 i, cs_reg_name(handle, op->mem.base));
88 if (op->mem.index != ARM_REG_INVALID)
89 _this_printf("\t\t\toperands[%u].mem.index: REG = %s\n",
90 i, cs_reg_name(handle, op->mem.index));
91 if (op->mem.scale != 1)
92 _this_printf("\t\t\toperands[%u].mem.scale: %u\n", i, op->mem.scale);
93 if (op->mem.disp != 0)
94 _this_printf("\t\t\toperands[%u].mem.disp: 0x%x\n", i, op->mem.disp);
95
96 break;
97 case ARM_OP_PIMM:
98 _this_printf("\t\toperands[%u].type: P-IMM = %u\n", i, op->imm);
99 break;
100 case ARM_OP_CIMM:
101 _this_printf("\t\toperands[%u].type: C-IMM = %u\n", i, op->imm);
102 break;
103 }
104
105 if (op->shift.type != ARM_SFT_INVALID && op->shift.value) {
106 if (op->shift.type < ARM_SFT_ASR_REG) {
107 // shift with constant value
108 _this_printf("\t\t\tShift: %u = %u\n", op->shift.type, op->shift.value);
109 } else {
110 // shift with register
111 _this_printf("\t\t\tShift: %u = %s\n", op->shift.type,
112 cs_reg_name(handle, op->shift.value));
113 }
114 }
115 }
116
117 if (arm->cc != ARM_CC_AL && arm->cc != ARM_CC_INVALID) {
118 _this_printf("\tCode condition: %u\n", arm->cc);
119 }
120
121 if (arm->update_flags) {
122 _this_printf("\tUpdate-flags: True\n");
123 }
124
125 if (arm->writeback) {
126 _this_printf("\tWrite-back: True\n");
127 }
128
129 #undef _this_printf
130 }
131
132 static void print_insn_detail(cs_insn *ins)
133 {
134 char a_buf[2048];
135 size_t cur=0, left=2048;
136 snprint_insn_detail(a_buf, &cur, &left, ins);
137 printf("%s\n", a_buf);
138 }
139
140 struct invalid_code {
141 unsigned char *code;
142 size_t size;
143 char *comment;
144 };
145
146 #define MAX_INVALID_CODES 16
147
148 struct invalid_instructions {
149 cs_arch arch;
150 cs_mode mode;
151 char *platform_comment;
152 int num_invalid_codes;
153 struct invalid_code invalid_codes[MAX_INVALID_CODES];
154 };
155
156 static void test_invalids()
157 {
158 struct invalid_instructions invalids[] = {{
159 CS_ARCH_ARM,
160 CS_MODE_THUMB,
161 "Thumb",
162 1,
163 {{
164 (unsigned char *)"\xbd\xe8\x1e\xff",
165 4,
166 "invalid thumb2 pop because sp used and because both pc and lr are "
167 "present at the same time"
168 }},
169 }};
170
171 struct invalid_instructions * invalid = NULL;
172
173 uint64_t address = 0x1000;
174 cs_insn *insn;
175 int i;
176 int j;
177 size_t count;
178
179 printf("\nShould be invalid\n"
180 "-----------------\n");
181
182 for (i = 0; i < sizeof(invalids)/sizeof(invalids[0]); i++) {
183 cs_err err;
184
185 invalid = invalids + i;
186 err = cs_open(invalid->arch, invalid->mode, &handle);
187
188 if (err) {
189 printf("Failed on cs_open() with error returned: %u\n", err);
190 continue;
191 }
192
193 cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);
194 cs_option(handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_NOREGNAME);
195
196 for (j = 0; j < invalid->num_invalid_codes; ++j) {
197 struct invalid_code *invalid_code = NULL;
198 char *hex_str = NULL;
199
200 invalid_code = invalid->invalid_codes + j;
201
202 hex_str = hex_string(invalid_code->code, invalid_code->size);
203
204 printf("%s %s: %s\n", invalid->platform_comment, hex_str, invalid_code->comment);
205
206 free(hex_str);
207
208 count = cs_disasm(handle,
209 invalid_code->code, invalid_code->size, address, 0, &insn
210 );
211
212 if (count) {
213 size_t k;
214 printf(" ERROR:\n");
215
216 for (k = 0; k < count; k++) {
217 printf(" 0x%"PRIx64":\t%s\t%s\n",
218 insn[k].address, insn[k].mnemonic, insn[k].op_str);
219 print_insn_detail(&insn[k]);
220 }
221 cs_free(insn, count);
222
223 } else {
224 printf(" SUCCESS: invalid\n");
225 }
226 }
227
228 cs_close(&handle);
229 }
230 }
231
232 struct valid_code {
233 unsigned char *code;
234 size_t size;
235 uint32_t start_addr;
236 char *expected_out;
237 char *comment;
238 };
239
240 #define MAX_VALID_CODES 16
241 struct valid_instructions {
242 cs_arch arch;
243 cs_mode mode;
244 char *platform_comment;
245 int num_valid_codes;
246 struct valid_code valid_codes[MAX_VALID_CODES];
247 };
248
249 static void test_valids()
250 {
251 struct valid_instructions valids[] = {{
252 CS_ARCH_ARM,
253 CS_MODE_THUMB,
254 "Thumb",
255 3,
256 {{ (unsigned char *)"\x00\xf0\x26\xe8", 4, 0x352,
257 "0x352:\tblx\t#0x3a0\n"
258 "\top_count: 1\n"
259 "\t\toperands[0].type: IMM = 0x3a0\n",
260
261 "thumb2 blx with misaligned immediate"
262 }, { (unsigned char *)"\x05\xdd", 2, 0x1f0,
263 "0x1f0:\tble\t#0x1fe\n"
264 "\top_count: 1\n"
265 "\t\toperands[0].type: IMM = 0x1fe\n"
266 "\tCode condition: 14\n",
267
268 "thumb b cc with thumb-aligned target"
269 }, { (unsigned char *)"\xbd\xe8\xf0\x8f", 4, 0,
270 "0x0:\tpop.w\t{r4, r5, r6, r7, r8, r9, r10, r11, pc}\n"
271 "\top_count: 9\n"
272 "\t\toperands[0].type: REG = r4\n"
273 "\t\toperands[1].type: REG = r5\n"
274 "\t\toperands[2].type: REG = r6\n"
275 "\t\toperands[3].type: REG = r7\n"
276 "\t\toperands[4].type: REG = r8\n"
277 "\t\toperands[5].type: REG = r9\n"
278 "\t\toperands[6].type: REG = r10\n"
279 "\t\toperands[7].type: REG = r11\n"
280 "\t\toperands[8].type: REG = pc\n",
281
282 "thumb2 pop that should be valid"
283 },
284 }
285 }};
286
287 struct valid_instructions *valid = NULL;
288 cs_insn *insn;
289 int i;
290 int j;
291 size_t count;
292
293
294 for (i = 0; i < sizeof(valids)/sizeof(valids[0]); i++) {
295 cs_err err;
296
297 valid = valids + i;
298 err = cs_open(valid->arch, valid->mode, &handle);
299
300 if (err) {
301 printf("Failed on cs_open() with error returned: %u\n", err);
302 continue;
303 }
304
305 cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);
306 cs_option(handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_NOREGNAME);
307
308 #define _this_printf(...) \
309 { \
310 size_t used = 0; \
311 used = snprintf(tmp_buf + cur, left, __VA_ARGS__); \
312 left -= used; \
313 cur += used; \
314 }
315 printf("\nShould be valid\n"
316 "---------------\n");
317
318 for (j = 0; j < valid->num_valid_codes; ++j) {
319 char tmp_buf[2048];
320 size_t left = 2048;
321 size_t cur = 0;
322 char * hex_str = NULL;
323
324 struct valid_code * valid_code = NULL;
325 valid_code = valid->valid_codes + j;
326
327 hex_str = hex_string(valid_code->code, valid_code->size);
328
329 printf("%s %s @ 0x%04x: %s\n %s",
330 valid->platform_comment, hex_str, valid_code->start_addr,
331 valid_code->comment, valid_code->expected_out);
332
333 free(hex_str);
334
335 count = cs_disasm(handle,
336 valid_code->code, valid_code->size,
337 valid_code->start_addr, 0, &insn
338 );
339
340 if (count) {
341 size_t k;
342 size_t max_len = 0;
343 size_t tmp_len = 0;
344
345 for (k = 0; k < count; k++) {
346 _this_printf(
347 "0x%"PRIx64":\t%s\t%s\n",
348 insn[k].address, insn[k].mnemonic,
349 insn[k].op_str
350 );
351
352 snprint_insn_detail(tmp_buf, &cur, &left, &insn[k]);
353 }
354
355 max_len = strlen(tmp_buf);
356 tmp_len = strlen(valid_code->expected_out);
357 if (tmp_len > max_len) {
358 max_len = tmp_len;
359 }
360
361 if (memcmp(tmp_buf, valid_code->expected_out, max_len)) {
362 printf(
363 " ERROR: '''\n%s''' does not match"
364 " expected '''\n%s'''\n",
365 tmp_buf, valid_code->expected_out
366 );
367 } else {
368 printf(" SUCCESS: valid\n");
369 }
370
371 cs_free(insn, count);
372
373 } else {
374 printf("ERROR: invalid\n");
375 }
376 }
377
378 cs_close(&handle);
379 }
380
381 #undef _this_prinf
382 }
383
384 int main()
385 {
386 test_invalids();
387 test_valids();
388 return 0;
389 }
390