From ba61d15cf91df3b8ff8f15b7f273e6e5516e831b Mon Sep 17 00:00:00 2001 From: Robin Salen Date: Sat, 21 Oct 2023 16:36:41 -0400 Subject: [PATCH 1/3] Add macro for copying sequences of bytes --- evm/src/cpu/kernel/asm/memory/memcpy.asm | 71 +++++++++++++++++++ .../asm/mpt/hash/hash_trie_specific.asm | 2 +- .../cpu/kernel/asm/transactions/router.asm | 2 +- 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/evm/src/cpu/kernel/asm/memory/memcpy.asm b/evm/src/cpu/kernel/asm/memory/memcpy.asm index fafec70465..cc872b1088 100644 --- a/evm/src/cpu/kernel/asm/memory/memcpy.asm +++ b/evm/src/cpu/kernel/asm/memory/memcpy.asm @@ -53,3 +53,74 @@ memcpy_finish: %jump(memcpy) %%after: %endmacro + +// Similar logic than memcpy, but optimized for copying sequences of bytes. +global memcpy_bytes: + // stack: DST, SRC, count, retdest + DUP7 + // stack: count, DST, SRC, count, retdest + %lt_const(0x20) + // stack: count < 32, DST, SRC, count, retdest + %jumpi(memcpy_bytes_finish) + + // We will pack 32 bytes into a U256 from the source, and then unpack it at the destination. + // Copy the next chunk of bytes. + PUSH 32 + DUP1 + DUP8 + DUP8 + DUP8 + // stack: SRC, 32, 32, DST, SRC, count, retdest + MLOAD_32BYTES + // stack: value, 32, DST, SRC, count, retdest + DUP5 + DUP5 + DUP5 + // stack: DST, value, 32, DST, SRC, count, retdest + MSTORE_32BYTES + // stack: DST, SRC, count, retdest + + // Increment dst_addr by 32. + SWAP2 + %add_const(0x20) + SWAP2 + // Increment src_addr by 32. + SWAP5 + %add_const(0x20) + SWAP5 + // Decrement count by 32. + SWAP6 + %sub_const(0x20) + SWAP6 + + // Continue the loop. + %jump(memcpy_bytes) + +memcpy_bytes_finish: + // stack: DST, SRC, count, retdest + + // Copy the last chunk of `count` bytes. + DUP7 + DUP1 + DUP8 + DUP8 + DUP8 + // stack: SRC, count, count, DST, SRC, count, retdest + MLOAD_32BYTES + // stack: value, count, DST, SRC, count, retdest + DUP5 + DUP5 + DUP5 + // stack: DST, value, count, DST, SRC, count, retdest + MSTORE_32BYTES + // stack: DST, SRC, count, retdest + + %pop7 + // stack: retdest + JUMP + +%macro memcpy_bytes + %stack (dst: 3, src: 3, count) -> (dst, src, count, %%after) + %jump(memcpy_bytes) +%%after: +%endmacro \ No newline at end of file diff --git a/evm/src/cpu/kernel/asm/mpt/hash/hash_trie_specific.asm b/evm/src/cpu/kernel/asm/mpt/hash/hash_trie_specific.asm index 8935b7f3f9..767927fbc6 100644 --- a/evm/src/cpu/kernel/asm/mpt/hash/hash_trie_specific.asm +++ b/evm/src/cpu/kernel/asm/mpt/hash/hash_trie_specific.asm @@ -116,7 +116,7 @@ global encode_txn: 0, @SEGMENT_TRIE_DATA, txn_rlp_ptr, // src addr. Kernel has context 0 txn_rlp_len, // mcpy len txn_rlp_len, rlp_pos) - %memcpy + %memcpy_bytes ADD // stack new_rlp_pos, retdest SWAP1 diff --git a/evm/src/cpu/kernel/asm/transactions/router.asm b/evm/src/cpu/kernel/asm/transactions/router.asm index c01216fb11..109334dd0d 100644 --- a/evm/src/cpu/kernel/asm/transactions/router.asm +++ b/evm/src/cpu/kernel/asm/transactions/router.asm @@ -57,7 +57,7 @@ global update_txn_trie: 0, @SEGMENT_RLP_RAW, 0, // src addr. Kernel has context 0 txn_rlp_len, // mcpy len txn_rlp_len, rlp_start, txn_counter, num_nibbles, value_ptr) - %memcpy + %memcpy_bytes ADD %set_trie_data_size // stack: txn_counter, num_nibbles, value_ptr, retdest From 503a31b6f06452bdd372fec7fd8c79d4cbeb6d6a Mon Sep 17 00:00:00 2001 From: Robin Salen Date: Mon, 23 Oct 2023 07:16:39 -0400 Subject: [PATCH 2/3] Reviews --- evm/src/cpu/kernel/asm/memory/memcpy.asm | 4 ++-- evm/src/cpu/kernel/asm/memory/syscalls.asm | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/evm/src/cpu/kernel/asm/memory/memcpy.asm b/evm/src/cpu/kernel/asm/memory/memcpy.asm index cc872b1088..108b7c6f18 100644 --- a/evm/src/cpu/kernel/asm/memory/memcpy.asm +++ b/evm/src/cpu/kernel/asm/memory/memcpy.asm @@ -54,7 +54,7 @@ memcpy_finish: %%after: %endmacro -// Similar logic than memcpy, but optimized for copying sequences of bytes. +// Similar logic to memcpy, but optimized for copying sequences of bytes. global memcpy_bytes: // stack: DST, SRC, count, retdest DUP7 @@ -123,4 +123,4 @@ memcpy_bytes_finish: %stack (dst: 3, src: 3, count) -> (dst, src, count, %%after) %jump(memcpy_bytes) %%after: -%endmacro \ No newline at end of file +%endmacro diff --git a/evm/src/cpu/kernel/asm/memory/syscalls.asm b/evm/src/cpu/kernel/asm/memory/syscalls.asm index 3548930c36..1eb3a80498 100644 --- a/evm/src/cpu/kernel/asm/memory/syscalls.asm +++ b/evm/src/cpu/kernel/asm/memory/syscalls.asm @@ -95,7 +95,7 @@ calldataload_large_offset: GET_CONTEXT %stack (context, kexit_info, dest_offset, offset, size) -> (context, @SEGMENT_MAIN_MEMORY, dest_offset, context, $segment, offset, size, wcopy_after, kexit_info) - %jump(memcpy) + %jump(memcpy_bytes) %endmacro wcopy_empty: @@ -152,7 +152,7 @@ global sys_returndatacopy: GET_CONTEXT %stack (context, kexit_info, dest_offset, offset, size) -> (context, @SEGMENT_MAIN_MEMORY, dest_offset, context, @SEGMENT_RETURNDATA, offset, size, wcopy_after, kexit_info) - %jump(memcpy) + %jump(memcpy_bytes) returndatacopy_empty: %stack (kexit_info, dest_offset, offset, size) -> (kexit_info) From 4140eb54f4b91212e9e259bf5d63ad2165ba2181 Mon Sep 17 00:00:00 2001 From: Robin Salen Date: Mon, 23 Oct 2023 07:20:52 -0400 Subject: [PATCH 3/3] Fix --- evm/src/cpu/kernel/asm/memory/syscalls.asm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/evm/src/cpu/kernel/asm/memory/syscalls.asm b/evm/src/cpu/kernel/asm/memory/syscalls.asm index 1eb3a80498..f23d261105 100644 --- a/evm/src/cpu/kernel/asm/memory/syscalls.asm +++ b/evm/src/cpu/kernel/asm/memory/syscalls.asm @@ -152,7 +152,7 @@ global sys_returndatacopy: GET_CONTEXT %stack (context, kexit_info, dest_offset, offset, size) -> (context, @SEGMENT_MAIN_MEMORY, dest_offset, context, @SEGMENT_RETURNDATA, offset, size, wcopy_after, kexit_info) - %jump(memcpy_bytes) + %jump(memcpy) returndatacopy_empty: %stack (kexit_info, dest_offset, offset, size) -> (kexit_info)