mem: Phase 1.4a — fat-pointer aggregates from #run serialize via host memory

The Phase 1.4 serializer left a silent malformed-const case: when the
interp evaluated a `#run` returning a string (or anything with a fat
pointer inside), the data field came in as a `.int` holding a libc
host address. `LLVMConstInt(ptr_type, addr, 1)` happily emitted `i0 0`
in the static const, and the runtime segfaulted on the first read.

Phase 1.4a closes this for string and slice destinations. The signature
of `valueToLLVMConst` now takes the IR `TypeId` (instead of just the
LLVM type) and a borrowed `*Interpreter`. A new helper
`serializeAggregateValue` splits on the IR type:

- `string` / `slice` (fat pointer `{data, len}`): extract `len`, read
  that many bytes from the data field's address (via `interp.heapSlice`
  for `heap_ptr`, via a new `readHostBytes` for `byte_ptr` / `.int`,
  via slice indexing for string literals). Emit the bytes as a private
  global byte array using the existing `emitConstStringGlobal`. The
  fat-pointer aggregate's data ptr resolves to the byte array's address.
- `struct`: walk the IR field types in lockstep with the value's
  fields; recurse with each declared field TypeId. This replaces the
  old LLVM-type-walk via `LLVMStructGetTypeAtIndex` which couldn't tell
  string-typed fields from generic ptr fields.
- `array`: walk with the element TypeId.

The remaining `.int → ptr` trap (a host address landing in a bare ptr
field outside a fat pointer) now bails loudly with a named diagnostic
identifying it as Phase 1.4a heap-walk follow-up territory. No
practical trigger in-tree, so deferred.

`Interpreter.heapSlice` promoted from package-private to `pub` so
the serializer can read interp-managed heap data.

Regression: `examples/136-comptime-string-global.sx` —
`GREETING :: #run build_greeting();` where `build_greeting` returns
`concat("hello", " world")`. Runtime prints `greeting = 'hello world'`
and `greeting.len = 11`. Pre-1.4a this segfaulted on the first read.

158/158 example tests; chess clean on macOS / iOS sim / Android via
`tools/verify-step.sh`.
This commit is contained in:
agra
2026-05-25 15:45:33 +03:00
parent da1063f1bb
commit 179310d62b
6 changed files with 231 additions and 60 deletions

View File

