Skip to content

Commit

Permalink
Test builtin getMemorySegmentAddresses after set stop ptr (#479)
Browse files Browse the repository at this point in the history
Test builtin getMemorySegmentAddresses after set stop ptr

Co-authored-by: Nikita Orlov <[email protected]>
  • Loading branch information
tcoratger and StringNick authored Mar 22, 2024
1 parent a61b1c2 commit 63d5b40
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 3 deletions.
106 changes: 105 additions & 1 deletion src/vm/builtins/builtin_runner/builtin_runner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,6 @@ pub const BuiltinRunner = union(BuiltinName) {
/// - An optional stop address of the memory segment (may be `null`).
pub fn getMemorySegmentAddresses(self: *Self) Tuple(&.{ usize, ?usize }) {
return switch (self.*) {
.SegmentArena => .{ 0, 0 },
inline else => |*builtin| builtin.getMemorySegmentAddresses(),
};
}
Expand Down Expand Up @@ -612,6 +611,35 @@ pub const BuiltinRunner = union(BuiltinName) {
};
}

/// Sets the stop pointer for the built-in runner.
///
/// This method sets the stop pointer for the specific type of built-in runner.
/// The stop pointer is used to control execution flow or terminate execution
/// based on certain conditions.
///
/// # Arguments
///
/// - `stop_ptr`: The stop pointer value to set.
///
/// # Remarks
///
/// This method is part of the `BuiltinRunner` union(enum) and is used to
/// set the stop pointer for various built-in runners.
pub fn setStopPtr(self: *Self, stop_ptr: usize) void {
switch (self.*) {
inline else => |*builtin| builtin.stop_ptr = stop_ptr,
}
}

/// Deinitializes the built-in runner.
///
/// This method is used to deinitialize the specific type of built-in runner,
/// performing cleanup operations and releasing any allocated resources.
///
/// # Remarks
///
/// This method should be called when the built-in runner is no longer needed
/// to properly release resources and prevent memory leaks.
pub fn deinit(self: *Self) void {
switch (self.*) {
.EcOp => |*ec_op| ec_op.deinit(),
Expand Down Expand Up @@ -1837,3 +1865,79 @@ test "BuiltinRunner: getMemorySegmentAddresses" {
poseidon_builtin.getMemorySegmentAddresses(),
);
}

test "BuiltinRunner: getMemorySegmentAddresses after set stop ptr" {

// Initialize an array list of `BuiltinRunner` instances
var builtins = std.ArrayList(BuiltinRunner).init(std.testing.allocator);

// Defer the deinitialization of the array list to ensure cleanup
defer builtins.deinit();

// Append various types of built-in runners to the array list

// Append a BitwiseBuiltinRunner instance
try builtins.append(.{
.Bitwise = BitwiseBuiltinRunner.init(&.{}, false),
});

// Append an EcOpBuiltinRunner instance
try builtins.append(.{
.EcOp = EcOpBuiltinRunner.init(std.testing.allocator, .{}, false),
});

// Append a HashBuiltinRunner instance
try builtins.append(.{
.Hash = HashBuiltinRunner.init(std.testing.allocator, 1, false),
});

// Append an OutputBuiltinRunner instance
try builtins.append(.{
.Output = OutputBuiltinRunner.init(std.testing.allocator, false),
});

// Append a RangeCheckBuiltinRunner instance
try builtins.append(.{
.RangeCheck = RangeCheckBuiltinRunner.init(8, 8, false),
});

// Initialize a KeccakInstanceDef instance and defer its deinitialization
var keccak_instance_def = try KeccakInstanceDef.initDefault(std.testing.allocator);
defer keccak_instance_def.deinit();

// Append a KeccakBuiltinRunner instance
try builtins.append(.{
.Keccak = try KeccakBuiltinRunner.init(std.testing.allocator, &keccak_instance_def, false),
});

// Append a SignatureBuiltinRunner instance
try builtins.append(.{
.Signature = SignatureBuiltinRunner.init(std.testing.allocator, &.{}, false),
});

// Append a PoseidonBuiltinRunner instance
try builtins.append(.{
.Poseidon = PoseidonBuiltinRunner.init(std.testing.allocator, 32, false),
});

// Append a SegmentArenaBuiltinRunner instance
try builtins.append(.{
.SegmentArena = SegmentArenaBuiltinRunner.init(false),
});

// Define a constant pointer value `ptr`
const ptr: usize = 3;

// Iterate over each item in the `builtins` array list
for (builtins.items) |*builtin| {

// Deinitialize the current built-in runner
builtin.deinit();

// Set the stop pointer for the current built-in runner
builtin.setStopPtr(ptr);

// Expect that the second element of the result of `getMemorySegmentAddresses` is equal to `ptr`
try expectEqual(ptr, builtin.getMemorySegmentAddresses()[1]);
}
}
10 changes: 8 additions & 2 deletions src/vm/builtins/builtin_runner/keccak.zig
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,20 @@ pub const KeccakBuiltinRunner = struct {
instance_def: *const KeccakInstanceDef,
included: bool,
) !Self {
var state_rep = try instance_def.state_rep.clone();
errdefer state_rep.deinit();

var cache = AutoHashMap(Relocatable, Felt252).init(allocator);
errdefer cache.deinit();

return .{
.ratio = instance_def.ratio,
.n_input_cells = @intCast(instance_def.state_rep.items.len),
.cells_per_instance = instance_def.cellsPerBuiltin(),
.included = included,
.state_rep = try instance_def.state_rep.clone(),
.state_rep = state_rep,
.instances_per_component = instance_def.instance_per_component,
.cache = AutoHashMap(Relocatable, Felt252).init(allocator),
.cache = cache,
};
}

Expand Down
4 changes: 4 additions & 0 deletions src/vm/builtins/builtin_runner/segment_arena.zig
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ pub const SegmentArenaBuiltinRunner = struct {
return result;
}

pub fn getMemorySegmentAddresses(self: *const Self) std.meta.Tuple(&.{ usize, ?usize }) {
return .{ @intCast(self.base.segment_index), self.stop_ptr };
}

pub fn deduceMemoryCell(
self: *const Self,
address: Relocatable,
Expand Down
1 change: 1 addition & 0 deletions src/vm/types/keccak_instance_def.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub const KeccakInstanceDef = struct {
/// A new `KeccakInstanceDef` instance initialized with default values.
pub fn initDefault(allocator: Allocator) !Self {
var instance_per_component = ArrayList(u32).init(allocator);
errdefer instance_per_component.deinit();
try instance_per_component.appendNTimes(200, 8);
return .{ .state_rep = instance_per_component };
}
Expand Down

0 comments on commit 63d5b40

Please sign in to comment.