From ece8e27545e63f250502adbcea5f3b42b5b05e83 Mon Sep 17 00:00:00 2001 From: crStiv Date: Tue, 1 Apr 2025 20:09:59 +0200 Subject: [PATCH 1/7] Update mod.rs --- vm/src/tests/mod.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/vm/src/tests/mod.rs b/vm/src/tests/mod.rs index ecf44b2c71..ca8321ec9b 100644 --- a/vm/src/tests/mod.rs +++ b/vm/src/tests/mod.rs @@ -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); + } +} From f1258ae89911119457b0dc42c0aa62b992db833e Mon Sep 17 00:00:00 2001 From: crStiv Date: Tue, 1 Apr 2025 20:15:19 +0200 Subject: [PATCH 2/7] Update vm_core.rs --- vm/src/vm/vm_core.rs | 77 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/vm/src/vm/vm_core.rs b/vm/src/vm/vm_core.rs index be89d2821d..ade73a41e0 100644 --- a/vm/src/vm/vm_core.rs +++ b/vm/src/vm/vm_core.rs @@ -103,6 +103,25 @@ pub struct VirtualMachine { pub(crate) relocation_table: Option>, } +/// Структура для хранения состояния выполнения виртуальной машины +#[derive(Debug, Clone, PartialEq)] +pub struct ExecutionState { + /// Текущий счетчик программы (Program Counter) + pub pc: Relocatable, + /// Текущий указатель распределения (Allocation Pointer) + pub ap: usize, + /// Текущий указатель кадра (Frame Pointer) + pub fp: usize, + /// Текущий шаг выполнения + pub current_step: usize, + /// Завершено ли выполнение + pub run_finished: bool, + /// Количество сегментов памяти + pub memory_segments_count: usize, + /// Информация о работающих builtins + pub active_builtins: Vec, +} + impl VirtualMachine { pub fn new(trace_enabled: bool, disable_trace_padding: bool) -> VirtualMachine { let run_context = RunContext { @@ -1249,6 +1268,30 @@ impl VirtualMachine { .finalize(Some(info.size), info.index as usize, None) } } + + /// Возвращает текущее состояние выполнения виртуальной машины + /// + /// Эта функция собирает все важные компоненты состояния VM, включая + /// текущие регистры (pc, ap, fp), информацию о шаге выполнения и + /// активных builtin функциях. + /// + /// # Возвращаемое значение + /// + /// `ExecutionState` структура с текущим состоянием VM + 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 { @@ -5390,4 +5433,38 @@ mod tests { Some(6) ); } + + /// Returns the current execution state of the virtual machine + /// + /// This function gathers all essential components of the VM state, including + /// the current registers (pc, ap, fp), execution step information, and + /// active built-in functions. + /// + /// # Return Value + /// + /// `ExecutionState` is a structure containing the current state of the VM + #[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()]); + } } From c666dcb9c59dcea732d29f2c54f5b89d076ccc80 Mon Sep 17 00:00:00 2001 From: crStiv Date: Tue, 1 Apr 2025 21:22:31 +0200 Subject: [PATCH 3/7] Update vm_core.rs --- vm/src/vm/vm_core.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/vm/src/vm/vm_core.rs b/vm/src/vm/vm_core.rs index ade73a41e0..b77a8755c8 100644 --- a/vm/src/vm/vm_core.rs +++ b/vm/src/vm/vm_core.rs @@ -103,22 +103,22 @@ pub struct VirtualMachine { pub(crate) relocation_table: Option>, } -/// Структура для хранения состояния выполнения виртуальной машины +/// Structure for storing the execution state of a virtual machine #[derive(Debug, Clone, PartialEq)] pub struct ExecutionState { - /// Текущий счетчик программы (Program Counter) + /// Current program counter (Program Counter) pub pc: Relocatable, - /// Текущий указатель распределения (Allocation Pointer) + /// Current allocation pointer (Allocation Pointer) pub ap: usize, - /// Текущий указатель кадра (Frame Pointer) + /// 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, - /// Информация о работающих builtins + /// Information about active built-ins pub active_builtins: Vec, } From 33980a590c2880c42d9bb30dea6dd6308acb859f Mon Sep 17 00:00:00 2001 From: crStiv Date: Tue, 1 Apr 2025 21:23:33 +0200 Subject: [PATCH 4/7] Update vm_core.rs --- vm/src/vm/vm_core.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/vm/src/vm/vm_core.rs b/vm/src/vm/vm_core.rs index b77a8755c8..b21eb27fd2 100644 --- a/vm/src/vm/vm_core.rs +++ b/vm/src/vm/vm_core.rs @@ -1269,15 +1269,15 @@ impl VirtualMachine { } } - /// Возвращает текущее состояние выполнения виртуальной машины + /// Returns the current execution state of the virtual machine /// - /// Эта функция собирает все важные компоненты состояния VM, включая - /// текущие регистры (pc, ap, fp), информацию о шаге выполнения и - /// активных builtin функциях. + /// 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` структура с текущим состоянием VM + /// `ExecutionState` structure containing the current VM state pub fn get_execution_state(&self) -> ExecutionState { ExecutionState { pc: self.run_context.pc, From fe500b73ad3e359476335999d314cebb90c59e9d Mon Sep 17 00:00:00 2001 From: crStiv Date: Wed, 2 Apr 2025 10:46:52 +0200 Subject: [PATCH 5/7] Update vm_core.rs --- vm/src/vm/vm_core.rs | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/vm/src/vm/vm_core.rs b/vm/src/vm/vm_core.rs index b21eb27fd2..9651e9265e 100644 --- a/vm/src/vm/vm_core.rs +++ b/vm/src/vm/vm_core.rs @@ -5434,16 +5434,6 @@ mod tests { ); } - /// Returns the current execution state of the virtual machine - /// - /// This function gathers all essential components of the VM state, including - /// the current registers (pc, ap, fp), execution step information, and - /// active built-in functions. - /// - /// # Return Value - /// - /// `ExecutionState` is a structure containing the current state of the VM - #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_execution_state_test() { let mut vm = vm!(); From 1066c2476a67143330f50f71090921b5966bdd76 Mon Sep 17 00:00:00 2001 From: crStiv Date: Wed, 2 Apr 2025 10:47:53 +0200 Subject: [PATCH 6/7] Update vm_core.rs --- vm/src/vm/vm_core.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/vm/src/vm/vm_core.rs b/vm/src/vm/vm_core.rs index 9651e9265e..11d8289ec4 100644 --- a/vm/src/vm/vm_core.rs +++ b/vm/src/vm/vm_core.rs @@ -5434,6 +5434,7 @@ mod tests { ); } + #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_execution_state_test() { let mut vm = vm!(); From c4411529c6df1c2e030bef2db7f6375d65a0da42 Mon Sep 17 00:00:00 2001 From: crStiv Date: Wed, 2 Apr 2025 10:49:52 +0200 Subject: [PATCH 7/7] Update CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e0afbb34a..80505f88ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)