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 LOGs implementation #1128

Merged
Merged
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
250 changes: 78 additions & 172 deletions system-contracts/contracts/EvmEmulator.yul
Original file line number Diff line number Diff line change
Expand Up @@ -1291,6 +1291,35 @@ object "EvmEmulator" {
$llvm_AlwaysInline_llvm$_copyRest(dest_end, 0, rest_len)
}
}

////////////////////////////////////////////////////////////////
// LOGS FUNCTIONALITY
////////////////////////////////////////////////////////////////

function _genericLog(sp, stackHead, evmGasLeft, topicCount, isStatic) -> newEvmGasLeft, offset, size, newSp, newStackHead {
newEvmGasLeft := chargeGas(evmGasLeft, 375)

if isStatic {
panic()
}

let rawOffset
popStackCheck(sp, add(2, topicCount))
rawOffset, newSp, newStackHead := popStackItemWithoutCheck(sp, stackHead)
size, newSp, newStackHead := popStackItemWithoutCheck(newSp, newStackHead)

checkMemIsAccessible(rawOffset, size)

// dynamicGas = 375 * topic_count + 8 * size + memory_expansion_cost
let dynamicGas := add(shl(3, size), expandMemory(rawOffset, size))
dynamicGas := add(dynamicGas, mul(375, topicCount))

newEvmGasLeft := chargeGas(newEvmGasLeft, dynamicGas)

if size {
offset := add(offset, MEM_OFFSET())
}
}

function simulate(
isCallerEVM,
Expand Down Expand Up @@ -2631,133 +2660,57 @@ object "EvmEmulator" {
ip := add(ip, 1)
}
case 0xA0 { // OP_LOG0
evmGasLeft := chargeGas(evmGasLeft, 375)

if isStatic {
panic()
}

let offset, size
popStackCheck(sp, 2)
offset, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
size, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)

checkMemIsAccessible(offset, size)

// dynamicGas = 375 * topic_count + 8 * size + memory_expansion_cost
let dynamicGas := add(shl(3, size), expandMemory(offset, size))
evmGasLeft := chargeGas(evmGasLeft, dynamicGas)

log0(add(offset, MEM_OFFSET()), size)
evmGasLeft, offset, size, sp, stackHead := _genericLog(sp, stackHead, evmGasLeft, 0, isStatic)
log0(offset, size)
ip := add(ip, 1)
}
case 0xA1 { // OP_LOG1
evmGasLeft := chargeGas(evmGasLeft, 375)

if isStatic {
panic()
}

let offset, size
popStackCheck(sp, 3)
offset, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
size, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)

checkMemIsAccessible(offset, size)

// dynamicGas = 375 * topic_count + 8 * size + memory_expansion_cost
let dynamicGas := add(shl(3, size), expandMemory(offset, size))
dynamicGas := add(dynamicGas, 375)
evmGasLeft := chargeGas(evmGasLeft, dynamicGas)

evmGasLeft, offset, size, sp, stackHead := _genericLog(sp, stackHead, evmGasLeft, 1, isStatic)
{
let topic1
topic1, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
log1(add(offset, MEM_OFFSET()), size, topic1)
log1(offset, size, topic1)
}
ip := add(ip, 1)
}
case 0xA2 { // OP_LOG2
evmGasLeft := chargeGas(evmGasLeft, 375)

if isStatic {
panic()
}

let offset, size
popStackCheck(sp, 4)
offset, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
size, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)

checkMemIsAccessible(offset, size)

// dynamicGas = 375 * topic_count + 8 * size + memory_expansion_cost
let dynamicGas := add(shl(3, size), expandMemory(offset, size))
dynamicGas := add(dynamicGas, 750)
evmGasLeft := chargeGas(evmGasLeft, dynamicGas)
evmGasLeft, offset, size, sp, stackHead := _genericLog(sp, stackHead, evmGasLeft, 2, isStatic)

{
let topic1, topic2
topic1, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
topic2, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
log2(add(offset, MEM_OFFSET()), size, topic1, topic2)
log2(offset, size, topic1, topic2)
}
ip := add(ip, 1)
}
case 0xA3 { // OP_LOG3
evmGasLeft := chargeGas(evmGasLeft, 375)

if isStatic {
panic()
}

let offset, size
popStackCheck(sp, 5)
offset, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
size, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)

checkMemIsAccessible(offset, size)

