Skip to content

Commit

Permalink
fix(EVM): Fix insufficient balance panics (#1095)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xVolosnikov authored Nov 27, 2024
1 parent 6c5b712 commit 782446b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 36 deletions.
62 changes: 38 additions & 24 deletions system-contracts/contracts/EvmEmulator.yul
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,12 @@ object "EvmEmulator" {
}
}

function insufficientBalance(value) -> res {
if value {
res := gt(value, selfbalance())
}
}

// It is the responsibility of the caller to ensure that ip is correct
function readIP(ip, bytecodeEndOffset) -> opcode {
if lt(ip, bytecodeEndOffset) {
Expand Down Expand Up @@ -825,12 +831,18 @@ object "EvmEmulator" {
}
}
default {
pushEvmFrame(gasToPass, isStatic)
// pass all remaining native gas
success := call(gas(), addr, value, argsOffset, argsSize, 0, 0)
frameGasLeft := _saveReturndataAfterEVMCall(retOffset, retSize)
if iszero(success) {
resetEvmFrame()
switch insufficientBalance(value)
case 0 {
pushEvmFrame(gasToPass, isStatic)
// pass all remaining native gas
success := call(gas(), addr, value, argsOffset, argsSize, 0, 0)
frameGasLeft := _saveReturndataAfterEVMCall(retOffset, retSize)
if iszero(success) {
resetEvmFrame()
}
}
default {
frameGasLeft := gasToPass
}
}
}
Expand Down Expand Up @@ -1041,12 +1053,7 @@ object "EvmEmulator" {

_eraseReturndataPointer()

let err := 0
if value {
if gt(value, selfbalance()) {
err := 1
}
}
let err := insufficientBalance(value)

if iszero(err) {
offset := add(MEM_OFFSET(), offset) // caller must ensure that it doesn't overflow
Expand Down Expand Up @@ -3325,6 +3332,12 @@ object "EvmEmulator" {
}
}

function insufficientBalance(value) -> res {
if value {
res := gt(value, selfbalance())
}
}

// It is the responsibility of the caller to ensure that ip is correct
function readIP(ip, bytecodeEndOffset) -> opcode {
if lt(ip, bytecodeEndOffset) {
Expand Down Expand Up @@ -3857,12 +3870,18 @@ object "EvmEmulator" {
}
}
default {
pushEvmFrame(gasToPass, isStatic)
// pass all remaining native gas
success := call(gas(), addr, value, argsOffset, argsSize, 0, 0)
frameGasLeft := _saveReturndataAfterEVMCall(retOffset, retSize)
if iszero(success) {
resetEvmFrame()
switch insufficientBalance(value)
case 0 {
pushEvmFrame(gasToPass, isStatic)
// pass all remaining native gas
success := call(gas(), addr, value, argsOffset, argsSize, 0, 0)
frameGasLeft := _saveReturndataAfterEVMCall(retOffset, retSize)
if iszero(success) {
resetEvmFrame()
}
}
default {
frameGasLeft := gasToPass
}
}
}
Expand Down Expand Up @@ -4073,12 +4092,7 @@ object "EvmEmulator" {

_eraseReturndataPointer()

let err := 0
if value {
if gt(value, selfbalance()) {
err := 1
}
}
let err := insufficientBalance(value)

if iszero(err) {
offset := add(MEM_OFFSET(), offset) // caller must ensure that it doesn't overflow
Expand Down
31 changes: 19 additions & 12 deletions system-contracts/evm-emulator/EvmEmulatorFunctions.template.yul
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,12 @@ function checkOverflow(data1, data2) {
}
}

function insufficientBalance(value) -> res {
if value {
res := gt(value, selfbalance())
}
}

// It is the responsibility of the caller to ensure that ip is correct
function readIP(ip, bytecodeEndOffset) -> opcode {
if lt(ip, bytecodeEndOffset) {
Expand Down Expand Up @@ -763,12 +769,18 @@ function _genericCall(addr, gasToPass, value, argsOffset, argsSize, retOffset, r
}
}
default {
pushEvmFrame(gasToPass, isStatic)
// pass all remaining native gas
success := call(gas(), addr, value, argsOffset, argsSize, 0, 0)
frameGasLeft := _saveReturndataAfterEVMCall(retOffset, retSize)
if iszero(success) {
resetEvmFrame()
switch insufficientBalance(value)
case 0 {
pushEvmFrame(gasToPass, isStatic)
// pass all remaining native gas
success := call(gas(), addr, value, argsOffset, argsSize, 0, 0)
frameGasLeft := _saveReturndataAfterEVMCall(retOffset, retSize)
if iszero(success) {
resetEvmFrame()
}
}
default {
frameGasLeft := gasToPass
}
}
}
Expand Down Expand Up @@ -979,12 +991,7 @@ function $llvm_NoInline_llvm$_genericCreate(offset, size, value, evmGasLeftOld,

_eraseReturndataPointer()

let err := 0
if value {
if gt(value, selfbalance()) {
err := 1
}
}
let err := insufficientBalance(value)

if iszero(err) {
offset := add(MEM_OFFSET(), offset) // caller must ensure that it doesn't overflow
Expand Down

0 comments on commit 782446b

Please sign in to comment.