Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(EVM): Handle calldata-related opcodes in constructor #1160

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 33 additions & 14 deletions system-contracts/contracts/EvmEmulator.yul
Original file line number Diff line number Diff line change
Expand Up @@ -1309,6 +1309,18 @@ object "EvmEmulator" {
}
}

function $llvm_AlwaysInline_llvm$_calldatasize() -> size {
size := 0
}

function $llvm_AlwaysInline_llvm$_calldatacopy(dstOffset, sourceOffset, truncatedLen) {
$llvm_AlwaysInline_llvm$_memsetToZero(dstOffset, truncatedLen)
}

function $llvm_AlwaysInline_llvm$_calldataload(calldataOffset) -> res {
res := 0
}

function simulate(
isCallerEVM,
evmGasLeft,
Expand Down Expand Up @@ -1667,18 +1679,14 @@ object "EvmEmulator" {

let calldataOffset := accessStackHead(sp, stackHead)

stackHead := 0
// EraVM will revert if offset + length overflows uint32
if lt(calldataOffset, UINT32_MAX()) {
stackHead := calldataload(calldataOffset)
}
stackHead := $llvm_AlwaysInline_llvm$_calldataload(calldataOffset)

ip := add(ip, 1)
}
case 0x36 { // OP_CALLDATASIZE
evmGasLeft := chargeGas(evmGasLeft, 2)

sp, stackHead := pushStackItem(sp, calldatasize(), stackHead)
sp, stackHead := pushStackItem(sp, $llvm_AlwaysInline_llvm$_calldatasize(), stackHead)
ip := add(ip, 1)
}
case 0x37 { // OP_CALLDATACOPY
Expand Down Expand Up @@ -1713,7 +1721,7 @@ object "EvmEmulator" {
}

if truncatedLen {
calldatacopy(dstOffset, sourceOffset, truncatedLen)
$llvm_AlwaysInline_llvm$_calldatacopy(dstOffset, sourceOffset, truncatedLen)
}

ip := add(ip, 1)
Expand Down Expand Up @@ -4805,18 +4813,14 @@ object "EvmEmulator" {

let calldataOffset := accessStackHead(sp, stackHead)

stackHead := 0
// EraVM will revert if offset + length overflows uint32
if lt(calldataOffset, UINT32_MAX()) {
stackHead := calldataload(calldataOffset)
}
stackHead := $llvm_AlwaysInline_llvm$_calldataload(calldataOffset)

ip := add(ip, 1)
}
case 0x36 { // OP_CALLDATASIZE
evmGasLeft := chargeGas(evmGasLeft, 2)

sp, stackHead := pushStackItem(sp, calldatasize(), stackHead)
sp, stackHead := pushStackItem(sp, $llvm_AlwaysInline_llvm$_calldatasize(), stackHead)
ip := add(ip, 1)
}
case 0x37 { // OP_CALLDATACOPY
Expand Down Expand Up @@ -4851,7 +4855,7 @@ object "EvmEmulator" {
}

if truncatedLen {
calldatacopy(dstOffset, sourceOffset, truncatedLen)
$llvm_AlwaysInline_llvm$_calldatacopy(dstOffset, sourceOffset, truncatedLen)
}

ip := add(ip, 1)
Expand Down Expand Up @@ -6284,6 +6288,21 @@ object "EvmEmulator" {
}


function $llvm_AlwaysInline_llvm$_calldatasize() -> size {
size := calldatasize()
}

function $llvm_AlwaysInline_llvm$_calldatacopy(dstOffset, sourceOffset, truncatedLen) {
calldatacopy(dstOffset, sourceOffset, truncatedLen)
}

function $llvm_AlwaysInline_llvm$_calldataload(calldataOffset) -> res {
// EraVM will revert if offset + length overflows uint32
if lt(calldataOffset, UINT32_MAX()) {
res := calldataload(calldataOffset)
}
}

if eq(isCallerEVM, 1) {
// Includes gas
returnOffset := sub(returnOffset, 32)
Expand Down
4 changes: 4 additions & 0 deletions system-contracts/evm-emulator/EvmEmulator.template.yul
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ object "EvmEmulator" {

<!-- @include EvmEmulatorFunctions.template.yul -->

<!-- @include calldata-opcodes/ConstructorScope.template.yul -->

function simulate(
isCallerEVM,
evmGasLeft,
Expand Down Expand Up @@ -134,6 +136,8 @@ object "EvmEmulator" {

<!-- @include EvmEmulatorLoop.template.yul -->

<!-- @include calldata-opcodes/RuntimeScope.template.yul -->

if eq(isCallerEVM, 1) {
// Includes gas
returnOffset := sub(returnOffset, 32)
Expand Down
10 changes: 3 additions & 7 deletions system-contracts/evm-emulator/EvmEmulatorLoop.template.yul
Original file line number Diff line number Diff line change
Expand Up @@ -347,18 +347,14 @@ for { } true { } {

let calldataOffset := accessStackHead(sp, stackHead)

stackHead := 0
// EraVM will revert if offset + length overflows uint32
if lt(calldataOffset, UINT32_MAX()) {
stackHead := calldataload(calldataOffset)
}
stackHead := $llvm_AlwaysInline_llvm$_calldataload(calldataOffset)

ip := add(ip, 1)
}
case 0x36 { // OP_CALLDATASIZE
evmGasLeft := chargeGas(evmGasLeft, 2)

sp, stackHead := pushStackItem(sp, calldatasize(), stackHead)
sp, stackHead := pushStackItem(sp, $llvm_AlwaysInline_llvm$_calldatasize(), stackHead)
ip := add(ip, 1)
}
case 0x37 { // OP_CALLDATACOPY
Expand Down Expand Up @@ -393,7 +389,7 @@ for { } true { } {
}

if truncatedLen {
calldatacopy(dstOffset, sourceOffset, truncatedLen)
$llvm_AlwaysInline_llvm$_calldatacopy(dstOffset, sourceOffset, truncatedLen)
}

ip := add(ip, 1)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function $llvm_AlwaysInline_llvm$_calldatasize() -> size {
size := 0
}

function $llvm_AlwaysInline_llvm$_calldatacopy(dstOffset, sourceOffset, truncatedLen) {
$llvm_AlwaysInline_llvm$_memsetToZero(dstOffset, truncatedLen)
}

function $llvm_AlwaysInline_llvm$_calldataload(calldataOffset) -> res {
res := 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function $llvm_AlwaysInline_llvm$_calldatasize() -> size {
size := calldatasize()
}

function $llvm_AlwaysInline_llvm$_calldatacopy(dstOffset, sourceOffset, truncatedLen) {
calldatacopy(dstOffset, sourceOffset, truncatedLen)
}

function $llvm_AlwaysInline_llvm$_calldataload(calldataOffset) -> res {
// EraVM will revert if offset + length overflows uint32
if lt(calldataOffset, UINT32_MAX()) {
res := calldataload(calldataOffset)
}
}
Loading