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 stack overflow check #1062

Open
wants to merge 1 commit into
base: stable/evm-emulator
Choose a base branch
from
Open
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
28 changes: 12 additions & 16 deletions system-contracts/contracts/EvmEmulator.yul
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,12 @@ object "EvmEmulator" {
offset := add(LAST_RETURNDATA_SIZE_OFFSET(), 64)
}

function MAX_STACK_SLOT_OFFSET() -> offset {
offset := add(STACK_OFFSET(), mul(1023, 32))
}

function BYTECODE_LEN_OFFSET() -> offset {
offset := add(STACK_OFFSET(), mul(1024, 32))
offset := add(MAX_STACK_SLOT_OFFSET(), 32)
}

function BYTECODE_OFFSET() -> offset {
Expand Down Expand Up @@ -514,7 +518,7 @@ object "EvmEmulator" {
}

function pushStackItem(sp, item, oldStackHead) -> newSp, stackHead {
if iszero(lt(sp, BYTECODE_LEN_OFFSET())) {
if iszero(lt(sp, MAX_STACK_SLOT_OFFSET())) {
panic()
}

Expand All @@ -541,12 +545,6 @@ object "EvmEmulator" {
}
}

function pushStackCheck(sp, numInputs) {
if iszero(lt(add(sp, mul(0x20, sub(numInputs, 1))), BYTECODE_LEN_OFFSET())) {
panic()
}
}

function accessStackHead(sp, stackHead) -> value {
if lt(sp, STACK_OFFSET()) {
panic()
Expand Down Expand Up @@ -3180,8 +3178,12 @@ object "EvmEmulator" {
offset := add(LAST_RETURNDATA_SIZE_OFFSET(), 64)
}

function MAX_STACK_SLOT_OFFSET() -> offset {
offset := add(STACK_OFFSET(), mul(1023, 32))
}

function BYTECODE_LEN_OFFSET() -> offset {
offset := add(STACK_OFFSET(), mul(1024, 32))
offset := add(MAX_STACK_SLOT_OFFSET(), 32)
}

function BYTECODE_OFFSET() -> offset {
Expand Down Expand Up @@ -3562,7 +3564,7 @@ object "EvmEmulator" {
}

function pushStackItem(sp, item, oldStackHead) -> newSp, stackHead {
if iszero(lt(sp, BYTECODE_LEN_OFFSET())) {
if iszero(lt(sp, MAX_STACK_SLOT_OFFSET())) {
panic()
}

Expand All @@ -3589,12 +3591,6 @@ object "EvmEmulator" {
}
}

function pushStackCheck(sp, numInputs) {
if iszero(lt(add(sp, mul(0x20, sub(numInputs, 1))), BYTECODE_LEN_OFFSET())) {
panic()
}
}

function accessStackHead(sp, stackHead) -> value {
if lt(sp, STACK_OFFSET()) {
panic()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,12 @@ function STACK_OFFSET() -> offset {
offset := add(LAST_RETURNDATA_SIZE_OFFSET(), 64)
}

function MAX_STACK_SLOT_OFFSET() -> offset {
offset := add(STACK_OFFSET(), mul(1023, 32))
}

function BYTECODE_LEN_OFFSET() -> offset {
offset := add(STACK_OFFSET(), mul(1024, 32))
offset := add(MAX_STACK_SLOT_OFFSET(), 32)
}

function BYTECODE_OFFSET() -> offset {
Expand Down Expand Up @@ -452,7 +456,7 @@ function popStackItem(sp, oldStackHead) -> a, newSp, stackHead {
}

function pushStackItem(sp, item, oldStackHead) -> newSp, stackHead {
if iszero(lt(sp, BYTECODE_LEN_OFFSET())) {
if iszero(lt(sp, MAX_STACK_SLOT_OFFSET())) {
panic()
}

Expand All @@ -479,12 +483,6 @@ function popStackCheck(sp, numInputs) {
}
}

function pushStackCheck(sp, numInputs) {
if iszero(lt(add(sp, mul(0x20, sub(numInputs, 1))), BYTECODE_LEN_OFFSET())) {
panic()
}
}

function accessStackHead(sp, stackHead) -> value {
if lt(sp, STACK_OFFSET()) {
panic()
Expand Down
Loading