From 977f77cbb30d34becb3171f1389c8a8c050da6a3 Mon Sep 17 00:00:00 2001 From: Vladislav Volosnikov Date: Thu, 21 Nov 2024 16:05:59 +0100 Subject: [PATCH] Increase memory stipend for EVM calls --- crates/zk_evm/src/opcodes/execution/far_call.rs | 6 +++++- .../src/main_vm/opcodes/call_ret_impl/far_call.rs | 11 +++++++++++ crates/zkevm_opcode_defs/src/system_params.rs | 2 ++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/crates/zk_evm/src/opcodes/execution/far_call.rs b/crates/zk_evm/src/opcodes/execution/far_call.rs index b974ff7..27fe4b4 100644 --- a/crates/zk_evm/src/opcodes/execution/far_call.rs +++ b/crates/zk_evm/src/opcodes/execution/far_call.rs @@ -613,7 +613,11 @@ impl> DecodedOpcode { let memory_stipend = if address_is_kernel(&address_for_next) { zkevm_opcode_defs::system_params::NEW_KERNEL_FRAME_MEMORY_STIPEND } else { - zkevm_opcode_defs::system_params::NEW_FRAME_MEMORY_STIPEND + if call_to_evm_simulator { + zkevm_opcode_defs::system_params::NEW_EVM_FRAME_MEMORY_STIPEND + } else { + zkevm_opcode_defs::system_params::NEW_FRAME_MEMORY_STIPEND + } }; let new_stack = CallStackEntry { diff --git a/crates/zkevm_circuits/src/main_vm/opcodes/call_ret_impl/far_call.rs b/crates/zkevm_circuits/src/main_vm/opcodes/call_ret_impl/far_call.rs index 68af0d5..b0a8232 100644 --- a/crates/zkevm_circuits/src/main_vm/opcodes/call_ret_impl/far_call.rs +++ b/crates/zkevm_circuits/src/main_vm/opcodes/call_ret_impl/far_call.rs @@ -1133,6 +1133,10 @@ where cs, zkevm_opcode_defs::system_params::NEW_FRAME_MEMORY_STIPEND, ); + let memory_size_stipend_for_evm = UInt32::allocated_constant( + cs, + zkevm_opcode_defs::system_params::NEW_EVM_FRAME_MEMORY_STIPEND, + ); let memory_stipend = UInt32::conditionally_select( cs, @@ -1141,6 +1145,13 @@ where &memory_size_stipend_for_userspace, ); + let memory_stipend = UInt32::conditionally_select( + cs, + versioned_byte_is_evm_bytecode, + &memory_size_stipend_for_evm, + &memory_stipend, + ); + new_callstack_entry.heap_upper_bound = memory_stipend; new_callstack_entry.aux_heap_upper_bound = memory_stipend; diff --git a/crates/zkevm_opcode_defs/src/system_params.rs b/crates/zkevm_opcode_defs/src/system_params.rs index 6319b93..b46b0c7 100644 --- a/crates/zkevm_opcode_defs/src/system_params.rs +++ b/crates/zkevm_opcode_defs/src/system_params.rs @@ -83,6 +83,8 @@ pub const ADDRESS_CODE_ORACLE: u16 = 0x8011; pub const BOOTLOADER_MAX_MEMORY: u32 = u32::MAX; // 4 KB for new frames is "free" pub const NEW_FRAME_MEMORY_STIPEND: u32 = 1u32 << 12; +// 56 KB for new EVM frames is "free" +pub const NEW_EVM_FRAME_MEMORY_STIPEND: u32 = 56 * 1u32 << 10; // 2 MB for kernel frames, where we can be sure about the behavior. // Note, that this number should high enough to allow any bytecode for `decommit` opcode. pub const NEW_KERNEL_FRAME_MEMORY_STIPEND: u32 = 1u32 << 21;