Skip to content

Commit

Permalink
Handle calldata-related opcodes in constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
0xVolosnikov committed Dec 20, 2024
1 parent 0419d3e commit 707d817
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 21 deletions.
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)
}
}

0 comments on commit 707d817

Please sign in to comment.