From 38525a206c5eb6bbd093496dca322c1b49b35bd3 Mon Sep 17 00:00:00 2001 From: Robin Salen Date: Fri, 10 Nov 2023 11:39:17 -0500 Subject: [PATCH 1/3] Use table for JUMPDEST --- .../cpu/kernel/asm/core/jumpdest_analysis.asm | 56 ++++++++++++++----- 1 file changed, 43 insertions(+), 13 deletions(-) diff --git a/evm/src/cpu/kernel/asm/core/jumpdest_analysis.asm b/evm/src/cpu/kernel/asm/core/jumpdest_analysis.asm index a9d8adf2ff..f6c862e676 100644 --- a/evm/src/cpu/kernel/asm/core/jumpdest_analysis.asm +++ b/evm/src/cpu/kernel/asm/core/jumpdest_analysis.asm @@ -23,7 +23,8 @@ loop: %jumpi(encountered_jumpdest) // stack: opcode, i, ctx, code_len, retdest - %code_bytes_to_skip + %add_const(code_bytes_to_skip) + %mload_kernel_code // stack: bytes_to_skip, i, ctx, code_len, retdest ADD %jump(continue) @@ -50,15 +51,44 @@ return: // // Note that the range of PUSH opcodes is [0x60, 0x80). I.e. PUSH1 is 0x60 // and PUSH32 is 0x7f. -%macro code_bytes_to_skip - // stack: opcode - %sub_const(0x60) - // stack: opcode - 0x60 - DUP1 %lt_const(0x20) - // stack: is_push_opcode, opcode - 0x60 - SWAP1 - %increment // n = opcode - 0x60 + 1 - // stack: n, is_push_opcode - MUL - // stack: bytes_to_skip -%endmacro +code_bytes_to_skip: + %rep 96 + BYTES 0 // 0x00-0x5f + %endrep + + BYTES 1 + BYTES 2 + BYTES 3 + BYTES 4 + BYTES 5 + BYTES 6 + BYTES 7 + BYTES 8 + BYTES 9 + BYTES 10 + BYTES 11 + BYTES 12 + BYTES 13 + BYTES 14 + BYTES 15 + BYTES 16 + BYTES 17 + BYTES 18 + BYTES 19 + BYTES 20 + BYTES 21 + BYTES 22 + BYTES 23 + BYTES 24 + BYTES 25 + BYTES 26 + BYTES 27 + BYTES 28 + BYTES 29 + BYTES 30 + BYTES 31 + BYTES 32 + + %rep 128 + BYTES 0 // 0x80-0xff + %endrep From 21b5a481df8d41cf0209c0b2fa0bb1e39325b336 Mon Sep 17 00:00:00 2001 From: Hamy Ratoanina Date: Fri, 10 Nov 2023 11:56:41 -0500 Subject: [PATCH 2/3] Refactor jumpdest loop --- .../cpu/kernel/asm/core/jumpdest_analysis.asm | 31 +++++++++---------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/evm/src/cpu/kernel/asm/core/jumpdest_analysis.asm b/evm/src/cpu/kernel/asm/core/jumpdest_analysis.asm index f6c862e676..68a381b724 100644 --- a/evm/src/cpu/kernel/asm/core/jumpdest_analysis.asm +++ b/evm/src/cpu/kernel/asm/core/jumpdest_analysis.asm @@ -18,27 +18,24 @@ loop: MLOAD_GENERAL // stack: opcode, i, ctx, code_len, retdest - DUP1 %eq_const(0x5b) - // stack: opcode == JUMPDEST, opcode, i, ctx, code_len, retdest - %jumpi(encountered_jumpdest) + DUP1 + // Slightly more efficient than `%eq_const(0x5b) ISZERO` + PUSH 0x5b + SUB + // stack: opcode != JUMPDEST, opcode, i, ctx, code_len, retdest + %jumpi(continue) + // stack: JUMPDEST, i, ctx, code_len, retdest + %stack (JUMPDEST, i, ctx) -> (ctx, @SEGMENT_JUMPDEST_BITS, i, 1, JUMPDEST, i, ctx) + MSTORE_GENERAL + +continue: // stack: opcode, i, ctx, code_len, retdest %add_const(code_bytes_to_skip) %mload_kernel_code // stack: bytes_to_skip, i, ctx, code_len, retdest ADD - %jump(continue) - -encountered_jumpdest: - // stack: opcode, i, ctx, code_len, retdest - POP - // stack: i, ctx, code_len, retdest - %stack (i, ctx) -> (ctx, @SEGMENT_JUMPDEST_BITS, i, 1, i, ctx) - MSTORE_GENERAL - -continue: // stack: i, ctx, code_len, retdest - %increment %jump(loop) return: @@ -53,10 +50,9 @@ return: // and PUSH32 is 0x7f. code_bytes_to_skip: %rep 96 - BYTES 0 // 0x00-0x5f + BYTES 1 // 0x00-0x5f %endrep - BYTES 1 BYTES 2 BYTES 3 BYTES 4 @@ -88,7 +84,8 @@ code_bytes_to_skip: BYTES 30 BYTES 31 BYTES 32 + BYTES 33 %rep 128 - BYTES 0 // 0x80-0xff + BYTES 1 // 0x80-0xff %endrep From 3c9c9b4d99308f353b7fe08562b952f0800438c6 Mon Sep 17 00:00:00 2001 From: Hamy Ratoanina Date: Fri, 10 Nov 2023 12:31:31 -0500 Subject: [PATCH 3/3] Update comment --- evm/src/cpu/kernel/asm/core/jumpdest_analysis.asm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/evm/src/cpu/kernel/asm/core/jumpdest_analysis.asm b/evm/src/cpu/kernel/asm/core/jumpdest_analysis.asm index 68a381b724..a99168058e 100644 --- a/evm/src/cpu/kernel/asm/core/jumpdest_analysis.asm +++ b/evm/src/cpu/kernel/asm/core/jumpdest_analysis.asm @@ -43,8 +43,8 @@ return: %pop3 JUMP -// Determines how many bytes to skip, if any, based on the opcode we read. -// If we read a PUSH opcode, we skip over n bytes, otherwise we skip 0. +// Determines how many bytes away is the next opcode, based on the opcode we read. +// If we read a PUSH opcode, next opcode is in n + 1 bytes, otherwise it's the next one. // // Note that the range of PUSH opcodes is [0x60, 0x80). I.e. PUSH1 is 0x60 // and PUSH32 is 0x7f.