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): Add stack overflow check in DUP opcodes #1061

Draft
wants to merge 4 commits into
base: stable/evm-emulator
Choose a base branch
from
Draft
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
38 changes: 22 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 @@ -479,6 +483,11 @@ object "EvmEmulator" {

function dupStackItem(sp, evmGas, position, oldStackHead) -> newSp, evmGasLeft, stackHead {
evmGasLeft := chargeGas(evmGas, 3)

if iszero(lt(sp, MAX_STACK_SLOT_OFFSET())) {
panic()
}

let tempSp := sub(sp, mul(0x20, sub(position, 1)))

if lt(tempSp, STACK_OFFSET()) {
Expand Down Expand Up @@ -514,7 +523,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 +550,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 +3183,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 @@ -3527,6 +3534,11 @@ object "EvmEmulator" {

function dupStackItem(sp, evmGas, position, oldStackHead) -> newSp, evmGasLeft, stackHead {
evmGasLeft := chargeGas(evmGas, 3)

if iszero(lt(sp, MAX_STACK_SLOT_OFFSET())) {
panic()
}

let tempSp := sub(sp, mul(0x20, sub(position, 1)))

if lt(tempSp, STACK_OFFSET()) {
Expand Down Expand Up @@ -3562,7 +3574,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 +3601,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
19 changes: 11 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 @@ -417,6 +421,11 @@ function performSystemCallRevertable(to, dataLength) -> success {

function dupStackItem(sp, evmGas, position, oldStackHead) -> newSp, evmGasLeft, stackHead {
evmGasLeft := chargeGas(evmGas, 3)

if iszero(lt(sp, MAX_STACK_SLOT_OFFSET())) {
panic()
}

let tempSp := sub(sp, mul(0x20, sub(position, 1)))

if lt(tempSp, STACK_OFFSET()) {
Expand Down Expand Up @@ -452,7 +461,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 +488,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