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 26, 2024
2 parents ae94e28 + c640a06 commit 334f406
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 10 deletions.
9 changes: 7 additions & 2 deletions src/instruction_handlers/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,19 @@ 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, W>, &Arguments, &mut W) -> ExecutionStatus,
business_logic: impl FnOnce(
&mut VirtualMachine<T, W>,
&Arguments,
&mut T,
&mut W,
) -> ExecutionStatus,
) -> ExecutionStatus {
tracer.before_instruction::<Opcode, _>(vm);
let result = unsafe {
let instruction = vm.state.current_frame.pc;
vm.state.current_frame.pc = instruction.add(1);

business_logic(vm, &(*instruction).arguments, world)
business_logic(vm, &(*instruction).arguments, tracer, world)
};
tracer.after_instruction::<Opcode, _>(vm);

Expand Down
10 changes: 6 additions & 4 deletions src/instruction_handlers/far_call.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{
common::instruction_boilerplate,
common::instruction_boilerplate_ext,
heap_access::grow_heap,
ret::{panic_from_failed_far_call, RETURN_COST},
AuxHeap, Heap,
Expand Down Expand Up @@ -45,7 +45,7 @@ fn far_call<
world: &mut W,
tracer: &mut T,
) -> ExecutionStatus {
instruction_boilerplate::<FarCall<M>, _, _>(vm, world, tracer, |vm, args, world| {
instruction_boilerplate_ext::<FarCall<M>, _, _>(vm, world, tracer, |vm, args, tracer, world| {
let (raw_abi, raw_abi_is_pointer) = Register1::get_with_pointer_flag(args, &mut vm.state);

let address_mask: U256 = U256::MAX >> (256 - 160);
Expand Down Expand Up @@ -118,8 +118,8 @@ fn far_call<

let Some((calldata, program, is_evm_interpreter)) = failing_part else {
vm.state.current_frame.gas += new_frame_gas.saturating_sub(RETURN_COST);
panic_from_failed_far_call(vm, exception_handler);
return;
panic_from_failed_far_call(vm, tracer, exception_handler);
return ExecutionStatus::Running;
};

let stipend = if is_evm_interpreter {
Expand Down Expand Up @@ -165,6 +165,8 @@ fn far_call<
| u8::from(abi.is_constructor_call);

vm.state.registers[2] = call_type.into();

ExecutionStatus::Running
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/instruction_handlers/heap_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ fn store<
world: &mut W,
tracer: &mut T,
) -> ExecutionStatus {
instruction_boilerplate_ext::<H::Write, _, _>(vm, world, tracer, |vm, args, _| {
instruction_boilerplate_ext::<H::Write, _, _>(vm, world, tracer, |vm, args, _, _| {
// Pointers need not be masked here even though we do not care about them being pointers.
// They will panic, though because they are larger than 2^32.
let (pointer, _) = In::get_with_pointer_flag(args, &mut vm.state);
Expand Down
8 changes: 6 additions & 2 deletions src/instruction_handlers/ret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn ret<T: Tracer, W, RT: TypeLevelReturnType, const TO_LABEL: bool>(
world: &mut W,
tracer: &mut T,
) -> ExecutionStatus {
instruction_boilerplate_ext::<opcodes::Ret<RT>, _, _>(vm, world, tracer, |vm, args, _| {
instruction_boilerplate_ext::<opcodes::Ret<RT>, _, _>(vm, world, tracer, |vm, args, _, _| {
let mut return_type = RT::VALUE;
let near_call_leftover_gas = vm.state.current_frame.gas;

Expand Down Expand Up @@ -120,11 +120,13 @@ fn ret<T: Tracer, W, RT: TypeLevelReturnType, const TO_LABEL: bool>(

/// Formally, a far call pushes a new frame and returns from it immediately if it panics.
/// This function instead panics without popping a frame to save on allocation.
/// TODO: when tracers are implemented, this function should count as a separate instruction!
pub(crate) fn panic_from_failed_far_call<T: Tracer, W>(
vm: &mut VirtualMachine<T, W>,
tracer: &mut T,
exception_handler: u16,
) {
tracer.before_instruction::<opcodes::Ret<Panic>, _>(vm);

// Gas is already subtracted in the far call code.
// No need to roll back, as no changes are made in this "frame".

Expand All @@ -136,6 +138,8 @@ pub(crate) fn panic_from_failed_far_call<T: Tracer, W>(
vm.state.flags = Flags::new(true, false, false);

vm.state.current_frame.set_pc_from_u16(exception_handler);

tracer.after_instruction::<opcodes::Ret<Panic>, _>(vm);
}

/// Panics, burning all available gas.
Expand Down
3 changes: 2 additions & 1 deletion src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ pub struct VirtualMachine<T, W> {

pub(crate) stack_pool: StackPool,

// Instructions that are jumped to when things go wrong.
/// Instruction that is jumped to when things go wrong while executing another.
/// Boxed, so the pointer isn't invalidated by moves.
pub(crate) panic: Box<Instruction<T, W>>,
}

Expand Down

0 comments on commit 334f406

Please sign in to comment.