diff --git a/src/integration_tests.zig b/src/integration_tests.zig index f07be99d..4a28aca1 100644 --- a/src/integration_tests.zig +++ b/src/integration_tests.zig @@ -302,5 +302,5 @@ pub fn cairo_run(allocator: std.mem.Allocator, pathname: []const u8, layout: []c // then var hint_processor: HintProcessor = .{}; try runner.runUntilPC(end, extensive_hints, &hint_processor); - try runner.endRun(); + try runner.endRun(allocator, true, false, &hint_processor); } diff --git a/src/vm/cairo_run.zig b/src/vm/cairo_run.zig index ad904999..a8dee5d6 100644 --- a/src/vm/cairo_run.zig +++ b/src/vm/cairo_run.zig @@ -74,7 +74,7 @@ pub fn runConfig(allocator: Allocator, config: Config) !void { // TODO: make flag for extensive_hints var hint_processor: HintProcessor = .{}; try runner.runUntilPC(end, false, &hint_processor); - try runner.endRun(); + try runner.endRun(allocator, true, false, &hint_processor); // TODO readReturnValues necessary for builtins if (config.output_trace != null or config.output_memory != null) { diff --git a/src/vm/runners/cairo_runner.zig b/src/vm/runners/cairo_runner.zig index a25f8591..deb8be69 100644 --- a/src/vm/runners/cairo_runner.zig +++ b/src/vm/runners/cairo_runner.zig @@ -715,14 +715,28 @@ pub const CairoRunner = struct { } } - pub fn endRun(self: *Self) !void { - // TODO relocate memory - // TODO call end_run in vm for builtins - if (self.run_ended) { + pub fn endRun( + self: *Self, + allocator: Allocator, + disable_trace_padding: bool, + disable_finalize_all: bool, + hint_processor: *HintProcessor, + ) !void { + if (self.run_ended) return CairoRunnerError.EndRunAlreadyCalled; + + try self.vm.segments.memory.relocateMemory(); + try self.vm.endRun(allocator, &self.execution_scopes); + + if (disable_finalize_all) return; + + _ = try self.vm.computeSegmentsEffectiveSizes(false); + + if (self.isProofMode() and !disable_trace_padding) { + // TODO : complete proof mode + _ = hint_processor; } - // TODO handle proof_mode case self.run_ended = true; } @@ -4342,3 +4356,56 @@ test "CairoRunner: checkDilutedCheckUsage with bitwise builtin" { // Call checkDilutedCheckUsage method and expect no error. try cairo_runner.checkDilutedCheckUsage(std.testing.allocator); } + +test "CairoRunner: endRun with a run that is already finished" { + // Create a CairoRunner instance for testing. + var cairo_runner = try CairoRunner.init( + std.testing.allocator, + try Program.initDefault(std.testing.allocator, true), + "plain", + ArrayList(MaybeRelocatable).init(std.testing.allocator), + try CairoVM.init( + std.testing.allocator, + .{}, + ), + false, + ); + defer cairo_runner.deinit(std.testing.allocator); + + cairo_runner.run_ended = true; + + var hint_processor: HintProcessor = .{}; + + try expectError( + CairoRunnerError.EndRunAlreadyCalled, + cairo_runner.endRun(std.testing.allocator, true, false, &hint_processor), + ); +} + +test "CairoRunner: endRun with a simple test" { + // Create a CairoRunner instance for testing. + var cairo_runner = try CairoRunner.init( + std.testing.allocator, + try Program.initDefault(std.testing.allocator, true), + "plain", + ArrayList(MaybeRelocatable).init(std.testing.allocator), + try CairoVM.init( + std.testing.allocator, + .{}, + ), + false, + ); + defer cairo_runner.deinit(std.testing.allocator); + + var hint_processor: HintProcessor = .{}; + + try cairo_runner.endRun(std.testing.allocator, true, false, &hint_processor); + + cairo_runner.run_ended = false; + + cairo_runner.relocated_memory.clearAndFree(); + + try cairo_runner.endRun(std.testing.allocator, true, true, &hint_processor); + + try expect(!cairo_runner.run_ended); +}