Skip to content

Commit

Permalink
Add memory stipend for EVM interpreter calls
Browse files Browse the repository at this point in the history
  • Loading branch information
slowli committed Jan 24, 2025
1 parent f801369 commit 34ec535
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 2 deletions.
6 changes: 6 additions & 0 deletions crates/vm2/src/callframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ use crate::{
Instruction, World,
};

// FIXME: use `zkevm_opcode_defs::system_params` once it's released
const NEW_EVM_FRAME_MEMORY_STIPEND: u32 = 56 << 10;

#[derive(Debug)]
pub(crate) struct Callframe<T, W> {
pub(crate) address: H160,
Expand Down Expand Up @@ -70,11 +73,14 @@ impl<T, W> Callframe<T, W> {
exception_handler: u16,
context_u128: u128,
is_static: bool,
is_evm_interpreter: bool,
world_before_this_frame: Snapshot,
) -> Self {
let is_kernel = is_kernel(address);
let heap_size = if is_kernel {
NEW_KERNEL_FRAME_MEMORY_STIPEND
} else if is_evm_interpreter {
NEW_EVM_FRAME_MEMORY_STIPEND
} else {
NEW_FRAME_MEMORY_STIPEND
};
Expand Down
5 changes: 3 additions & 2 deletions crates/vm2/src/instruction_handlers/far_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ where
0
};

let failing_part = (|| {
let fallible_part = (|| {
let decommit_result = vm.world_diff.decommit(
world,
tracer,
Expand Down Expand Up @@ -105,7 +105,7 @@ where

// A far call pushes a new frame and returns from it in the next instruction if it panics.
let (calldata, program, is_evm_interpreter) =
failing_part.unwrap_or_else(|| (U256::zero().into(), Program::new_panicking(), false));
fallible_part.unwrap_or_else(|| (U256::zero().into(), Program::new_panicking(), false));

let new_frame_is_static = IS_STATIC || vm.state.current_frame.is_static;
vm.push_frame::<M>(
Expand All @@ -114,6 +114,7 @@ where
new_frame_gas,
exception_handler,
new_frame_is_static && !is_evm_interpreter,
is_evm_interpreter,
calldata.memory_page,
vm.world_diff.snapshot(),
);
Expand Down
1 change: 1 addition & 0 deletions crates/vm2/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ impl<T, W> State<T, W> {
0,
0,
false,
false,
world_before_this_frame,
),
previous_frames: vec![],
Expand Down
1 change: 1 addition & 0 deletions crates/vm2/src/tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ mod test {
(*counter).into(),
*counter,
false,
false,
HeapId::from_u32_unchecked(5),
vm.world_diff.snapshot(),
);
Expand Down
2 changes: 2 additions & 0 deletions crates/vm2/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ impl<T: Tracer, W> VirtualMachine<T, W> {
gas: u32,
exception_handler: u16,
is_static: bool,
is_evm_interpreter: bool,
calldata_heap: HeapId,
world_before_this_frame: Snapshot,
) {
Expand Down Expand Up @@ -227,6 +228,7 @@ impl<T: Tracer, W> VirtualMachine<T, W> {
self.state.context_u128
},
is_static,
is_evm_interpreter,
world_before_this_frame,
);
self.state.context_u128 = 0;
Expand Down

0 comments on commit 34ec535

Please sign in to comment.