Skip to content

Commit

Permalink
Merge branch 'stable-tracer-interface' into fvs-circuit-statistics-tr…
Browse files Browse the repository at this point in the history
…acers-interface
  • Loading branch information
montekki authored Aug 23, 2024
2 parents 35d284b + c339dde commit 6e2921c
Show file tree
Hide file tree
Showing 40 changed files with 344 additions and 311 deletions.
11 changes: 8 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[package]
name = "vm2"
version = "0.1.0"
edition = "2021"
edition.workspace = true
homepage = "https://zksync.io/"
license = "MIT OR Apache-2.0"
authors = ["The Matter Labs Team <[email protected]>"]
license.workspace = true
authors.workspace = true

[dependencies]
eravm-stable-interface = { path = "./eravm-stable-interface" }
Expand Down Expand Up @@ -34,3 +34,8 @@ single_instruction_test = ["arbitrary", "u256/arbitrary", "zk_evm", "anyhow"]

[workspace]
members = [".", "afl-fuzz", "eravm-stable-interface"]

[workspace.package]
edition = "2021"
license = "MIT OR Apache-2.0"
authors = ["The Matter Labs Team <[email protected]>"]
2 changes: 1 addition & 1 deletion afl-fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "differential_fuzzing"
version = "0.1.0"
edition = "2021"
edition.workspace = true
publish = false

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion afl-fuzz/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ use vm2::{single_instruction_test::MockWorld, VirtualMachine};

#[derive(Arbitrary, Debug)]
pub struct VmAndWorld<T: Tracer> {
pub vm: VirtualMachine<T>,
pub vm: VirtualMachine<T, MockWorld>,
pub world: MockWorld,
}
6 changes: 3 additions & 3 deletions eravm-stable-interface/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[package]
name = "eravm-stable-interface"
version = "0.1.0"
edition = "2021"
license = "MIT OR Apache-2.0"
authors = ["The Matter Labs Team <[email protected]>"]
edition.workspace = true
license.workspace = true
authors.workspace = true

[dependencies]
primitive-types = "0.12.1"
4 changes: 4 additions & 0 deletions src/bitset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,26 @@
pub struct Bitset([u64; 1 << 10]);

impl Bitset {
#[inline(always)]
pub fn get(&self, i: u16) -> bool {
let (slot, bit) = slot_and_bit(i);
self.0[slot] & bit != 0
}

#[inline(always)]
pub fn set(&mut self, i: u16) {
let (slot, bit) = slot_and_bit(i);
self.0[slot] |= bit;
}

#[inline(always)]
pub fn clear(&mut self, i: u16) {
let (slot, bit) = slot_and_bit(i);
self.0[slot] &= !bit;
}
}

#[inline(always)]
fn slot_and_bit(i: u16) -> (usize, u64) {
((i >> 6) as usize, 1u64 << (i & 0b111111))
}
Expand Down
12 changes: 6 additions & 6 deletions src/callframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use u256::H160;
use zkevm_opcode_defs::system_params::{NEW_FRAME_MEMORY_STIPEND, NEW_KERNEL_FRAME_MEMORY_STIPEND};

