Debian Codegraph @

tests/test_arm64.c

raw symbols SCIP
1 /* Capstone Disassembler Engine */
2 /* By Nguyen Anh Quynh <[email protected]>, 2013-2019 */
3
4 #include <assert.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7
8 #include <capstone/platform.h>
9 #include <capstone/capstone.h>
10
11 static csh handle;
12
13 struct platform {
14 cs_arch arch;
15 cs_mode mode;
16 unsigned char *code;
17 size_t size;
18 const char *comment;
19 };
20
21 static void print_string_hex(const char *comment, unsigned char *str, size_t len)
22 {
23 unsigned char *c;
24
25 printf("%s", comment);
26 for (c = str; c < str + len; c++) {
27 printf("0x%02x ", *c & 0xff);
28 }
29
30 printf("\n");
31 }
32
33 static void print_insn_detail(cs_insn *ins)
34 {
35 cs_arm64 *arm64;
36 int i;
37 cs_regs regs_read, regs_write;
38 unsigned char regs_read_count, regs_write_count;
39 unsigned char access;
40
41 // detail can be NULL if SKIPDATA option is turned ON
42 if (ins->detail == NULL)
43 return;
44
45 arm64 = &(ins->detail->arm64);
46 if (arm64->op_count)
47 printf("\top_count: %u\n", arm64->op_count);
48
49 for (i = 0; i < arm64->op_count; i++) {
50 cs_arm64_op *op = &(arm64->operands[i]);
51 switch(op->type) {
52 default:
53 break;
54 case ARM64_OP_REG:
55 printf("\t\toperands[%u].type: REG = %s\n", i, cs_reg_name(handle, op->reg));
56 break;
57 case ARM64_OP_IMM:
58 printf("\t\toperands[%u].type: IMM = 0x%" PRIx64 "\n", i, op->imm);
59 break;
60 case ARM64_OP_FP:
61 #if defined(_KERNEL_MODE)
62 // Issue #681: Windows kernel does not support formatting float point
63 printf("\t\toperands[%u].type: FP = <float_point_unsupported>\n", i);
64 #else
65 printf("\t\toperands[%u].type: FP = %f\n", i, op->fp);
66 #endif
67 break;
68 case ARM64_OP_MEM:
69 printf("\t\toperands[%u].type: MEM\n", i);
70 if (op->mem.base != ARM64_REG_INVALID)
71 printf("\t\t\toperands[%u].mem.base: REG = %s\n", i, cs_reg_name(handle, op->mem.base));
72 if (op->mem.index != ARM64_REG_INVALID)
73 printf("\t\t\toperands[%u].mem.index: REG = %s\n", i, cs_reg_name(handle, op->mem.index));
74 if (op->mem.disp != 0)
75 printf("\t\t\toperands[%u].mem.disp: 0x%x\n", i, op->mem.disp);
76
77 break;
78 case ARM64_OP_CIMM:
79 printf("\t\toperands[%u].type: C-IMM = %u\n", i, (int)op->imm);
80 break;
81 case ARM64_OP_REG_MRS:
82 printf("\t\toperands[%u].type: REG_MRS = 0x%x\n", i, op->reg);
83 break;
84 case ARM64_OP_REG_MSR:
85 printf("\t\toperands[%u].type: REG_MSR = 0x%x\n", i, op->reg);
86 break;
87 case ARM64_OP_PSTATE:
88 printf("\t\toperands[%u].type: PSTATE = 0x%x\n", i, op->pstate);
89 break;
90 case ARM64_OP_SYS:
91 printf("\t\toperands[%u].type: SYS = 0x%x\n", i, op->sys);
92 break;
93 case ARM64_OP_PREFETCH:
94 printf("\t\toperands[%u].type: PREFETCH = 0x%x\n", i, op->prefetch);
95 break;
96 case ARM64_OP_BARRIER:
97 printf("\t\toperands[%u].type: BARRIER = 0x%x\n", i, op->barrier);
98 break;
99 }
100
101 access = op->access;
102 switch(access) {
103 default:
104 break;
105 case CS_AC_READ:
106 printf("\t\toperands[%u].access: READ\n", i);
107 break;
108 case CS_AC_WRITE:
109 printf("\t\toperands[%u].access: WRITE\n", i);
110 break;
111 case CS_AC_READ | CS_AC_WRITE:
112 printf("\t\toperands[%u].access: READ | WRITE\n", i);
113 break;
114 }
115
116 if (op->shift.type != ARM64_SFT_INVALID &&
117 op->shift.value)
118 printf("\t\t\tShift: type = %u, value = %u\n",
119 op->shift.type, op->shift.value);
120
121 if (op->ext != ARM64_EXT_INVALID)
122 printf("\t\t\tExt: %u\n", op->ext);
123
124 if (op->vas != ARM64_VAS_INVALID)
125 printf("\t\t\tVector Arrangement Specifier: 0x%x\n", op->vas);
126
127 if (op->vector_index != -1)
128 printf("\t\t\tVector Index: %u\n", op->vector_index);
129 }
130
131 if (arm64->update_flags)
132 printf("\tUpdate-flags: True\n");
133
134 if (arm64->writeback)
135 printf("\tWrite-back: %s\n", arm64->post_index ? "Post" : "Pre");
136
137 if (arm64->cc)
138 printf("\tCode-condition: %u\n", arm64->cc);
139
140 // Print out all registers accessed by this instruction (either implicit or explicit)
141 if (!cs_regs_access(handle, ins,
142 regs_read, &regs_read_count,
143 regs_write, &regs_write_count)) {
144 if (regs_read_count) {
145 printf("\tRegisters read:");
146 for(i = 0; i < regs_read_count; i++) {
147 printf(" %s", cs_reg_name(handle, regs_read[i]));
148 }
149 printf("\n");
150 }
151
152 if (regs_write_count) {
153 printf("\tRegisters modified:");
154 for(i = 0; i < regs_write_count; i++) {
155 printf(" %s", cs_reg_name(handle, regs_write[i]));
156 }
157 printf("\n");
158 }
159 }
160
161 printf("\n");
162 }
163
164 static void test()
165 {
166 #define ARM64_CODE "\x09\x00\x38\xd5" \
167 "\xbf\x40\x00\xd5" \
168 "\x0c\x05\x13\xd5" \
169 "\x20\x50\x02\x0e" \
170 "\x20\xe4\x3d\x0f" \
171 "\x00\x18\xa0\x5f" \
172 "\xa2\x00\xae\x9e" \
173 "\x9f\x37\x03\xd5" \
174 "\xbf\x33\x03\xd5" \
175 "\xdf\x3f\x03\xd5" \
176 "\x21\x7c\x02\x9b" \
177 "\x21\x7c\x00\x53" \
178 "\x00\x40\x21\x4b" \
179 "\xe1\x0b\x40\xb9" \
180 "\x20\x04\x81\xda" \
181 "\x20\x08\x02\x8b" \
182 "\x10\x5b\xe8\x3c" \
183 "\xfd\x7b\xba\xa9" \
184 "\xfd\xc7\x43\xf8"
185
186 struct platform platforms[] = {
187 {
188 CS_ARCH_ARM64,
189 CS_MODE_ARM,
190 (unsigned char *)ARM64_CODE,
191 sizeof(ARM64_CODE) - 1,
192 "ARM-64"
193 },
194 };
195
196 uint64_t address = 0x2c;
197 cs_insn *insn;
198 int i;
199 size_t count;
200
201 for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
202 cs_err err = cs_open(platforms[i].arch, platforms[i].mode, &handle);
203 if (err) {
204 printf("Failed on cs_open() with error returned: %u\n", err);
205 abort();
206 }
207
208 cs_option(handle, CS_OPT_DETAIL, CS_OPT_ON);
209
210 count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
211 if (count) {
212 size_t j;
213
214 printf("****************\n");
215 printf("Platform: %s\n", platforms[i].comment);
216 print_string_hex("Code: ", platforms[i].code, platforms[i].size);
217 printf("Disasm:\n");
218
219 for (j = 0; j < count; j++) {
220 printf("0x%" PRIx64 ":\t%s\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str);
221 print_insn_detail(&insn[j]);
222 }
223 printf("0x%" PRIx64 ":\n", insn[j-1].address + insn[j-1].size);
224
225 // free memory allocated by cs_disasm()
226 cs_free(insn, count);
227 } else {
228 printf("****************\n");
229 printf("Platform: %s\n", platforms[i].comment);
230 print_string_hex("Code: ", platforms[i].code, platforms[i].size);
231 printf("ERROR: Failed to disasm given code!\n");
232 abort();
233 }
234
235 printf("\n");
236
237 cs_close(&handle);
238 }
239 }
240
241 void test_macros() {
242 assert(CS_AARCH64(_INS_BL) == ARM64_INS_BL);
243 assert(CS_AARCH64pre(CS_ARCH_) == CS_ARCH_ARM64);
244 assert(CS_AARCH64CC(_AL) == ARM64_CC_AL);
245 assert(CS_AARCH64_VL_(16B) == ARM64_VAS_16B);
246 cs_detail detail = { 0 };
247 CS_cs_aarch64() arm64_detail = { 0 };
248 detail.arm64 = arm64_detail;
249 CS_aarch64_op() op = { 0 };
250 detail.CS_aarch64_.operands[0] = op;
251 CS_aarch64_reg() reg = 1;
252 CS_aarch64_cc() cc = ARM64_CC_AL;
253 CS_aarch64_extender() arm64_extender = ARM64_EXT_SXTB;
254 CS_aarch64_shifter() arm64_shifter = ARM64_SFT_LSL;
255 CS_aarch64_vas() arm64_vas = ARM64_VAS_16B;
256 }
257
258 int main()
259 {
260 test();
261 test_macros();
262
263 return 0;
264 }
265