Skip to content

Commit

Permalink
evm: apply new opcode updates and add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
andelf committed Jan 13, 2025
1 parent b102f1b commit bb63a03
Show file tree
Hide file tree
Showing 8 changed files with 308 additions and 56 deletions.
38 changes: 23 additions & 15 deletions arch/EVM/EVMDisassembler.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* Capstone Disassembly Engine */
/* By Nguyen Anh Quynh, 2018 */
/* By Andelf, 2025 */

#include <string.h>
#include <stddef.h> // offsetof macro
Expand Down Expand Up @@ -36,9 +37,9 @@ static const short opcodes[256] = {
EVM_INS_XOR,
EVM_INS_NOT,
EVM_INS_BYTE,
-1,
-1,
-1,
EVM_INS_SHL,
EVM_INS_SHR,
EVM_INS_SAR,
-1,
-1,
EVM_INS_SHA3,
Expand Down Expand Up @@ -79,11 +80,11 @@ static const short opcodes[256] = {
EVM_INS_NUMBER,
EVM_INS_DIFFICULTY,
EVM_INS_GASLIMIT,
-1,
-1,
-1,
-1,
-1,
EVM_INS_CHAINID,
EVM_INS_SELFBALANCE,
EVM_INS_BASEFEE,
EVM_INS_BLOBHASH,
EVM_INS_BLOBBASEFEE,
-1,
-1,
-1,
Expand All @@ -101,10 +102,10 @@ static const short opcodes[256] = {
EVM_INS_MSIZE,
EVM_INS_GAS,
EVM_INS_JUMPDEST,
-1,
-1,
-1,
-1,
EVM_INS_TLOAD,
EVM_INS_TSTORE,
EVM_INS_MCOPY,
EVM_INS_PUSH0,
EVM_INS_PUSH1,
EVM_INS_PUSH2,
EVM_INS_PUSH3,
Expand Down Expand Up @@ -254,7 +255,7 @@ static const short opcodes[256] = {
EVM_INS_CALLCODE,
EVM_INS_RETURN,
EVM_INS_DELEGATECALL,
EVM_INS_CALLBLACKBOX,
EVM_INS_CREATE2,
-1,
-1,
-1,
Expand All @@ -264,7 +265,7 @@ static const short opcodes[256] = {
-1,
EVM_INS_REVERT,
-1,
EVM_INS_SUICIDE,
EVM_INS_SELFDESTRUCT,
};

bool EVM_getInstruction(csh ud, const uint8_t *code, size_t code_len,
Expand Down Expand Up @@ -326,6 +327,9 @@ bool EVM_getInstruction(csh ud, const uint8_t *code, size_t code_len,
case EVM_INS_MULMOD:
case EVM_INS_EXP:
case EVM_INS_SIGNEXTEND:
case EVM_INS_SHL:
case EVM_INS_SHR:
case EVM_INS_SAR:
MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = EVM_GRP_MATH;
MI->flat_insn->detail->groups_count++;
break;
Expand All @@ -335,6 +339,7 @@ bool EVM_getInstruction(csh ud, const uint8_t *code, size_t code_len,
case EVM_INS_CALLDATACOPY:
case EVM_INS_CODECOPY:
case EVM_INS_EXTCODECOPY:
case EVM_INS_MCOPY:
MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = EVM_GRP_MEM_WRITE;
MI->flat_insn->detail->groups_count++;
break;
Expand All @@ -346,16 +351,19 @@ bool EVM_getInstruction(csh ud, const uint8_t *code, size_t code_len,
case EVM_INS_RETURN:
case EVM_INS_DELEGATECALL:
case EVM_INS_REVERT:
case EVM_INS_CREATE2:
MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = EVM_GRP_MEM_READ;
MI->flat_insn->detail->groups_count++;
break;

case EVM_INS_SSTORE:
case EVM_INS_TSTORE:
MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = EVM_GRP_STORE_WRITE;
MI->flat_insn->detail->groups_count++;
break;

case EVM_INS_SLOAD:
case EVM_INS_TLOAD:
MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = EVM_GRP_STORE_READ;
MI->flat_insn->detail->groups_count++;
break;
Expand All @@ -367,7 +375,7 @@ bool EVM_getInstruction(csh ud, const uint8_t *code, size_t code_len,
break;

case EVM_INS_STOP:
case EVM_INS_SUICIDE:
case EVM_INS_SELFDESTRUCT:
MI->flat_insn->detail->groups[MI->flat_insn->detail->groups_count] = EVM_GRP_HALT;
MI->flat_insn->detail->groups_count++;
break;
Expand Down
29 changes: 15 additions & 14 deletions arch/EVM/EVMMapping.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* Capstone Disassembly Engine */
/* By Nguyen Anh Quynh, 2018 */
/* By Andelf, 2025 */

#ifdef CAPSTONE_HAS_EVM

Expand Down Expand Up @@ -73,9 +74,9 @@ static const name_map insn_name_maps[256] = {
{ EVM_INS_XOR, "xor" },
{ EVM_INS_NOT, "not" },
{ EVM_INS_BYTE, "byte" },
{ EVM_INS_INVALID, NULL },
{ EVM_INS_INVALID, NULL },
{ EVM_INS_INVALID, NULL },
{ EVM_INS_SHL, "shl" },
{ EVM_INS_SHR, "shr" },
{ EVM_INS_SAR, "sar" },
{ EVM_INS_INVALID, NULL },
{ EVM_INS_INVALID, NULL },
{ EVM_INS_SHA3, "sha3" },
Expand Down Expand Up @@ -116,11 +117,11 @@ static const name_map insn_name_maps[256] = {
{ EVM_INS_NUMBER, "number" },
{ EVM_INS_DIFFICULTY, "difficulty" },
{ EVM_INS_GASLIMIT, "gaslimit" },
{ EVM_INS_INVALID, NULL },
{ EVM_INS_INVALID, NULL },
{ EVM_INS_INVALID, NULL },
{ EVM_INS_INVALID, NULL },
{ EVM_INS_INVALID, NULL },
{ EVM_INS_CHAINID, "chainid" },
{ EVM_INS_SELFBALANCE, "selfbalance" },
{ EVM_INS_BASEFEE, "basefee" },
{ EVM_INS_BLOBHASH, "blobhash" },
{ EVM_INS_BLOBBASEFEE, "blobbasefee" },
{ EVM_INS_INVALID, NULL },
{ EVM_INS_INVALID, NULL },
{ EVM_INS_INVALID, NULL },
Expand All @@ -138,10 +139,10 @@ static const name_map insn_name_maps[256] = {
{ EVM_INS_MSIZE, "msize" },
{ EVM_INS_GAS, "gas" },
{ EVM_INS_JUMPDEST, "jumpdest" },
{ EVM_INS_INVALID, NULL },
{ EVM_INS_INVALID, NULL },
{ EVM_INS_INVALID, NULL },
{ EVM_INS_INVALID, NULL },
{ EVM_INS_TLOAD, "tload" },
{ EVM_INS_TSTORE, "tstore" },
{ EVM_INS_MCOPY, "mcopy" },
{ EVM_INS_PUSH0, "push0" },
{ EVM_INS_PUSH1, "push1" },
{ EVM_INS_PUSH2, "push2" },
{ EVM_INS_PUSH3, "push3" },
Expand Down Expand Up @@ -291,7 +292,7 @@ static const name_map insn_name_maps[256] = {
{ EVM_INS_CALLCODE, "callcode" },
{ EVM_INS_RETURN, "return" },
{ EVM_INS_DELEGATECALL, "delegatecall" },
{ EVM_INS_CALLBLACKBOX, "callblackbox" },
{ EVM_INS_CREATE2, "create2" },
{ EVM_INS_INVALID, NULL },
{ EVM_INS_INVALID, NULL },
{ EVM_INS_INVALID, NULL },
Expand All @@ -301,7 +302,7 @@ static const name_map insn_name_maps[256] = {
{ EVM_INS_INVALID, NULL },
{ EVM_INS_REVERT, "revert" },
{ EVM_INS_INVALID, NULL },
{ EVM_INS_SUICIDE, "suicide" },
{ EVM_INS_SELFDESTRUCT, "selfdestruct" },
};
#endif

Expand Down
37 changes: 19 additions & 18 deletions arch/EVM/EVMMappingInsn.inc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* Capstone Disassembly Engine */
/* By Nguyen Anh Quynh, 2018 */
/* By Andelf, 2025 */

{ 0, 0, 0 }, // STOP
{ 2, 1, 3 }, // ADD
Expand Down Expand Up @@ -28,9 +29,9 @@
{ 2, 1, 3 }, // XOR
{ 1, 1, 3 }, // NOT
{ 2, 1, 3 }, // BYTE
{ 0, 0, 0xffffffff }, // unused
{ 0, 0, 0xffffffff }, // unused
{ 0, 0, 0xffffffff }, // unused
{ 2, 1, 3 }, // SHL
{ 2, 1, 3 }, // SHR
{ 2, 1, 3 }, // SAR
{ 0, 0, 0xffffffff }, // unused
{ 0, 0, 0xffffffff }, // unused
{ 2, 1, 30 }, // SHA3
Expand Down Expand Up @@ -71,11 +72,11 @@
{ 0, 1, 2 }, // NUMBER
{ 0, 1, 2 }, // DIFFICULTY
{ 0, 1, 2 }, // GASLIMIT
{ 0, 0, 0xffffffff }, // unused
{ 0, 0, 0xffffffff }, // unused
{ 0, 0, 0xffffffff }, // unused
{ 0, 0, 0xffffffff }, // unused
{ 0, 0, 0xffffffff }, // unused
{ 0, 1, 2 }, // CHAINID
{ 0, 1, 5 }, // SELFBALANCE
{ 0, 1, 2 }, // BASEFEE
{ 0, 1, 3 }, // BLOBHASH
{ 0, 1, 2 }, // BLOBBASEFEE
{ 0, 0, 0xffffffff }, // unused
{ 0, 0, 0xffffffff }, // unused
{ 0, 0, 0xffffffff }, // unused
Expand All @@ -93,10 +94,10 @@
{ 0, 1, 2 }, // MSIZE
{ 0, 1, 2 }, // GAS
{ 0, 0, 1 }, // JUMPDEST
{ 0, 0, 0xffffffff }, // unused
{ 0, 0, 0xffffffff }, // unused
{ 0, 0, 0xffffffff }, // unused
{ 0, 0, 0xffffffff }, // unused
{ 1, 1, 100 }, // TLOAD
{ 2, 0, 100 }, // TSTORE
{ 3, 0, 3 }, // MCOPY
{ 0, 1, 3 }, // PUSH0
{ 0, 1, 3 }, // PUSH1
{ 0, 1, 3 }, // PUSH2
{ 0, 1, 3 }, // PUSH3
Expand Down Expand Up @@ -242,18 +243,18 @@
{ 0, 0, 0xffffffff }, // unused
{ 0, 0, 0xffffffff }, // unused
{ 3, 1, 32000 }, // CREATE
{ 7, 1, 40 }, // CALL
{ 7, 1, 40 }, // CALLCODE
{ 7, 1, 100 }, // CALL
{ 7, 1, 100 }, // CALLCODE
{ 2, 0, 0 }, // RETURN
{ 6, 1, 40 }, // DELEGATECALL
{ 7, 1, 40 }, // CALLBLACKBOX
{ 6, 1, 100 }, // DELEGATECALL
{ 4, 1, 32000 }, // CREATE2
{ 0, 0, 0xffffffff }, // unused
{ 0, 0, 0xffffffff }, // unused
{ 0, 0, 0xffffffff }, // unused
{ 0, 0, 0xffffffff }, // unused
{ 6, 1, 40 }, // STATICCALL
{ 6, 1, 100 }, // STATICCALL
{ 0, 0, 0xffffffff }, // unused
{ 0, 0, 0xffffffff }, // unused
{ 2, 0, 0 }, // REVERT
{ 0, 0, 0xffffffff }, // unused
{ 1, 0, 0 }, // SUICIDE
{ 1, 0, 5000 }, // SELFDESTRUCT
16 changes: 14 additions & 2 deletions bindings/java/capstone/Evm_const.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public class Evm_const {
public static final int EVM_INS_XOR = 24;
public static final int EVM_INS_NOT = 25;
public static final int EVM_INS_BYTE = 26;
public static final int EVM_INS_SHL = 27;
public static final int EVM_INS_SHR = 28;
public static final int EVM_INS_SAR = 29;
public static final int EVM_INS_SHA3 = 32;
public static final int EVM_INS_ADDRESS = 48;
public static final int EVM_INS_BALANCE = 49;
Expand All @@ -48,6 +51,11 @@ public class Evm_const {
public static final int EVM_INS_NUMBER = 67;
public static final int EVM_INS_DIFFICULTY = 68;
public static final int EVM_INS_GASLIMIT = 69;
public static final int EVM_INS_CHAINID = 70;
public static final int EVM_INS_SELFBALANCE = 71;
public static final int EVM_INS_BASEFEE = 72;
public static final int EVM_INS_BLOBHASH = 73;
public static final int EVM_INS_BLOBBASEFEE = 74;
public static final int EVM_INS_POP = 80;
public static final int EVM_INS_MLOAD = 81;
public static final int EVM_INS_MSTORE = 82;
Expand All @@ -60,6 +68,10 @@ public class Evm_const {
public static final int EVM_INS_MSIZE = 89;
public static final int EVM_INS_GAS = 90;
public static final int EVM_INS_JUMPDEST = 91;
public static final int EVM_INS_TLOAD = 92;
public static final int EVM_INS_TSTORE = 93;
public static final int EVM_INS_MCOPY = 94;
public static final int EVM_INS_PUSH0 = 95;
public static final int EVM_INS_PUSH1 = 96;
public static final int EVM_INS_PUSH2 = 97;
public static final int EVM_INS_PUSH3 = 98;
Expand Down Expand Up @@ -134,10 +146,10 @@ public class Evm_const {
public static final int EVM_INS_CALLCODE = 242;
public static final int EVM_INS_RETURN = 243;
public static final int EVM_INS_DELEGATECALL = 244;
public static final int EVM_INS_CALLBLACKBOX = 245;
public static final int EVM_INS_CREATE2 = 245;
public static final int EVM_INS_STATICCALL = 250;
public static final int EVM_INS_REVERT = 253;
public static final int EVM_INS_SUICIDE = 255;
public static final int EVM_INS_SELFDESTRUCT = 255;
public static final int EVM_INS_INVALID = 512;
public static final int EVM_INS_ENDING = 513;

Expand Down
16 changes: 14 additions & 2 deletions bindings/ocaml/evm_const.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ let _EVM_INS_OR = 23;;
let _EVM_INS_XOR = 24;;
let _EVM_INS_NOT = 25;;
let _EVM_INS_BYTE = 26;;
let _EVM_INS_SHL = 27;;
let _EVM_INS_SHR = 28;;
let _EVM_INS_SAR = 29;;
let _EVM_INS_SHA3 = 32;;
let _EVM_INS_ADDRESS = 48;;
let _EVM_INS_BALANCE = 49;;
Expand All @@ -45,6 +48,11 @@ let _EVM_INS_TIMESTAMP = 66;;
let _EVM_INS_NUMBER = 67;;
let _EVM_INS_DIFFICULTY = 68;;
let _EVM_INS_GASLIMIT = 69;;
let _EVM_INS_CHAINID = 70;;
let _EVM_INS_SELFBALANCE = 71;;
let _EVM_INS_BASEFEE = 72;;
let _EVM_INS_BLOBHASH = 73;;
let _EVM_INS_BLOBBASEFEE = 74;;
let _EVM_INS_POP = 80;;
let _EVM_INS_MLOAD = 81;;
let _EVM_INS_MSTORE = 82;;
Expand All @@ -57,6 +65,10 @@ let _EVM_INS_PC = 88;;
let _EVM_INS_MSIZE = 89;;
let _EVM_INS_GAS = 90;;
let _EVM_INS_JUMPDEST = 91;;
let _EVM_INS_TLOAD = 92;;
let _EVM_INS_TSTORE = 93;;
let _EVM_INS_MCOPY = 94;;
let _EVM_INS_PUSH0 = 95;;
let _EVM_INS_PUSH1 = 96;;
let _EVM_INS_PUSH2 = 97;;
let _EVM_INS_PUSH3 = 98;;
Expand Down Expand Up @@ -131,10 +143,10 @@ let _EVM_INS_CALL = 241;;
let _EVM_INS_CALLCODE = 242;;
let _EVM_INS_RETURN = 243;;
let _EVM_INS_DELEGATECALL = 244;;
let _EVM_INS_CALLBLACKBOX = 245;;
let _EVM_INS_CREATE2 = 245;;
let _EVM_INS_STATICCALL = 250;;
let _EVM_INS_REVERT = 253;;
let _EVM_INS_SUICIDE = 255;;
let _EVM_INS_SELFDESTRUCT = 255;;
let _EVM_INS_INVALID = 512;;
let _EVM_INS_ENDING = 513;;

Expand Down
Loading

0 comments on commit bb63a03

Please sign in to comment.