From a12eeb644c29c616c48cf21f144132830e9d2d0f Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 2 May 2024 16:34:58 -0300 Subject: [PATCH 1/3] Fix segment arena final_stack --- vm/src/vm/runners/builtin_runner/mod.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/vm/src/vm/runners/builtin_runner/mod.rs b/vm/src/vm/runners/builtin_runner/mod.rs index 886ed00578..722cdbfc8d 100644 --- a/vm/src/vm/runners/builtin_runner/mod.rs +++ b/vm/src/vm/runners/builtin_runner/mod.rs @@ -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(( From 67c49f25bed157c878619bd693c01f20d9708327 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 2 May 2024 16:39:36 -0300 Subject: [PATCH 2/3] Update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a68ff2657..1c1e70cf56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ #### Upcoming Changes +* bugfix: Fix BuiltinRunner::final_stack for SegmentArena[#1747](https://github.com/lambdaclass/cairo-vm/pull/1747) + * `cairo1-run` CLI: Allow loading arguments from file[#1739](https://github.com/lambdaclass/cairo-vm/pull/1739) * BREAKING: Remove unused `CairoRunner` field `original_steps`[#1742](https://github.com/lambdaclass/cairo-vm/pull/1742) From b2177cd5b96c90a36386898ebfde7f842908819f Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 2 May 2024 16:56:00 -0300 Subject: [PATCH 3/3] Update test --- vm/src/vm/runners/builtin_runner/segment_arena.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/vm/src/vm/runners/builtin_runner/segment_arena.rs b/vm/src/vm/runners/builtin_runner/segment_arena.rs index 95d849f541..6b9da00c54 100644 --- a/vm/src/vm/runners/builtin_runner/segment_arena.rs +++ b/vm/src/vm/runners/builtin_runner/segment_arena.rs @@ -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) )))) );