Skip to content

Commit

Permalink
Merge pull request #515 from lambdaclass/extcodecopy-fix
Browse files Browse the repository at this point in the history
[EVM-Equivalence-YUL] Extcodecopy fix
  • Loading branch information
jrchatruc committed Jun 10, 2024
2 parents a2b77bd + c9d9883 commit 45d2c6d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
17 changes: 13 additions & 4 deletions system-contracts/contracts/EvmInterpreterFunctions.template.yul
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.
Expand Down Expand Up @@ -1244,15 +1249,19 @@ 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))
if iszero(iszero(_getRawCodeHash(addr))) {
pop(_fetchDeployedCodeWithDest(addr, offset, len,add(dest,MEM_OFFSET_INNER())))
}

}

function performCreate(evmGas,oldSp,isStatic) -> evmGasLeft, sp {
Expand Down
34 changes: 26 additions & 8 deletions system-contracts/contracts/EvmInterpreterPreprocessed.yul
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -315,7 +320,7 @@ object "EVMInterpreter" {
_len := codeLen
}

returndatacopy(_offset, 32, _len)
returndatacopy(dest, add(32,_offset), _len)
}

// Returns the length of the bytecode.
Expand Down Expand Up @@ -1318,15 +1323,19 @@ 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))
if iszero(iszero(_getRawCodeHash(addr))) {
pop(_fetchDeployedCodeWithDest(addr, offset, len,add(dest,MEM_OFFSET_INNER())))
}

}

function performCreate(evmGas,oldSp,isStatic) -> evmGasLeft, sp {
Expand Down Expand Up @@ -2919,6 +2928,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)
Expand All @@ -2938,7 +2952,7 @@ object "EVMInterpreter" {
_len := codeLen
}

returndatacopy(_offset, 32, _len)
returndatacopy(dest, add(32,_offset), _len)
}

// Returns the length of the bytecode.
Expand Down Expand Up @@ -3941,15 +3955,19 @@ 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))
if iszero(iszero(_getRawCodeHash(addr))) {
pop(_fetchDeployedCodeWithDest(addr, offset, len,add(dest,MEM_OFFSET_INNER())))
}

}

function performCreate(evmGas,oldSp,isStatic) -> evmGasLeft, sp {
Expand Down

0 comments on commit 45d2c6d

Please sign in to comment.