Rizin
unix-like reverse engineering framework and cli tools
EVMDisassembler.c File Reference
#include <string.h>
#include <stddef.h>
#include "EVMDisassembler.h"
#include "EVMMapping.h"

Go to the source code of this file.

Functions

bool EVM_getInstruction (csh ud, const uint8_t *code, size_t code_len, MCInst *MI, uint16_t *size, uint64_t address, void *inst_info)
 

Variables

static short opcodes [256]
 

Function Documentation

◆ EVM_getInstruction()

bool EVM_getInstruction ( csh  ud,
const uint8_t code,
size_t  code_len,
MCInst MI,
uint16_t size,
uint64_t  address,
void *  inst_info 
)

Definition at line 270 of file EVMDisassembler.c.

272 {
273  unsigned char opcode;
274 
275  if (code_len == 0)
276  return false;
277 
278  opcode = code[0];
279  if (opcodes[opcode] == -1) {
280  // invalid opcode
281  return false;
282  }
283 
284  // valid opcode
285  MI->address = address;
286  MI->OpcodePub = MI->Opcode = opcode;
287 
288  if (opcode >= EVM_INS_PUSH1 && opcode <= EVM_INS_PUSH32) {
289  unsigned char len = (opcode - EVM_INS_PUSH1 + 1);
290  if (code_len < 1 + len) {
291  // not enough data
292  return false;
293  }
294 
295  *size = 1 + len;
296  memcpy(MI->evm_data, code + 1, len);
297  } else
298  *size = 1;
299 
300  if (MI->flat_insn->detail) {
301  memset(MI->flat_insn->detail, 0, offsetof(cs_detail, evm)+sizeof(cs_evm));
302  EVM_get_insn_id((cs_struct *)ud, MI->flat_insn, opcode);
303 
304  if (MI->flat_insn->detail->evm.pop) {
305  MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = EVM_GRP_STACK_READ;
306  MI->flat_insn->detail->groups_count++;
307  }
308 
309  if (MI->flat_insn->detail->evm.push) {
310  MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = EVM_GRP_STACK_WRITE;
311  MI->flat_insn->detail->groups_count++;
312  }
313 
314  // setup groups
315  switch(opcode) {
316  default:
317  break;
318  case EVM_INS_ADD:
319  case EVM_INS_MUL:
320  case EVM_INS_SUB:
321  case EVM_INS_DIV:
322  case EVM_INS_SDIV:
323  case EVM_INS_MOD:
324  case EVM_INS_SMOD:
325  case EVM_INS_ADDMOD:
326  case EVM_INS_MULMOD:
327  case EVM_INS_EXP:
328  case EVM_INS_SIGNEXTEND:
329  MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = EVM_GRP_MATH;
330  MI->flat_insn->detail->groups_count++;
331  break;
332 
333  case EVM_INS_MSTORE:
334  case EVM_INS_MSTORE8:
336  case EVM_INS_CODECOPY:
337  case EVM_INS_EXTCODECOPY:
338  MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = EVM_GRP_MEM_WRITE;
339  MI->flat_insn->detail->groups_count++;
340  break;
341 
342  case EVM_INS_MLOAD:
343  case EVM_INS_CREATE:
344  case EVM_INS_CALL:
345  case EVM_INS_CALLCODE:
346  case EVM_INS_RETURN:
348  case EVM_INS_REVERT:
349  MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = EVM_GRP_MEM_READ;
350  MI->flat_insn->detail->groups_count++;
351  break;
352 
353  case EVM_INS_SSTORE:
354  MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = EVM_GRP_STORE_WRITE;
355  MI->flat_insn->detail->groups_count++;
356  break;
357 
358  case EVM_INS_SLOAD:
359  MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = EVM_GRP_STORE_READ;
360  MI->flat_insn->detail->groups_count++;
361  break;
362 
363  case EVM_INS_JUMP:
364  case EVM_INS_JUMPI:
365  MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = EVM_GRP_JUMP;
366  MI->flat_insn->detail->groups_count++;
367  break;
368 
369  case EVM_INS_STOP:
370  case EVM_INS_SUICIDE:
371  MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = EVM_GRP_HALT;
372  MI->flat_insn->detail->groups_count++;
373  break;
374 
375  }
376  }
377 
378  return true;
379 }
size_t len
Definition: 6502dis.c:15
static short opcodes[256]
void EVM_get_insn_id(cs_struct *h, cs_insn *insn, unsigned int id)
@ EVM_INS_DIV
Definition: evm.h:30
@ EVM_INS_CODECOPY
Definition: evm.h:59
@ EVM_INS_SLOAD
Definition: evm.h:75
@ EVM_INS_CALL
Definition: evm.h:153
@ EVM_INS_MLOAD
Definition: evm.h:72
@ EVM_INS_SMOD
Definition: evm.h:33
@ EVM_INS_MUL
Definition: evm.h:28
@ EVM_INS_DELEGATECALL
Definition: evm.h:156
@ EVM_INS_RETURN
Definition: evm.h:155
@ EVM_INS_ADDMOD
Definition: evm.h:34
@ EVM_INS_SSTORE
Definition: evm.h:76
@ EVM_INS_CALLDATACOPY
Definition: evm.h:57
@ EVM_INS_ADD
Definition: evm.h:27
@ EVM_INS_PUSH1
Definition: evm.h:83
@ EVM_INS_MSTORE8
Definition: evm.h:74
@ EVM_INS_EXP
Definition: evm.h:36
@ EVM_INS_SUB
Definition: evm.h:29
@ EVM_INS_MULMOD
Definition: evm.h:35
@ EVM_INS_PUSH32
Definition: evm.h:114
@ EVM_INS_EXTCODECOPY
Definition: evm.h:62
@ EVM_INS_STOP
Definition: evm.h:26
@ EVM_INS_CREATE
Definition: evm.h:152
@ EVM_INS_REVERT
Definition: evm.h:159
@ EVM_INS_MSTORE
Definition: evm.h:73
@ EVM_INS_CALLCODE
Definition: evm.h:154
@ EVM_INS_JUMP
Definition: evm.h:77
@ EVM_INS_SDIV
Definition: evm.h:31
@ EVM_INS_SIGNEXTEND
Definition: evm.h:37
@ EVM_INS_MOD
Definition: evm.h:32
@ EVM_INS_JUMPI
Definition: evm.h:78
@ EVM_INS_SUICIDE
Definition: evm.h:160
@ EVM_GRP_MATH
math instructions
Definition: evm.h:172
@ EVM_GRP_STORE_READ
instructions read from storage
Definition: evm.h:178
@ EVM_GRP_STACK_WRITE
instructions write to stack
Definition: evm.h:173
@ EVM_GRP_JUMP
all jump instructions
Definition: evm.h:170
@ EVM_GRP_MEM_WRITE
instructions write to memory
Definition: evm.h:175
@ EVM_GRP_MEM_READ
instructions read from memory
Definition: evm.h:176
@ EVM_GRP_STACK_READ
instructions read from stack
Definition: evm.h:174
@ EVM_GRP_HALT
instructions halt execution
Definition: evm.h:179
@ EVM_GRP_STORE_WRITE
instructions write to storage
Definition: evm.h:177
voidpf void uLong size
Definition: ioapi.h:138
#define offsetof(type, member)
return memset(p, 0, total)
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
cs_insn * flat_insn
Definition: MCInst.h:95
unsigned OpcodePub
Definition: MCInst.h:89
unsigned char evm_data[32]
Definition: MCInst.h:110
unsigned Opcode
Definition: MCInst.h:93
uint64_t address
Definition: MCInst.h:96
Definition: inftree9.h:24
Instruction structure.
Definition: evm.h:18

