diff --git a/system-contracts/contracts/EvmEmulator.yul b/system-contracts/contracts/EvmEmulator.yul index df0517377..b72c68e49 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 @@ -335,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) } @@ -1038,12 +1032,15 @@ object "EvmEmulator" { } function _saveReturndataAfterZkEVMCall() { + swapActivePointerWithEvmReturndataPointer() loadReturndataIntoActivePtr() + swapActivePointerWithEvmReturndataPointer() mstore(LAST_RETURNDATA_SIZE_OFFSET(), returndatasize()) } function _saveReturndataAfterEVMCall(_outputOffset, _outputLen) -> _gasLeft { let rtsz := returndatasize() + swapActivePointerWithEvmReturndataPointer() loadReturndataIntoActivePtr() // if (rtsz > 31) @@ -1067,11 +1064,14 @@ object "EvmEmulator" { // Skip first 32 bytes of the returnData ptrAddIntoActive(32) } + swapActivePointerWithEvmReturndataPointer() } function _eraseReturndataPointer() { + swapActivePointerWithEvmReturndataPointer() let activePtrSize := getActivePtrDataSize() ptrShrinkIntoActive(and(activePtrSize, 0xFFFFFFFF))// uint32(activePtrSize) + swapActivePointerWithEvmReturndataPointer() mstore(LAST_RETURNDATA_SIZE_OFFSET(), 0) } @@ -1233,6 +1233,7 @@ object "EvmEmulator" { } function _saveConstructorReturnGas() -> gasLeft, addr { + swapActivePointerWithEvmReturndataPointer() loadReturndataIntoActivePtr() if lt(returndatasize(), 64) { @@ -1244,6 +1245,8 @@ object "EvmEmulator" { gasLeft := activePointerLoad(0) addr := activePointerLoad(32) + swapActivePointerWithEvmReturndataPointer() + _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() } + swapActivePointerWithEvmReturndataPointer() copyActivePtrData(add(MEM_OFFSET(), dstOffset), sourceOffset, len) + swapActivePointerWithEvmReturndataPointer() 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 @@ -3265,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) } @@ -3968,12 +3965,15 @@ object "EvmEmulator" { } function _saveReturndataAfterZkEVMCall() { + swapActivePointerWithEvmReturndataPointer() loadReturndataIntoActivePtr() + swapActivePointerWithEvmReturndataPointer() mstore(LAST_RETURNDATA_SIZE_OFFSET(), returndatasize()) } function _saveReturndataAfterEVMCall(_outputOffset, _outputLen) -> _gasLeft { let rtsz := returndatasize() + swapActivePointerWithEvmReturndataPointer() loadReturndataIntoActivePtr() // if (rtsz > 31) @@ -3997,11 +3997,14 @@ object "EvmEmulator" { // Skip first 32 bytes of the returnData ptrAddIntoActive(32) } + swapActivePointerWithEvmReturndataPointer() } function _eraseReturndataPointer() { + swapActivePointerWithEvmReturndataPointer() let activePtrSize := getActivePtrDataSize() ptrShrinkIntoActive(and(activePtrSize, 0xFFFFFFFF))// uint32(activePtrSize) + swapActivePointerWithEvmReturndataPointer() mstore(LAST_RETURNDATA_SIZE_OFFSET(), 0) } @@ -4163,6 +4166,7 @@ object "EvmEmulator" { } function _saveConstructorReturnGas() -> gasLeft, addr { + swapActivePointerWithEvmReturndataPointer() loadReturndataIntoActivePtr() if lt(returndatasize(), 64) { @@ -4174,6 +4178,8 @@ object "EvmEmulator" { gasLeft := activePointerLoad(0) addr := activePointerLoad(32) + swapActivePointerWithEvmReturndataPointer() + _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() } + swapActivePointerWithEvmReturndataPointer() copyActivePtrData(add(MEM_OFFSET(), dstOffset), sourceOffset, len) + swapActivePointerWithEvmReturndataPointer() 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..9fe9e30d0 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 @@ -275,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) } @@ -978,12 +974,15 @@ function getGasForPrecompiles(addr, argsSize) -> gasToCharge { } function _saveReturndataAfterZkEVMCall() { + swapActivePointerWithEvmReturndataPointer() loadReturndataIntoActivePtr() + swapActivePointerWithEvmReturndataPointer() mstore(LAST_RETURNDATA_SIZE_OFFSET(), returndatasize()) } function _saveReturndataAfterEVMCall(_outputOffset, _outputLen) -> _gasLeft { let rtsz := returndatasize() + swapActivePointerWithEvmReturndataPointer() loadReturndataIntoActivePtr() // if (rtsz > 31) @@ -1007,11 +1006,14 @@ function _saveReturndataAfterEVMCall(_outputOffset, _outputLen) -> _gasLeft { // Skip first 32 bytes of the returnData ptrAddIntoActive(32) } + swapActivePointerWithEvmReturndataPointer() } function _eraseReturndataPointer() { + swapActivePointerWithEvmReturndataPointer() let activePtrSize := getActivePtrDataSize() ptrShrinkIntoActive(and(activePtrSize, 0xFFFFFFFF))// uint32(activePtrSize) + swapActivePointerWithEvmReturndataPointer() mstore(LAST_RETURNDATA_SIZE_OFFSET(), 0) } @@ -1173,6 +1175,7 @@ function performSystemCallForCreate(value, bytecodeStart, bytecodeLen) -> succes } function _saveConstructorReturnGas() -> gasLeft, addr { + swapActivePointerWithEvmReturndataPointer() loadReturndataIntoActivePtr() if lt(returndatasize(), 64) { @@ -1184,6 +1187,8 @@ function _saveConstructorReturnGas() -> gasLeft, addr { gasLeft := activePointerLoad(0) addr := activePointerLoad(32) + swapActivePointerWithEvmReturndataPointer() + _eraseReturndataPointer() } diff --git a/system-contracts/evm-emulator/EvmEmulatorLoop.template.yul b/system-contracts/evm-emulator/EvmEmulatorLoop.template.yul index 8c998b677..5a504a26c 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() } + swapActivePointerWithEvmReturndataPointer() copyActivePtrData(add(MEM_OFFSET(), dstOffset), sourceOffset, len) + swapActivePointerWithEvmReturndataPointer() ip := add(ip, 1) } case 0x3F { // OP_EXTCODEHASH