Skip to content

Commit

Permalink
Remove invalid todos adding overflow checks
Browse files Browse the repository at this point in the history
  • Loading branch information
IAvecilla committed Sep 10, 2024
1 parent fe5d5f5 commit bbac2a0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 45 deletions.
48 changes: 18 additions & 30 deletions system-contracts/contracts/EvmInterpreter.yul
Original file line number Diff line number Diff line change
Expand Up @@ -741,12 +741,6 @@ object "EVMInterpreter" {
ret := farCallAbi
}

function ensureAcceptableMemLocation(location) {
if gt(location,MAX_POSSIBLE_MEM()) {
revert(0,0) // Check if this is what's needed
}
}

function addGasIfEvmRevert(isCallerEVM,offset,size,evmGasLeft) -> newOffset,newSize {
newOffset := offset
newSize := size
Expand Down Expand Up @@ -1970,13 +1964,8 @@ object "EVMInterpreter" {
offset, sp := popStackItemWithoutCheck(sp)
size, sp := popStackItemWithoutCheck(sp)

checkMultipleOverflow(offset,size,MEM_OFFSET_INNER(), evmGasLeft)
checkMultipleOverflow(destOffset,size,MEM_OFFSET_INNER(), evmGasLeft)

// TODO invalid?
if or(gt(add(add(offset, size), MEM_OFFSET_INNER()), MAX_POSSIBLE_MEM()), gt(add(add(destOffset, size), MEM_OFFSET_INNER()), MAX_POSSIBLE_MEM())) {
$llvm_AlwaysInline_llvm$_memsetToZero(add(destOffset, MEM_OFFSET_INNER()), size)
}
checkOverflow(destOffset, size, evmGasLeft)
checkMemOverflowByOffset(add(destOffset,size), evmGasLeft)

// dynamicGas = 3 * minimum_word_size + memory_expansion_cost
// minimum_word_size = (size + 31) / 32
Expand Down Expand Up @@ -2014,6 +2003,7 @@ object "EVMInterpreter" {
offset := add(add(offset, BYTECODE_OFFSET()), 32)

checkOverflow(dst,len, evmGasLeft)
checkOverflow(offset,len, evmGasLeft)
checkMemOverflow(add(dst, len), evmGasLeft)
// Check bytecode overflow
if gt(add(offset, len), sub(MEM_OFFSET(), 1)) {
Expand Down Expand Up @@ -2370,7 +2360,8 @@ object "EVMInterpreter" {
offset, sp := popStackItemWithoutCheck(sp)
size, sp := popStackItemWithoutCheck(sp)

// TODO overflow checks
checkOverflow(offset, size, evmGasLeft)
checkOverflow(destOffset, size, evmGasLeft)
checkMemOverflowByOffset(add(offset, size), evmGasLeft)
checkMemOverflowByOffset(add(destOffset, size), evmGasLeft)

Expand Down Expand Up @@ -2963,6 +2954,7 @@ object "EVMInterpreter" {
size, sp := popStackItemWithoutCheck(sp)

checkOverflow(offset,size, evmGasLeft)
checkMemOverflowByOffset(add(offset,size), evmGasLeft)
evmGasLeft := chargeGas(evmGasLeft,expandMemory(add(offset,size)))

returnLen := size
Expand Down Expand Up @@ -3002,11 +2994,13 @@ object "EVMInterpreter" {
offset, sp := popStackItemWithoutCheck(sp)
size, sp := popStackItemWithoutCheck(sp)

// TODO invalid?
ensureAcceptableMemLocation(offset)
ensureAcceptableMemLocation(size)
checkOverflow(offset,size, evmGasLeft)
checkMemOverflowByOffset(add(offset, size), evmGasLeft)
evmGasLeft := chargeGas(evmGasLeft,expandMemory(add(offset,size)))

checkOverflow(offset,MEM_OFFSET_INNER(), evmGasLeft)
offset := add(offset, MEM_OFFSET_INNER())
offset,size := addGasIfEvmRevert(isCallerEVM,offset,size,evmGasLeft)

Expand Down Expand Up @@ -3715,12 +3709,6 @@ object "EVMInterpreter" {
ret := farCallAbi
}

function ensureAcceptableMemLocation(location) {
if gt(location,MAX_POSSIBLE_MEM()) {
revert(0,0) // Check if this is what's needed
}
}

function addGasIfEvmRevert(isCallerEVM,offset,size,evmGasLeft) -> newOffset,newSize {
newOffset := offset
newSize := size
Expand Down Expand Up @@ -4944,13 +4932,8 @@ object "EVMInterpreter" {
offset, sp := popStackItemWithoutCheck(sp)
size, sp := popStackItemWithoutCheck(sp)

checkMultipleOverflow(offset,size,MEM_OFFSET_INNER(), evmGasLeft)
checkMultipleOverflow(destOffset,size,MEM_OFFSET_INNER(), evmGasLeft)

// TODO invalid?
if or(gt(add(add(offset, size), MEM_OFFSET_INNER()), MAX_POSSIBLE_MEM()), gt(add(add(destOffset, size), MEM_OFFSET_INNER()), MAX_POSSIBLE_MEM())) {
$llvm_AlwaysInline_llvm$_memsetToZero(add(destOffset, MEM_OFFSET_INNER()), size)
}
checkOverflow(destOffset, size, evmGasLeft)
checkMemOverflowByOffset(add(destOffset,size), evmGasLeft)

// dynamicGas = 3 * minimum_word_size + memory_expansion_cost
// minimum_word_size = (size + 31) / 32
Expand Down Expand Up @@ -4988,6 +4971,7 @@ object "EVMInterpreter" {
offset := add(add(offset, BYTECODE_OFFSET()), 32)

checkOverflow(dst,len, evmGasLeft)
checkOverflow(offset,len, evmGasLeft)
checkMemOverflow(add(dst, len), evmGasLeft)
// Check bytecode overflow
if gt(add(offset, len), sub(MEM_OFFSET(), 1)) {
Expand Down Expand Up @@ -5344,7 +5328,8 @@ object "EVMInterpreter" {
offset, sp := popStackItemWithoutCheck(sp)
size, sp := popStackItemWithoutCheck(sp)

// TODO overflow checks
checkOverflow(offset, size, evmGasLeft)
checkOverflow(destOffset, size, evmGasLeft)
checkMemOverflowByOffset(add(offset, size), evmGasLeft)
checkMemOverflowByOffset(add(destOffset, size), evmGasLeft)

Expand Down Expand Up @@ -5937,6 +5922,7 @@ object "EVMInterpreter" {
size, sp := popStackItemWithoutCheck(sp)

checkOverflow(offset,size, evmGasLeft)
checkMemOverflowByOffset(add(offset,size), evmGasLeft)
evmGasLeft := chargeGas(evmGasLeft,expandMemory(add(offset,size)))

returnLen := size
Expand Down Expand Up @@ -5976,11 +5962,13 @@ object "EVMInterpreter" {
offset, sp := popStackItemWithoutCheck(sp)
size, sp := popStackItemWithoutCheck(sp)

// TODO invalid?
ensureAcceptableMemLocation(offset)
ensureAcceptableMemLocation(size)
checkOverflow(offset,size, evmGasLeft)
checkMemOverflowByOffset(add(offset, size), evmGasLeft)
evmGasLeft := chargeGas(evmGasLeft,expandMemory(add(offset,size)))

checkOverflow(offset,MEM_OFFSET_INNER(), evmGasLeft)
offset := add(offset, MEM_OFFSET_INNER())
offset,size := addGasIfEvmRevert(isCallerEVM,offset,size,evmGasLeft)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -659,12 +659,6 @@ function getFarCallABI(
ret := farCallAbi
}

function ensureAcceptableMemLocation(location) {
if gt(location,MAX_POSSIBLE_MEM()) {
revert(0,0) // Check if this is what's needed
}
}

function addGasIfEvmRevert(isCallerEVM,offset,size,evmGasLeft) -> newOffset,newSize {
newOffset := offset
newSize := size
Expand Down
18 changes: 9 additions & 9 deletions system-contracts/evm-interpreter/EvmInterpreterLoop.template.yul
Original file line number Diff line number Diff line change
Expand Up @@ -410,13 +410,8 @@ for { } true { } {
offset, sp := popStackItemWithoutCheck(sp)
size, sp := popStackItemWithoutCheck(sp)

checkMultipleOverflow(offset,size,MEM_OFFSET_INNER(), evmGasLeft)
checkMultipleOverflow(destOffset,size,MEM_OFFSET_INNER(), evmGasLeft)

// TODO invalid?
if or(gt(add(add(offset, size), MEM_OFFSET_INNER()), MAX_POSSIBLE_MEM()), gt(add(add(destOffset, size), MEM_OFFSET_INNER()), MAX_POSSIBLE_MEM())) {
$llvm_AlwaysInline_llvm$_memsetToZero(add(destOffset, MEM_OFFSET_INNER()), size)
}
checkOverflow(destOffset, size, evmGasLeft)
checkMemOverflowByOffset(add(destOffset,size), evmGasLeft)

// dynamicGas = 3 * minimum_word_size + memory_expansion_cost
// minimum_word_size = (size + 31) / 32
Expand Down Expand Up @@ -454,6 +449,7 @@ for { } true { } {
offset := add(add(offset, BYTECODE_OFFSET()), 32)

checkOverflow(dst,len, evmGasLeft)
checkOverflow(offset,len, evmGasLeft)
checkMemOverflow(add(dst, len), evmGasLeft)
// Check bytecode overflow
if gt(add(offset, len), sub(MEM_OFFSET(), 1)) {
Expand Down Expand Up @@ -810,7 +806,8 @@ for { } true { } {
offset, sp := popStackItemWithoutCheck(sp)
size, sp := popStackItemWithoutCheck(sp)

// TODO overflow checks
checkOverflow(offset, size, evmGasLeft)
checkOverflow(destOffset, size, evmGasLeft)
checkMemOverflowByOffset(add(offset, size), evmGasLeft)
checkMemOverflowByOffset(add(destOffset, size), evmGasLeft)

Expand Down Expand Up @@ -1403,6 +1400,7 @@ for { } true { } {
size, sp := popStackItemWithoutCheck(sp)

checkOverflow(offset,size, evmGasLeft)
checkMemOverflowByOffset(add(offset,size), evmGasLeft)
evmGasLeft := chargeGas(evmGasLeft,expandMemory(add(offset,size)))

returnLen := size
Expand Down Expand Up @@ -1442,11 +1440,13 @@ for { } true { } {
offset, sp := popStackItemWithoutCheck(sp)
size, sp := popStackItemWithoutCheck(sp)

// TODO invalid?
ensureAcceptableMemLocation(offset)
ensureAcceptableMemLocation(size)
checkOverflow(offset,size, evmGasLeft)
checkMemOverflowByOffset(add(offset, size), evmGasLeft)
evmGasLeft := chargeGas(evmGasLeft,expandMemory(add(offset,size)))

checkOverflow(offset,MEM_OFFSET_INNER(), evmGasLeft)
offset := add(offset, MEM_OFFSET_INNER())
offset,size := addGasIfEvmRevert(isCallerEVM,offset,size,evmGasLeft)

Expand Down

0 comments on commit bbac2a0

Please sign in to comment.