Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for new EVM opcodes #23878

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions libr/arch/p/bpf_cs/plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
#define CSINC BPF
#define CSINC_MODE get_capstone_mode(as)

// See-also: https://github.com/capstone-engine/capstone/commit/812e654c857348bf95ae4ab2e7db0ccf779a4cb8
#if CS_API_MAJOR < 6
#define BPF_INS_JA BPF_INS_JMP
#endif

static int get_capstone_mode(RArchSession *as) {
int mode = R_ARCH_CONFIG_IS_BIG_ENDIAN (as->config)
? CS_MODE_BIG_ENDIAN: CS_MODE_LITTLE_ENDIAN;
Expand Down Expand Up @@ -62,7 +67,7 @@ static bool decode(RArchSession *a, RAnalOp *op, RArchDecodeMask mask) {
}
if (insn->detail) {
switch (insn->id) {
case BPF_INS_JMP:
case BPF_INS_JA:
op->type = R_ANAL_OP_TYPE_JMP;
op->jump = JUMP (0);
break;
Expand Down Expand Up @@ -332,7 +337,7 @@ void bpf_jump(RArchSession *a, RAnalOp *op, cs_insn *insn, char *condition) {

static void analop_esil(RArchSession *a, RAnalOp *op, cs_insn *insn, ut64 addr) {
switch (insn->id) {
case BPF_INS_JMP:
case BPF_INS_JA:
esilprintf (op, "0x%" PFMT64x ",pc,=", op->jump);
break;
case BPF_INS_JEQ:
Expand Down
3 changes: 3 additions & 0 deletions libr/arch/p/evm/evm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down
10 changes: 6 additions & 4 deletions libr/arch/p/evm/evm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -71,12 +73,12 @@ typedef enum {
EVM_OP_MSIZE,
EVM_OP_GAS,
EVM_OP_JUMPDEST,

EVM_OP_TLOAD = 0x5c,
EVM_OP_TLOAD,
EVM_OP_TSTORE,
EVM_OP_MCOPY,
EVM_OP_PUSH0,
EVM_OP_PUSH1,

EVM_OP_PUSH0 = 0x5f,
EVM_OP_PUSH1 = 0x60,
EVM_OP_PUSH2,
EVM_OP_PUSH3,
EVM_OP_PUSH4,
Expand Down
22 changes: 22 additions & 0 deletions libr/arch/p/evm/plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,15 @@ static bool decode(RArchSession *s, RAnalOp *op, RAnalOpMask mask) {
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;
Expand All @@ -180,9 +189,15 @@ static bool decode(RArchSession *s, RAnalOp *op, RAnalOpMask mask) {
op->type = R_ANAL_OP_TYPE_MUL;
break;
case EVM_INS_STOP:
#if CS_API_MAJOR >= 6
case EVM_INS_SELFDESTRUCT:
op->type = R_ANAL_OP_TYPE_TRAP;
break;
#else
case EVM_INS_SUICIDE:
op->type = R_ANAL_OP_TYPE_TRAP;
break;
#endif
case EVM_INS_DELEGATECALL:
case EVM_INS_CALLDATACOPY:
case EVM_INS_CALLDATALOAD:
Expand Down Expand Up @@ -226,6 +241,13 @@ static bool decode(RArchSession *s, RAnalOp *op, RAnalOpMask mask) {
case EVM_INS_DUP16:
op->type = R_ANAL_OP_TYPE_PUSH;
break;
#if CS_API_MAJOR >= 6
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;
#endif
case EVM_INS_PUSH1:
esilprintf (op, "0x%s,sp,=[1],32,sp,+=", insn->op_str);
op->type = R_ANAL_OP_TYPE_PUSH;
Expand Down
4 changes: 3 additions & 1 deletion libr/asm/d/evm.sdb.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ gaslimit=gas limit of current block
chainid=push current chain id onto stack
selfbalance=balance of executing contract, in wei
basefee=base fee of current block
blobhash=value of the base fee
blobbasefee= value of blob base-fee
pop=remove item from top of stack and discard it
mload=read word from memory at offset ost
mstore=write a word to memory
Expand All @@ -65,7 +67,7 @@ jumpdest=mark valid jump destination
tload=load word from transient storage
tstore=save word to transient storage
mcopy=copy memory areas
push0=push value 0 onto stack
push0=push constant value 0 onto stack
push1=push 1-byte value onto stack
push2=push 2-byte value onto stack
push3=push 3-byte value onto stack
Expand Down
2 changes: 1 addition & 1 deletion shlr/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ CS_ARCHIVE=https://$(CS_URL_BASE)/archive
CS_UPD=20201203
# NOTE: when you update CS_TIP or CS_BRA, also update them in shlr/meson.build
ifeq ($(USE_CSNEXT),1)
CS_TIP=0a29bf80017b8422c79ba51e3ad5c34ba5ee6142
CS_TIP=8ac2843b9bc91e29c0287d27f9c49cff2ad44776
CS_BRA=next
else
ifeq ($(USE_CS4),1)
Expand Down
2 changes: 1 addition & 1 deletion shlr/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ else
patches_files = []
# NOTE: when you update CS_TIP or CS_BRA, also update them in shlr/Makefile
if capstone_version == 'next'
CS_TIP = '0a29bf80017b8422c79ba51e3ad5c34ba5ee6142'
CS_TIP = '8ac2843b9bc91e29c0287d27f9c49cff2ad44776'
CS_BRA = 'next'
patches_files = [
'fix-x86-16.patch',
Expand Down
Loading