// dynamicGas = 375 * topic_count + 8 * size + memory_expansion_cost
let dynamicGas := add(shl(3, size), expandMemory(offset, size))
dynamicGas := add(dynamicGas, 1125)
evmGasLeft := chargeGas(evmGasLeft, dynamicGas)
evmGasLeft, offset, size, sp, stackHead := _genericLog(sp, stackHead, evmGasLeft, 3, isStatic)

{
let topic1, topic2, topic3
topic1, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
topic2, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
topic3, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
log3(add(offset, MEM_OFFSET()), size, topic1, topic2, topic3)
log3(offset, size, topic1, topic2, topic3)
}
ip := add(ip, 1)
}
case 0xA4 { // OP_LOG4
evmGasLeft := chargeGas(evmGasLeft, 375)

if isStatic {
panic()
}

let offset, size
popStackCheck(sp, 6)
offset, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
size, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)

checkMemIsAccessible(offset, size)

// dynamicGas = 375 * topic_count + 8 * size + memory_expansion_cost
let dynamicGas := add(shl(3, size), expandMemory(offset, size))
dynamicGas := add(dynamicGas, 1500)
evmGasLeft := chargeGas(evmGasLeft, dynamicGas)
evmGasLeft, offset, size, sp, stackHead := _genericLog(sp, stackHead, evmGasLeft, 4, isStatic)

{
let topic1, topic2, topic3, topic4
topic1, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
topic2, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
topic3, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
topic4, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
log4(add(offset, MEM_OFFSET()), size, topic1, topic2, topic3, topic4)
log4(offset, size, topic1, topic2, topic3, topic4)
}
ip := add(ip, 1)
}
Expand Down Expand Up @@ -4468,6 +4421,35 @@ object "EvmEmulator" {
$llvm_AlwaysInline_llvm$_copyRest(dest_end, 0, rest_len)
}
}

////////////////////////////////////////////////////////////////
// LOGS FUNCTIONALITY
////////////////////////////////////////////////////////////////

function _genericLog(sp, stackHead, evmGasLeft, topicCount, isStatic) -> newEvmGasLeft, offset, size, newSp, newStackHead {
newEvmGasLeft := chargeGas(evmGasLeft, 375)

if isStatic {
panic()
}

let rawOffset
popStackCheck(sp, add(2, topicCount))
rawOffset, newSp, newStackHead := popStackItemWithoutCheck(sp, stackHead)
size, newSp, newStackHead := popStackItemWithoutCheck(newSp, newStackHead)

checkMemIsAccessible(rawOffset, size)

// dynamicGas = 375 * topic_count + 8 * size + memory_expansion_cost
let dynamicGas := add(shl(3, size), expandMemory(rawOffset, size))
dynamicGas := add(dynamicGas, mul(375, topicCount))

newEvmGasLeft := chargeGas(newEvmGasLeft, dynamicGas)

if size {
offset := add(offset, MEM_OFFSET())
}
}

function $llvm_NoInline_llvm$_simulate(
isCallerEVM,
Expand Down Expand Up @@ -5808,133 +5790,57 @@ object "EvmEmulator" {
ip := add(ip, 1)
}
case 0xA0 { // OP_LOG0
evmGasLeft := chargeGas(evmGasLeft, 375)

if isStatic {
panic()
}

let offset, size
popStackCheck(sp, 2)
offset, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
size, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)

checkMemIsAccessible(offset, size)

// dynamicGas = 375 * topic_count + 8 * size + memory_expansion_cost
let dynamicGas := add(shl(3, size), expandMemory(offset, size))
evmGasLeft := chargeGas(evmGasLeft, dynamicGas)

log0(add(offset, MEM_OFFSET()), size)
evmGasLeft, offset, size, sp, stackHead := _genericLog(sp, stackHead, evmGasLeft, 0, isStatic)
log0(offset, size)
ip := add(ip, 1)
}
case 0xA1 { // OP_LOG1
evmGasLeft := chargeGas(evmGasLeft, 375)

if isStatic {
panic()
}

let offset, size
popStackCheck(sp, 3)
offset, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
size, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)

checkMemIsAccessible(offset, size)

// dynamicGas = 375 * topic_count + 8 * size + memory_expansion_cost
let dynamicGas := add(shl(3, size), expandMemory(offset, size))
dynamicGas := add(dynamicGas, 375)
evmGasLeft := chargeGas(evmGasLeft, dynamicGas)

