Skip to content

Commit

Permalink
Simplify panic returndatasize logic
Browse files Browse the repository at this point in the history
  • Loading branch information
0xVolosnikov committed Jan 3, 2025
1 parent 0397674 commit d8b49e5
Show file tree
Hide file tree
Showing 2 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 @@ -86,12 +86,12 @@ object "EvmEmulator" {
addr := 0x0000000000000000000000000000000000008009
}

function IS_CALLER_EVM_OFFSET() -> offset {
function PANIC_RETURNDATASIZE_OFFSET() -> offset {
offset := mul(23, 32)
}

function ORIGIN_CACHE_OFFSET() -> offset {
offset := add(IS_CALLER_EVM_OFFSET(), 32)
offset := add(PANIC_RETURNDATASIZE_OFFSET(), 32)
}

function GASPRICE_CACHE_OFFSET() -> offset {
Expand Down Expand Up @@ -212,12 +212,10 @@ object "EvmEmulator" {
}

function panic() { // revert consuming all EVM gas
if mload(IS_CALLER_EVM_OFFSET()) {
mstore(0, 0)
revert(0, 32)
}

revert(0, 0)
// we return empty 32 bytes encoding 0 gas left if caller is EVM, and 0 bytes if caller isn't EVM
// it is done without if-else block so this function will be inlined
mstore(0, 0)
revert(0, mload(PANIC_RETURNDATASIZE_OFFSET()))
}

function cached(cacheIndex, value) -> _value {
Expand Down Expand Up @@ -659,7 +657,7 @@ object "EvmEmulator" {
let _returndatasize := returndatasize()
if _returndatasize {
callerEVM := true
mstore(IS_CALLER_EVM_OFFSET(), true)
mstore(PANIC_RETURNDATASIZE_OFFSET(), 32) // we should return 0 gas after panics

returndatacopy(0, 0, 32)
passGas := mload(0)
Expand Down Expand Up @@ -3010,12 +3008,12 @@ object "EvmEmulator" {
addr := 0x0000000000000000000000000000000000008009
}

function IS_CALLER_EVM_OFFSET() -> offset {
function PANIC_RETURNDATASIZE_OFFSET() -> offset {
offset := mul(23, 32)
}

function ORIGIN_CACHE_OFFSET() -> offset {
offset := add(IS_CALLER_EVM_OFFSET(), 32)
offset := add(PANIC_RETURNDATASIZE_OFFSET(), 32)
}

function GASPRICE_CACHE_OFFSET() -> offset {
Expand Down Expand Up @@ -3136,12 +3134,10 @@ object "EvmEmulator" {
}

function panic() { // revert consuming all EVM gas
if mload(IS_CALLER_EVM_OFFSET()) {
mstore(0, 0)
revert(0, 32)
}

revert(0, 0)
// we return empty 32 bytes encoding 0 gas left if caller is EVM, and 0 bytes if caller isn't EVM
// it is done without if-else block so this function will be inlined
mstore(0, 0)
revert(0, mload(PANIC_RETURNDATASIZE_OFFSET()))
}

function cached(cacheIndex, value) -> _value {
Expand Down Expand Up @@ -3583,7 +3579,7 @@ object "EvmEmulator" {
let _returndatasize := returndatasize()
if _returndatasize {
callerEVM := true
mstore(IS_CALLER_EVM_OFFSET(), true)
mstore(PANIC_RETURNDATASIZE_OFFSET(), 32) // we should return 0 gas after panics

returndatacopy(0, 0, 32)
passGas := mload(0)
Expand Down
16 changes: 7 additions & 9 deletions system-contracts/evm-emulator/EvmEmulatorFunctions.template.yul
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ function MSG_VALUE_SYSTEM_CONTRACT() -> addr {
addr := 0x0000000000000000000000000000000000008009
}

function IS_CALLER_EVM_OFFSET() -> offset {
function PANIC_RETURNDATASIZE_OFFSET() -> offset {
offset := mul(23, 32)
}

function ORIGIN_CACHE_OFFSET() -> offset {
offset := add(IS_CALLER_EVM_OFFSET(), 32)
offset := add(PANIC_RETURNDATASIZE_OFFSET(), 32)
}

function GASPRICE_CACHE_OFFSET() -> offset {
Expand Down Expand Up @@ -152,12 +152,10 @@ function $llvm_NoInline_llvm$_invalid() { // revert consuming all EVM gas
}

function panic() { // revert consuming all EVM gas
if mload(IS_CALLER_EVM_OFFSET()) {
mstore(0, 0)
revert(0, 32)
}

revert(0, 0)
// we return empty 32 bytes encoding 0 gas left if caller is EVM, and 0 bytes if caller isn't EVM
// it is done without if-else block so this function will be inlined
mstore(0, 0)
revert(0, mload(PANIC_RETURNDATASIZE_OFFSET()))
}

function cached(cacheIndex, value) -> _value {
Expand Down Expand Up @@ -599,7 +597,7 @@ function consumeEvmFrame() -> passGas, isStatic, callerEVM {
let _returndatasize := returndatasize()
if _returndatasize {
callerEVM := true
mstore(IS_CALLER_EVM_OFFSET(), true)
mstore(PANIC_RETURNDATASIZE_OFFSET(), 32) // we should return 0 gas after panics

returndatacopy(0, 0, 32)
passGas := mload(0)
Expand Down

0 comments on commit d8b49e5

Please sign in to comment.