We are moving EVM to radare2-extras

This commit is contained in:
Fedor Sakharov 2017-11-20 23:19:35 +03:00 committed by radare
parent 5957fcd087
commit 881e797e8b
7 changed files with 0 additions and 461 deletions

View File

@ -1,3 +0,0 @@
all:
gcc -DHAS_MAIN=1 evm.c
./a.out

View File

@ -1,401 +0,0 @@
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdarg.h>
#include <sys/types.h>
typedef enum {
EVM_OP_STOP = 0x00,
EVM_OP_ADD,
EVM_OP_MUL,
EVM_OP_SUB,
EVM_OP_DIV,
EVM_OP_SDIV,
EVM_OP_MOD,
EVM_OP_SMOD,
EVM_OP_ADDMOD,
EVM_OP_MULMOD,
EVM_OP_EXP,
EVM_OP_SIGNEXTEND,
EVM_OP_LT,
EVM_OP_GT,
EVM_OP_SLT,
EVM_OP_SGT,
EVM_OP_EQ = 0x10,
EVM_OP_ISZERO,
EVM_OP_AND,
EVM_OP_OR,
EVM_OP_XOR,
EVM_OP_NOT,
EVM_OP_BYTE,
EVM_OP_SHA3 = 0x20,
EVM_OP_ADDRESS = 0x30,
EVM_OP_BALANCE,
EVM_OP_ORIGIN,
EVM_OP_CALLER,
EVM_OP_CALLVALUE,
EVM_OP_CALLDATALOAD,
EVM_OP_CALLDATASIZE,
EVM_OP_CALLDATACOPY,
EVM_OP_CODESIZE,
EVM_OP_CODECOPY,
EVM_OP_GASPRICE,
EVM_OP_EXTCODESIZE,
EVM_OP_EXTCODECOPY,
EVM_OP_BLOCKHASH = 0x40,
EVM_OP_COINBASE,
EVM_OP_TIMESTAMP,
EVM_OP_NUMBER,
EVM_OP_DIFFICULTY,
EVM_OP_GASLIMIT,
EVM_OP_POP = 0x50,
EVM_OP_MLOAD,
EVM_OP_MSTORE,
EVM_OP_MSTORE8,
EVM_OP_SLOAD,
EVM_OP_SSTORE,
EVM_OP_JUMP,
EVM_OP_JUMPI,
EVM_OP_PC,
EVM_OP_MSIZE,
EVM_OP_GAS,
EVM_OP_JUMPDEST,
EVM_OP_P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,
EVM_OP_SELFDESTRUCT = 0xff
} EvmOpcodes;
typedef struct EvmOp {
EvmOpcodes op;
int len;
uint64_t imm;
const char *txt;
char txt_buf[32];
} EvmOp;
typedef struct {
const char *txt;
int len;
} EvmOpDef;
static EvmOpDef opcodes[256] = {
[EVM_OP_STOP] = { "stop", 1 },
[EVM_OP_ADD] = { "add", 1 },
[EVM_OP_MUL] = { "mul", 1 },
[EVM_OP_SUB] = { "sub", 1 },
[EVM_OP_DIV] = { "div", 1 },
[EVM_OP_SDIV] = { "sdiv", 1 },
[EVM_OP_MOD] = { "mod", 1 },
[EVM_OP_SMOD] = { "smod", 1 },
[EVM_OP_ADDMOD] = { "addmod", 1 },
[EVM_OP_MULMOD] = { "mulmod", 1 },
[EVM_OP_EXP] = { "exp", 1 },
[EVM_OP_SIGNEXTEND] = { "signextend", 1 },
[EVM_OP_LT] = { "lt", 1 },
[EVM_OP_GT] = { "gt", 1 },
[EVM_OP_SLT] = { "slt", 1 },
[EVM_OP_SGT] = { "sgt", 1 },
[EVM_OP_EQ] = { "eq", 1 },
[EVM_OP_ISZERO] = { "iszero", 1 },
[EVM_OP_AND] = { "and", 1 },
[EVM_OP_OR] = { "or" },
[EVM_OP_XOR] = { "xor" },
[EVM_OP_NOT] = { "not" },
[EVM_OP_BYTE] = { "byte" },
[EVM_OP_SHA3] = { "sha3" },
[EVM_OP_ADDRESS] = { "address" },
[EVM_OP_BALANCE] = { "balance" },
[EVM_OP_ORIGIN] = { "origin" },
[EVM_OP_CALLER] = { "caller" },
[EVM_OP_CALLVALUE] = { "callvalue" },
[EVM_OP_CALLDATALOAD] = { "calldataload" },
[EVM_OP_CALLDATASIZE] = { "calldatasize" },
[EVM_OP_CALLDATACOPY] = { "calldatacopy" },
[EVM_OP_CODESIZE] = { "codesize" },
[EVM_OP_CODECOPY] = { "codecopy" },
[EVM_OP_GASPRICE] = { "gasprice" },
[EVM_OP_EXTCODESIZE] = { "extcodesize" },
[EVM_OP_EXTCODECOPY] = { "extcodecopy" },
[EVM_OP_BLOCKHASH] = { "blockhash" },
[EVM_OP_COINBASE] = { "coinbase" },
[EVM_OP_TIMESTAMP] = { "timestamp" },
[EVM_OP_NUMBER] = { "number" },
[EVM_OP_DIFFICULTY] = { "difficulty" },
[EVM_OP_GASLIMIT] = { "gaslimit", 1 },
[EVM_OP_POP] = { "pop", 1 },
[EVM_OP_MLOAD] = { "mload", 1 },
[EVM_OP_MSTORE] = { "mstore" },
[EVM_OP_MSTORE8] = { "mstore8" },
[EVM_OP_SLOAD] = { "sload" },
[EVM_OP_SSTORE] = { "sstore" },
[EVM_OP_JUMP] = { "jump" },
[EVM_OP_JUMPI] = { "jumpi" },
[EVM_OP_PC] = { "pc" },
[EVM_OP_MSIZE] = { "msize" },
[EVM_OP_GAS] = { "gas" },
[EVM_OP_JUMPDEST] = { "jumpdest" },
// ....
[EVM_OP_CREATE] = { "create", 1 },
[EVM_OP_CALL] = { "call", 1 },
[EVM_OP_CALLCODE] = { "callcode", 1 },
[EVM_OP_RETURN] = { "return", 1 },
[EVM_OP_DELEGATECALL] = { "delegatecall", 1 },
[EVM_OP_SELFDESTRUCT] = { "selfdestruct", 1 },
};
static void settxtf(EvmOp *op, const char *fmt, ...) {
if (strchr (fmt, '%')) {
va_list ap;
va_start (ap, fmt);
op->txt = op->txt_buf;
vsnprintf (op->txt_buf, sizeof (op->txt_buf), fmt, ap);
va_end (ap);
} else {
op->txt = fmt;
}
}
int evm_asm (const char *str, unsigned char *buf, int buf_len) {
int i, len = -1;
for (i = 0; i< 0xff; i++) {
EvmOpDef *opdef = &opcodes[i];
if (opdef->txt) {
if (!strcmp (opdef->txt, str)) {
buf[0] = i;
return 1;
}
}
}
// TODO: add support for: push, swap, dup, log
return len;
}
int evm_dis (EvmOp *op, const unsigned char *buf, int buf_len) {
op->len = 1;
op->op = buf[0];
EvmOpDef *opdef = &opcodes[buf[0]];
if (opdef->txt) {
op->txt = opdef->txt;
op->len = opdef->len;
if (op->len < 1) {
op->len = 1;
}
return op->len;
}
switch (op->op) {
case EVM_OP_PUSH1:
case EVM_OP_PUSH2:
case EVM_OP_PUSH3:
case EVM_OP_PUSH4:
case EVM_OP_PUSH5:
case EVM_OP_PUSH6:
case EVM_OP_PUSH7:
case EVM_OP_PUSH8:
case EVM_OP_PUSH9:
case EVM_OP_PUSH10:
case EVM_OP_PUSH11:
case EVM_OP_PUSH12:
case EVM_OP_PUSH13:
case EVM_OP_PUSH14:
case EVM_OP_PUSH15:
case EVM_OP_PUSH16:
case EVM_OP_PUSH17:
case EVM_OP_PUSH18:
case EVM_OP_PUSH19:
case EVM_OP_PUSH20:
case EVM_OP_PUSH21:
case EVM_OP_PUSH22:
case EVM_OP_PUSH23:
case EVM_OP_PUSH24:
case EVM_OP_PUSH25:
case EVM_OP_PUSH26:
case EVM_OP_PUSH27:
case EVM_OP_PUSH28:
case EVM_OP_PUSH29:
case EVM_OP_PUSH30:
case EVM_OP_PUSH31:
case EVM_OP_PUSH32:
{
int i, pushSize = buf[0] - EVM_OP_PUSH1;
op->imm = 0;
for (i = 0; i < pushSize + 1; i++) {
op->imm <<= 8;
op->imm |= buf[i + 1];
}
settxtf (op, "push%d 0x%x", pushSize + 1, op->imm);
op->len = 2 + pushSize;
}
break;
case EVM_OP_DUP1:
case EVM_OP_DUP2:
case EVM_OP_DUP3:
case EVM_OP_DUP4:
case EVM_OP_DUP5:
case EVM_OP_DUP6:
case EVM_OP_DUP7:
case EVM_OP_DUP8:
case EVM_OP_DUP9:
case EVM_OP_DUP10:
case EVM_OP_DUP11:
case EVM_OP_DUP12:
case EVM_OP_DUP13:
case EVM_OP_DUP14:
case EVM_OP_DUP15:
case EVM_OP_DUP16:
{
int dupSize = buf[0] - EVM_OP_DUP1 + 1;
settxtf (op, "dup%d", dupSize);
op->len = dupSize + 1;
}
break;
case EVM_OP_SWAP1:
case EVM_OP_SWAP2:
case EVM_OP_SWAP3:
case EVM_OP_SWAP4:
case EVM_OP_SWAP5:
case EVM_OP_SWAP6:
case EVM_OP_SWAP7:
case EVM_OP_SWAP8:
case EVM_OP_SWAP9:
case EVM_OP_SWAP10:
case EVM_OP_SWAP11:
case EVM_OP_SWAP12:
case EVM_OP_SWAP13:
case EVM_OP_SWAP14:
case EVM_OP_SWAP15:
case EVM_OP_SWAP16:
{
int swapSize = buf[0] - EVM_OP_SWAP1 + 1;
settxtf (op, "swap%d", swapSize);
op->len = swapSize + 1;
}
break;
case EVM_OP_LOG0:
case EVM_OP_LOG1:
case EVM_OP_LOG2:
case EVM_OP_LOG3:
case EVM_OP_LOG4:
{
int logSize = buf[0] - EVM_OP_LOG0;
settxtf (op, "log%d", logSize);
op->len = logSize + 1;
}
break;
default:
settxtf (op, "invalid");
op->len = 0;
break;
}
return op->len;
}
typedef const unsigned char* buf_t;
#if HAS_MAIN
int main() {
#if 0
[1] 6060 PUSH1 0x60
[3] 6040 PUSH1 0x40
[4] 52 MSTORE
[5] 36 CALLDATASIZE
[6] 15 ISZERO // WRONG ?? should be 11
[9] PUSH2 0x00da
[10] JUMPI
[12] PUSH1 0xe0
[14] PUSH1 0x02
[15] EXP
#endif
int i;
EvmOp op = {0};
buf_t b = (buf_t)"\x60\x60\x60\x40\x52\x36\x15\x61\x00\xda\x57\x60\xe0\x60\x02";
for (i= 0; i < sizeof (b); i++) {
evm_dis (&op, b + i, sizeof (b) - i);
printf ("len=%d op=0x%02x %s\n", op.len, op.op, op.txt);
if (op.len < 1) {
break;
}
i += op.len - 1;
}
return 0;
}
#endif

