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): Fix insufficient balance panics on calls #1094

Closed
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
64 changes: 40 additions & 24 deletions system-contracts/contracts/EvmEmulator.yul
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,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 @@ -817,12 +823,19 @@ 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
_eraseReturndataPointer()
}
}
}
Expand Down Expand Up @@ -1053,12 +1066,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 @@ -3344,6 +3352,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 @@ -3865,12 +3879,19 @@ 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
_eraseReturndataPointer()
}
}
}
Expand Down Expand Up @@ -4101,12 +4122,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
32 changes: 20 additions & 12 deletions system-contracts/evm-emulator/EvmEmulatorFunctions.template.yul
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,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 @@ -755,12 +761,19 @@ 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
_eraseReturndataPointer()
}
}
}
Expand Down Expand Up @@ -991,12 +1004,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
Loading