References MCInst::address, MCInst::evm_data, EVM_get_insn_id(), EVM_GRP_HALT, EVM_GRP_JUMP, EVM_GRP_MATH, EVM_GRP_MEM_READ, EVM_GRP_MEM_WRITE, EVM_GRP_STACK_READ, EVM_GRP_STACK_WRITE, EVM_GRP_STORE_READ, EVM_GRP_STORE_WRITE, EVM_INS_ADD, EVM_INS_ADDMOD, EVM_INS_CALL, EVM_INS_CALLCODE, EVM_INS_CALLDATACOPY, EVM_INS_CODECOPY, EVM_INS_CREATE, EVM_INS_DELEGATECALL, EVM_INS_DIV, EVM_INS_EXP, EVM_INS_EXTCODECOPY, EVM_INS_JUMP, EVM_INS_JUMPI, EVM_INS_MLOAD, EVM_INS_MOD, EVM_INS_MSTORE, EVM_INS_MSTORE8, EVM_INS_MUL, EVM_INS_MULMOD, EVM_INS_PUSH1, EVM_INS_PUSH32, EVM_INS_RETURN, EVM_INS_REVERT, EVM_INS_SDIV, EVM_INS_SIGNEXTEND, EVM_INS_SLOAD, EVM_INS_SMOD, EVM_INS_SSTORE, EVM_INS_STOP, EVM_INS_SUB, EVM_INS_SUICIDE, MCInst::flat_insn, len, memcpy(), memset(), offsetof, MCInst::Opcode, MCInst::OpcodePub, and opcodes.

Variable Documentation

◆ opcodes

short opcodes[256]
static

Definition at line 11 of file EVMDisassembler.c.

Referenced by EVM_getInstruction().