File diff suppressed because one or more lines are too long

Binary file not shown.

View File

@ -1,43 +0,0 @@
/* radare - LGPL - Copyright 2017 - pancake */
#include <stdio.h>
#include <string.h>
#include <r_types.h>
#include <r_lib.h>
#include <r_asm.h>
#include "../arch/evm/evm.c"
static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) {
EvmOp eop = {0};
evm_dis (&eop, buf, len);
strncpy (op->buf_asm, eop.txt, sizeof (op->buf_asm));
op->buf_asm[sizeof (op->buf_asm) - 1] = 0;
op->size = eop.len;
return eop.len;
}
static int assemble(RAsm *a, RAsmOp *op, const char *buf) {
op->size = evm_asm (buf, op->buf, sizeof (op->buf));
return op->size;
}
RAsmPlugin r_asm_plugin_evm = {
.name = "evm",
.author = "pancake",
.version = "0.0.1",
.arch = "evm",
.license = "MIT",
.bits = 32,
.endian = R_SYS_ENDIAN_BIG,
.desc = "evm",
.disassemble = &disassemble,
.assemble = &assemble
};
#ifndef CORELIB
RLibStruct radare_plugin = {
.type = R_LIB_TYPE_ASM,
.data = &r_asm_plugin_evm,
.version = R2_VERSION
};
#endif

View File

@ -1,10 +0,0 @@
OBJ_EVM=asm_evm.o
TARGET_EVM=asm_evm.${EXT_SO}
STATIC_OBJ+=${OBJ_EVM}
ifeq ($(WITHPIC),1)
ALL_TARGETS+=${TARGET_EVM}
${TARGET_EVM}: ${OBJ_EVM}
${CC} $(call libname,asm_evm) ${LDFLAGS} ${CFLAGS} -o ${TARGET_EVM} ${OBJ_EVM}
endif

View File

@ -56,7 +56,6 @@ asm.dalvik
asm.dcpu16
asm.hexagon_gnu
asm.ebc
asm.evm
asm.gb
asm.h8300
asm.hppa_gnu