Skip to content
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

bugfix: Fix BuiltinRunner::final_stack for SegmentArena #1747

Merged
merged 7 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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

* bugfix: Fix BuiltinRunner::final_stack for SegmentArena[#1747](https://github.com/lambdaclass/cairo-vm/pull/1747)

* perf: use a more compact representation for `MemoryCell` [#1672](https://github.com/lambdaclass/cairo-vm/pull/1672)
* BREAKING: `Memory::get_value` will now always return `Cow::Owned` variants, code that relied on `Cow::Borrowed` may break

Expand Down
8 changes: 7 additions & 1 deletion vm/src/vm/runners/builtin_runner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,13 @@ impl BuiltinRunner {
))));
}
let stop_ptr = stop_pointer.offset;
let num_instances = self.get_used_instances(segments)?;
let mut num_instances = self.get_used_instances(segments)?;
if matches!(self, BuiltinRunner::SegmentArena(_)) {
// SegmentArena builtin starts with one instance pre-loaded
// This is reflected in the builtin base's offset, but as we compare `stop_ptr.offset` agains `used`
// instead of comparing `stop_ptr` against `base + used` we need to account for the base offset (aka the pre-loaded instance) here
num_instances += 1;
}
let used = num_instances * self.cells_per_instance() as usize;
if stop_ptr != used {
return Err(RunnerError::InvalidStopPointer(Box::new((
Expand Down
8 changes: 7 additions & 1 deletion vm/src/vm/runners/builtin_runner/segment_arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,17 @@ mod tests {
vm.segments.segment_used_sizes = Some(vec![6]);

let pointer = Relocatable::from((2, 2));
// USED_CELLS = SEGMENT_USED_SIZE - INITIAL_SIZE = 6 - 3 = 3
// NUM_INSTANCES = DIV_CEIL(USED_CELLS, CELLS_PER_INSTANCE) = DIV_CEIL(3, 3) = 1

// STOP_PTR == BASE + NUM_INSTANCES * CELLS_PER_INSTANCE
// (0, 0) == (0, 3) + 1 * 3
// (0, 0) == (0, 6)
assert_eq!(
builtin.final_stack(&vm.segments, pointer),
Err(RunnerError::InvalidStopPointer(Box::new((
BuiltinName::segment_arena,
relocatable!(0, 3),
relocatable!(0, 6),
relocatable!(0, 0)
))))
);
Expand Down
Loading