Rizin
unix-like reverse engineering framework and cli tools
test_evm.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <capstone/platform.h>
#include <capstone/capstone.h>

Go to the source code of this file.

Classes

struct  platform
 

Macros

#define EVM_CODE   "\x60\x61\x50"
 

Functions

static void print_string_hex (const char *comment, unsigned char *str, size_t len)
 
static void print_insn_detail (csh cs_handle, cs_insn *ins)
 
static void test ()
 
int main ()
 

Variables

static csh handle
 

Macro Definition Documentation

◆ EVM_CODE

#define EVM_CODE   "\x60\x61\x50"

Function Documentation

◆ main()

int main ( void  )

Definition at line 120 of file test_evm.c.

121 {
122  test();
123 
124  return 0;
125 }
static void test()
Definition: test_evm.c:62

References test().

◆ print_insn_detail()

static void print_insn_detail ( csh  cs_handle,
cs_insn *  ins 
)
static

Definition at line 32 of file test_evm.c.

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 }
#define NULL
Definition: cris-opc.c:27
CAPSTONE_EXPORT const char *CAPSTONE_API cs_group_name(csh ud, unsigned int group)
Definition: cs.c:1178
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
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
static csh handle
Definition: test_evm.c:10

References cs_group_name(), cs_evm::fee, handle, NULL, cs_evm::pop, printf(), and cs_evm::push.

Referenced by test().

◆ print_string_hex()

static void print_string_hex ( const char *  comment,
unsigned char *  str,
size_t  len 
)
static

Definition at line 20 of file test_evm.c.

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 }
size_t len
Definition: 6502dis.c:15
#define c(i)
Definition: sha256.c:43

References c, len, printf(), and cmd_descs_generate::str.

Referenced by test().

◆ test()

static void test ( )
static

Definition at line 62 of file test_evm.c.

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 }
lzma_index ** i
Definition: index.h:629
static bool err
Definition: armass.c:435
@ CS_ARCH_EVM
Ethereum architecture.
Definition: capstone.h:86
@ CS_OPT_DETAIL
Break down instruction structure into details.
Definition: capstone.h:171
@ CS_OPT_ON
Turn ON an option (CS_OPT_DETAIL, CS_OPT_SKIPDATA).
Definition: capstone.h:183
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 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
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
Definition: inftree9.h:24
#define PRIx64
Definition: sysdefs.h:94
static void print_string_hex(const char *comment, unsigned char *str, size_t len)
Definition: test_evm.c:20
#define EVM_CODE
static void print_insn_detail(csh cs_handle, cs_insn *ins)
Definition: test_evm.c:32
mnemonic
Definition: z80asm.h:48

References arch, platform::comment, count, CS_ARCH_EVM, cs_close(), cs_disasm(), cs_free(), cs_open(), CS_OPT_DETAIL, CS_OPT_ON, cs_option(), err, EVM_CODE, handle, i, platforms, print_insn_detail(), print_string_hex(), printf(), and PRIx64.

Referenced by main().

Variable Documentation

◆ handle

csh handle
static

Definition at line 10 of file test_evm.c.

Referenced by print_insn_detail(), and test().