From 1b2b0f9fb527817600432cd950ef180fcdb7294b Mon Sep 17 00:00:00 2001 From: Vladislav Volosnikov Date: Thu, 15 Aug 2024 19:51:36 +0200 Subject: [PATCH] Check offset overflow earlier in memory opcodes --- .../contracts/EvmInterpreterLoop.template.yul | 6 +++--- .../contracts/EvmInterpreterPreprocessed.yul | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/system-contracts/contracts/EvmInterpreterLoop.template.yul b/system-contracts/contracts/EvmInterpreterLoop.template.yul index 931ede4c5..5448f156a 100644 --- a/system-contracts/contracts/EvmInterpreterLoop.template.yul +++ b/system-contracts/contracts/EvmInterpreterLoop.template.yul @@ -619,11 +619,11 @@ for { } true { } { offset, sp := popStackItem(sp, evmGasLeft) + checkOverflow(offset, MEM_OFFSET_INNER(), evmGasLeft) checkMemOverflow(add(offset, MEM_OFFSET_INNER()), evmGasLeft) let expansionGas := expandMemory(add(offset, 32)) evmGasLeft := chargeGas(evmGasLeft, expansionGas) - checkOverflow(offset,MEM_OFFSET_INNER(), evmGasLeft) let memValue := mload(add(MEM_OFFSET_INNER(), offset)) sp := pushStackItem(sp, memValue, evmGasLeft) ip := add(ip, 1) @@ -637,11 +637,11 @@ for { } true { } { offset, sp := popStackItemWithoutCheck(sp) value, sp := popStackItemWithoutCheck(sp) + checkOverflow(offset, MEM_OFFSET_INNER(), evmGasLeft) checkMemOverflow(add(offset, MEM_OFFSET_INNER()), evmGasLeft) let expansionGas := expandMemory(add(offset, 32)) evmGasLeft := chargeGas(evmGasLeft, expansionGas) - checkOverflow(offset,MEM_OFFSET_INNER(), evmGasLeft) mstore(add(MEM_OFFSET_INNER(), offset), value) ip := add(ip, 1) } @@ -654,11 +654,11 @@ for { } true { } { offset, sp := popStackItemWithoutCheck(sp) value, sp := popStackItemWithoutCheck(sp) + checkOverflow(offset, MEM_OFFSET_INNER(), evmGasLeft) checkMemOverflow(add(offset, MEM_OFFSET_INNER()), evmGasLeft) let expansionGas := expandMemory(add(offset, 1)) evmGasLeft := chargeGas(evmGasLeft, expansionGas) - checkOverflow(offset,MEM_OFFSET_INNER(), evmGasLeft) mstore8(add(MEM_OFFSET_INNER(), offset), value) ip := add(ip, 1) } diff --git a/system-contracts/contracts/EvmInterpreterPreprocessed.yul b/system-contracts/contracts/EvmInterpreterPreprocessed.yul index 57f7753e1..eedb9665c 100644 --- a/system-contracts/contracts/EvmInterpreterPreprocessed.yul +++ b/system-contracts/contracts/EvmInterpreterPreprocessed.yul @@ -2167,11 +2167,11 @@ object "EVMInterpreter" { offset, sp := popStackItem(sp, evmGasLeft) + checkOverflow(offset, MEM_OFFSET_INNER(), evmGasLeft) checkMemOverflow(add(offset, MEM_OFFSET_INNER()), evmGasLeft) let expansionGas := expandMemory(add(offset, 32)) evmGasLeft := chargeGas(evmGasLeft, expansionGas) - checkOverflow(offset,MEM_OFFSET_INNER(), evmGasLeft) let memValue := mload(add(MEM_OFFSET_INNER(), offset)) sp := pushStackItem(sp, memValue, evmGasLeft) ip := add(ip, 1) @@ -2185,11 +2185,11 @@ object "EVMInterpreter" { offset, sp := popStackItemWithoutCheck(sp) value, sp := popStackItemWithoutCheck(sp) + checkOverflow(offset, MEM_OFFSET_INNER(), evmGasLeft) checkMemOverflow(add(offset, MEM_OFFSET_INNER()), evmGasLeft) let expansionGas := expandMemory(add(offset, 32)) evmGasLeft := chargeGas(evmGasLeft, expansionGas) - checkOverflow(offset,MEM_OFFSET_INNER(), evmGasLeft) mstore(add(MEM_OFFSET_INNER(), offset), value) ip := add(ip, 1) } @@ -2202,11 +2202,11 @@ object "EVMInterpreter" { offset, sp := popStackItemWithoutCheck(sp) value, sp := popStackItemWithoutCheck(sp) + checkOverflow(offset, MEM_OFFSET_INNER(), evmGasLeft) checkMemOverflow(add(offset, MEM_OFFSET_INNER()), evmGasLeft) let expansionGas := expandMemory(add(offset, 1)) evmGasLeft := chargeGas(evmGasLeft, expansionGas) - checkOverflow(offset,MEM_OFFSET_INNER(), evmGasLeft) mstore8(add(MEM_OFFSET_INNER(), offset), value) ip := add(ip, 1) } @@ -5163,11 +5163,11 @@ object "EVMInterpreter" { offset, sp := popStackItem(sp, evmGasLeft) + checkOverflow(offset, MEM_OFFSET_INNER(), evmGasLeft) checkMemOverflow(add(offset, MEM_OFFSET_INNER()), evmGasLeft) let expansionGas := expandMemory(add(offset, 32)) evmGasLeft := chargeGas(evmGasLeft, expansionGas) - checkOverflow(offset,MEM_OFFSET_INNER(), evmGasLeft) let memValue := mload(add(MEM_OFFSET_INNER(), offset)) sp := pushStackItem(sp, memValue, evmGasLeft) ip := add(ip, 1) @@ -5181,11 +5181,11 @@ object "EVMInterpreter" { offset, sp := popStackItemWithoutCheck(sp) value, sp := popStackItemWithoutCheck(sp) + checkOverflow(offset, MEM_OFFSET_INNER(), evmGasLeft) checkMemOverflow(add(offset, MEM_OFFSET_INNER()), evmGasLeft) let expansionGas := expandMemory(add(offset, 32)) evmGasLeft := chargeGas(evmGasLeft, expansionGas) - checkOverflow(offset,MEM_OFFSET_INNER(), evmGasLeft) mstore(add(MEM_OFFSET_INNER(), offset), value) ip := add(ip, 1) } @@ -5198,11 +5198,11 @@ object "EVMInterpreter" { offset, sp := popStackItemWithoutCheck(sp) value, sp := popStackItemWithoutCheck(sp) + checkOverflow(offset, MEM_OFFSET_INNER(), evmGasLeft) checkMemOverflow(add(offset, MEM_OFFSET_INNER()), evmGasLeft) let expansionGas := expandMemory(add(offset, 1)) evmGasLeft := chargeGas(evmGasLeft, expansionGas) - checkOverflow(offset,MEM_OFFSET_INNER(), evmGasLeft) mstore8(add(MEM_OFFSET_INNER(), offset), value) ip := add(ip, 1) }