#[derive(PartialEq, Debug)]
pub struct Callframe<T> {
pub struct Callframe<T, W> {
pub address: H160,
pub code_address: H160,
pub caller: H160,
Expand All @@ -29,8 +29,8 @@ pub struct Callframe<T> {

pub(crate) near_calls: Vec<NearCallFrame>,

pub(crate) pc: *const Instruction<T>,
pub(crate) program: Program<T>,
pub(crate) pc: *const Instruction<T, W>,
pub(crate) program: Program<T, W>,

pub heap: HeapId,
pub aux_heap: HeapId,
Expand Down Expand Up @@ -63,13 +63,13 @@ pub(crate) struct NearCallFrame {
world_before_this_frame: Snapshot,
}

impl<T> Callframe<T> {
impl<T, W> Callframe<T, W> {
#[allow(clippy::too_many_arguments)]
pub(crate) fn new(
address: H160,
code_address: H160,
caller: H160,
program: Program<T>,
program: Program<T, W>,
stack: Box<Stack>,
heap: HeapId,
aux_heap: HeapId,
Expand Down Expand Up @@ -232,7 +232,7 @@ pub(crate) struct CallframeSnapshot {
heaps_i_was_keeping_alive: usize,
}

impl<T> Clone for Callframe<T> {
impl<T, W> Clone for Callframe<T, W> {
fn clone(&self) -> Self {
Self {
address: self.address,
Expand Down
15 changes: 9 additions & 6 deletions src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ use zkevm_opcode_defs::{
SWAP_OPERANDS_FLAG_IDX_FOR_PTR_OPCODE, UMA_INCREMENT_FLAG_IDX,
};

pub fn decode_program<T: Tracer>(raw: &[u64], is_bootloader: bool) -> Vec<Instruction<T>> {
pub fn decode_program<T: Tracer, W: World<T>>(
raw: &[u64],
is_bootloader: bool,
) -> Vec<Instruction<T, W>> {
raw.iter()
.take(1 << 16)
.map(|i| decode(*i, is_bootloader))
Expand All @@ -35,7 +38,7 @@ pub fn decode_program<T: Tracer>(raw: &[u64], is_bootloader: bool) -> Vec<Instru
.collect()
}

fn unimplemented_instruction<T>(variant: Opcode) -> Instruction<T> {
fn unimplemented_instruction<T, W>(variant: Opcode) -> Instruction<T, W> {
let mut arguments = Arguments::new(Predicate::Always, 0, ModeRequirements::none());
let variant_as_number: u16 = unsafe { std::mem::transmute(variant) };
Immediate1(variant_as_number).write_source(&mut arguments);
Expand All @@ -44,9 +47,9 @@ fn unimplemented_instruction<T>(variant: Opcode) -> Instruction<T> {
arguments,
}
}
fn unimplemented_handler<T>(
vm: &mut VirtualMachine<T>,
_: &mut dyn World<T>,
fn unimplemented_handler<T, W>(
vm: &mut VirtualMachine<T, W>,
_: &mut W,
_: &mut T,
) -> ExecutionStatus {
let variant: Opcode = unsafe {
Expand All @@ -59,7 +62,7 @@ fn unimplemented_handler<T>(
ExecutionStatus::Stopped(ExecutionEnd::Panicked)
}

pub(crate) fn decode<T: Tracer>(raw: u64, is_bootloader: bool) -> Instruction<T> {
pub(crate) fn decode<T: Tracer, W: World<T>>(raw: u64, is_bootloader: bool) -> Instruction<T, W> {
let (parsed, _) = EncodingModeProduction::parse_preliminary_variant_and_absolute_number(raw);

let predicate = match parsed.condition {
Expand Down
10 changes: 5 additions & 5 deletions src/decommit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use zkevm_opcode_defs::{
impl WorldDiff {
pub(crate) fn decommit<T>(
&mut self,
world: &mut dyn World<T>,
world: &mut impl World<T>,
address: U256,
default_aa_code_hash: [u8; 32],
evm_interpreter_code_hash: [u8; 32],
Expand Down Expand Up @@ -82,16 +82,16 @@ impl WorldDiff {
#[doc(hidden)] // should be used for testing purposes only; can break VM operation otherwise
pub fn decommit_opcode<T>(
&mut self,
world: &mut dyn World<T>,
world: &mut impl World<T>,
code_hash: U256,
) -> (Vec<u8>, bool) {
let was_decommitted = self.decommitted_hashes.insert(code_hash, ()).is_some();
(world.decommit_code(code_hash), !was_decommitted)
}

pub(crate) fn pay_for_decommit<T>(
pub(crate) fn pay_for_decommit<T, W: World<T>>(
&mut self,
world: &mut dyn World<T>,
world: &mut W,
decommit: UnpaidDecommit,
gas: &mut u32,
) -> Option<(Program<T>, usize)> {
Expand Down Expand Up @@ -124,7 +124,7 @@ pub(crate) struct UnpaidDecommit {
/// May be used to load code when the VM first starts up.
/// Doesn't check for any errors.
/// Doesn't cost anything but also doesn't make the code free in future decommits.
pub fn initial_decommit<T>(world: &mut impl World<T>, address: H160) -> Program<T> {
pub fn initial_decommit<T, W: World<T>>(world: &mut W, address: H160) -> Program<T, W> {
let deployer_system_contract_address =
Address::from_low_u64_be(DEPLOYER_SYSTEM_CONTRACT_ADDRESS_LOW as u64);
let code_info = world
Expand Down
20 changes: 9 additions & 11 deletions src/instruction.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use std::fmt;

use crate::{
addressing_modes::Arguments, mode_requirements::ModeRequirements, vm::VirtualMachine,
Predicate, World,
addressing_modes::Arguments, mode_requirements::ModeRequirements, vm::VirtualMachine, Predicate,
};

pub struct Instruction<T> {
pub(crate) handler: Handler<T>,
pub struct Instruction<T, W> {
pub(crate) handler: Handler<T, W>,
pub(crate) arguments: Arguments,
}

impl<T> fmt::Debug for Instruction<T> {
impl<T, W> fmt::Debug for Instruction<T, W> {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("Instruction")
Expand All @@ -19,8 +18,7 @@ impl<T> fmt::Debug for Instruction<T> {
}
}

pub(crate) type Handler<T> =
fn(&mut VirtualMachine<T>, &mut dyn World<T>, &mut T) -> ExecutionStatus;
pub(crate) type Handler<T, W> = fn(&mut VirtualMachine<T, W>, &mut W, &mut T) -> ExecutionStatus;
pub enum ExecutionStatus {
Running,
Stopped(ExecutionEnd),
Expand All @@ -36,15 +34,15 @@ pub enum ExecutionEnd {
SuspendedOnHook(u32),
}

pub fn jump_to_beginning<T>() -> Instruction<T> {
pub fn jump_to_beginning<T, W>() -> Instruction<T, W> {
Instruction {
handler: jump_to_beginning_handler,
arguments: Arguments::new(Predicate::Always, 0, ModeRequirements::none()),
}
}
fn jump_to_beginning_handler<T>(
vm: &mut VirtualMachine<T>,
_: &mut dyn World<T>,
fn jump_to_beginning_handler<T, W>(
vm: &mut VirtualMachine<T, W>,
_: &mut W,
_: &mut T,
) -> ExecutionStatus {
let first_instruction = vm.state.current_frame.program.instruction(0).unwrap();
Expand Down
13 changes: 7 additions & 6 deletions src/instruction_handlers/binop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
},
instruction::{ExecutionStatus, Instruction},
predication::Flags,
VirtualMachine, World,
VirtualMachine,
};
use eravm_stable_interface::{
opcodes::{Add, And, Div, Mul, Or, RotateLeft, RotateRight, ShiftLeft, ShiftRight, Sub, Xor},
Expand All @@ -17,17 +17,18 @@ use u256::U256;

fn binop<
T: Tracer,
W,
Op: Binop,
In1: Source,
Out: Destination,
const SWAP: bool,
const SET_FLAGS: bool,
>(
vm: &mut VirtualMachine<T>,
world: &mut dyn World<T>,
vm: &mut VirtualMachine<T, W>,
world: &mut W,
tracer: &mut T,
) -> ExecutionStatus {
instruction_boilerplate::<Op, _>(vm, world, tracer, |vm, args, _| {
instruction_boilerplate::<Op, _, _>(vm, world, tracer, |vm, args, _| {
let a = In1::get(args, &mut vm.state);
let b = Register2::get(args, &mut vm.state);
let (a, b) = if SWAP { (b, a) } else { (a, b) };
Expand Down Expand Up @@ -202,7 +203,7 @@ impl Binop for Div {

use super::monomorphization::*;

impl<T: Tracer> Instruction<T> {
impl<T: Tracer, W> Instruction<T, W> {
#[inline(always)]
pub fn from_binop<Op: Binop>(
src1: AnySource,
Expand All @@ -214,7 +215,7 @@ impl<T: Tracer> Instruction<T> {
set_flags: bool,
) -> Self {
Self {
handler: monomorphize!(binop [T Op] match_source src1 match_destination out match_boolean swap match_boolean set_flags),
handler: monomorphize!(binop [T W Op] match_source src1 match_destination out match_boolean swap match_boolean set_flags),
arguments: arguments
.write_source(&src1)
.write_source(&src2)
Expand Down
22 changes: 9 additions & 13 deletions src/instruction_handlers/common.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::{addressing_modes::Arguments, instruction::ExecutionStatus, VirtualMachine, World};
use crate::{addressing_modes::Arguments, instruction::ExecutionStatus, VirtualMachine};
use eravm_stable_interface::{OpcodeType, Tracer};

#[inline(always)]
pub(crate) fn instruction_boilerplate<Opcode: OpcodeType, T: Tracer>(
vm: &mut VirtualMachine<T>,
world: &mut dyn World<T>,
pub(crate) fn instruction_boilerplate<Opcode: OpcodeType, T: Tracer, W>(
vm: &mut VirtualMachine<T, W>,
world: &mut W,
tracer: &mut T,
business_logic: impl FnOnce(&mut VirtualMachine<T>, &Arguments, &mut dyn World<T>),
business_logic: impl FnOnce(&mut VirtualMachine<T, W>, &Arguments, &mut W),
) -> ExecutionStatus {
tracer.before_instruction::<Opcode, _>(vm);
unsafe {
Expand All @@ -20,15 +20,11 @@ pub(crate) fn instruction_boilerplate<Opcode: OpcodeType, T: Tracer>(
}

#[inline(always)]
pub(crate) fn instruction_boilerplate_ext<Opcode: OpcodeType, T: Tracer>(
vm: &mut VirtualMachine<T>,
world: &mut dyn World<T>,
pub(crate) fn instruction_boilerplate_ext<Opcode: OpcodeType, T: Tracer, W>(
vm: &mut VirtualMachine<T, W>,
world: &mut W,
tracer: &mut T,
business_logic: impl FnOnce(
&mut VirtualMachine<T>,
&Arguments,
&mut dyn World<T>,
) -> ExecutionStatus,
business_logic: impl FnOnce(&mut VirtualMachine<T, W>, &Arguments, &mut W) -> ExecutionStatus,
) -> ExecutionStatus {
tracer.before_instruction::<Opcode, _>(vm);
let result = unsafe {
Expand Down
Loading

0 comments on commit 6e2921c

Please sign in to comment.