@@ -673,7 +673,7 @@ pub const LLVMEmitter = struct {
std.debug.print("error: comptime init of '{s}' failed: {s} (op={s}{s}{s})\n", .{ gname, @errorName(err), op, sep, detail });
break :blk .void_val;
};
const init_val = self.valueToLLVMConst(result, llvm_ty, self.ir_mod.types.getString(global.name));
const init_val = self.valueToLLVMConst(result, global.ty, &interp_inst, self.ir_mod.types.getString(global.name));
c.LLVMSetInitializer(llvm_global, init_val);
} else if (global.init_val) |iv| {
const init_val = switch (iv) {
@@ -731,67 +731,68 @@ pub const LLVMEmitter = struct {
}
}
/// Read `len` bytes from `addr` in the current process. Used to lift
/// comptime-evaluated heap data into a static binary constant — the
/// interp ran in this process, so any libc-malloc'd buffer it
/// produced is still mapped and readable. Returns `null` on a
/// null/zero address (callers handle empty-slice as a special case
/// before calling this).
fn readHostBytes(addr: usize, len: usize) ?[]const u8 {
if (addr == 0) return null;
const ptr: [*]const u8 = @ptrFromInt(addr);
return ptr[0..len];
}
/// Serialize an interp `Value` to an LLVM constant for use as a static
/// global initializer. `global_name` is included in any diagnostic the
/// path produces, so the user can locate the offending `#run` site.
/// global initializer. `ty` is the IR-level type of the destination;
/// the LLVM type is derived from it. `interp` gives access to the
/// interpreter's heap so heap_ptr values can be walked. `global_name`
/// is included in any diagnostic the path produces so the user can
/// locate the offending `#run` site.
///
/// Returns `LLVMGetUndef` on bail — the build continues so adjacent
/// constants can still emit, but the surfaced diagnostic surfaces the
/// problem clearly.
/// constants can still emit, but the diagnostic makes the problem clear.
fn valueToLLVMConst(
self: *LLVMEmitter,
val: Value,
llvm_ty: c.LLVMTypeRef,
ty: TypeId,
interp: *const Interpreter,
global_name: []const u8,
) c.LLVMValueRef {
const llvm_ty = self.toLLVMType(ty);
return switch (val) {
.int => |v| c.LLVMConstInt(llvm_ty, @bitCast(v), 1),
.int => |v| blk: {
// Host-pointer-as-int trap: the interp marshals raw pointers
// (libc-malloc'd buffers, etc.) into a .int that holds the
// host address. When that address is meant for a `ptr` slot
// in the destination type, emitting `LLVMConstInt` against
// the ptr type silently produces a malformed `i0 0`. The
// string/slice paths above handle this case by reading the
// pointed-to bytes; anything else with an int landing in a
// ptr slot is a Phase-1.4a heap-walk case we don't yet
// know how to serialize.
const kind = c.LLVMGetTypeKind(llvm_ty);
if (kind == c.LLVMPointerTypeKind) {
std.debug.print(
"error: comptime init of '{s}' produced a raw integer for a pointer field — needs IR-typed heap-walk serialization (Phase 1.4a heap-walk follow-up)\n",
.{global_name},
);
break :blk c.LLVMGetUndef(llvm_ty);
}
break :blk c.LLVMConstInt(llvm_ty, @bitCast(v), 1);
},
.float => |v| c.LLVMConstReal(llvm_ty, v),
.boolean => |v| c.LLVMConstInt(llvm_ty, @intFromBool(v), 0),
.null_val => c.LLVMConstNull(llvm_ty),
.void_val, .undef => c.LLVMGetUndef(llvm_ty),
.func_ref => |fid| self.func_map.get(fid.index()) orelse c.LLVMConstNull(llvm_ty),
.string => |s| self.emitConstStringGlobal(s),
.aggregate => |fields| blk: {
const kind = c.LLVMGetTypeKind(llvm_ty);
if (kind == c.LLVMStructTypeKind) {
const field_count = c.LLVMCountStructElementTypes(llvm_ty);
if (field_count != @as(c_uint, @intCast(fields.len))) {
std.debug.print(
"error: comptime init of '{s}' produced aggregate with {} fields but the destination type expects {}\n",
.{ global_name, fields.len, field_count },
);
break :blk c.LLVMGetUndef(llvm_ty);
}
var field_vals = std.ArrayList(c.LLVMValueRef).empty;
defer field_vals.deinit(self.alloc);
for (fields, 0..) |f, i| {
const field_ty = c.LLVMStructGetTypeAtIndex(llvm_ty, @intCast(i));
field_vals.append(self.alloc, self.valueToLLVMConst(f, field_ty, global_name)) catch unreachable;
}
break :blk c.LLVMConstNamedStruct(llvm_ty, field_vals.items.ptr, @intCast(field_vals.items.len));
}
if (kind == c.LLVMArrayTypeKind) {
const elem_ty = c.LLVMGetElementType(llvm_ty);
var elem_vals = std.ArrayList(c.LLVMValueRef).empty;
defer elem_vals.deinit(self.alloc);
for (fields) |f| {
elem_vals.append(self.alloc, self.valueToLLVMConst(f, elem_ty, global_name)) catch unreachable;
}
break :blk c.LLVMConstArray2(elem_ty, elem_vals.items.ptr, @intCast(elem_vals.items.len));
}
std.debug.print(
"error: comptime init of '{s}' produced an aggregate but the destination LLVM type is neither struct nor array (kind={})\n",
.{ global_name, kind },
);
break :blk c.LLVMGetUndef(llvm_ty);
},
.aggregate => |fields| self.serializeAggregateValue(fields, ty, interp, global_name),
// The remaining Value variants cannot become static binary
// constants. Bail loudly with the global name so the user can
// identify the offending #run site.
// - heap_ptr / byte_ptr: pointer into interp/host memory; can't survive into a binary const without type-threaded serialization (Phase 1.4a follow-up).
// - slot_ptr: frame-local; meaningless outside the call that produced it.
// - closure: env is dynamic.
// - type_tag: compile-time-only Type value.
// constants outside of a fat-pointer aggregate. Bail loudly.
// (`heap_ptr` / `byte_ptr` / `int → ptr` are handled inside
// `serializeAggregateValue` when they appear in a string or
// slice fat-pointer's data field.)
.heap_ptr, .byte_ptr, .slot_ptr, .closure, .type_tag => blk: {
std.debug.print(
"error: comptime init of '{s}' produced a {s} value, which cannot be serialized as a static constant\n",
@@ -802,6 +803,106 @@ pub const LLVMEmitter = struct {
};
}
/// Helper for `valueToLLVMConst` — serialize an aggregate value
/// against an IR TypeId. Splits on the type:
///
/// - `string` / `slice` — fat pointer `{ data, len }`. The data
/// field can be a heap_ptr (interp-managed memory), byte_ptr
/// (raw host address), int (same), or string literal. The len
/// field is consulted to know how many bytes to capture from
/// the data. Bytes are emitted as a private global byte array
/// and the aggregate constant points at it.
/// - `struct` — walk the IR field types in lockstep with the
/// value fields; recurse per field with its declared TypeId.
/// - `array` — walk elements with the array's element TypeId.
fn serializeAggregateValue(
self: *LLVMEmitter,
fields: []const Value,
ty: TypeId,
interp: *const Interpreter,
global_name: []const u8,
) c.LLVMValueRef {
const llvm_ty = self.toLLVMType(ty);
// Fat-pointer types: extract len, then read bytes from the data
// field's address (whatever flavour the interp produced for it).
const is_string = (ty == .string);
const is_slice = !ty.isBuiltin() and self.ir_mod.types.get(ty) == .slice;
if ((is_string or is_slice) and fields.len == 2) {
const data = fields[0];
const len_i = fields[1].asInt() orelse {
std.debug.print(
"error: comptime init of '{s}' produced a fat-pointer aggregate whose len field is not an integer\n",
.{global_name},
);
return c.LLVMGetUndef(llvm_ty);
};
const len: usize = @intCast(len_i);
const bytes_opt: ?[]const u8 = switch (data) {
.heap_ptr => |hp| blk: {
const mem = interp.heapSlice(hp) orelse break :blk null;
break :blk if (len <= mem.len) mem[0..len] else null;
},
.byte_ptr => |addr| readHostBytes(addr, len),
.int => |v| blk: {
if (v == 0 and len == 0) break :blk &.{}; // empty slice
if (v == 0) break :blk null;
break :blk readHostBytes(@as(usize, @bitCast(v)), len);
},
.string => |s| if (len <= s.len) s[0..len] else null,
else => null,
};
const bytes = bytes_opt orelse {
std.debug.print(
"error: comptime init of '{s}' produced a fat-pointer aggregate whose data field ({s}) cannot be resolved to {} bytes — needs Phase 1.4a heap-walk for this shape\n",
.{ global_name, @tagName(data), len },
);
return c.LLVMGetUndef(llvm_ty);
};
return self.emitConstStringGlobal(bytes);
}
// Generic struct: walk IR fields by their declared TypeIds.
if (!ty.isBuiltin()) {
const info = self.ir_mod.types.get(ty);
if (info == .@"struct") {
const ir_fields = info.@"struct".fields;
if (ir_fields.len != fields.len) {
std.debug.print(
"error: comptime init of '{s}' produced aggregate with {} fields but struct '{s}' expects {}\n",
.{ global_name, fields.len, self.ir_mod.types.getString(info.@"struct".name), ir_fields.len },
);
return c.LLVMGetUndef(llvm_ty);
}
var field_vals = std.ArrayList(c.LLVMValueRef).empty;
defer field_vals.deinit(self.alloc);
for (ir_fields, fields) |ir_field, fv| {
field_vals.append(self.alloc, self.valueToLLVMConst(fv, ir_field.ty, interp, global_name)) catch unreachable;
}
return c.LLVMConstNamedStruct(llvm_ty, field_vals.items.ptr, @intCast(field_vals.items.len));
}
if (info == .array) {
const elem_ty = info.array.element;
const llvm_elem_ty = self.toLLVMType(elem_ty);
var elem_vals = std.ArrayList(c.LLVMValueRef).empty;
defer elem_vals.deinit(self.alloc);
for (fields) |fv| {
elem_vals.append(self.alloc, self.valueToLLVMConst(fv, elem_ty, interp, global_name)) catch unreachable;
}
return c.LLVMConstArray2(llvm_elem_ty, elem_vals.items.ptr, @intCast(elem_vals.items.len));
}
}
std.debug.print(
"error: comptime init of '{s}' produced an aggregate but the destination type ({s}) is neither struct, array, string, nor slice\n",
.{ global_name, self.ir_mod.types.typeName(ty) },
);
return c.LLVMGetUndef(llvm_ty);
}
// ── Function declaration ────────────────────────────────────────
fn declareFunction(self: *LLVMEmitter, func: *const Function, func_idx: u32) void {

View File

@@ -296,7 +296,7 @@ pub const Interpreter = struct {
}
}
fn heapSlice(self: *const Interpreter, hp: Value.HeapPtr) ?[]u8 {
pub fn heapSlice(self: *const Interpreter, hp: Value.HeapPtr) ?[]u8 {
if (hp.id >= self.heap.items.len) return null;
const mem = self.heap.items[hp.id];
if (hp.offset >= mem.len) return null;