Rizin
unix-like reverse engineering framework and cli tools
test_arm64.c
Go to the documentation of this file.
1 /* Capstone Disassembler Engine */
2 /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2019 */
3 
4 #include <stdio.h>
5 #include <stdlib.h>
6 
7 #include <capstone/platform.h>
8 #include <capstone/capstone.h>
9 
10 static csh handle;
11 
12 struct platform {
13  cs_arch arch;
14  cs_mode mode;
15  unsigned char *code;
16  size_t size;
17  const char *comment;
18 };
19 
20 static void print_string_hex(const char *comment, unsigned char *str, size_t len)
21 {
22  unsigned char *c;
23 
24  printf("%s", comment);
25  for (c = str; c < str + len; c++) {
26  printf("0x%02x ", *c & 0xff);
27  }
28 
29  printf("\n");
30 }
31 
32 static void print_insn_detail(cs_insn *ins)
33 {
34  cs_arm64 *arm64;
35  int i;
36  cs_regs regs_read, regs_write;
37  unsigned char regs_read_count, regs_write_count;
38  unsigned char access;
39 
40  // detail can be NULL if SKIPDATA option is turned ON
41  if (ins->detail == NULL)
42  return;
43 
44  arm64 = &(ins->detail->arm64);
45  if (arm64->op_count)
46  printf("\top_count: %u\n", arm64->op_count);
47 
48  for (i = 0; i < arm64->op_count; i++) {
49  cs_arm64_op *op = &(arm64->operands[i]);
50  switch(op->type) {
51  default:
52  break;
53  case ARM64_OP_REG:
54  printf("\t\toperands[%u].type: REG = %s\n", i, cs_reg_name(handle, op->reg));
55  break;
56  case ARM64_OP_IMM:
57  printf("\t\toperands[%u].type: IMM = 0x%" PRIx64 "\n", i, op->imm);
58  break;
59  case ARM64_OP_FP:
60 #if defined(_KERNEL_MODE)
61  // Issue #681: Windows kernel does not support formatting float point
62  printf("\t\toperands[%u].type: FP = <float_point_unsupported>\n", i);
63 #else
64  printf("\t\toperands[%u].type: FP = %f\n", i, op->fp);
65 #endif
66  break;
67  case ARM64_OP_MEM:
68  printf("\t\toperands[%u].type: MEM\n", i);
69  if (op->mem.base != ARM64_REG_INVALID)
70  printf("\t\t\toperands[%u].mem.base: REG = %s\n", i, cs_reg_name(handle, op->mem.base));
71  if (op->mem.index != ARM64_REG_INVALID)
72  printf("\t\t\toperands[%u].mem.index: REG = %s\n", i, cs_reg_name(handle, op->mem.index));
73  if (op->mem.disp != 0)
74  printf("\t\t\toperands[%u].mem.disp: 0x%x\n", i, op->mem.disp);
75 
76  break;
77  case ARM64_OP_CIMM:
78  printf("\t\toperands[%u].type: C-IMM = %u\n", i, (int)op->imm);
79  break;
80  case ARM64_OP_REG_MRS:
81  printf("\t\toperands[%u].type: REG_MRS = 0x%x\n", i, op->reg);
82  break;
83  case ARM64_OP_REG_MSR:
84  printf("\t\toperands[%u].type: REG_MSR = 0x%x\n", i, op->reg);
85  break;
86  case ARM64_OP_PSTATE:
87  printf("\t\toperands[%u].type: PSTATE = 0x%x\n", i, op->pstate);
88  break;
89  case ARM64_OP_SYS:
90  printf("\t\toperands[%u].type: SYS = 0x%x\n", i, op->sys);
91  break;
92  case ARM64_OP_PREFETCH:
93  printf("\t\toperands[%u].type: PREFETCH = 0x%x\n", i, op->prefetch);
94  break;
95  case ARM64_OP_BARRIER:
96  printf("\t\toperands[%u].type: BARRIER = 0x%x\n", i, op->barrier);
97  break;
98  }
99 
100  access = op->access;
101  switch(access) {
102  default:
103  break;
104  case CS_AC_READ:
105  printf("\t\toperands[%u].access: READ\n", i);
106  break;
107  case CS_AC_WRITE:
108  printf("\t\toperands[%u].access: WRITE\n", i);
109  break;
110  case CS_AC_READ | CS_AC_WRITE:
111  printf("\t\toperands[%u].access: READ | WRITE\n", i);
112  break;
113  }
114 
115  if (op->shift.type != ARM64_SFT_INVALID &&
116  op->shift.value)
117  printf("\t\t\tShift: type = %u, value = %u\n",
118  op->shift.type, op->shift.value);
119 
120  if (op->ext != ARM64_EXT_INVALID)
121  printf("\t\t\tExt: %u\n", op->ext);
122 
123  if (op->vas != ARM64_VAS_INVALID)
124  printf("\t\t\tVector Arrangement Specifier: 0x%x\n", op->vas);
125 
126  if (op->vess != ARM64_VESS_INVALID)
127  printf("\t\t\tVector Element Size Specifier: %u\n", op->vess);
128 
129  if (op->vector_index != -1)
130  printf("\t\t\tVector Index: %u\n", op->vector_index);
131  }
132 
133  if (arm64->update_flags)
134  printf("\tUpdate-flags: True\n");
135 
136  if (arm64->writeback)
137  printf("\tWrite-back: True\n");
138 
139  if (arm64->cc)
140  printf("\tCode-condition: %u\n", arm64->cc);
141 
142  // Print out all registers accessed by this instruction (either implicit or explicit)
143  if (!cs_regs_access(handle, ins,
144  regs_read, &regs_read_count,
145  regs_write, &regs_write_count)) {
146  if (regs_read_count) {
147  printf("\tRegisters read:");
148  for(i = 0; i < regs_read_count; i++) {
149  printf(" %s", cs_reg_name(handle, regs_read[i]));
150  }
151  printf("\n");
152  }
153 
154  if (regs_write_count) {
155  printf("\tRegisters modified:");
156  for(i = 0; i < regs_write_count; i++) {
157  printf(" %s", cs_reg_name(handle, regs_write[i]));
158  }
159  printf("\n");
160  }
161  }
162 
163  printf("\n");
164 }
165 
166 static void test()
167 {
168 #define ARM64_CODE "\x09\x00\x38\xd5\xbf\x40\x00\xd5\x0c\x05\x13\xd5\x20\x50\x02\x0e\x20\xe4\x3d\x0f\x00\x18\xa0\x5f\xa2\x00\xae\x9e\x9f\x37\x03\xd5\xbf\x33\x03\xd5\xdf\x3f\x03\xd5\x21\x7c\x02\x9b\x21\x7c\x00\x53\x00\x40\x21\x4b\xe1\x0b\x40\xb9\x20\x04\x81\xda\x20\x08\x02\x8b\x10\x5b\xe8\x3c"
169 
170  struct platform platforms[] = {
171  {
173  CS_MODE_ARM,
174  (unsigned char *)ARM64_CODE,
175  sizeof(ARM64_CODE) - 1,
176  "ARM-64"
177  },
178  };
179 
180  uint64_t address = 0x2c;
181  cs_insn *insn;
182  int i;
183  size_t count;
184 
185  for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
186  cs_err err = cs_open(platforms[i].arch, platforms[i].mode, &handle);
187  if (err) {
188  printf("Failed on cs_open() with error returned: %u\n", err);
189  abort();
190  }
191 
193 
194  count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
195  if (count) {
196  size_t j;
197 
198  printf("****************\n");
199  printf("Platform: %s\n", platforms[i].comment);
201  printf("Disasm:\n");
202 
203  for (j = 0; j < count; j++) {
204  printf("0x%" PRIx64 ":\t%s\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str);
205  print_insn_detail(&insn[j]);
206  }
207  printf("0x%" PRIx64 ":\n", insn[j-1].address + insn[j-1].size);
208 
209  // free memory allocated by cs_disasm()
210  cs_free(insn, count);
211  } else {
212  printf("****************\n");
213  printf("Platform: %s\n", platforms[i].comment);
215  printf("ERROR: Failed to disasm given code!\n");
216  abort();
217  }
218 
219  printf("\n");
220 
221  cs_close(&handle);
222  }
223 }
224 
225 int main()
226 {
227  test();
228 
229  return 0;
230 }
231 
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
static bool err
Definition: armass.c:435
@ ARM64_VESS_INVALID
Definition: arm64.h:208
@ ARM64_OP_FP
= CS_OP_FP (Floating-Point operand).
Definition: arm64.h:238
@ ARM64_OP_PSTATE
PState operand.
Definition: arm64.h:242
@ ARM64_OP_BARRIER
Memory barrier operand (ISB/DMB/DSB instructions).
Definition: arm64.h:245
@ ARM64_OP_REG
= CS_OP_REG (Register operand).
Definition: arm64.h:235
@ ARM64_OP_PREFETCH
Prefetch operand (PRFM).
Definition: arm64.h:244
@ ARM64_OP_MEM
= CS_OP_MEM (Memory operand).
Definition: arm64.h:237
@ ARM64_OP_SYS
SYS operand for IC/DC/AT/TLBI instructions.
Definition: arm64.h:243
@ ARM64_OP_REG_MRS
MRS register operand.
Definition: arm64.h:240
@ ARM64_OP_CIMM
C-Immediate.
Definition: arm64.h:239
@ ARM64_OP_IMM
= CS_OP_IMM (Immediate operand).
Definition: arm64.h:236
@ ARM64_OP_REG_MSR
MSR register operand.
Definition: arm64.h:241
@ ARM64_VAS_INVALID
Definition: arm64.h:194
@ ARM64_SFT_INVALID
Definition: arm64.h:19
@ ARM64_REG_INVALID
Definition: arm64.h:348
@ ARM64_EXT_INVALID
Definition: arm64.h:29
cs_arch
Architecture type.
Definition: capstone.h:74
@ CS_ARCH_ARM64
ARM-64, also called AArch64.
Definition: capstone.h:76
cs_mode
Mode type.
Definition: capstone.h:102
@ CS_MODE_ARM
32-bit ARM
Definition: capstone.h:104
@ CS_OPT_DETAIL
Break down instruction structure into details.
Definition: capstone.h:171
size_t csh
Definition: capstone.h:71
@ CS_OPT_ON
Turn ON an option (CS_OPT_DETAIL, CS_OPT_SKIPDATA).
Definition: capstone.h:183
@ CS_AC_READ
Operand read from memory or register.
Definition: capstone.h:204
@ CS_AC_WRITE
Operand write to memory or register.
Definition: capstone.h:205
#define NULL
Definition: cris-opc.c:27
CAPSTONE_EXPORT cs_err CAPSTONE_API cs_regs_access(csh ud, const cs_insn *insn, cs_regs regs_read, uint8_t *regs_read_count, cs_regs regs_write, uint8_t *regs_write_count)
Definition: cs.c:1504
CAPSTONE_EXPORT size_t CAPSTONE_API cs_disasm(csh ud, const uint8_t *buffer, size_t size, uint64_t offset, size_t count, cs_insn **insn)
Definition: cs.c:798
CAPSTONE_EXPORT cs_err CAPSTONE_API cs_open(cs_arch arch, cs_mode mode, csh *handle)
Definition: cs.c:453
CAPSTONE_EXPORT void CAPSTONE_API cs_free(cs_insn *insn, size_t count)
Definition: cs.c:1017
CAPSTONE_EXPORT const char *CAPSTONE_API cs_reg_name(csh ud, unsigned int reg)
Definition: cs.c:1154
CAPSTONE_EXPORT cs_err CAPSTONE_API cs_close(csh *handle)
Definition: cs.c:501
CAPSTONE_EXPORT cs_err CAPSTONE_API cs_option(csh ud, cs_opt_type type, size_t value)
Definition: cs.c:646
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
cs_arch arch
Definition: cstool.c:13
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void count
Definition: sflib.h:98
struct platform platforms[]
Definition: fuzz_diff.c:18
voidpf void uLong size
Definition: ioapi.h:138
const char int mode
Definition: ioapi.h:137
static static fork const void static count static fd const char static mode const char static pathname const char static path const char static dev const char static group static getpid static getuid void void static data static pause access
Definition: sflib.h:64
unsigned long uint64_t
Definition: sftypes.h:28
#define c(i)
Definition: sha256.c:43
Definition: inftree9.h:24
Instruction operand.
Definition: arm64.h:630
Instruction structure.
Definition: arm64.h:658
unsigned char * code
#define PRIx64
Definition: sysdefs.h:94
static csh handle
Definition: test_arm64.c:10
static void print_string_hex(const char *comment, unsigned char *str, size_t len)
Definition: test_arm64.c:20
#define ARM64_CODE
static void print_insn_detail(cs_insn *ins)
Definition: test_arm64.c:32
static void test()
Definition: test_arm64.c:166
int main()
Definition: test_arm64.c:225
Definition: dis.c:32
mnemonic
Definition: z80asm.h:48