From ff503008ea3540963d9e06b57051cf9bdc5fa1f9 Mon Sep 17 00:00:00 2001 From: Richard Feldman Date: Sun, 9 Nov 2025 21:05:14 -0500 Subject: [PATCH] Update some comments --- src/interpreter_shim/main.zig | 12 +++++++----- test/fx/platform/host.zig | 12 +++++++----- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/interpreter_shim/main.zig b/src/interpreter_shim/main.zig index 79f57be062..39a52bb206 100644 --- a/src/interpreter_shim/main.zig +++ b/src/interpreter_shim/main.zig @@ -76,9 +76,10 @@ const ShimError = error{ /// Expected format in shared memory: [u64 parent_address][u32 entry_count][ModuleEnv data][u32[] def_indices] export fn roc_entrypoint(entry_idx: u32, ops: *builtins.host_abi.RocOps, ret_ptr: *anyopaque, arg_ptr: ?*anyopaque) callconv(.c) void { evaluateFromSharedMemory(entry_idx, ops, ret_ptr, arg_ptr) catch |err| { - var buf: [512]u8 = undefined; - const msg2 = std.fmt.bufPrint(&buf, "Error evaluating from shared memory: {s} (entry_idx={})", .{ @errorName(err), entry_idx }) catch "Error evaluating from shared memory"; + // Use heap allocation for error message to avoid fixed buffer size limits + const msg2 = std.fmt.allocPrint(std.heap.page_allocator, "Error evaluating from shared memory: {s} (entry_idx={})", .{ @errorName(err), entry_idx }) catch "Error evaluating from shared memory"; ops.crash(msg2); + // Note: We're about to crash, so it's ok to leak this allocation }; } @@ -99,15 +100,16 @@ fn initializeSharedMemoryOnce(roc_ops: *RocOps) ShimError!void { } const allocator = std.heap.page_allocator; - var buf: [256]u8 = undefined; // Get page size const page_size = SharedMemoryAllocator.getSystemPageSize() catch 4096; // Create shared memory allocator from coordination info var shm = SharedMemoryAllocator.fromCoordination(allocator, page_size) catch |err| { - const msg2 = std.fmt.bufPrint(&buf, "Failed to create shared memory allocator: {s}", .{@errorName(err)}) catch "Failed to create shared memory allocator"; + // Use heap allocation for error message to avoid fixed buffer size limits + const msg2 = std.fmt.allocPrint(allocator, "Failed to create shared memory allocator: {s}", .{@errorName(err)}) catch "Failed to create shared memory allocator"; roc_ops.crash(msg2); + // Note: We're about to crash, so it's ok to leak this allocation return error.SharedMemoryError; }; @@ -231,7 +233,7 @@ fn setupModuleEnv(shm: *SharedMemoryAllocator, roc_ops: *RocOps) ShimError!*Modu } else ""; // Deserialize the ModuleEnv with the relocated module_name - // Empty string is used for source since it's not needed in the interpreter + // Source text is not available because we don't put it in the shared memory. module_envs[i] = try serialized_ptr.deserialize(offset, std.heap.page_allocator, "", module_name); } diff --git a/test/fx/platform/host.zig b/test/fx/platform/host.zig index d18653525d..94699b46de 100644 --- a/test/fx/platform/host.zig +++ b/test/fx/platform/host.zig @@ -190,10 +190,12 @@ fn platform_main() !void { }; // Call the app's main! entrypoint - var unit_result: [0]u8 = undefined; // Result is {} which is zero-sized - // For a function with signature () => {}, the argument is an empty tuple (zero parameters) - // An empty tuple is zero-sized, so we pass a zero-sized value - // Note: Can't pass null here - Roc-generated code dereferences the pointer even for zero-sized types + var ret: [0]u8 = undefined; // Result is {} which is zero-sized var args: [0]u8 = undefined; - roc__main_for_host(&roc_ops, @as(*anyopaque, @ptrCast(&unit_result)), @as(*anyopaque, @ptrCast(&args))); + // Note: although this is a function with no args and a zero-sized return value, + // we can't currently pass null pointers for either of these because Roc will + // currently dereference both of these eagerly even though it won't use either, + // causing a segfault if you pass null. This should be changed! Dereferencing + // garbage memory is obviously pointless, and there's no reason we should do it. + roc__main_for_host(&roc_ops, @as(*anyopaque, @ptrCast(&ret)), @as(*anyopaque, @ptrCast(&args))); }