From 6610ec44877740da4eba56019c764d57d7417701 Mon Sep 17 00:00:00 2001 From: Daniel Lubarov Date: Mon, 18 Jul 2022 14:55:15 -0700 Subject: [PATCH 1/3] Implement memcpy This can be used, for example, to copy `CALL` data (which is a slice of the caller's main memory) to the callee's `CALLDATA` segment. --- evm/src/cpu/kernel/asm/basic_macros.asm | 18 ++++++++ evm/src/cpu/kernel/asm/memory.asm | 56 ++++++++++++++++++++++++- 2 files changed, 72 insertions(+), 2 deletions(-) diff --git a/evm/src/cpu/kernel/asm/basic_macros.asm b/evm/src/cpu/kernel/asm/basic_macros.asm index 7bf001b4..854e0a1a 100644 --- a/evm/src/cpu/kernel/asm/basic_macros.asm +++ b/evm/src/cpu/kernel/asm/basic_macros.asm @@ -26,6 +26,24 @@ %endrep %endmacro +%macro pop5 + %rep 5 + pop + %endrep +%endmacro + +%macro pop6 + %rep 6 + pop + %endrep +%endmacro + +%macro pop7 + %rep 7 + pop + %endrep +%endmacro + %macro add_const(c) // stack: input, ... PUSH $c diff --git a/evm/src/cpu/kernel/asm/memory.asm b/evm/src/cpu/kernel/asm/memory.asm index e3af9954..0bf0fbd1 100644 --- a/evm/src/cpu/kernel/asm/memory.asm +++ b/evm/src/cpu/kernel/asm/memory.asm @@ -1,4 +1,4 @@ -// Load a byte from the given segment of the current context's memory space. +// Load a value from the given segment of the current context's memory space. // Note that main memory values are one byte each, but in general memory values // can be 256 bits. This macro deals with a single address (unlike MLOAD), so // if it is used with main memory, it will load a single byte. @@ -12,7 +12,7 @@ // stack: value %endmacro -// Store a byte to the given segment of the current context's memory space. +// Store a value to the given segment of the current context's memory space. // Note that main memory values are one byte each, but in general memory values // can be 256 bits. This macro deals with a single address (unlike MSTORE), so // if it is used with main memory, it will store a single byte. @@ -25,3 +25,55 @@ MSTORE_GENERAL // stack: (empty) %endmacro + +// Copies `count` values from +// SRC = (src_ctx, src_segment, src_addr) +// to +// DST = (dst_ctx, dst_segment, dst_addr). +// These tuple definitions are used for brevity in the stack comments below. +global memcpy: + JUMPDEST + // stack: DST, SRC, count, retdest + DUP7 + // stack: count, DST, SRC, count, retdest + ISZERO + // stack: count == 0, DST, SRC, count, retdest + %jumpi memcpy_finish + // stack: DST, SRC, count, retdest + + // Copy the next value. + DUP6 + DUP6 + DUP6 + // stack: SRC, DST, SRC, count, retdest + MLOAD_GENERAL + // stack: value, DST, SRC, count, retdest + DUP4 + DUP4 + DUP4 + // stack: DST, value, DST, SRC, count, retdest + MSTORE_GENERAL + // stack: DST, SRC, count, retdest + + // Increment dst_addr. + SWAP2 + %add_const(1) + SWAP2 + // Increment src_addr. + SWAP5 + %add_const(1) + SWAP5 + // Decrement count. + SWAP6 + %sub_const(1) + SWAP6 + + // Recurse! + JUMP memcpy + +memcpy_finish: + JUMPDEST + // stack: DST, SRC, count, retdest + %pop7 + // stack: retdest + JUMP From 80d32f89b6cac50c57f0a76728cbd48b852bee55 Mon Sep 17 00:00:00 2001 From: Daniel Lubarov Date: Mon, 18 Jul 2022 15:58:12 -0700 Subject: [PATCH 2/3] fixes --- evm/src/cpu/kernel/aggregator.rs | 1 + evm/src/cpu/kernel/asm/memory.asm | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/evm/src/cpu/kernel/aggregator.rs b/evm/src/cpu/kernel/aggregator.rs index c6c47387..8784e337 100644 --- a/evm/src/cpu/kernel/aggregator.rs +++ b/evm/src/cpu/kernel/aggregator.rs @@ -28,6 +28,7 @@ pub(crate) fn combined_kernel() -> Kernel { include_str!("asm/exp.asm"), include_str!("asm/curve_mul.asm"), include_str!("asm/curve_add.asm"), + include_str!("asm/memory.asm"), include_str!("asm/moddiv.asm"), include_str!("asm/secp256k1/curve_mul.asm"), include_str!("asm/secp256k1/curve_add.asm"), diff --git a/evm/src/cpu/kernel/asm/memory.asm b/evm/src/cpu/kernel/asm/memory.asm index 0bf0fbd1..42368af5 100644 --- a/evm/src/cpu/kernel/asm/memory.asm +++ b/evm/src/cpu/kernel/asm/memory.asm @@ -38,7 +38,7 @@ global memcpy: // stack: count, DST, SRC, count, retdest ISZERO // stack: count == 0, DST, SRC, count, retdest - %jumpi memcpy_finish + %jumpi(memcpy_finish) // stack: DST, SRC, count, retdest // Copy the next value. @@ -69,7 +69,7 @@ global memcpy: SWAP6 // Recurse! - JUMP memcpy + %jump(memcpy) memcpy_finish: JUMPDEST From 5b1f5640398132105f996ce093b90d43cb4d7e12 Mon Sep 17 00:00:00 2001 From: Daniel Lubarov Date: Tue, 19 Jul 2022 07:20:57 -0700 Subject: [PATCH 3/3] Feedback --- evm/src/cpu/kernel/asm/memory.asm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/evm/src/cpu/kernel/asm/memory.asm b/evm/src/cpu/kernel/asm/memory.asm index 42368af5..88748a3e 100644 --- a/evm/src/cpu/kernel/asm/memory.asm +++ b/evm/src/cpu/kernel/asm/memory.asm @@ -68,7 +68,7 @@ global memcpy: %sub_const(1) SWAP6 - // Recurse! + // Continue the loop. %jump(memcpy) memcpy_finish: