From 86a3395dc05f0a684e5ea57d6cee07e26af231bc Mon Sep 17 00:00:00 2001 From: Vladislav Volosnikov Date: Fri, 10 Jan 2025 12:23:59 +0100 Subject: [PATCH 1/2] Store bytecode in the active pointer --- system-contracts/contracts/EvmEmulator.yul | 38 +++++++++++-------- .../evm-emulator/EvmEmulator.template.yul | 4 -- .../EvmEmulatorFunctions.template.yul | 13 +++++-- .../evm-emulator/EvmEmulatorLoop.template.yul | 4 +- 4 files changed, 33 insertions(+), 26 deletions(-) diff --git a/system-contracts/contracts/EvmEmulator.yul b/system-contracts/contracts/EvmEmulator.yul index df0517377..11f167dbf 100644 --- a/system-contracts/contracts/EvmEmulator.yul +++ b/system-contracts/contracts/EvmEmulator.yul @@ -17,8 +17,6 @@ object "EvmEmulator" { } mstore(BYTECODE_LEN_OFFSET(), size) - - swapActivePointerWithBytecodePointer() } function padBytecode(offset, len) -> blobLen { @@ -308,16 +306,12 @@ object "EvmEmulator" { // It is the responsibility of the caller to ensure that ip is correct function $llvm_AlwaysInline_llvm$_readIP(ip) -> opcode { - swapActivePointerWithBytecodePointer() opcode := shr(248, activePointerLoad(ip)) - swapActivePointerWithBytecodePointer() } // It is the responsibility of the caller to ensure that start and length is correct function readBytes(start, length) -> value { - swapActivePointerWithBytecodePointer() let rawValue := activePointerLoad(start) - swapActivePointerWithBytecodePointer() value := shr(mul(8, sub(32, length)), rawValue) // will be padded by zeroes if out of bounds @@ -1038,12 +1032,15 @@ object "EvmEmulator" { } function _saveReturndataAfterZkEVMCall() { + swapActivePointerWithBytecodePointer() loadReturndataIntoActivePtr() + swapActivePointerWithBytecodePointer() mstore(LAST_RETURNDATA_SIZE_OFFSET(), returndatasize()) } function _saveReturndataAfterEVMCall(_outputOffset, _outputLen) -> _gasLeft { let rtsz := returndatasize() + swapActivePointerWithBytecodePointer() loadReturndataIntoActivePtr() // if (rtsz > 31) @@ -1067,11 +1064,14 @@ object "EvmEmulator" { // Skip first 32 bytes of the returnData ptrAddIntoActive(32) } + swapActivePointerWithBytecodePointer() } function _eraseReturndataPointer() { + swapActivePointerWithBytecodePointer() let activePtrSize := getActivePtrDataSize() ptrShrinkIntoActive(and(activePtrSize, 0xFFFFFFFF))// uint32(activePtrSize) + swapActivePointerWithBytecodePointer() mstore(LAST_RETURNDATA_SIZE_OFFSET(), 0) } @@ -1233,6 +1233,7 @@ object "EvmEmulator" { } function _saveConstructorReturnGas() -> gasLeft, addr { + swapActivePointerWithBytecodePointer() loadReturndataIntoActivePtr() if lt(returndatasize(), 64) { @@ -1244,6 +1245,8 @@ object "EvmEmulator" { gasLeft := activePointerLoad(0) addr := activePointerLoad(32) + swapActivePointerWithBytecodePointer() + _eraseReturndataPointer() } @@ -1776,9 +1779,7 @@ object "EvmEmulator" { } if truncatedLen { - swapActivePointerWithBytecodePointer() copyActivePtrData(dstOffset, sourceOffset, truncatedLen) - swapActivePointerWithBytecodePointer() } ip := add(ip, 1) @@ -1885,7 +1886,9 @@ object "EvmEmulator" { panic() } + swapActivePointerWithBytecodePointer() copyActivePtrData(add(MEM_OFFSET(), dstOffset), sourceOffset, len) + swapActivePointerWithBytecodePointer() ip := add(ip, 1) } case 0x3F { // OP_EXTCODEHASH @@ -2984,8 +2987,6 @@ object "EvmEmulator" { loadReturndataIntoActivePtr() mstore(BYTECODE_LEN_OFFSET(), codeLen) - - swapActivePointerWithBytecodePointer() } //////////////////////////////////////////////////////////////// @@ -3238,16 +3239,12 @@ object "EvmEmulator" { // It is the responsibility of the caller to ensure that ip is correct function $llvm_AlwaysInline_llvm$_readIP(ip) -> opcode { - swapActivePointerWithBytecodePointer() opcode := shr(248, activePointerLoad(ip)) - swapActivePointerWithBytecodePointer() } // It is the responsibility of the caller to ensure that start and length is correct function readBytes(start, length) -> value { - swapActivePointerWithBytecodePointer() let rawValue := activePointerLoad(start) - swapActivePointerWithBytecodePointer() value := shr(mul(8, sub(32, length)), rawValue) // will be padded by zeroes if out of bounds @@ -3968,12 +3965,15 @@ object "EvmEmulator" { } function _saveReturndataAfterZkEVMCall() { + swapActivePointerWithBytecodePointer() loadReturndataIntoActivePtr() + swapActivePointerWithBytecodePointer() mstore(LAST_RETURNDATA_SIZE_OFFSET(), returndatasize()) } function _saveReturndataAfterEVMCall(_outputOffset, _outputLen) -> _gasLeft { let rtsz := returndatasize() + swapActivePointerWithBytecodePointer() loadReturndataIntoActivePtr() // if (rtsz > 31) @@ -3997,11 +3997,14 @@ object "EvmEmulator" { // Skip first 32 bytes of the returnData ptrAddIntoActive(32) } + swapActivePointerWithBytecodePointer() } function _eraseReturndataPointer() { + swapActivePointerWithBytecodePointer() let activePtrSize := getActivePtrDataSize() ptrShrinkIntoActive(and(activePtrSize, 0xFFFFFFFF))// uint32(activePtrSize) + swapActivePointerWithBytecodePointer() mstore(LAST_RETURNDATA_SIZE_OFFSET(), 0) } @@ -4163,6 +4166,7 @@ object "EvmEmulator" { } function _saveConstructorReturnGas() -> gasLeft, addr { + swapActivePointerWithBytecodePointer() loadReturndataIntoActivePtr() if lt(returndatasize(), 64) { @@ -4174,6 +4178,8 @@ object "EvmEmulator" { gasLeft := activePointerLoad(0) addr := activePointerLoad(32) + swapActivePointerWithBytecodePointer() + _eraseReturndataPointer() } @@ -4694,9 +4700,7 @@ object "EvmEmulator" { } if truncatedLen { - swapActivePointerWithBytecodePointer() copyActivePtrData(dstOffset, sourceOffset, truncatedLen) - swapActivePointerWithBytecodePointer() } ip := add(ip, 1) @@ -4803,7 +4807,9 @@ object "EvmEmulator" { panic() } + swapActivePointerWithBytecodePointer() copyActivePtrData(add(MEM_OFFSET(), dstOffset), sourceOffset, len) + swapActivePointerWithBytecodePointer() ip := add(ip, 1) } case 0x3F { // OP_EXTCODEHASH diff --git a/system-contracts/evm-emulator/EvmEmulator.template.yul b/system-contracts/evm-emulator/EvmEmulator.template.yul index abbd3662c..248363644 100644 --- a/system-contracts/evm-emulator/EvmEmulator.template.yul +++ b/system-contracts/evm-emulator/EvmEmulator.template.yul @@ -17,8 +17,6 @@ object "EvmEmulator" { } mstore(BYTECODE_LEN_OFFSET(), size) - - swapActivePointerWithBytecodePointer() } function padBytecode(offset, len) -> blobLen { @@ -116,8 +114,6 @@ object "EvmEmulator" { loadReturndataIntoActivePtr() mstore(BYTECODE_LEN_OFFSET(), codeLen) - - swapActivePointerWithBytecodePointer() } diff --git a/system-contracts/evm-emulator/EvmEmulatorFunctions.template.yul b/system-contracts/evm-emulator/EvmEmulatorFunctions.template.yul index 4785ebd3b..04c7d2a6a 100644 --- a/system-contracts/evm-emulator/EvmEmulatorFunctions.template.yul +++ b/system-contracts/evm-emulator/EvmEmulatorFunctions.template.yul @@ -248,16 +248,12 @@ function insufficientBalance(value) -> res { // It is the responsibility of the caller to ensure that ip is correct function $llvm_AlwaysInline_llvm$_readIP(ip) -> opcode { - swapActivePointerWithBytecodePointer() opcode := shr(248, activePointerLoad(ip)) - swapActivePointerWithBytecodePointer() } // It is the responsibility of the caller to ensure that start and length is correct function readBytes(start, length) -> value { - swapActivePointerWithBytecodePointer() let rawValue := activePointerLoad(start) - swapActivePointerWithBytecodePointer() value := shr(mul(8, sub(32, length)), rawValue) // will be padded by zeroes if out of bounds @@ -978,12 +974,15 @@ function getGasForPrecompiles(addr, argsSize) -> gasToCharge { } function _saveReturndataAfterZkEVMCall() { + swapActivePointerWithBytecodePointer() loadReturndataIntoActivePtr() + swapActivePointerWithBytecodePointer() mstore(LAST_RETURNDATA_SIZE_OFFSET(), returndatasize()) } function _saveReturndataAfterEVMCall(_outputOffset, _outputLen) -> _gasLeft { let rtsz := returndatasize() + swapActivePointerWithBytecodePointer() loadReturndataIntoActivePtr() // if (rtsz > 31) @@ -1007,11 +1006,14 @@ function _saveReturndataAfterEVMCall(_outputOffset, _outputLen) -> _gasLeft { // Skip first 32 bytes of the returnData ptrAddIntoActive(32) } + swapActivePointerWithBytecodePointer() } function _eraseReturndataPointer() { + swapActivePointerWithBytecodePointer() let activePtrSize := getActivePtrDataSize() ptrShrinkIntoActive(and(activePtrSize, 0xFFFFFFFF))// uint32(activePtrSize) + swapActivePointerWithBytecodePointer() mstore(LAST_RETURNDATA_SIZE_OFFSET(), 0) } @@ -1173,6 +1175,7 @@ function performSystemCallForCreate(value, bytecodeStart, bytecodeLen) -> succes } function _saveConstructorReturnGas() -> gasLeft, addr { + swapActivePointerWithBytecodePointer() loadReturndataIntoActivePtr() if lt(returndatasize(), 64) { @@ -1184,6 +1187,8 @@ function _saveConstructorReturnGas() -> gasLeft, addr { gasLeft := activePointerLoad(0) addr := activePointerLoad(32) + swapActivePointerWithBytecodePointer() + _eraseReturndataPointer() } diff --git a/system-contracts/evm-emulator/EvmEmulatorLoop.template.yul b/system-contracts/evm-emulator/EvmEmulatorLoop.template.yul index 8c998b677..dc20cb79d 100644 --- a/system-contracts/evm-emulator/EvmEmulatorLoop.template.yul +++ b/system-contracts/evm-emulator/EvmEmulatorLoop.template.yul @@ -429,9 +429,7 @@ for { } true { } { } if truncatedLen { - swapActivePointerWithBytecodePointer() copyActivePtrData(dstOffset, sourceOffset, truncatedLen) - swapActivePointerWithBytecodePointer() } ip := add(ip, 1) @@ -538,7 +536,9 @@ for { } true { } { panic() } + swapActivePointerWithBytecodePointer() copyActivePtrData(add(MEM_OFFSET(), dstOffset), sourceOffset, len) + swapActivePointerWithBytecodePointer() ip := add(ip, 1) } case 0x3F { // OP_EXTCODEHASH From 8a54027d228e88b3265c48c6a30c4fd97ed06f49 Mon Sep 17 00:00:00 2001 From: Vladislav Volosnikov Date: Fri, 10 Jan 2025 12:26:26 +0100 Subject: [PATCH 2/2] Rename function --- system-contracts/contracts/EvmEmulator.yul | 44 +++++++++---------- .../EvmEmulatorFunctions.template.yul | 18 ++++---- .../evm-emulator/EvmEmulatorLoop.template.yul | 4 +- 3 files changed, 33 insertions(+), 33 deletions(-) diff --git a/system-contracts/contracts/EvmEmulator.yul b/system-contracts/contracts/EvmEmulator.yul index 11f167dbf..b72c68e49 100644 --- a/system-contracts/contracts/EvmEmulator.yul +++ b/system-contracts/contracts/EvmEmulator.yul @@ -329,7 +329,7 @@ object "EvmEmulator" { verbatim_2i_0o("active_ptr_swap", index0, index1) } - function swapActivePointerWithBytecodePointer() { + function swapActivePointerWithEvmReturndataPointer() { verbatim_2i_0o("active_ptr_swap", 0, 2) } @@ -1032,15 +1032,15 @@ object "EvmEmulator" { } function _saveReturndataAfterZkEVMCall() { - swapActivePointerWithBytecodePointer() + swapActivePointerWithEvmReturndataPointer() loadReturndataIntoActivePtr() - swapActivePointerWithBytecodePointer() + swapActivePointerWithEvmReturndataPointer() mstore(LAST_RETURNDATA_SIZE_OFFSET(), returndatasize()) } function _saveReturndataAfterEVMCall(_outputOffset, _outputLen) -> _gasLeft { let rtsz := returndatasize() - swapActivePointerWithBytecodePointer() + swapActivePointerWithEvmReturndataPointer() loadReturndataIntoActivePtr() // if (rtsz > 31) @@ -1064,14 +1064,14 @@ object "EvmEmulator" { // Skip first 32 bytes of the returnData ptrAddIntoActive(32) } - swapActivePointerWithBytecodePointer() + swapActivePointerWithEvmReturndataPointer() } function _eraseReturndataPointer() { - swapActivePointerWithBytecodePointer() + swapActivePointerWithEvmReturndataPointer() let activePtrSize := getActivePtrDataSize() ptrShrinkIntoActive(and(activePtrSize, 0xFFFFFFFF))// uint32(activePtrSize) - swapActivePointerWithBytecodePointer() + swapActivePointerWithEvmReturndataPointer() mstore(LAST_RETURNDATA_SIZE_OFFSET(), 0) } @@ -1233,7 +1233,7 @@ object "EvmEmulator" { } function _saveConstructorReturnGas() -> gasLeft, addr { - swapActivePointerWithBytecodePointer() + swapActivePointerWithEvmReturndataPointer() loadReturndataIntoActivePtr() if lt(returndatasize(), 64) { @@ -1245,7 +1245,7 @@ object "EvmEmulator" { gasLeft := activePointerLoad(0) addr := activePointerLoad(32) - swapActivePointerWithBytecodePointer() + swapActivePointerWithEvmReturndataPointer() _eraseReturndataPointer() } @@ -1886,9 +1886,9 @@ object "EvmEmulator" { panic() } - swapActivePointerWithBytecodePointer() + swapActivePointerWithEvmReturndataPointer() copyActivePtrData(add(MEM_OFFSET(), dstOffset), sourceOffset, len) - swapActivePointerWithBytecodePointer() + swapActivePointerWithEvmReturndataPointer() ip := add(ip, 1) } case 0x3F { // OP_EXTCODEHASH @@ -3262,7 +3262,7 @@ object "EvmEmulator" { verbatim_2i_0o("active_ptr_swap", index0, index1) } - function swapActivePointerWithBytecodePointer() { + function swapActivePointerWithEvmReturndataPointer() { verbatim_2i_0o("active_ptr_swap", 0, 2) } @@ -3965,15 +3965,15 @@ object "EvmEmulator" { } function _saveReturndataAfterZkEVMCall() { - swapActivePointerWithBytecodePointer() + swapActivePointerWithEvmReturndataPointer() loadReturndataIntoActivePtr() - swapActivePointerWithBytecodePointer() + swapActivePointerWithEvmReturndataPointer() mstore(LAST_RETURNDATA_SIZE_OFFSET(), returndatasize()) } function _saveReturndataAfterEVMCall(_outputOffset, _outputLen) -> _gasLeft { let rtsz := returndatasize() - swapActivePointerWithBytecodePointer() + swapActivePointerWithEvmReturndataPointer() loadReturndataIntoActivePtr() // if (rtsz > 31) @@ -3997,14 +3997,14 @@ object "EvmEmulator" { // Skip first 32 bytes of the returnData ptrAddIntoActive(32) } - swapActivePointerWithBytecodePointer() + swapActivePointerWithEvmReturndataPointer() } function _eraseReturndataPointer() { - swapActivePointerWithBytecodePointer() + swapActivePointerWithEvmReturndataPointer() let activePtrSize := getActivePtrDataSize() ptrShrinkIntoActive(and(activePtrSize, 0xFFFFFFFF))// uint32(activePtrSize) - swapActivePointerWithBytecodePointer() + swapActivePointerWithEvmReturndataPointer() mstore(LAST_RETURNDATA_SIZE_OFFSET(), 0) } @@ -4166,7 +4166,7 @@ object "EvmEmulator" { } function _saveConstructorReturnGas() -> gasLeft, addr { - swapActivePointerWithBytecodePointer() + swapActivePointerWithEvmReturndataPointer() loadReturndataIntoActivePtr() if lt(returndatasize(), 64) { @@ -4178,7 +4178,7 @@ object "EvmEmulator" { gasLeft := activePointerLoad(0) addr := activePointerLoad(32) - swapActivePointerWithBytecodePointer() + swapActivePointerWithEvmReturndataPointer() _eraseReturndataPointer() } @@ -4807,9 +4807,9 @@ object "EvmEmulator" { panic() } - swapActivePointerWithBytecodePointer() + swapActivePointerWithEvmReturndataPointer() copyActivePtrData(add(MEM_OFFSET(), dstOffset), sourceOffset, len) - swapActivePointerWithBytecodePointer() + swapActivePointerWithEvmReturndataPointer() ip := add(ip, 1) } case 0x3F { // OP_EXTCODEHASH diff --git a/system-contracts/evm-emulator/EvmEmulatorFunctions.template.yul b/system-contracts/evm-emulator/EvmEmulatorFunctions.template.yul index 04c7d2a6a..9fe9e30d0 100644 --- a/system-contracts/evm-emulator/EvmEmulatorFunctions.template.yul +++ b/system-contracts/evm-emulator/EvmEmulatorFunctions.template.yul @@ -271,7 +271,7 @@ function swapActivePointer(index0, index1) { verbatim_2i_0o("active_ptr_swap", index0, index1) } -function swapActivePointerWithBytecodePointer() { +function swapActivePointerWithEvmReturndataPointer() { verbatim_2i_0o("active_ptr_swap", 0, 2) } @@ -974,15 +974,15 @@ function getGasForPrecompiles(addr, argsSize) -> gasToCharge { } function _saveReturndataAfterZkEVMCall() { - swapActivePointerWithBytecodePointer() + swapActivePointerWithEvmReturndataPointer() loadReturndataIntoActivePtr() - swapActivePointerWithBytecodePointer() + swapActivePointerWithEvmReturndataPointer() mstore(LAST_RETURNDATA_SIZE_OFFSET(), returndatasize()) } function _saveReturndataAfterEVMCall(_outputOffset, _outputLen) -> _gasLeft { let rtsz := returndatasize() - swapActivePointerWithBytecodePointer() + swapActivePointerWithEvmReturndataPointer() loadReturndataIntoActivePtr() // if (rtsz > 31) @@ -1006,14 +1006,14 @@ function _saveReturndataAfterEVMCall(_outputOffset, _outputLen) -> _gasLeft { // Skip first 32 bytes of the returnData ptrAddIntoActive(32) } - swapActivePointerWithBytecodePointer() + swapActivePointerWithEvmReturndataPointer() } function _eraseReturndataPointer() { - swapActivePointerWithBytecodePointer() + swapActivePointerWithEvmReturndataPointer() let activePtrSize := getActivePtrDataSize() ptrShrinkIntoActive(and(activePtrSize, 0xFFFFFFFF))// uint32(activePtrSize) - swapActivePointerWithBytecodePointer() + swapActivePointerWithEvmReturndataPointer() mstore(LAST_RETURNDATA_SIZE_OFFSET(), 0) } @@ -1175,7 +1175,7 @@ function performSystemCallForCreate(value, bytecodeStart, bytecodeLen) -> succes } function _saveConstructorReturnGas() -> gasLeft, addr { - swapActivePointerWithBytecodePointer() + swapActivePointerWithEvmReturndataPointer() loadReturndataIntoActivePtr() if lt(returndatasize(), 64) { @@ -1187,7 +1187,7 @@ function _saveConstructorReturnGas() -> gasLeft, addr { gasLeft := activePointerLoad(0) addr := activePointerLoad(32) - swapActivePointerWithBytecodePointer() + swapActivePointerWithEvmReturndataPointer() _eraseReturndataPointer() } diff --git a/system-contracts/evm-emulator/EvmEmulatorLoop.template.yul b/system-contracts/evm-emulator/EvmEmulatorLoop.template.yul index dc20cb79d..5a504a26c 100644 --- a/system-contracts/evm-emulator/EvmEmulatorLoop.template.yul +++ b/system-contracts/evm-emulator/EvmEmulatorLoop.template.yul @@ -536,9 +536,9 @@ for { } true { } { panic() } - swapActivePointerWithBytecodePointer() + swapActivePointerWithEvmReturndataPointer() copyActivePtrData(add(MEM_OFFSET(), dstOffset), sourceOffset, len) - swapActivePointerWithBytecodePointer() + swapActivePointerWithEvmReturndataPointer() ip := add(ip, 1) } case 0x3F { // OP_EXTCODEHASH