Skip to content

fix: add VM Execution State #2043

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

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#### Upcoming Changes

* feat: add ExecutionState structure and get_execution_state method [#2043](https://github.com/lambdaclass/cairo-vm/pull/2043)

* feat: add get_current_step getter [#2034](https://github.com/lambdaclass/cairo-vm/pull/2034)

* feat: implement VirtualMachine::is_accessed [#2033](https://github.com/lambdaclass/cairo-vm/pull/2033)
Expand Down
27 changes: 27 additions & 0 deletions vm/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,3 +327,30 @@ fn get_casm_contract_builtins(
.map(|s| BuiltinName::from_str(s).expect("Invalid builtin name"))
.collect()
}

#[cfg(test)]
mod execution_state_tests {
use crate::vm::vm_core::VirtualMachine;

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_get_execution_state_integration() {
// Create a virtual machine directly for testing
let mut vm = VirtualMachine::new(true, false);

// Set some values for testing
vm.run_context.ap = 123;
vm.run_context.fp = 456;
vm.current_step = 10;

// Get the execution state
let execution_state = vm.get_execution_state();

// Check the main state attributes
assert_eq!(execution_state.ap, 123);
assert_eq!(execution_state.fp, 456);
assert_eq!(execution_state.current_step, 10);
assert_eq!(execution_state.run_finished, false); // Expect false as we didn't set it
assert!(execution_state.memory_segments_count > 0);
}
}
68 changes: 68 additions & 0 deletions vm/src/vm/vm_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,25 @@ pub struct VirtualMachine {
pub(crate) relocation_table: Option<Vec<usize>>,
}

/// Structure for storing the execution state of a virtual machine
#[derive(Debug, Clone, PartialEq)]
pub struct ExecutionState {
/// Current program counter (Program Counter)
pub pc: Relocatable,
/// Current allocation pointer (Allocation Pointer)
pub ap: usize,
/// Current frame pointer (Frame Pointer)
pub fp: usize,
/// Current execution step
pub current_step: usize,
/// Whether execution is finished
pub run_finished: bool,
/// Number of memory segments
pub memory_segments_count: usize,
/// Information about active built-ins
pub active_builtins: Vec<String>,
}

impl VirtualMachine {
pub fn new(trace_enabled: bool, disable_trace_padding: bool) -> VirtualMachine {
let run_context = RunContext {
Expand Down Expand Up @@ -1249,6 +1268,30 @@ impl VirtualMachine {
.finalize(Some(info.size), info.index as usize, None)
}
}

/// Returns the current execution state of the virtual machine
///
/// This function gathers all important components of the VM state, including
/// the current registers (pc, ap, fp), execution step information, and
/// active built-in functions.
///
/// # Return value
///
/// `ExecutionState` structure containing the current VM state
pub fn get_execution_state(&self) -> ExecutionState {
ExecutionState {
pc: self.run_context.pc,
ap: self.run_context.ap,
fp: self.run_context.fp,
current_step: self.current_step,
run_finished: self.run_finished,
memory_segments_count: self.segments.num_segments(),
active_builtins: self.builtin_runners
.iter()
.map(|runner| runner.name().to_string())
.collect(),
}
}
}

pub struct VirtualMachineBuilder {
Expand Down Expand Up @@ -5390,4 +5433,29 @@ mod tests {
Some(6)
);
}

#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn get_execution_state_test() {
let mut vm = vm!();
vm.run_context.ap = 1;
vm.run_context.fp = 2;
vm.run_context.pc = Relocatable::from((3, 4));
vm.current_step = 5;
vm.run_finished = true;
vm.builtin_runners.push(BuiltinRunner::from(HashBuiltinRunner::new(
Some(12),
true,
)));
vm.segments.segment_used_sizes = Some(vec![1]);

let execution_state = vm.get_execution_state();
assert_eq!(execution_state.pc, Relocatable::from((3, 4)));
assert_eq!(execution_state.ap, 1);
assert_eq!(execution_state.fp, 2);
assert_eq!(execution_state.current_step, 5);
assert_eq!(execution_state.run_finished, true);
assert_eq!(execution_state.memory_segments_count, 1);
assert_eq!(execution_state.active_builtins, vec!["pedersen".to_string()]);
}
}