Skip to content

Commit

Permalink
Fix stack overflow check
Browse files Browse the repository at this point in the history
  • Loading branch information
0xVolosnikov committed Nov 13, 2024
1 parent 397092d commit e2c568d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
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
14 changes: 6 additions & 8 deletions system-contracts/evm-emulator/EvmEmulatorFunctions.template.yul
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

0 comments on commit e2c568d

Please sign in to comment.