From f9900d78f32998e70dedf9cd39ff22e20038e73c Mon Sep 17 00:00:00 2001 From: silur Date: Fri, 9 Feb 2024 20:34:47 +0100 Subject: [PATCH] remove evm arch,anal,debug, fix evm io --- libr/Makefile | 7 - libr/anal/Makefile | 3 - libr/anal/p/Makefile | 1 - libr/anal/p/anal_evm.c | 638 --------------------------------------- libr/anal/p/evm.mk | 18 -- libr/asm/Makefile | 3 - libr/asm/arch/evm/evm.c | 263 ---------------- libr/asm/arch/evm/evm.h | 174 ----------- libr/asm/p/Makefile | 1 - libr/asm/p/arch_evm.c | 47 --- libr/asm/p/asm_evm.c | 47 --- libr/asm/p/evm.mk | 17 -- libr/debug/Makefile | 3 - libr/debug/p/Makefile | 1 - libr/debug/p/debug_evm.c | 286 ------------------ libr/debug/p/evm.mk | 14 - libr/io/p/io_evm.c | 8 +- 17 files changed, 5 insertions(+), 1526 deletions(-) delete mode 100644 libr/anal/p/anal_evm.c delete mode 100644 libr/anal/p/evm.mk delete mode 100644 libr/asm/arch/evm/evm.c delete mode 100644 libr/asm/arch/evm/evm.h delete mode 100644 libr/asm/p/arch_evm.c delete mode 100644 libr/asm/p/asm_evm.c delete mode 100644 libr/asm/p/evm.mk delete mode 100644 libr/debug/p/debug_evm.c delete mode 100644 libr/debug/p/evm.mk diff --git a/libr/Makefile b/libr/Makefile index 8a6599b630..32d7872fde 100644 --- a/libr/Makefile +++ b/libr/Makefile @@ -34,11 +34,7 @@ zyan: $(MAKE) -C asm zyan evm: - $(MAKE) -C asm evm - $(MAKE) -C anal evm $(MAKE) -C io evm - $(MAKE) -C debug evm - nios: $(MAKE) -C anal nios @@ -77,10 +73,7 @@ zyan-install: cp -f asm/p/asm_x86_zyan.$(LIBEXT) $(R2PM_PLUGDIR) evm-install: - cp -f asm/p/asm_evm.$(LIBEXT) $(R2PM_PLUGDIR) - cp -f anal/p/anal_evm.$(LIBEXT) $(R2PM_PLUGDIR) cp -f io/p/io_evm.$(LIBEXT) $(R2PM_PLUGDIR) - cp -f debug/p/debug_evm.$(LIBEXT) $(R2PM_PLUGDIR) nios-install: cp -f asm/p/asm_nios.$(LIBEXT) $(R2PM_PLUGDIR) diff --git a/libr/anal/Makefile b/libr/anal/Makefile index 3360e3f8a5..53a507bd17 100644 --- a/libr/anal/Makefile +++ b/libr/anal/Makefile @@ -17,9 +17,6 @@ m68k_net: atombios: cd p ; $(MAKE) anal_atombios.$(LIBEXT) -evm: - cd p ; $(MAKE) anal_evm.$(LIBEXT) - nios: cd p ; $(MAKE) anal_nios.$(LIBEXT) diff --git a/libr/anal/p/Makefile b/libr/anal/p/Makefile index bab013ea1b..a191006815 100644 --- a/libr/anal/p/Makefile +++ b/libr/anal/p/Makefile @@ -6,7 +6,6 @@ ARCHS+=x86_bea.mk ARCHS+=x86_simple.mk ARCHS+=x86_udis.mk ARCHS+=m68k_net.mk -ARCHS+=evm.mk ARCHS+=atombios.mk ARCHS+=rl78.mk ARCHS+=nios.mk diff --git a/libr/anal/p/anal_evm.c b/libr/anal/p/anal_evm.c deleted file mode 100644 index 10db08dffa..0000000000 --- a/libr/anal/p/anal_evm.c +++ /dev/null @@ -1,638 +0,0 @@ -#include -#include - -#include - -#include -#include -#include -#include -#include -#include - -#include "evm.h" - -struct evm_anal_info { - Sdb *pushs_db; -}; - -static struct evm_anal_info *evm_ai = NULL; - -static unsigned opcodes_types[] = { - [EVM_OP_STOP] = R_ANAL_OP_TYPE_RET, - [EVM_OP_ADD] = R_ANAL_OP_TYPE_ADD, - [EVM_OP_MUL] = R_ANAL_OP_TYPE_MUL, - [EVM_OP_SUB] = R_ANAL_OP_TYPE_SUB, - [EVM_OP_DIV] = R_ANAL_OP_TYPE_DIV, - [EVM_OP_SDIV] = R_ANAL_OP_TYPE_DIV, - [EVM_OP_MOD] = R_ANAL_OP_TYPE_MOD, - [EVM_OP_SMOD] = R_ANAL_OP_TYPE_MOD, - [EVM_OP_ADDMOD] = R_ANAL_OP_TYPE_ADD, - [EVM_OP_MULMOD] = R_ANAL_OP_TYPE_MUL, - [EVM_OP_EXP] = R_ANAL_OP_TYPE_MUL, - [EVM_OP_SIGNEXTEND] = R_ANAL_OP_TYPE_CAST, - [EVM_OP_LT] = R_ANAL_OP_TYPE_COND, - [EVM_OP_GT] = R_ANAL_OP_TYPE_COND, - [EVM_OP_SLT] = R_ANAL_OP_TYPE_COND, - [EVM_OP_SGT] = R_ANAL_OP_TYPE_COND, - - [EVM_OP_EQ] = R_ANAL_OP_TYPE_CMP, - [EVM_OP_ISZERO] = R_ANAL_OP_TYPE_CMP, - [EVM_OP_AND] = R_ANAL_OP_TYPE_AND, - [EVM_OP_OR] = R_ANAL_OP_TYPE_OR, - [EVM_OP_XOR] = R_ANAL_OP_TYPE_XOR, - [EVM_OP_NOT] = R_ANAL_OP_TYPE_NOT, - [EVM_OP_BYTE] = R_ANAL_OP_TYPE_MOV, - [EVM_OP_SHA3] = R_ANAL_OP_TYPE_CRYPTO, - - [EVM_OP_ADDRESS] = R_ANAL_OP_TYPE_CRYPTO, - [EVM_OP_BALANCE] = R_ANAL_OP_TYPE_CRYPTO, - [EVM_OP_ORIGIN] = R_ANAL_OP_TYPE_CRYPTO, - [EVM_OP_CALLER] = R_ANAL_OP_TYPE_CRYPTO, - [EVM_OP_CALLVALUE] = R_ANAL_OP_TYPE_CRYPTO, - [EVM_OP_CALLDATALOAD] = R_ANAL_OP_TYPE_CRYPTO, - [EVM_OP_CALLDATASIZE] = R_ANAL_OP_TYPE_CRYPTO, - [EVM_OP_CALLDATACOPY] = R_ANAL_OP_TYPE_CRYPTO, - [EVM_OP_CODESIZE] = R_ANAL_OP_TYPE_CRYPTO, - [EVM_OP_CODECOPY] = R_ANAL_OP_TYPE_CRYPTO, - [EVM_OP_GASPRICE] = R_ANAL_OP_TYPE_CRYPTO, - [EVM_OP_EXTCODESIZE] = R_ANAL_OP_TYPE_CRYPTO, - [EVM_OP_EXTCODECOPY] = R_ANAL_OP_TYPE_CRYPTO, - [EVM_OP_RETURNDATASIZE] = R_ANAL_OP_TYPE_PUSH, - [EVM_OP_RETURNDATACOPY] = R_ANAL_OP_TYPE_STORE, - - [EVM_OP_BLOCKHASH] = R_ANAL_OP_TYPE_CRYPTO, - [EVM_OP_COINBASE] = R_ANAL_OP_TYPE_CRYPTO, - [EVM_OP_TIMESTAMP] = R_ANAL_OP_TYPE_CRYPTO, - [EVM_OP_NUMBER] = R_ANAL_OP_TYPE_CRYPTO, - [EVM_OP_DIFFICULTY] = R_ANAL_OP_TYPE_CRYPTO, - [EVM_OP_GASLIMIT] = R_ANAL_OP_TYPE_CRYPTO, - - [EVM_OP_POP] = R_ANAL_OP_TYPE_POP, - [EVM_OP_MLOAD] = R_ANAL_OP_TYPE_LOAD, - [EVM_OP_MSTORE] = R_ANAL_OP_TYPE_STORE, - [EVM_OP_MSTORE8] = R_ANAL_OP_TYPE_STORE, - [EVM_OP_SLOAD] = R_ANAL_OP_TYPE_LOAD, - [EVM_OP_SSTORE] = R_ANAL_OP_TYPE_STORE, - [EVM_OP_JUMP] = R_ANAL_OP_TYPE_JMP, - [EVM_OP_JUMPI] = R_ANAL_OP_TYPE_JMP, - [EVM_OP_PC] = R_ANAL_OP_TYPE_MOV, - [EVM_OP_MSIZE] = R_ANAL_OP_TYPE_MOV, - [EVM_OP_GAS] = R_ANAL_OP_TYPE_MOV, - [EVM_OP_JUMPDEST] = R_ANAL_OP_TYPE_NOP, - - [EVM_OP_PUSH1] = R_ANAL_OP_TYPE_PUSH, - [EVM_OP_PUSH2] = R_ANAL_OP_TYPE_PUSH, - [EVM_OP_PUSH3] = R_ANAL_OP_TYPE_PUSH, - [EVM_OP_PUSH4] = R_ANAL_OP_TYPE_PUSH, - [EVM_OP_PUSH5] = R_ANAL_OP_TYPE_PUSH, - [EVM_OP_PUSH6] = R_ANAL_OP_TYPE_PUSH, - [EVM_OP_PUSH7] = R_ANAL_OP_TYPE_PUSH, - [EVM_OP_PUSH8] = R_ANAL_OP_TYPE_PUSH, - [EVM_OP_PUSH9] = R_ANAL_OP_TYPE_PUSH, - [EVM_OP_PUSH10] = R_ANAL_OP_TYPE_PUSH, - [EVM_OP_PUSH11] = R_ANAL_OP_TYPE_PUSH, - [EVM_OP_PUSH12] = R_ANAL_OP_TYPE_PUSH, - [EVM_OP_PUSH13] = R_ANAL_OP_TYPE_PUSH, - [EVM_OP_PUSH14] = R_ANAL_OP_TYPE_PUSH, - [EVM_OP_PUSH15] = R_ANAL_OP_TYPE_PUSH, - [EVM_OP_PUSH16] = R_ANAL_OP_TYPE_PUSH, - [EVM_OP_PUSH17] = R_ANAL_OP_TYPE_PUSH, - [EVM_OP_PUSH18] = R_ANAL_OP_TYPE_PUSH, - [EVM_OP_PUSH19] = R_ANAL_OP_TYPE_PUSH, - [EVM_OP_PUSH20] = R_ANAL_OP_TYPE_PUSH, - [EVM_OP_PUSH21] = R_ANAL_OP_TYPE_PUSH, - [EVM_OP_PUSH22] = R_ANAL_OP_TYPE_PUSH, - [EVM_OP_PUSH23] = R_ANAL_OP_TYPE_PUSH, - [EVM_OP_PUSH24] = R_ANAL_OP_TYPE_PUSH, - [EVM_OP_PUSH25] = R_ANAL_OP_TYPE_PUSH, - [EVM_OP_PUSH26] = R_ANAL_OP_TYPE_PUSH, - [EVM_OP_PUSH27] = R_ANAL_OP_TYPE_PUSH, - [EVM_OP_PUSH28] = R_ANAL_OP_TYPE_PUSH, - [EVM_OP_PUSH29] = R_ANAL_OP_TYPE_PUSH, - [EVM_OP_PUSH30] = R_ANAL_OP_TYPE_PUSH, - [EVM_OP_PUSH31] = R_ANAL_OP_TYPE_PUSH, - [EVM_OP_PUSH32] = R_ANAL_OP_TYPE_PUSH, - [EVM_OP_DUP1] = R_ANAL_OP_TYPE_MOV, - [EVM_OP_DUP2] = R_ANAL_OP_TYPE_MOV, - [EVM_OP_DUP3] = R_ANAL_OP_TYPE_MOV, - [EVM_OP_DUP4] = R_ANAL_OP_TYPE_MOV, - [EVM_OP_DUP5] = R_ANAL_OP_TYPE_MOV, - [EVM_OP_DUP6] = R_ANAL_OP_TYPE_MOV, - [EVM_OP_DUP7] = R_ANAL_OP_TYPE_MOV, - [EVM_OP_DUP8] = R_ANAL_OP_TYPE_MOV, - [EVM_OP_DUP9] = R_ANAL_OP_TYPE_MOV, - [EVM_OP_DUP10] = R_ANAL_OP_TYPE_MOV, - [EVM_OP_DUP11] = R_ANAL_OP_TYPE_MOV, - [EVM_OP_DUP12] = R_ANAL_OP_TYPE_MOV, - [EVM_OP_DUP13] = R_ANAL_OP_TYPE_MOV, - [EVM_OP_DUP14] = R_ANAL_OP_TYPE_MOV, - [EVM_OP_DUP15] = R_ANAL_OP_TYPE_MOV, - [EVM_OP_DUP16] = R_ANAL_OP_TYPE_MOV, - [EVM_OP_SWAP1] = R_ANAL_OP_TYPE_MOV, - [EVM_OP_SWAP2] = R_ANAL_OP_TYPE_MOV, - [EVM_OP_SWAP3] = R_ANAL_OP_TYPE_MOV, - [EVM_OP_SWAP4] = R_ANAL_OP_TYPE_MOV, - [EVM_OP_SWAP5] = R_ANAL_OP_TYPE_MOV, - [EVM_OP_SWAP6] = R_ANAL_OP_TYPE_MOV, - [EVM_OP_SWAP7] = R_ANAL_OP_TYPE_MOV, - [EVM_OP_SWAP8] = R_ANAL_OP_TYPE_MOV, - [EVM_OP_SWAP9] = R_ANAL_OP_TYPE_MOV, - [EVM_OP_SWAP10] = R_ANAL_OP_TYPE_MOV, - [EVM_OP_SWAP11] = R_ANAL_OP_TYPE_MOV, - [EVM_OP_SWAP12] = R_ANAL_OP_TYPE_MOV, - [EVM_OP_SWAP13] = R_ANAL_OP_TYPE_MOV, - [EVM_OP_SWAP14] = R_ANAL_OP_TYPE_MOV, - [EVM_OP_SWAP15] = R_ANAL_OP_TYPE_MOV, - [EVM_OP_SWAP16] = R_ANAL_OP_TYPE_MOV, - - [EVM_OP_LOG0] = R_ANAL_OP_TYPE_TRAP, - [EVM_OP_LOG1] = R_ANAL_OP_TYPE_TRAP, - [EVM_OP_LOG2] = R_ANAL_OP_TYPE_TRAP, - [EVM_OP_LOG3] = R_ANAL_OP_TYPE_TRAP, - [EVM_OP_LOG4] = R_ANAL_OP_TYPE_TRAP, - - [EVM_OP_CREATE] = R_ANAL_OP_TYPE_CRYPTO, - [EVM_OP_CALL] = R_ANAL_OP_TYPE_CRYPTO, - [EVM_OP_CALLCODE] = R_ANAL_OP_TYPE_CRYPTO, - [EVM_OP_RETURN] = R_ANAL_OP_TYPE_RET, - [EVM_OP_DELEGATECALL] = R_ANAL_OP_TYPE_CRYPTO, - [EVM_OP_REVERT] = R_ANAL_OP_TYPE_RET, - [EVM_OP_SELFDESTRUCT] = R_ANAL_OP_TYPE_CRYPTO, -}; - -struct evm_sigs_info { - RIO *rio; - RIODesc *riodesc; - ut8 *contents; - size_t contents_len; - json_t *root; - - Sdb *sigs_db; - Sdb *found_sigs_db; -}; - -static struct evm_sigs_info *sigs_info = 0; - -static int evm_oplen(ut8 opcode) { - int ret; - const EvmOpDef *opdef = &opcodes[opcode]; - - if (opdef->txt) { - return opdef->len; - } - switch (opcode) { - 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 pushSize = opcode - 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); - */ - ret = 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: - { - // settxtf (op, "dup%d", dupSize); - ret = 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: - { - // settxtf (op, "swap%d", swapSize); - ret = 1; - } - break; - case EVM_OP_LOG0: - case EVM_OP_LOG1: - case EVM_OP_LOG2: - case EVM_OP_LOG3: - case EVM_OP_LOG4: - { - // settxtf (op, "log%d", logSize); - ret = 1; - } - break; - default: - // settxtf (op, "invalid"); - ret = 1; - break; - } - - return ret; -} - -/* Jumps/calls in EVM are done via first pushing dst value - * on the stack, and then calling a jump/jumpi instruction, for example: - * 0x0000000d push 0x42 - * 0x0000000f jumpi - * - * we are storing the value in push instruction to db, but not at the - * addr of the push instruction, but at the addr of next jumpi instruction. - * So in our example we are inserting (0xf, 0x42) - */ -static int evm_add_push_to_db(ut64 addr, const ut8 *buf, int len) { - ut8 opcode = buf[0]; - ut64 next_cmd_addr = addr + evm_oplen (opcode); - ut64 dst_addr = 0; - size_t i, push_size; - char key[16] = {0}, value[16] = {0}; - - push_size = opcode - EVM_OP_PUSH1; - - for (i = 0; i < push_size + 1; i++) { - dst_addr <<= 8; - dst_addr |= buf[i + 1]; - } - - if (evm_ai) { - snprintf (key, sizeof(key) - 1, "%08x", (unsigned) next_cmd_addr); - snprintf (value, sizeof(value) - 1, "%08x", (unsigned) dst_addr); - sdb_set (evm_ai->pushs_db, key, value, 0); - } - - return 0; -} - -static st64 evm_get_jmp_addr(ut64 addr) { - char key[16] = {0}; - const char *value; - unsigned ret; - - snprintf (key, sizeof(key) - 1, "%08x", (unsigned) addr); - - value = sdb_const_get (evm_ai->pushs_db, key, 0); - - if (value) { - sscanf(value, "%08x", &ret); - - return ret; - } else { - return -1; - } -} - -static int evm_op(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *buf, int len) { - ut32 *push4op; - st64 ret; - ut8 opcode; - char sig[64] = {0}; - - opcode = buf[0]; - - memset (op, 0, sizeof(RAnalOp)); - - op->type = opcodes_types[opcode]; - if (!op->type) { - op->type = R_ANAL_OP_TYPE_UNK; - } - op->addr = addr; - op->jump = op->fail = -1; - op->ptr = op->val = -1; - - r_strbuf_init (&op->esil); - - switch (opcode) { - case EVM_OP_JUMP: - case EVM_OP_JUMPI: - op->fail = addr + 1; - - if (opcode == EVM_OP_JUMP) { - op->type = R_ANAL_OP_TYPE_JMP; - } else { - op->type = R_ANAL_OP_TYPE_CJMP; - } - - ret = evm_get_jmp_addr(addr); - - if (ret >= 0) { - op->jump = ret; - } else { - op->type = R_ANAL_OP_TYPE_UJMP; - } - - op->eob = true; - break; - case EVM_OP_PC: - break; - case EVM_OP_MSIZE: - break; - case EVM_OP_GAS: - break; - case EVM_OP_JUMPDEST: - break; - case EVM_OP_PUSH1: - case EVM_OP_PUSH2: - case EVM_OP_PUSH3: - case EVM_OP_PUSH4: - evm_add_push_to_db (addr, buf, len); - - if (opcode == EVM_OP_PUSH4 && sigs_info && sigs_info->sigs_db) { - push4op = (ut32 *)(buf + 1); - - snprintf(sig, sizeof(sig) - 1, "0x%08x", ntohl((unsigned)(*push4op))); - - const char *data = sdb_const_get (sigs_info->sigs_db, sig, 0); - - if (data) { - char addr_str[16]; - char value[1024]; - - snprintf(addr_str, sizeof(addr_str) - 1, "0x%08x", (unsigned int)addr); - - addr_str[16 - 1] = '\0'; - - snprintf(value, sizeof(value) - 1, "calls a function %s with signature %s", - data, sig); - - value[1023] = '\0'; - - sdb_set(sigs_info->found_sigs_db, addr_str, value, 0); - r_meta_set_string(anal, 'C', addr, data); - } - } - - break; - default: - break; - } - - op->size = evm_oplen (opcode); - return evm_oplen (opcode); -} - - -static int evm_load_symbol_map (RAnal *anal, const char *file) { - size_t i; - int ret = 0; - bool rc; - - if (sigs_info) { - r_io_close (sigs_info->rio); - r_io_free (sigs_info->rio); - sdb_free (sigs_info->sigs_db); - sdb_free (sigs_info->found_sigs_db); - - free (sigs_info->contents); - free (sigs_info); - } - - sigs_info = malloc(sizeof(*sigs_info)); - memset(sigs_info, 0, sizeof(*sigs_info)); - - if (!sigs_info) { - ret = -1; - goto out; - } - - if (sigs_info) { - sigs_info->rio = r_io_new (); - sigs_info->riodesc = r_io_open (sigs_info->rio, file, R_PERM_R, 0644); - - if (!sigs_info->riodesc) { - printf ("Failed to open %s: %s\n", file, strerror (errno)); - ret = -1; - - goto out_free; - } else { - json_error_t error; - sigs_info->contents_len = r_io_size (sigs_info->rio); - sigs_info->contents = malloc(sigs_info->contents_len + 1); - - rc = r_io_read (sigs_info->rio, sigs_info->contents, sigs_info->contents_len); - - if (rc == false) { - printf ("Failed to read file\n"); - ret = -1; - - goto out_close; - } - - sigs_info->contents[sigs_info->contents_len] = '\0'; - - sigs_info->root = json_loads ((const char*)sigs_info->contents, 0, &error); - - if (!sigs_info->root) { - printf ("Failed to parse json document on line %d: %s\n", - error.line, error.text); - } else { - printf ("Parsed successfully\n"); - sigs_info->sigs_db = sdb_new0 (); - sigs_info->found_sigs_db = sdb_new0 (); - - for (i = 0; i < json_array_size (sigs_info->root); i++) { - json_t *elem, *sig, *args, *name; - char *name_str, *sig_str, *args_str, *value; - size_t value_size; - - elem = json_array_get (sigs_info->root, i); - - if (!elem) { - continue; - } - - sig = json_object_get (elem, "sig"); - name = json_object_get (elem, "name"); - args = json_object_get (elem, "args"); - - if (!sig || !name || !args) { - continue; - } - - sig_str = strdup (json_string_value (sig)); - name_str = strdup (json_string_value (name)); - args_str = strdup (json_string_value (args)); - - value_size = strlen(name_str) + strlen(args_str) + 8; - - value = malloc(sizeof(char) * value_size); - - snprintf(value, value_size - 1, "%s(%s)", name_str, args_str); - - sdb_set (sigs_info->sigs_db, sig_str, value, 0); - - free (value); - - free (args_str); - free (name_str); - free (sig_str); - } - - json_decref (sigs_info->root); - free (sigs_info->contents); - - sigs_info->root = 0; - sigs_info->contents = 0; - } - } - } - - goto out; - -out_close: - r_io_close (sigs_info->rio); - -out_free: - r_io_free (sigs_info->rio); - sigs_info->rio = NULL; - free (sigs_info); - sigs_info = NULL; - -out: - return ret; -} - -static int evm_list_found_symbols_cb (void *user, const char *k, const char *v) { - printf ("%s | %s\n", k, v); - - return 1; -} - -static int evm_list_found_symbols (RAnal *anal) { - if (sigs_info && sigs_info->found_sigs_db) { - sdb_foreach (sigs_info->found_sigs_db, evm_list_found_symbols_cb, NULL); - } - - return 0; -} - -static void evm_cmd_ext_help () { - printf ("a!l - Read a JSON file with function signatures\n" - "a!f - List found function signatures\n" - "a!h - Show this help message\n"); -} - -static int evm_cmd_ext (RAnal *anal, const char *input) { - const char *arg = input; - arg++; - - while (*arg == ' ') { - arg++; - } - - switch (input[0]) { - case 'l': - printf("here %s\n", arg); - evm_load_symbol_map (anal, arg); - break; - case 'f': - evm_list_found_symbols (anal); - break; - case 'h': - default: - evm_cmd_ext_help (); - return -1; - } - - return 0; -} - -static int evm_anal_init (void *user) { - if (!evm_ai) { - evm_ai = (struct evm_anal_info*)malloc (sizeof(*evm_ai)); - - evm_ai->pushs_db = sdb_new0 (); - } - - return 0; -} - -static int evm_anal_fini (void *user) { - if (evm_ai) { - if (evm_ai->pushs_db) { - sdb_free (evm_ai->pushs_db); - } - - free(evm_ai); - evm_ai = NULL; - } - - return 0; -} - -RAnalPlugin r_anal_plugin_evm = { - .name = "evm", - .desc = "ETHEREUM VM code analysis plugin", - .license = "LGPL3", - .arch = "evm", - .bits = 8, - .op = evm_op, - .init = evm_anal_init, - .fini = evm_anal_fini, - .esil = false, - .cmd_ext = evm_cmd_ext, -}; - -#ifndef CORELIB -struct r_lib_struct_t radare_plugin = { - .type = R_LIB_TYPE_ANAL, - .data = &r_anal_plugin_evm, - .version = R2_VERSION -}; -#endif diff --git a/libr/anal/p/evm.mk b/libr/anal/p/evm.mk deleted file mode 100644 index d49a3bcd4d..0000000000 --- a/libr/anal/p/evm.mk +++ /dev/null @@ -1,18 +0,0 @@ -OBJ_EVM=anal_evm.o -OBJ_EVM+=../../asm/arch/evm/evm.o - -STATIC_OBJ+=${OBJ_EVM} -TARGET_evm=anal_evm.${LIBEXT} - -ALL_TARGETS+=${TARGET_evm} -LINKFLAGS+=-ljansson - -CFLAGS_EVM=$(CFLAGS) -CFLAGS_EVM+=-I../../asm/arch/evm -CFLAGS_EVM+=-Wall -shared -fPIC ${LDFLAGS_LIB} - -$(OBJ_EVM): %.o : %.c - $(CC) -c $(CFLAGS_EVM) $< -o $@ - -${TARGET_evm}: ${OBJ_EVM} ${SHARED_OBJ} - ${CC} $(call libname,anal_evm) ${LDFLAGS} ${CFLAGS_EVM} -o ${TARGET_evm} ${OBJ_EVM} ${LINKFLAGS} diff --git a/libr/asm/Makefile b/libr/asm/Makefile index 814ccebd0b..3c12d06192 100644 --- a/libr/asm/Makefile +++ b/libr/asm/Makefile @@ -39,9 +39,6 @@ m68k_net: zyan: $(MAKE) -C p asm_x86_zyan.$(LIBEXT) -evm: - $(MAKE) -C p asm_evm.$(LIBEXT) - zyan-install: zyan mkdir -p $(R2PM_PLUGDIR) cp -f p/asm_x86_zyan.$(LIBEXT) $(R2PM_PLUGDIR) diff --git a/libr/asm/arch/evm/evm.c b/libr/asm/arch/evm/evm.c deleted file mode 100644 index 5dd03174fc..0000000000 --- a/libr/asm/arch/evm/evm.c +++ /dev/null @@ -1,263 +0,0 @@ -#include -#include -#include -#include -#include - -#include "evm.h" - -const 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", 1 }, - [EVM_OP_XOR] = { "xor", 1 }, - [EVM_OP_NOT] = { "not", 1 }, - [EVM_OP_BYTE] = { "byte", 1 }, - [EVM_OP_SHL] = { "shl", 1 }, - [EVM_OP_SHR] = { "shr", 1 }, - [EVM_OP_SAR] = { "sar", 1 }, - [EVM_OP_SHA3] = { "sha3", 1 }, - [EVM_OP_ADDRESS] = { "address", 1 }, - [EVM_OP_BALANCE] = { "balance", 1 }, - [EVM_OP_ORIGIN] = { "origin", 1 }, - [EVM_OP_CALLER] = { "caller", 1 }, - [EVM_OP_CALLVALUE] = { "callvalue", 1 }, - [EVM_OP_CALLDATALOAD] = { "calldataload", 1 }, - [EVM_OP_CALLDATASIZE] = { "calldatasize", 1 }, - [EVM_OP_CALLDATACOPY] = { "calldatacopy", 1 }, - [EVM_OP_CODESIZE] = { "codesize", 1 }, - [EVM_OP_CODECOPY] = { "codecopy", 1 }, - [EVM_OP_GASPRICE] = { "gasprice", 1 }, - [EVM_OP_EXTCODESIZE] = { "extcodesize", 1 }, - [EVM_OP_EXTCODECOPY] = { "extcodecopy", 1 }, - [EVM_OP_RETURNDATASIZE] = { "returndatasize", 1}, - [EVM_OP_RETURNDATACOPY] = { "returndatacopy", 1}, - [EVM_OP_EXTCODEHASH] = { "extcodehash", 1}, - [EVM_OP_BLOCKHASH] = { "blockhash", 1 }, - [EVM_OP_COINBASE] = { "coinbase", 1 }, - [EVM_OP_TIMESTAMP] = { "timestamp", 1 }, - [EVM_OP_NUMBER] = { "number", 1 }, - [EVM_OP_DIFFICULTY] = { "difficulty", 1 }, - [EVM_OP_GASLIMIT] = { "gaslimit", 1 }, - [EVM_OP_CHAINID] = { "chainid", 1 }, - [EVM_OP_SELFBALANCE] = { "selfbalance", 1 }, - [EVM_OP_POP] = { "pop", 1 }, - [EVM_OP_MLOAD] = { "mload", 1 }, - [EVM_OP_MSTORE] = { "mstore", 1 }, - [EVM_OP_MSTORE8] = { "mstore8", 1 }, - [EVM_OP_SLOAD] = { "sload", 1 }, - [EVM_OP_SSTORE] = { "sstore", 1 }, - [EVM_OP_JUMP] = { "jump", 1 }, - [EVM_OP_JUMPI] = { "jumpi", 1 }, - [EVM_OP_PC] = { "pc", 1 }, - [EVM_OP_MSIZE] = { "msize", 1 }, - [EVM_OP_GAS] = { "gas", 1 }, - [EVM_OP_JUMPDEST] = { "jumpdest", 1 }, - // .... - [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_CREATE2] = { "create2", 1 }, - [EVM_OP_STATICCALL] = { "staticcall", 1 }, - [EVM_OP_REVERT] = { "revert", 1 }, - [EVM_OP_INVALID] = { "invalid", 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, RStrBuf *buf, int buf_len) { - int i, len = -1; - for (i = 0; i < 0xff; i++) { - const EvmOpDef *opdef = &opcodes[i]; - if (opdef->txt) { - if (!strcmp (opdef->txt, str)) { - r_strbuf_appendf (buf, "%d", 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]; - const 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 pushSize = buf[0] - EVM_OP_PUSH1; - char hexbuf[64] = {0}; - int res = r_hex_bin2str (buf + 1, pushSize + 1, hexbuf); - if (res < 1 || !*hexbuf) { - strcpy (hexbuf, "0"); - } - settxtf (op, "push%d 0x%s", pushSize + 1, hexbuf); - 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 = 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 = 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 = 1; - } - break; - default: - settxtf (op, "unassigned"); - 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 diff --git a/libr/asm/arch/evm/evm.h b/libr/asm/arch/evm/evm.h deleted file mode 100644 index c5a01f89d5..0000000000 --- a/libr/asm/arch/evm/evm.h +++ /dev/null @@ -1,174 +0,0 @@ -#ifndef EVM_EVM_H -#define EVM_EVM_H - -#include - -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 = 0x10, - EVM_OP_GT, - EVM_OP_SLT, - EVM_OP_SGT, - EVM_OP_EQ, - EVM_OP_ISZERO, - EVM_OP_AND, - EVM_OP_OR, - EVM_OP_XOR, - EVM_OP_NOT, - EVM_OP_BYTE, - EVM_OP_SHL, - EVM_OP_SHR, - EVM_OP_SAR, - 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_RETURNDATASIZE, - EVM_OP_RETURNDATACOPY, - EVM_OP_EXTCODEHASH, - EVM_OP_BLOCKHASH, - EVM_OP_COINBASE, - EVM_OP_TIMESTAMP, - EVM_OP_NUMBER, - EVM_OP_DIFFICULTY, - EVM_OP_GASLIMIT, - EVM_OP_CHAINID, - EVM_OP_SELFBALANCE, - - 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_PUSH1 = 0x60, - EVM_OP_PUSH2, - EVM_OP_PUSH3, - EVM_OP_PUSH4, - EVM_OP_PUSH5, - EVM_OP_PUSH6, - EVM_OP_PUSH7, - EVM_OP_PUSH8, - EVM_OP_PUSH9, - EVM_OP_PUSH10, - EVM_OP_PUSH11, - EVM_OP_PUSH12, - EVM_OP_PUSH13, - EVM_OP_PUSH14, - EVM_OP_PUSH15, - EVM_OP_PUSH16, - EVM_OP_PUSH17, - EVM_OP_PUSH18, - EVM_OP_PUSH19, - EVM_OP_PUSH20, - EVM_OP_PUSH21, - EVM_OP_PUSH22, - EVM_OP_PUSH23, - EVM_OP_PUSH24, - EVM_OP_PUSH25, - EVM_OP_PUSH26, - EVM_OP_PUSH27, - EVM_OP_PUSH28, - EVM_OP_PUSH29, - EVM_OP_PUSH30, - EVM_OP_PUSH31, - EVM_OP_PUSH32, - EVM_OP_DUP1, - EVM_OP_DUP2, - EVM_OP_DUP3, - EVM_OP_DUP4, - EVM_OP_DUP5, - EVM_OP_DUP6, - EVM_OP_DUP7, - EVM_OP_DUP8, - EVM_OP_DUP9, - EVM_OP_DUP10, - EVM_OP_DUP11, - EVM_OP_DUP12, - EVM_OP_DUP13, - EVM_OP_DUP14, - EVM_OP_DUP15, - EVM_OP_DUP16, - EVM_OP_SWAP1, - EVM_OP_SWAP2, - EVM_OP_SWAP3, - EVM_OP_SWAP4, - EVM_OP_SWAP5, - EVM_OP_SWAP6, - EVM_OP_SWAP7, - EVM_OP_SWAP8, - EVM_OP_SWAP9, - EVM_OP_SWAP10, - EVM_OP_SWAP11, - EVM_OP_SWAP12, - EVM_OP_SWAP13, - EVM_OP_SWAP14, - EVM_OP_SWAP15, - EVM_OP_SWAP16, - - EVM_OP_LOG0 = 0xa0, - EVM_OP_LOG1, - EVM_OP_LOG2, - EVM_OP_LOG3, - EVM_OP_LOG4, - - EVM_OP_CREATE = 0xf0, - EVM_OP_CALL, - EVM_OP_CALLCODE, - EVM_OP_RETURN, - EVM_OP_DELEGATECALL, - EVM_OP_CREATE2, - EVM_OP_STATICCALL = 0xfa, - EVM_OP_REVERT = 0xfd, - EVM_OP_INVALID, - EVM_OP_SELFDESTRUCT -} EvmOpcodes; - -typedef struct EvmOp { - EvmOpcodes op; - int len; - const char *txt; - // Largest opcode is "push32 0x" followed by hex value of 32 bytes. - char txt_buf[9+64+1]; -} EvmOp; - -typedef struct { - const char *txt; - int len; -} EvmOpDef; - -extern const EvmOpDef opcodes[256]; -int evm_dis(EvmOp *op, const unsigned char *buf, int buf_len); -int evm_asm(const char *str, RStrBuf *buf, int buf_len); - -#endif /* EVM_EVM_H */ diff --git a/libr/asm/p/Makefile b/libr/asm/p/Makefile index c18599f7e9..b4f7cba563 100644 --- a/libr/asm/p/Makefile +++ b/libr/asm/p/Makefile @@ -17,7 +17,6 @@ ARCHS+=vc4.mk ARCHS+=mc6809.mk ARCHS+=z80_nc.mk ARCHS+=x86_zyan.mk -ARCHS+=evm.mk ARCHS+=atombios.mk diff --git a/libr/asm/p/arch_evm.c b/libr/asm/p/arch_evm.c deleted file mode 100644 index 429129db0e..0000000000 --- a/libr/asm/p/arch_evm.c +++ /dev/null @@ -1,47 +0,0 @@ -/* radare - LGPL - Copyright 2017-2023 - pancake */ - -#include -#include -#include -#include -#include -#include "evm.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); - op->size = eop.len; - r_asm_op_set_asm (op, eop.txt); - - 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_a_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_ARCH, - .data = &r_arch_plugin_evm, - .version = R2_VERSION -}; -#endif diff --git a/libr/asm/p/asm_evm.c b/libr/asm/p/asm_evm.c deleted file mode 100644 index 07538df5bf..0000000000 --- a/libr/asm/p/asm_evm.c +++ /dev/null @@ -1,47 +0,0 @@ -/* radare - LGPL - Copyright 2017 - pancake */ - -#include -#include -#include -#include -#include -#include "evm.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); - op->size = eop.len; - r_asm_op_set_asm (op, eop.txt); - - 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 diff --git a/libr/asm/p/evm.mk b/libr/asm/p/evm.mk deleted file mode 100644 index 0f96d03e3c..0000000000 --- a/libr/asm/p/evm.mk +++ /dev/null @@ -1,17 +0,0 @@ -OBJ_EVM=asm_evm.o -OBJ_EVM+=../arch/evm/evm.o - -STATIC_OBJ+=${OBJ_EVM} -TARGET_EVM=asm_evm.${LIBEXT} - -ALL_TARGETS+=${TARGET_EVM} - -CFLAGS_EVM=$(CFLAGS) -CFLAGS_EVM+=-I../arch/evm -CFLAGS_EVM+=-Wall -shared -fPIC ${LDFLAGS_LIB} - -$(OBJ_EVM): %.o : %.c - $(CC) -c $(CFLAGS_EVM) $< -o $@ - -${TARGET_EVM}: ${OBJ_EVM} - ${CC} ${CFLAGS_EVM} ${LDFLAGS} -o ${TARGET_EVM} ${OBJ_EVM} -lr_util diff --git a/libr/debug/Makefile b/libr/debug/Makefile index b9c5309cfb..b94b3827c5 100644 --- a/libr/debug/Makefile +++ b/libr/debug/Makefile @@ -1,6 +1,3 @@ -evm: - $(MAKE) -C p debug_evm.$(LIBEXT) - unicorn: $(MAKE) -C p debug_unicorn.$(LIBEXT) diff --git a/libr/debug/p/Makefile b/libr/debug/p/Makefile index 72be21ee0d..233e4b3cb1 100644 --- a/libr/debug/p/Makefile +++ b/libr/debug/p/Makefile @@ -1,6 +1,5 @@ ARCHS= -ARCHS+=evm.mk ARCHS+=unicorn.mk include ../../plugs.mk diff --git a/libr/debug/p/debug_evm.c b/libr/debug/p/debug_evm.c deleted file mode 100644 index 6af7b00fcb..0000000000 --- a/libr/debug/p/debug_evm.c +++ /dev/null @@ -1,286 +0,0 @@ -/* 2017 - montekki */ - -#include -#include - -typedef struct { - size_t curr_instruction; - - ut64 *breakpoints; - size_t breakpoints_capacity; - size_t breakpoints_length; - -} RIOEvmState; - -typedef struct { - ut8 depth; - ut8 error; - unsigned pc; - unsigned gas; - unsigned gas_cost; - - ut8 *memory; - size_t memlength; - - ut8 *stack; - size_t stack_length; - char *op; -} RIOEvmOp; - -typedef struct { - void *curl; - char *host; - int port; - char *tx; - char *tx_full; - char *tx_to; - char *tx_from; - char *to_code_resp; - char *to_code; - - ut8 *code; - size_t code_size; - - char *response; - size_t curr_resp_size; - - RIOEvmOp *ops; - size_t ops_size; - - size_t curr_op; -} RIOEvm; - -static RIOEvm *rio = NULL; -static RIOEvmState *rios = NULL; - -static bool r_debug_evm_step(RDebug *dbg) { - if (!rio->ops_size || rios->curr_instruction >= rio->ops_size) { - return false; - } - - rios->curr_instruction++; - rio->curr_op = rios->curr_instruction; - - return true; -} - -static RList* r_debug_evm_threads(RDebug *dbg, int pid) { - return NULL; -} - -static int r_debug_evm_reg_read(RDebug *dbg, int type, ut8 *buf, int size) { - if (!rios || !rio || !rio->ops_size) { - return 0; - } - ((ut16*)(buf))[0] = (ut16)rio->ops[rios->curr_instruction].pc; - ((ut16*)(buf))[1] = (ut16)0x8fff; - return 4; -} - -static RList *r_debug_evm_map_get(RDebug *dbg) { - RList *list; - RDebugMap *map; - - if (rio && !rio->ops_size) { - return NULL; - } - - list = r_list_new(); - - map = r_debug_map_new ("code", 0x0000, 0x7FFF, r_str_rwx("rwx"), 0); - r_list_append (list, map); - - map = r_debug_map_new ("stack", 0x8FFF, 0xFFFF, r_str_rwx("rwx"), 0); - r_list_append (list, map); - - map = r_debug_map_new ("memory", 0x10000, 0x1FFFF, r_str_rwx("rwx"), 0); - r_list_append (list, map); - - return list; -} - -static int r_debug_evm_reg_write(RDebug *dbg, int type, const ut8 *buf, int size) { - return false; -} - -static ssize_t find_breakpoint(ut64 addr, ut64 *addrs, size_t addrs_len); - -static bool r_debug_evm_continue(RDebug *dbg, int pid, int tid, int sig) { - size_t i; - ssize_t bpt_addr; - - if (!rio->ops_size) { - return false; - } - - bpt_addr = find_breakpoint(rio->ops[rios->curr_instruction].pc, - rios->breakpoints, rios->breakpoints_length); - - if (bpt_addr >= 0 && rio->ops[rios->curr_instruction].pc == rios->breakpoints[bpt_addr]) { - rios->curr_instruction++; - } - - for (i = rios->curr_instruction; i < rio->ops_size; i++) { - bpt_addr = find_breakpoint(rio->ops[i].pc, rios->breakpoints, rios->breakpoints_length); - - if (bpt_addr >= 0) { - rios->curr_instruction = i; - rio->curr_op = rios->curr_instruction; - - return true; - } - } - - return true; -} - -static RDebugReasonType r_debug_evm_wait(RDebug *dbg, int pid) { - return R_DEBUG_REASON_UNKNOWN; -} - -#define DEFAULT_BPT_AR_CAPACITY 64 - -static bool r_debug_evm_attach(RDebug *dbg, int pid) { - RIODesc *d = dbg->iob.io->desc; - - if (!rios) { - rios = R_NEW0 (RIOEvmState); - - rios->breakpoints = (ut64*)malloc(sizeof(ut64) * DEFAULT_BPT_AR_CAPACITY); - rios->breakpoints_length = 0; - rios->breakpoints_capacity = DEFAULT_BPT_AR_CAPACITY; - } - - if (d && d->plugin && d->plugin->name && d->data) { - if (!strcmp("evm", d->plugin->name)) { - rio = d->data; - } - } - - return true; -} - -static bool r_debug_evm_detach(RDebug *dbg, int pid) { - return true; -} - -static const char *r_debug_evm_reg_profile(RDebug *dbg, int pid) { - return strdup ( - "=PC pc\n" - "=SP sp\n" - "gpr pc .16 0 0\n" - "gpr sp .16 2 0\n" - ); -} - -static ssize_t find_breakpoint(ut64 addr, ut64 *addrs, size_t addrs_len) { - if (addrs_len <= 0) { - return -1; - } - - size_t middle = addrs_len / 2; - - if (addrs[middle] == addr) { - return middle; - } else if (addrs[middle] < addr) { - return find_breakpoint(addr, addrs + middle + 1, addrs_len - middle - 1); - } else { - return find_breakpoint(addr, addrs, middle); - } - - return -1; -} - -static int compare_addrs(const void *a1, const void *a2) { - return *(ut64*)a1 - *(ut64*)a2; -} - -static int r_debug_evm_breakpoint (struct r_bp_t *bp, RBreakpointItem *b, bool set) { - if (!b || !rio->ops_size) { - return false; - } - - if (set) { - ssize_t idx = find_breakpoint(b->addr, rios->breakpoints, rios->breakpoints_length); - - if (idx >= 0) { - return true; - } - - if (rios->breakpoints_length >= rios->breakpoints_capacity) { - rios->breakpoints = realloc(rios->breakpoints, - rios->breakpoints_capacity + 64); - rios->breakpoints_capacity += 64; - } - - rios->breakpoints[rios->breakpoints_length] = b->addr; - rios->breakpoints_length++; - - qsort(rios->breakpoints, rios->breakpoints_length, sizeof(ut64), compare_addrs); - - } else { - size_t i; - ssize_t idx = find_breakpoint(b->addr, rios->breakpoints, rios->breakpoints_length); - - if (idx < 0) { - return false; - } - - for (i = idx; i < rios->breakpoints_length - 1; i++) { - rios->breakpoints[i] = rios->breakpoints[i + 1]; - } - - rios->breakpoints_length -= 1; - } - - return true; -} - -static bool r_debug_evm_kill(RDebug *dbg, int pid, int tid, int sig) { - return true; -} - -static bool r_debug_evm_select(RDebug *dbg, int pid, int tid) { - return true; -} - -static RDebugInfo* r_debug_evm_info(RDebug *dbg, const char *arg) { - RDebugInfo *rdi = R_NEW0 (RDebugInfo); - // meh - return rdi; -} - -static RList* r_debug_evm_frames(RDebug *dbg, ut64 at) { - return NULL; -} - -RDebugPlugin r_debug_plugin_evm = { - .name = "evm", - .license = "LGPL3", - .arch = "evm", - .bits = R_SYS_BITS_16 | R_SYS_BITS_8, - .step = r_debug_evm_step, - .cont = r_debug_evm_continue, - .attach = &r_debug_evm_attach, - .detach = &r_debug_evm_detach, - .threads = &r_debug_evm_threads, - .canstep = 1, - .wait = &r_debug_evm_wait, - .map_get = r_debug_evm_map_get, - .breakpoint = r_debug_evm_breakpoint, - .reg_read = r_debug_evm_reg_read, - .reg_write = r_debug_evm_reg_write, - .reg_profile = (void *)r_debug_evm_reg_profile, - .kill = &r_debug_evm_kill, - .info = &r_debug_evm_info, - .select = &r_debug_evm_select, - .frames = &r_debug_evm_frames, -}; - -#ifndef CORELIB -struct r_lib_struct_t radare_plugin = { - .type = R_LIB_TYPE_DBG, - .data = &r_debug_plugin_evm, - .version = R2_VERSION -}; -#endif diff --git a/libr/debug/p/evm.mk b/libr/debug/p/evm.mk deleted file mode 100644 index 1cd3718b55..0000000000 --- a/libr/debug/p/evm.mk +++ /dev/null @@ -1,14 +0,0 @@ -#LDFLAGS+=-lcurl - -#-include ../../global.mk -#-include ../../../global.mk - -OBJ_EVM=debug_evm.o - -STATIC_OBJ+=${OBJ_EVM} -TARGET_EVM=debug_evm.${LIBEXT} -ALL_TARGETS+=${TARGET_EVM} - -${TARGET_EVM}: ${OBJ_EVM} - ${CC} $(call libname,debug_evm) ${CFLAGS} -o ${TARGET_EVM} \ - ${LDFLAGS} ${OBJ_EVM} ${LINKFLAGS} diff --git a/libr/io/p/io_evm.c b/libr/io/p/io_evm.c index eb04b31b9a..0778659e8f 100644 --- a/libr/io/p/io_evm.c +++ b/libr/io/p/io_evm.c @@ -754,9 +754,11 @@ static char *__system(RIO *io, RIODesc *fd, const char *cmd) { } RIOPlugin r_io_plugin_evm = { - .name = "evm", - .license = "LGPL3", - .desc = "Attach to EVM RPC debug api evm://localhost:8545:contractaddr:txaddr", + .meta = { + .name = "evm", + .license = "LGPL3", + .desc = "Attach to EVM RPC debug api evm://localhost:8545:contractaddr:txaddr", + }, .open = __open, .close = __close, .read = __read,