-
Notifications
You must be signed in to change notification settings - Fork 156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add VM hooks as rust conditional feature #761
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 4 additions & 8 deletions
12
src/hint_processor/builtin_hint_processor/skip_next_instruction.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,266 @@ | ||
//! VM hooks | ||
//! | ||
//! Make it possible to execute custom arbitrary code at different stages of the VM execution | ||
//! | ||
//! If added to the VM, hooks function will be called during the VM execution at specific stages. | ||
//! | ||
//! Available hooks: | ||
//! - before_first_step, executed before entering the execution loop in [run_until_pc](CairoRunner::run_until_pc) | ||
//! - pre_step_instruction, executed before each instruction_step in [step](VirtualMachine::step) | ||
//! - post_step_instruction, executed after each instruction_step in [step](VirtualMachine::step) | ||
|
||
use std::{any::Any, collections::HashMap, sync::Arc}; | ||
|
||
use felt::Felt; | ||
|
||
use crate::{ | ||
hint_processor::hint_processor_definition::HintProcessor, types::exec_scope::ExecutionScopes, | ||
}; | ||
|
||
use super::{ | ||
errors::vm_errors::VirtualMachineError, runners::cairo_runner::CairoRunner, | ||
vm_core::VirtualMachine, | ||
}; | ||
|
||
type BeforeFirstStepHookFunc = Arc< | ||
dyn Fn( | ||
&mut VirtualMachine, | ||
&mut CairoRunner, | ||
&HashMap<usize, Vec<Box<dyn Any>>>, | ||
) -> Result<(), VirtualMachineError> | ||
+ Sync | ||
+ Send, | ||
>; | ||
|
||
type StepHookFunc = Arc< | ||
dyn Fn( | ||
&mut VirtualMachine, | ||
&mut dyn HintProcessor, | ||
&mut ExecutionScopes, | ||
&HashMap<usize, Vec<Box<dyn Any>>>, | ||
&HashMap<String, Felt>, | ||
) -> Result<(), VirtualMachineError> | ||
+ Sync | ||
+ Send, | ||
>; | ||
|
||
/// The hooks to be executed during the VM run | ||
/// | ||
/// They can be individually ignored by setting them to [None] | ||
#[derive(Clone, Default)] | ||
pub struct Hooks { | ||
before_first_step: Option<BeforeFirstStepHookFunc>, | ||
pre_step_instruction: Option<StepHookFunc>, | ||
post_step_instruction: Option<StepHookFunc>, | ||
} | ||
|
||
impl Hooks { | ||
pub fn new( | ||
before_first_step: Option<BeforeFirstStepHookFunc>, | ||
pre_step_instruction: Option<StepHookFunc>, | ||
post_step_instruction: Option<StepHookFunc>, | ||
) -> Self { | ||
Hooks { | ||
before_first_step, | ||
pre_step_instruction, | ||
post_step_instruction, | ||
} | ||
} | ||
} | ||
|
||
impl VirtualMachine { | ||
pub fn execute_before_first_step( | ||
&mut self, | ||
runner: &mut CairoRunner, | ||
hint_data_dictionary: &HashMap<usize, Vec<Box<dyn Any>>>, | ||
) -> Result<(), VirtualMachineError> { | ||
if let Some(hook_func) = self.hooks.clone().before_first_step { | ||
Oppen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(hook_func)(self, runner, hint_data_dictionary)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn execute_pre_step_instruction( | ||
&mut self, | ||
hint_executor: &mut dyn HintProcessor, | ||
exec_scope: &mut ExecutionScopes, | ||
hint_data_dictionary: &HashMap<usize, Vec<Box<dyn Any>>>, | ||
constants: &HashMap<String, Felt>, | ||
) -> Result<(), VirtualMachineError> { | ||
if let Some(hook_func) = self.hooks.clone().pre_step_instruction { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same |
||
(hook_func)( | ||
self, | ||
hint_executor, | ||
exec_scope, | ||
hint_data_dictionary, | ||
constants, | ||
)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn execute_post_step_instruction( | ||
&mut self, | ||
hint_executor: &mut dyn HintProcessor, | ||
exec_scope: &mut ExecutionScopes, | ||
hint_data_dictionary: &HashMap<usize, Vec<Box<dyn Any>>>, | ||
constants: &HashMap<String, Felt>, | ||
) -> Result<(), VirtualMachineError> { | ||
if let Some(hook_func) = self.hooks.clone().post_step_instruction { | ||
Oppen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(hook_func)( | ||
self, | ||
hint_executor, | ||
exec_scope, | ||
hint_data_dictionary, | ||
constants, | ||
)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::path::Path; | ||
|
||
use super::*; | ||
use crate::{ | ||
hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor, | ||
types::program::Program, | ||
utils::test_utils::{cairo_runner, vm}, | ||
}; | ||
|
||
#[test] | ||
fn empty_hooks() { | ||
let program = Program::from_file(Path::new("cairo_programs/sqrt.json"), Some("main")) | ||
.expect("Call to `Program::from_file()` failed."); | ||
|
||
let mut hint_processor = BuiltinHintProcessor::new_empty(); | ||
let mut cairo_runner = cairo_runner!(program); | ||
let mut vm = vm!(); | ||
vm.hooks = Hooks::new(None, None, None); | ||
|
||
let end = cairo_runner.initialize(&mut vm).unwrap(); | ||
assert!(cairo_runner | ||
.run_until_pc(end, &mut vm, &mut hint_processor) | ||
.is_ok()); | ||
} | ||
|
||
#[test] | ||
fn hook_failure() { | ||
Oppen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let program = Program::from_file(Path::new("cairo_programs/sqrt.json"), Some("main")) | ||
.expect("Call to `Program::from_file()` failed."); | ||
|
||
fn before_first_step_hook( | ||
_vm: &mut VirtualMachine, | ||
_runner: &mut CairoRunner, | ||
_hint_data: &HashMap<usize, Vec<Box<dyn Any>>>, | ||
) -> Result<(), VirtualMachineError> { | ||
Err(VirtualMachineError::Unexpected) | ||
} | ||
|
||
fn pre_step_hook( | ||
_vm: &mut VirtualMachine, | ||
_hint_processor: &mut dyn HintProcessor, | ||
_exec_scope: &mut ExecutionScopes, | ||
_hint_data: &HashMap<usize, Vec<Box<dyn Any>>>, | ||
_constants: &HashMap<String, Felt>, | ||
) -> Result<(), VirtualMachineError> { | ||
Err(VirtualMachineError::Unexpected) | ||
} | ||
|
||
fn post_step_hook( | ||
_vm: &mut VirtualMachine, | ||
_hint_processor: &mut dyn HintProcessor, | ||
_exec_scope: &mut ExecutionScopes, | ||
_hint_data: &HashMap<usize, Vec<Box<dyn Any>>>, | ||
_constants: &HashMap<String, Felt>, | ||
) -> Result<(), VirtualMachineError> { | ||
Err(VirtualMachineError::Unexpected) | ||
} | ||
|
||
// Before first fail | ||
let mut hint_processor = BuiltinHintProcessor::new_empty(); | ||
let mut cairo_runner = cairo_runner!(program); | ||
let mut vm = vm!(); | ||
vm.hooks = Hooks::new(Some(Arc::new(before_first_step_hook)), None, None); | ||
|
||
let end = cairo_runner.initialize(&mut vm).unwrap(); | ||
assert!(cairo_runner | ||
.run_until_pc(end, &mut vm, &mut hint_processor) | ||
.is_err()); | ||
|
||
// Pre step fail | ||
let mut hint_processor = BuiltinHintProcessor::new_empty(); | ||
let mut cairo_runner = cairo_runner!(program); | ||
let mut vm = vm!(); | ||
vm.hooks = Hooks::new(None, Some(Arc::new(pre_step_hook)), None); | ||
|
||
let end = cairo_runner.initialize(&mut vm).unwrap(); | ||
assert!(cairo_runner | ||
.run_until_pc(end, &mut vm, &mut hint_processor) | ||
.is_err()); | ||
|
||
// Post step fail | ||
let mut hint_processor = BuiltinHintProcessor::new_empty(); | ||
let mut cairo_runner = cairo_runner!(program); | ||
let mut vm = vm!(); | ||
vm.hooks = Hooks::new(None, None, Some(Arc::new(post_step_hook))); | ||
|
||
let end = cairo_runner.initialize(&mut vm).unwrap(); | ||
assert!(cairo_runner | ||
.run_until_pc(end, &mut vm, &mut hint_processor) | ||
.is_err()); | ||
} | ||
|
||
#[test] | ||
fn hook_success() { | ||
Oppen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let program = Program::from_file(Path::new("cairo_programs/sqrt.json"), Some("main")) | ||
.expect("Call to `Program::from_file()` failed."); | ||
|
||
fn before_first_step_hook( | ||
_vm: &mut VirtualMachine, | ||
_runner: &mut CairoRunner, | ||
_hint_data: &HashMap<usize, Vec<Box<dyn Any>>>, | ||
) -> Result<(), VirtualMachineError> { | ||
Ok(()) | ||
} | ||
|
||
fn pre_step_hook( | ||
_vm: &mut VirtualMachine, | ||
_hint_processor: &mut dyn HintProcessor, | ||
_exec_scope: &mut ExecutionScopes, | ||
_hint_data: &HashMap<usize, Vec<Box<dyn Any>>>, | ||
_constants: &HashMap<String, Felt>, | ||
) -> Result<(), VirtualMachineError> { | ||
Ok(()) | ||
} | ||
|
||
fn post_step_hook( | ||
_vm: &mut VirtualMachine, | ||
_hint_processor: &mut dyn HintProcessor, | ||
_exec_scope: &mut ExecutionScopes, | ||
_hint_data: &HashMap<usize, Vec<Box<dyn Any>>>, | ||
_constants: &HashMap<String, Felt>, | ||
) -> Result<(), VirtualMachineError> { | ||
Ok(()) | ||
} | ||
|
||
let mut hint_processor = BuiltinHintProcessor::new_empty(); | ||
let mut cairo_runner = cairo_runner!(program); | ||
let mut vm = vm!(); | ||
vm.hooks = Hooks::new( | ||
Some(Arc::new(before_first_step_hook)), | ||
Some(Arc::new(pre_step_hook)), | ||
Some(Arc::new(post_step_hook)), | ||
); | ||
|
||
let end = cairo_runner.initialize(&mut vm).unwrap(); | ||
assert!(cairo_runner | ||
.run_until_pc(end, &mut vm, &mut hint_processor) | ||
.is_ok()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.