Rizin
unix-like reverse engineering framework and cli tools
test_evm.c
Go to the documentation of this file.
1 /* Capstone Disassembler Engine */
2 /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2018-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(csh cs_handle, cs_insn *ins)
33 {
34  cs_evm *evm;
35 
36  // detail can be NULL on "data" instruction if SKIPDATA option is turned ON
37  if (ins->detail == NULL)
38  return;
39 
40  evm = &(ins->detail->evm);
41 
42  if (evm->pop)
43  printf("\tPop: %u\n", evm->pop);
44 
45  if (evm->push)
46  printf("\tPush: %u\n", evm->push);
47 
48  if (evm->fee)
49  printf("\tGas fee: %u\n", evm->fee);
50 
51  if (ins->detail->groups_count) {
52  int j;
53 
54  printf("\tGroups: ");
55  for(j = 0; j < ins->detail->groups_count; j++) {
56  printf("%s ", cs_group_name(handle, ins->detail->groups[j]));
57  }
58  printf("\n");
59  }
60 }
61 
62 static void test()
63 {
64 #define EVM_CODE "\x60\x61\x50"
65 
66  struct platform platforms[] = {
67  {
69  0,
70  (unsigned char *)EVM_CODE,
71  sizeof(EVM_CODE) - 1,
72  "EVM"
73  },
74  };
75 
76  uint64_t address = 0x80001000;
77  cs_insn *insn;
78  int i;
79  size_t count;
80 
81  for (i = 0; i < sizeof(platforms)/sizeof(platforms[0]); i++) {
82  cs_err err = cs_open(platforms[i].arch, platforms[i].mode, &handle);
83  if (err) {
84  printf("Failed on cs_open() with error returned: %u\n", err);
85  abort();
86  }
87 
89 
90  count = cs_disasm(handle, platforms[i].code, platforms[i].size, address, 0, &insn);
91  if (count) {
92  size_t j;
93  printf("****************\n");
94  printf("Platform: %s\n", platforms[i].comment);
96  printf("Disasm:\n");
97 
98  for (j = 0; j < count; j++) {
99  printf("0x%" PRIx64 ":\t%s\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str);
100  print_insn_detail(handle, &insn[j]);
101  }
102  printf("0x%" PRIx64 ":\n", insn[j-1].address + insn[j-1].size);
103 
104  // free memory allocated by cs_disasm()
105  cs_free(insn, count);
106  } else {
107  printf("****************\n");
108  printf("Platform: %s\n", platforms[i].comment);
110  printf("ERROR: Failed to disasm given code!\n");
111  abort();
112  }
113 
114  printf("\n");
115 
116  cs_close(&handle);
117  }
118 }
119 
120 int main()
121 {
122  test();
123 
124  return 0;
125 }
126 
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
static bool err
Definition: armass.c:435
cs_arch
Architecture type.
Definition: capstone.h:74
@ CS_ARCH_EVM
Ethereum architecture.
Definition: capstone.h:86
cs_mode
Mode type.
Definition: capstone.h:102
@ 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
#define NULL
Definition: cris-opc.c:27
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 const char *CAPSTONE_API cs_group_name(csh ud, unsigned int group)
Definition: cs.c:1178
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 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
unsigned long uint64_t
Definition: sftypes.h:28
#define c(i)
Definition: sha256.c:43
Definition: inftree9.h:24
Instruction structure.
Definition: evm.h:18
unsigned int fee
gas fee for the instruction
Definition: evm.h:21
unsigned char push
number of items pushed into the stack
Definition: evm.h:20
unsigned char pop
number of items popped from the stack
Definition: evm.h:19
unsigned char * code
#define PRIx64
Definition: sysdefs.h:94
static csh handle
Definition: test_evm.c:10
static void print_string_hex(const char *comment, unsigned char *str, size_t len)
Definition: test_evm.c:20
static void test()
Definition: test_evm.c:62
#define EVM_CODE
static void print_insn_detail(csh cs_handle, cs_insn *ins)
Definition: test_evm.c:32
int main()
Definition: test_evm.c:120
mnemonic
Definition: z80asm.h:48