From ff2fe0f2e73494600c12a535be899f168f0e06c0 Mon Sep 17 00:00:00 2001 From: Gianbelinche Date: Fri, 7 Jun 2024 12:53:11 -0300 Subject: [PATCH 1/2] Fix extcodecopy not storing on dest --- .../EvmInterpreterFunctions.template.yul | 14 +++++++--- .../contracts/EvmInterpreterPreprocessed.yul | 28 +++++++++++++------ 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/system-contracts/contracts/EvmInterpreterFunctions.template.yul b/system-contracts/contracts/EvmInterpreterFunctions.template.yul index 1a1395895..66b2400f2 100644 --- a/system-contracts/contracts/EvmInterpreterFunctions.template.yul +++ b/system-contracts/contracts/EvmInterpreterFunctions.template.yul @@ -222,6 +222,11 @@ function getIsStaticFromCallFlags() -> isStatic { // Basically performs an extcodecopy, while returning the length of the bytecode. function _fetchDeployedCode(addr, _offset, _len) -> codeLen { + codeLen := _fetchDeployedCodeWithDest(addr, 0, _len, _offset) +} + +// Basically performs an extcodecopy, while returning the length of the bytecode. +function _fetchDeployedCodeWithDest(addr, _offset, _len, dest) -> codeLen { let codeHash := _getRawCodeHash(addr) mstore(0, codeHash) @@ -241,7 +246,7 @@ function _fetchDeployedCode(addr, _offset, _len) -> codeLen { _len := codeLen } - returndatacopy(_offset, 32, _len) + returndatacopy(dest, add(32,_offset), _len) } // Returns the length of the bytecode. @@ -1244,15 +1249,16 @@ function performExtCodeCopy(evmGas,oldSp) -> evmGasLeft, sp { let len_32 := shr(5, len) for {let i := 0} lt(i, len_32) { i := add(i, 1) } { - mstore(shl(5,i),0) + mstore(add(dest,shl(5,i)),0) } let size_32 := shl(5,len_32) let rest_32 := sub(len, size_32) for {let i := 0} lt(i, rest_32) { i := add(i, 1) } { - mstore8(add(size_32,i),0) + mstore8(add(dest,add(size_32,i)),0) } + // Gets the code from the addr - pop(_fetchDeployedCode(addr, add(offset, MEM_OFFSET_INNER()), len)) + pop(_fetchDeployedCodeWithDest(addr, offset, len,add(dest,MEM_OFFSET_INNER()))) } function performCreate(evmGas,oldSp,isStatic) -> evmGasLeft, sp { diff --git a/system-contracts/contracts/EvmInterpreterPreprocessed.yul b/system-contracts/contracts/EvmInterpreterPreprocessed.yul index 28b920add..3d44c17a1 100644 --- a/system-contracts/contracts/EvmInterpreterPreprocessed.yul +++ b/system-contracts/contracts/EvmInterpreterPreprocessed.yul @@ -296,6 +296,11 @@ object "EVMInterpreter" { // Basically performs an extcodecopy, while returning the length of the bytecode. function _fetchDeployedCode(addr, _offset, _len) -> codeLen { + codeLen := _fetchDeployedCodeWithDest(addr, 0, _len, _offset) + } + + // Basically performs an extcodecopy, while returning the length of the bytecode. + function _fetchDeployedCodeWithDest(addr, _offset, _len, dest) -> codeLen { let codeHash := _getRawCodeHash(addr) mstore(0, codeHash) @@ -315,7 +320,7 @@ object "EVMInterpreter" { _len := codeLen } - returndatacopy(_offset, 32, _len) + returndatacopy(dest, add(32,_offset), _len) } // Returns the length of the bytecode. @@ -1318,15 +1323,16 @@ object "EVMInterpreter" { let len_32 := shr(5, len) for {let i := 0} lt(i, len_32) { i := add(i, 1) } { - mstore(shl(5,i),0) + mstore(add(dest,shl(5,i)),0) } let size_32 := shl(5,len_32) let rest_32 := sub(len, size_32) for {let i := 0} lt(i, rest_32) { i := add(i, 1) } { - mstore8(add(size_32,i),0) + mstore8(add(dest,add(size_32,i)),0) } + // Gets the code from the addr - pop(_fetchDeployedCode(addr, add(offset, MEM_OFFSET_INNER()), len)) + pop(_fetchDeployedCodeWithDest(addr, offset, len,add(dest,MEM_OFFSET_INNER()))) } function performCreate(evmGas,oldSp,isStatic) -> evmGasLeft, sp { @@ -2919,6 +2925,11 @@ object "EVMInterpreter" { // Basically performs an extcodecopy, while returning the length of the bytecode. function _fetchDeployedCode(addr, _offset, _len) -> codeLen { + codeLen := _fetchDeployedCodeWithDest(addr, 0, _len, _offset) + } + + // Basically performs an extcodecopy, while returning the length of the bytecode. + function _fetchDeployedCodeWithDest(addr, _offset, _len, dest) -> codeLen { let codeHash := _getRawCodeHash(addr) mstore(0, codeHash) @@ -2938,7 +2949,7 @@ object "EVMInterpreter" { _len := codeLen } - returndatacopy(_offset, 32, _len) + returndatacopy(dest, add(32,_offset), _len) } // Returns the length of the bytecode. @@ -3941,15 +3952,16 @@ object "EVMInterpreter" { let len_32 := shr(5, len) for {let i := 0} lt(i, len_32) { i := add(i, 1) } { - mstore(shl(5,i),0) + mstore(add(dest,shl(5,i)),0) } let size_32 := shl(5,len_32) let rest_32 := sub(len, size_32) for {let i := 0} lt(i, rest_32) { i := add(i, 1) } { - mstore8(add(size_32,i),0) + mstore8(add(dest,add(size_32,i)),0) } + // Gets the code from the addr - pop(_fetchDeployedCode(addr, add(offset, MEM_OFFSET_INNER()), len)) + pop(_fetchDeployedCodeWithDest(addr, offset, len,add(dest,MEM_OFFSET_INNER()))) } function performCreate(evmGas,oldSp,isStatic) -> evmGasLeft, sp { From c9d98838c39b82542dc749a4db097eb9360c34ab Mon Sep 17 00:00:00 2001 From: Gianbelinche Date: Fri, 7 Jun 2024 12:58:29 -0300 Subject: [PATCH 2/2] Fix extcodecopy reverting con empty address --- .../contracts/EvmInterpreterFunctions.template.yul | 5 ++++- .../contracts/EvmInterpreterPreprocessed.yul | 10 ++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/system-contracts/contracts/EvmInterpreterFunctions.template.yul b/system-contracts/contracts/EvmInterpreterFunctions.template.yul index 66b2400f2..483293281 100644 --- a/system-contracts/contracts/EvmInterpreterFunctions.template.yul +++ b/system-contracts/contracts/EvmInterpreterFunctions.template.yul @@ -1258,7 +1258,10 @@ function performExtCodeCopy(evmGas,oldSp) -> evmGasLeft, sp { } // Gets the code from the addr - pop(_fetchDeployedCodeWithDest(addr, offset, len,add(dest,MEM_OFFSET_INNER()))) + if iszero(iszero(_getRawCodeHash(addr))) { + pop(_fetchDeployedCodeWithDest(addr, offset, len,add(dest,MEM_OFFSET_INNER()))) + } + } function performCreate(evmGas,oldSp,isStatic) -> evmGasLeft, sp { diff --git a/system-contracts/contracts/EvmInterpreterPreprocessed.yul b/system-contracts/contracts/EvmInterpreterPreprocessed.yul index 3d44c17a1..902c784bf 100644 --- a/system-contracts/contracts/EvmInterpreterPreprocessed.yul +++ b/system-contracts/contracts/EvmInterpreterPreprocessed.yul @@ -1332,7 +1332,10 @@ object "EVMInterpreter" { } // Gets the code from the addr - pop(_fetchDeployedCodeWithDest(addr, offset, len,add(dest,MEM_OFFSET_INNER()))) + if iszero(iszero(_getRawCodeHash(addr))) { + pop(_fetchDeployedCodeWithDest(addr, offset, len,add(dest,MEM_OFFSET_INNER()))) + } + } function performCreate(evmGas,oldSp,isStatic) -> evmGasLeft, sp { @@ -3961,7 +3964,10 @@ object "EVMInterpreter" { } // Gets the code from the addr - pop(_fetchDeployedCodeWithDest(addr, offset, len,add(dest,MEM_OFFSET_INNER()))) + if iszero(iszero(_getRawCodeHash(addr))) { + pop(_fetchDeployedCodeWithDest(addr, offset, len,add(dest,MEM_OFFSET_INNER()))) + } + } function performCreate(evmGas,oldSp,isStatic) -> evmGasLeft, sp {