Skip to content

[DNM] feat: implement VirtualMachine::is_accessed #2028

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 1 commit 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: implement VirtualMachine::is_accessed [#2024](https://github.com/lambdaclass/cairo-vm/pull/2024)

* fix: Keep None values in memory segments for the prover input info [#2021](https://github.com/lambdaclass/cairo-vm/pull/2021)

* refactor: Clap attribute macros from #[clap(...)] to #[arg(...)] and #[command(...)] in v4.x [#2003] (https://github.com/lambdaclass/cairo-vm/pull/2003)
Expand Down
4 changes: 4 additions & 0 deletions vm/src/vm/vm_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,10 @@
self.segments.memory.mem_eq(lhs, rhs, len)
}

pub fn is_accessed(&self, addr: &Relocatable) -> Result<bool, MemoryError> {
self.segments.is_accessed(addr)
}

Check warning on line 976 in vm/src/vm/vm_core.rs

View check run for this annotation

Codecov / codecov/patch

vm/src/vm/vm_core.rs#L974-L976

Added lines #L974 - L976 were not covered by tests

///Gets `n_ret` return values from memory
pub fn get_return_values(&self, n_ret: usize) -> Result<Vec<MaybeRelocatable>, MemoryError> {
let addr = (self.run_context.get_ap() - n_ret)
Expand Down
25 changes: 18 additions & 7 deletions vm/src/vm/vm_memory/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,7 @@
{
let relocatable: Relocatable = key.try_into().ok()?;

let data = if relocatable.segment_index.is_negative() {
&self.temp_data
} else {
&self.data
};
let (i, j) = from_relocatable_to_indexes(relocatable);
let value = data.get(i)?.get(j)?.get_value()?;
let value = self.get_cell(relocatable)?.get_value()?;
Some(Cow::Owned(self.relocate_value(&value).ok()?.into_owned()))
}

Expand Down Expand Up @@ -648,6 +642,23 @@
Ok(values)
}

fn get_cell(&self, addr: Relocatable) -> Option<&MemoryCell> {
let (i, j) = from_relocatable_to_indexes(addr);
let data = if addr.segment_index < 0 {
&self.temp_data
} else {
&self.data
};
data.get(i)?.get(j)
}

pub fn is_accessed(&self, addr: &Relocatable) -> Result<bool, MemoryError> {
Ok(self
.get_cell(*addr)
.ok_or(MemoryError::UnknownMemoryCell(Box::new(*addr)))?
.is_accessed())
}

Check warning on line 660 in vm/src/vm/vm_memory/memory.rs

View check run for this annotation

Codecov / codecov/patch

vm/src/vm/vm_memory/memory.rs#L655-L660

Added lines #L655 - L660 were not covered by tests

pub fn mark_as_accessed(&mut self, addr: Relocatable) {
let (i, j) = from_relocatable_to_indexes(addr);
let data = if addr.segment_index < 0 {
Expand Down
4 changes: 4 additions & 0 deletions vm/src/vm/vm_memory/memory_segments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@
}
}

pub fn is_accessed(&self, addr: &Relocatable) -> Result<bool, MemoryError> {
self.memory.is_accessed(addr)
}

Check warning on line 205 in vm/src/vm/vm_memory/memory_segments.rs

View check run for this annotation

Codecov / codecov/patch

vm/src/vm/vm_memory/memory_segments.rs#L203-L205

Added lines #L203 - L205 were not covered by tests

/// Counts the memory holes (aka unaccessed memory cells) in memory
/// # Parameters
/// - `builtin_segment_indexes`: Set representing the segments indexes of the builtins initialized in the VM, except for the output builtin.
Expand Down
Loading