From c1bb9a26712c8db76bb25332534efad785318fd3 Mon Sep 17 00:00:00 2001 From: Andelf Date: Sun, 12 Jan 2025 14:35:05 +0800 Subject: [PATCH] Add support for new EVM opcodes --- libr/arch/p/evm/evm.c | 7 +++ libr/arch/p/evm/evm.h | 6 ++ libr/arch/p/evm/plugin.c | 118 +++++++++++++++++++++++++++++---------- shlr/Makefile | 3 +- 4 files changed, 102 insertions(+), 32 deletions(-) diff --git a/libr/arch/p/evm/evm.c b/libr/arch/p/evm/evm.c index 682acfedb4bc4..9e9c3208f2e3b 100644 --- a/libr/arch/p/evm/evm.c +++ b/libr/arch/p/evm/evm.c @@ -58,6 +58,9 @@ static const EvmOpDef opcodes[256] = { [EVM_OP_GASLIMIT] = { "gaslimit", 1 }, [EVM_OP_CHAINID] = { "chainid", 1 }, [EVM_OP_SELFBALANCE] = { "selfbalance", 1 }, + [EVM_OP_BASEFEE] = { "basefee", 1 }, + [EVM_OP_BLOBHASH] = { "blobhash", 1 }, + [EVM_OP_BLOBBASEFEE] = { "blobbasefee", 1 }, [EVM_OP_POP] = { "pop", 1 }, [EVM_OP_MLOAD] = { "mload", 1 }, [EVM_OP_MSTORE] = { "mstore", 1 }, @@ -70,6 +73,10 @@ static const EvmOpDef opcodes[256] = { [EVM_OP_MSIZE] = { "msize", 1 }, [EVM_OP_GAS] = { "gas", 1 }, [EVM_OP_JUMPDEST] = { "jumpdest", 1 }, + [EVM_OP_TLOAD] = { "tload", 1 }, + [EVM_OP_TSTORE] = { "tstore", 1 }, + [EVM_OP_MCOPY] = { "mcopy", 1 }, + [EVM_OP_PUSH0] = { "push0", 1 }, // .... [EVM_OP_CREATE] = { "create", 1 }, [EVM_OP_CALL] = { "call", 1 }, diff --git a/libr/arch/p/evm/evm.h b/libr/arch/p/evm/evm.h index 3b87ccc2edbf5..db6414cd3313e 100644 --- a/libr/arch/p/evm/evm.h +++ b/libr/arch/p/evm/evm.h @@ -58,6 +58,8 @@ typedef enum { EVM_OP_CHAINID, EVM_OP_SELFBALANCE, EVM_OP_BASEFEE, + EVM_OP_BLOBHASH, + EVM_OP_BLOBBASEFEE, EVM_OP_POP = 0x50, EVM_OP_MLOAD, @@ -71,7 +73,11 @@ typedef enum { EVM_OP_MSIZE, EVM_OP_GAS, EVM_OP_JUMPDEST, + EVM_OP_TLOAD, + EVM_OP_TSTORE, + EVM_OP_MCOPY, + EVM_OP_PUSH0 = 0x5f, EVM_OP_PUSH1 = 0x60, EVM_OP_PUSH2, EVM_OP_PUSH3, diff --git a/libr/arch/p/evm/plugin.c b/libr/arch/p/evm/plugin.c index bcbd82c7b7779..b4b5d874d67ac 100644 --- a/libr/arch/p/evm/plugin.c +++ b/libr/arch/p/evm/plugin.c @@ -128,6 +128,27 @@ static bool decode(RArchSession *s, RAnalOp *op, RAnalOpMask mask) { case EVM_INS_SMOD: op->type = R_ANAL_OP_TYPE_MOD; break; + case EVM_INS_SHL: + op->type = R_ANAL_OP_TYPE_SHL; + break; + case EVM_INS_SHR: + op->type = R_ANAL_OP_TYPE_SHR; + break; + case EVM_INS_SAR: + op->type = R_ANAL_OP_TYPE_SAR; + break; + case EVM_INS_ADD: + case EVM_INS_ADDMOD: + op->type = R_ANAL_OP_TYPE_ADD; + break; + case EVM_INS_LT: + case EVM_INS_GT: + case EVM_INS_SLT: + case EVM_INS_SGT: + case EVM_INS_EQ: + case EVM_INS_ISZERO: + op->type = R_ANAL_OP_TYPE_CMP; + break; case EVM_INS_JUMP: op->type = R_ANAL_OP_TYPE_JMP; op->fail = op->addr + 1; @@ -144,50 +165,38 @@ static bool decode(RArchSession *s, RAnalOp *op, RAnalOpMask mask) { break; case EVM_INS_MLOAD: case EVM_INS_SLOAD: + case EVM_INS_TLOAD: + case EVM_INS_MCOPY: op->type = R_ANAL_OP_TYPE_LOAD; break; case EVM_INS_MSTORE: case EVM_INS_MSTORE8: case EVM_INS_SSTORE: + case EVM_INS_TSTORE: op->type = R_ANAL_OP_TYPE_STORE; break; - case EVM_INS_LT: - case EVM_INS_GT: - case EVM_INS_SLT: - case EVM_INS_SGT: - case EVM_INS_EQ: - case EVM_INS_ISZERO: - op->type = R_ANAL_OP_TYPE_CMP; - break; - case EVM_INS_COINBASE: - case EVM_INS_BLOCKHASH: - break; case EVM_INS_SHA3: op->type = R_ANAL_OP_TYPE_CRYPTO; break; - case EVM_INS_CODECOPY: case EVM_INS_SWAP1: case EVM_INS_SWAP2: + case EVM_INS_SWAP3: + case EVM_INS_SWAP4: + case EVM_INS_SWAP5: + case EVM_INS_SWAP6: + case EVM_INS_SWAP7: + case EVM_INS_SWAP8: + case EVM_INS_SWAP9: + case EVM_INS_SWAP10: + case EVM_INS_SWAP11: case EVM_INS_SWAP12: op->type = R_ANAL_OP_TYPE_MOV; break; - case EVM_INS_GAS: - op->type = R_ANAL_OP_TYPE_MOV; - break; case EVM_INS_MUL: case EVM_INS_EXP: case EVM_INS_MULMOD: op->type = R_ANAL_OP_TYPE_MUL; break; - case EVM_INS_STOP: - case EVM_INS_SUICIDE: - op->type = R_ANAL_OP_TYPE_TRAP; - break; - case EVM_INS_DELEGATECALL: - case EVM_INS_CALLDATACOPY: - case EVM_INS_CALLDATALOAD: - op->type = R_ANAL_OP_TYPE_CALL; - break; case EVM_INS_DIV: case EVM_INS_SDIV: op->type = R_ANAL_OP_TYPE_DIV; @@ -204,6 +213,55 @@ static bool decode(RArchSession *s, RAnalOp *op, RAnalOpMask mask) { case EVM_INS_NOT: op->type = R_ANAL_OP_TYPE_NOT; break; + case EVM_INS_STOP: + case EVM_INS_SELFDESTRUCT: + op->type = R_ANAL_OP_TYPE_TRAP; + break; + case EVM_INS_CREATE: + case EVM_INS_CREATE2: + op->type = R_ANAL_OP_TYPE_NEW; + break; + case EVM_INS_CALL: + case EVM_INS_CALLCODE: + case EVM_INS_DELEGATECALL: + case EVM_INS_STATICCALL: + op->type = R_ANAL_OP_TYPE_CALL; + break; + case EVM_INS_CODESIZE: + case EVM_INS_CALLDATASIZE: + case EVM_INS_EXTCODESIZE: + case EVM_INS_RETURNDATASIZE: + op->type = R_ANAL_OP_TYPE_LENGTH; + break; + case EVM_INS_CODECOPY: + case EVM_INS_EXTCODECOPY: + case EVM_INS_RETURNDATACOPY: + case EVM_INS_CALLDATACOPY: + case EVM_INS_CALLDATALOAD: + case EVM_INS_ADDRESS: + case EVM_INS_BALANCE: + case EVM_INS_ORIGIN: + case EVM_INS_CALLER: + case EVM_INS_CALLVALUE: + case EVM_INS_GASPRICE: + case EVM_INS_BLOCKHASH: + case EVM_INS_COINBASE: + case EVM_INS_TIMESTAMP: + case EVM_INS_NUMBER: + case EVM_INS_DIFFICULTY: + case EVM_INS_GASLIMIT: + case EVM_INS_CHAINID: + case EVM_INS_SELFBALANCE: + case EVM_INS_BASEFEE: + case EVM_INS_BLOBHASH: + case EVM_INS_BLOBBASEFEE: + case EVM_INS_GAS: + op->type = R_ANAL_OP_TYPE_CRYPTO; + break; + case EVM_INS_PC: + case EVM_INS_MSIZE: + op->type = R_ANAL_OP_TYPE_RPUSH; + break; case EVM_INS_REVERT: case EVM_INS_RETURN: op->type = R_ANAL_OP_TYPE_RET; @@ -226,6 +284,11 @@ static bool decode(RArchSession *s, RAnalOp *op, RAnalOpMask mask) { case EVM_INS_DUP16: op->type = R_ANAL_OP_TYPE_PUSH; break; + case EVM_INS_PUSH0: + esilprintf (op, "0x0,sp,=[1],32,sp,+="); + op->type = R_ANAL_OP_TYPE_PUSH; + evm_add_push_to_db (s, op, addr, buf, len); + break; case EVM_INS_PUSH1: esilprintf (op, "0x%s,sp,=[1],32,sp,+=", insn->op_str); op->type = R_ANAL_OP_TYPE_PUSH; @@ -291,16 +354,9 @@ static bool decode(RArchSession *s, RAnalOp *op, RAnalOpMask mask) { op->type = R_ANAL_OP_TYPE_PUSH; opsize = op->size = 33; break; - case EVM_INS_ADD: - case EVM_INS_ADDMOD: - op->type = R_ANAL_OP_TYPE_ADD; - break; case EVM_INS_POP: op->type = R_ANAL_OP_TYPE_POP; break; - case EVM_INS_CODESIZE: - op->type = R_ANAL_OP_TYPE_LENGTH; - break; case EVM_INS_LOG0: case EVM_INS_LOG1: case EVM_INS_LOG2: diff --git a/shlr/Makefile b/shlr/Makefile index 321cce02fcd73..75c5fde41ae5a 100644 --- a/shlr/Makefile +++ b/shlr/Makefile @@ -43,7 +43,8 @@ CS_TAR=https://codeload.github.com/capstone-engine/capstone/tar.gz/$(CS_VER) CS_PATCHES=0 else CS_TAR= -CS_URL_BASE=github.com/capstone-engine/capstone +# CS_URL_BASE=github.com/capstone-engine/capstone +CS_URL_BASE=github.com/andelf/capstone CS_URL=$(GIT_PREFIX)$(CS_URL_BASE).git CS_ARCHIVE=https://$(CS_URL_BASE)/archive CS_UPD=20201203