evmGasLeft, offset, size, sp, stackHead := _genericLog(sp, stackHead, evmGasLeft, 1, isStatic)
{
let topic1
topic1, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
log1(add(offset, MEM_OFFSET()), size, topic1)
log1(offset, size, topic1)
}
ip := add(ip, 1)
}
case 0xA2 { // OP_LOG2
evmGasLeft := chargeGas(evmGasLeft, 375)

if isStatic {
panic()
}

let offset, size
popStackCheck(sp, 4)
offset, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
size, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)

checkMemIsAccessible(offset, size)

// dynamicGas = 375 * topic_count + 8 * size + memory_expansion_cost
let dynamicGas := add(shl(3, size), expandMemory(offset, size))
dynamicGas := add(dynamicGas, 750)
evmGasLeft := chargeGas(evmGasLeft, dynamicGas)
evmGasLeft, offset, size, sp, stackHead := _genericLog(sp, stackHead, evmGasLeft, 2, isStatic)

{
let topic1, topic2
topic1, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
topic2, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
log2(add(offset, MEM_OFFSET()), size, topic1, topic2)
log2(offset, size, topic1, topic2)
}
ip := add(ip, 1)
}
case 0xA3 { // OP_LOG3
evmGasLeft := chargeGas(evmGasLeft, 375)

if isStatic {
panic()
}

let offset, size
popStackCheck(sp, 5)
offset, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
size, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)

checkMemIsAccessible(offset, size)

// dynamicGas = 375 * topic_count + 8 * size + memory_expansion_cost
let dynamicGas := add(shl(3, size), expandMemory(offset, size))
dynamicGas := add(dynamicGas, 1125)
evmGasLeft := chargeGas(evmGasLeft, dynamicGas)
evmGasLeft, offset, size, sp, stackHead := _genericLog(sp, stackHead, evmGasLeft, 3, isStatic)

{
let topic1, topic2, topic3
topic1, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
topic2, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
topic3, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
log3(add(offset, MEM_OFFSET()), size, topic1, topic2, topic3)
log3(offset, size, topic1, topic2, topic3)
}
ip := add(ip, 1)
}
case 0xA4 { // OP_LOG4
evmGasLeft := chargeGas(evmGasLeft, 375)

if isStatic {
panic()
}

let offset, size
popStackCheck(sp, 6)
offset, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
size, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)

checkMemIsAccessible(offset, size)

// dynamicGas = 375 * topic_count + 8 * size + memory_expansion_cost
let dynamicGas := add(shl(3, size), expandMemory(offset, size))
dynamicGas := add(dynamicGas, 1500)
evmGasLeft := chargeGas(evmGasLeft, dynamicGas)
evmGasLeft, offset, size, sp, stackHead := _genericLog(sp, stackHead, evmGasLeft, 4, isStatic)

{
let topic1, topic2, topic3, topic4
topic1, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
topic2, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
topic3, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
topic4, sp, stackHead := popStackItemWithoutCheck(sp, stackHead)
log4(add(offset, MEM_OFFSET()), size, topic1, topic2, topic3, topic4)
log4(offset, size, topic1, topic2, topic3, topic4)
}
ip := add(ip, 1)
}
Expand Down
29 changes: 29 additions & 0 deletions system-contracts/evm-emulator/EvmEmulatorFunctions.template.yul
Original file line number Diff line number Diff line change
Expand Up @@ -1228,4 +1228,33 @@ function $llvm_AlwaysInline_llvm$_memsetToZero(dest,len) {
if rest_len {
$llvm_AlwaysInline_llvm$_copyRest(dest_end, 0, rest_len)
}
}

////////////////////////////////////////////////////////////////
// LOGS FUNCTIONALITY
////////////////////////////////////////////////////////////////

function _genericLog(sp, stackHead, evmGasLeft, topicCount, isStatic) -> newEvmGasLeft, offset, size, newSp, newStackHead {
newEvmGasLeft := chargeGas(evmGasLeft, 375)

if isStatic {
panic()
}

let rawOffset
popStackCheck(sp, add(2, topicCount))
rawOffset, newSp, newStackHead := popStackItemWithoutCheck(sp, stackHead)
size, newSp, newStackHead := popStackItemWithoutCheck(newSp, newStackHead)

checkMemIsAccessible(rawOffset, size)

// dynamicGas = 375 * topic_count + 8 * size + memory_expansion_cost
let dynamicGas := add(shl(3, size), expandMemory(rawOffset, size))
dynamicGas := add(dynamicGas, mul(375, topicCount))

newEvmGasLeft := chargeGas(newEvmGasLeft, dynamicGas)

if size {
offset := add(offset, MEM_OFFSET())
}
}
Loading
Loading