Skip to content

Commit

Permalink
fix(EVM): Fix CALL in static context (#1091)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xVolosnikov authored Nov 27, 2024
1 parent fcb2e2b commit 871d1fe
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 27 deletions.
32 changes: 14 additions & 18 deletions system-contracts/contracts/EvmEmulator.yul
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ object "EvmEmulator" {
// CALLS FUNCTIONALITY
////////////////////////////////////////////////////////////////

function performCall(oldSp, evmGasLeft, oldStackHead) -> newGasLeft, sp, stackHead {
function performCall(oldSp, evmGasLeft, oldStackHead, isStatic) -> newGasLeft, sp, stackHead {
let gasToPass, addr, value, argsOffset, argsSize, retOffset, retSize

popStackCheck(oldSp, 7)
Expand Down Expand Up @@ -673,6 +673,10 @@ object "EvmEmulator" {
gasUsed := add(gasUsed, expandMemory2(retOffset, retSize, argsOffset, argsSize))

if gt(value, 0) {
if isStatic {
panic()
}

gasUsed := add(gasUsed, 9000) // positive_value_cost

if isAddrEmpty(addr) {
Expand All @@ -696,7 +700,7 @@ object "EvmEmulator" {
argsSize,
add(retOffset, MEM_OFFSET()),
retSize,
false
isStatic
)

newGasLeft := add(evmGasLeft, frameGasLeft)
Expand Down Expand Up @@ -2627,13 +2631,7 @@ object "EvmEmulator" {
}
case 0xF1 { // OP_CALL
// A function was implemented in order to avoid stack depth errors.
switch isStatic
case 0 {
evmGasLeft, sp, stackHead := performCall(sp, evmGasLeft, stackHead)
}
default {
evmGasLeft, sp, stackHead := performStaticCall(sp, evmGasLeft, stackHead)
}
evmGasLeft, sp, stackHead := performCall(sp, evmGasLeft, stackHead, isStatic)
ip := add(ip, 1)
}
case 0xF3 { // OP_RETURN
Expand Down Expand Up @@ -3668,7 +3666,7 @@ object "EvmEmulator" {
// CALLS FUNCTIONALITY
////////////////////////////////////////////////////////////////

function performCall(oldSp, evmGasLeft, oldStackHead) -> newGasLeft, sp, stackHead {
function performCall(oldSp, evmGasLeft, oldStackHead, isStatic) -> newGasLeft, sp, stackHead {
let gasToPass, addr, value, argsOffset, argsSize, retOffset, retSize

popStackCheck(oldSp, 7)
Expand Down Expand Up @@ -3700,6 +3698,10 @@ object "EvmEmulator" {
gasUsed := add(gasUsed, expandMemory2(retOffset, retSize, argsOffset, argsSize))

if gt(value, 0) {
if isStatic {
panic()
}

gasUsed := add(gasUsed, 9000) // positive_value_cost

if isAddrEmpty(addr) {
Expand All @@ -3723,7 +3725,7 @@ object "EvmEmulator" {
argsSize,
add(retOffset, MEM_OFFSET()),
retSize,
false
isStatic
)

newGasLeft := add(evmGasLeft, frameGasLeft)
Expand Down Expand Up @@ -5654,13 +5656,7 @@ object "EvmEmulator" {
}
case 0xF1 { // OP_CALL
// A function was implemented in order to avoid stack depth errors.
switch isStatic
case 0 {
evmGasLeft, sp, stackHead := performCall(sp, evmGasLeft, stackHead)
}
default {
evmGasLeft, sp, stackHead := performStaticCall(sp, evmGasLeft, stackHead)
}
evmGasLeft, sp, stackHead := performCall(sp, evmGasLeft, stackHead, isStatic)
ip := add(ip, 1)
}
case 0xF3 { // OP_RETURN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ function resetEvmFrame() {
// CALLS FUNCTIONALITY
////////////////////////////////////////////////////////////////

function performCall(oldSp, evmGasLeft, oldStackHead) -> newGasLeft, sp, stackHead {
function performCall(oldSp, evmGasLeft, oldStackHead, isStatic) -> newGasLeft, sp, stackHead {
let gasToPass, addr, value, argsOffset, argsSize, retOffset, retSize

popStackCheck(oldSp, 7)
Expand Down Expand Up @@ -611,6 +611,10 @@ function performCall(oldSp, evmGasLeft, oldStackHead) -> newGasLeft, sp, stackHe
gasUsed := add(gasUsed, expandMemory2(retOffset, retSize, argsOffset, argsSize))

if gt(value, 0) {
if isStatic {
panic()
}

gasUsed := add(gasUsed, 9000) // positive_value_cost

if isAddrEmpty(addr) {
Expand All @@ -634,7 +638,7 @@ function performCall(oldSp, evmGasLeft, oldStackHead) -> newGasLeft, sp, stackHe
argsSize,
add(retOffset, MEM_OFFSET()),
retSize,
false
isStatic
)

newGasLeft := add(evmGasLeft, frameGasLeft)
Expand Down
8 changes: 1 addition & 7 deletions system-contracts/evm-emulator/EvmEmulatorLoop.template.yul
Original file line number Diff line number Diff line change
Expand Up @@ -1425,13 +1425,7 @@ for { } true { } {
}
case 0xF1 { // OP_CALL
// A function was implemented in order to avoid stack depth errors.
switch isStatic
case 0 {
evmGasLeft, sp, stackHead := performCall(sp, evmGasLeft, stackHead)
}
default {
evmGasLeft, sp, stackHead := performStaticCall(sp, evmGasLeft, stackHead)
}
evmGasLeft, sp, stackHead := performCall(sp, evmGasLeft, stackHead, isStatic)
ip := add(ip, 1)
}
case 0xF3 { // OP_RETURN
Expand Down

0 comments on commit 871d1fe

Please sign in to comment.