Skip to content

Commit

Permalink
implement invalid instruction as a generic static variable
Browse files Browse the repository at this point in the history
  • Loading branch information
joonazan committed Sep 18, 2024
1 parent 6f9d832 commit e1e557c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 20 deletions.
4 changes: 3 additions & 1 deletion crates/vm2/src/callframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{mem, ptr};

use primitive_types::H160;
use zkevm_opcode_defs::system_params::{NEW_FRAME_MEMORY_STIPEND, NEW_KERNEL_FRAME_MEMORY_STIPEND};
use zksync_vm2_interface::HeapId;
use zksync_vm2_interface::{HeapId, Tracer};

use crate::{
decommit::is_kernel,
Expand Down Expand Up @@ -105,7 +105,9 @@ impl<T, W> Callframe<T, W> {
world_before_this_frame,
}
}
}

impl<T: Tracer, W> Callframe<T, W> {
pub(crate) fn push_near_call(
&mut self,
gas_to_call: u32,
Expand Down
16 changes: 10 additions & 6 deletions crates/vm2/src/instruction_handlers/ret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,17 @@ pub(crate) fn panic_from_failed_far_call<T: Tracer, W>(
tracer.after_instruction::<opcodes::Ret<Panic>, _>(vm);
}

/// Panics, burning all available gas.
static INVALID_INSTRUCTION: Instruction<(), ()> = Instruction::from_invalid();
trait GenericStatics<T, W> {
const INVALID: Instruction<T, W>;
}

pub(crate) fn invalid_instruction<'a, T, W>() -> &'a Instruction<T, W> {
// Safety: the handler of an invalid instruction is never read.
unsafe { &*std::ptr::addr_of!(INVALID_INSTRUCTION).cast() }
impl<T: Tracer, W> GenericStatics<T, W> for () {
const INVALID: Instruction<T, W> = Instruction::from_invalid();
}

/// Panics, burning all available gas.
pub(crate) fn invalid_instruction<'a, T: Tracer, W>() -> &'a Instruction<T, W> {
&<()>::INVALID
}

pub(crate) const RETURN_COST: u32 = 5;
Expand Down Expand Up @@ -214,7 +219,6 @@ impl<T: Tracer, W> Instruction<T, W> {
/// Creates a *invalid* instruction that will panic by draining all gas.
pub const fn from_invalid() -> Self {
Self {
// This field is never read because the instruction fails at the gas cost stage.
handler: ret::<T, W, Panic, false>,
arguments: Arguments::new(
Predicate::Always,
Expand Down
6 changes: 4 additions & 2 deletions crates/vm2/src/single_instruction_test/print_mock_info.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use zksync_vm2_interface::Tracer;

use crate::{callframe::Callframe, state::State, VirtualMachine};

impl<T, W> VirtualMachine<T, W> {
impl<T: Tracer, W> VirtualMachine<T, W> {
pub fn print_mock_info(&self) {
self.state.print_mock_info();
println!("Events: {:?}", self.world_diff.events());
Expand All @@ -12,7 +14,7 @@ impl<T, W> VirtualMachine<T, W> {
}
}

impl<T, W> State<T, W> {
impl<T: Tracer, W> State<T, W> {
pub(crate) fn print_mock_info(&self) {
if let Some((heap_id, heap)) = self.heaps.read.read_that_happened() {
println!("Heap: {heap_id:?}");
Expand Down
20 changes: 11 additions & 9 deletions crates/vm2/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use primitive_types::{H160, U256};
use zksync_vm2_interface::HeapId;
use zksync_vm2_interface::{HeapId, Tracer};

use crate::{
addressing_modes::Addressable,
Expand Down Expand Up @@ -86,6 +86,16 @@ impl<T, W> State<T, W> {
}
}

pub(crate) fn set_context_u128(&mut self, value: u128) {
self.context_u128 = value;
}

pub(crate) fn get_context_u128(&self) -> u128 {
self.current_frame.context_u128
}
}

impl<T: Tracer, W> State<T, W> {
/// Returns the total unspent gas in the VM, including stipends.
pub(crate) fn total_unspent_gas(&self) -> u32 {
self.current_frame.gas
Expand All @@ -96,14 +106,6 @@ impl<T, W> State<T, W> {
.sum::<u32>()
}

pub(crate) fn set_context_u128(&mut self, value: u128) {
self.context_u128 = value;
}

pub(crate) fn get_context_u128(&self) -> u128 {
self.current_frame.context_u128
}

pub(crate) fn snapshot(&self) -> StateSnapshot {
StateSnapshot {
registers: self.registers,
Expand Down
3 changes: 1 addition & 2 deletions crates/vm2/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,7 @@ impl<T: Tracer, W: World<T>> VirtualMachine<T, W> {
}
}

// Private methods. We don't constrain `T` and `W` to ease potential refactoring.
impl<T, W> VirtualMachine<T, W> {
impl<T: Tracer, W> VirtualMachine<T, W> {
#[allow(clippy::too_many_arguments)]
pub(crate) fn push_frame<M: TypeLevelCallingMode>(
&mut self,
Expand Down

0 comments on commit e1e557c

Please sign in to comment.