diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-23 23:46:20 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-23 23:48:21 -0300 |
| commit | 0252c830daf6184a9faefa32a7d1f384f5a86b94 (patch) | |
| tree | c6a9c8d27312e4a096f4a17d8a935aa1aa97264b /core/walk.zig | |
| parent | e71603b674b6f640274f988bb108c66e9392e42d (diff) | |
| download | pardes-0252c830daf6184a9faefa32a7d1f384f5a86b94.tar.gz pardes-0252c830daf6184a9faefa32a7d1f384f5a86b94.zip | |
walk: what a value read back had made before it was left out, or took its default, is freed: the merged gate's shell-test (its SafeAllocator counts every allocation) failed the dump fuzz with 8 leaks once the fuzz read the byte dump and 1024 images (core-test's run does not count the image allocator's): a pane another build's (or a damaged) dump cannot give is left out (`elements`/`listInto`, an array of optional pointers) and a field whose kind changed takes its default or is left as it was (`fields`, by name), but what their read had made before it failed (the pane itself, a name, a slice; a custom type's `dumpRead`) stayed in the reader's list with nothing holding it: freed at no rollback, since the load succeeded; a leak on every Upgrade from a build whose pane another cannot read, and on every such damaged dump; now each reader marks where a value's read began (`Mark`: its made list and its customs) and `letGo` frees what was made since, customs first, each entry left dead (a later reference to it is refused, as a half-made one is); TEST FIRST: walk.zig "what a value left out had made is freed: a pane another build cannot give leaks nothing, from a dump or an image" (a pane whose `n` changed kind with no default, after its name was made: 4 leaks on the base, a pane and its name from each reader; none with it); the dump fuzz under shell-test's allocator passes (13/13)
Co-Authored-By: Claude Opus <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core/walk.zig')
| -rw-r--r-- | core/walk.zig | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/core/walk.zig b/core/walk.zig index 91400bc1..5e91490c 100644 --- a/core/walk.zig +++ b/core/walk.zig @@ -1142,6 +1142,10 @@ fn countOf(n: u64) DecodeError!usize { /// failed load frees. const Made = struct { bytes: []u8, alignment: std.mem.Alignment, gpa: std.mem.Allocator, dead: bool = false }; +/// How far a reader had made things before a value: what that value made +/// is everything after it (`letGo`). +const Mark = struct { made: usize, customs: usize }; + /// A type as the dump's writer had it. const SrcType = struct { kind: Kind, @@ -1227,6 +1231,23 @@ pub const Loader = struct { l.shared.clearRetainingCapacity(); } + fn mark(l: *const Self) Mark { + return .{ .made = l.shared.items.len, .customs = l.customs.items.len }; + } + + /// What was made since `at`, freed: the value it was made for was left + /// out or took its default (another build's, or a damaged dump's), so + /// nothing holds it. A reference to it later is refused (`dead`). + fn letGo(l: *Self, at: Mark) void { + for (l.customs.items[at.customs..]) |c| c.free(c.ptr, c.gpa); + l.customs.shrinkRetainingCapacity(at.customs); + for (l.shared.items[at.made..]) |*m| { + if (m.bytes.len > 0) m.gpa.rawFree(m.bytes, m.alignment, @returnAddress()); + m.bytes = &.{}; + m.dead = true; + } + } + fn made(l: *Self, bytes: []u8, alignment: std.mem.Alignment) DecodeError!usize { try l.shared.append(l.own, .{ .bytes = bytes, .alignment = alignment, .gpa = l.gpa }); return l.shared.items.len - 1; @@ -1589,9 +1610,11 @@ pub const Loader = struct { continue; } try l.fresh(E, &out[i]); + const before = l.mark(); l.value(E, ty, &out[i]) catch |err| { if (err != error.Incompatible) return err; if (droppable) { + l.letGo(before); out[i] = null; l.note("{s}[{d}] left out", .{ l.field, i }); } else failed = true; @@ -1630,10 +1653,13 @@ pub const Loader = struct { found.set(at); inline for (names, 0..) |field, i| if (i == at) { l.field = field; + const before = l.mark(); if (l.oneField(T, field, f.ty, out, packed_struct, existing)) { if (comptime pairAt(T, field)) |p| held[p] = l.filled; } else |err| { if (err != error.Incompatible) return err; + // What the read made before it failed: nothing holds it. + l.letGo(before); const attr = comptime @typeInfo(T).@"struct".field_attrs[std.meta.fieldIndex(T, field).?]; if (comptime attr.default_value_ptr) |dp| { @field(out, field) = @as(*const @FieldType(T, field), @ptrCast(@alignCast(dp))).*; @@ -2066,6 +2092,21 @@ pub const Get = struct { l.memo.clearRetainingCapacity(); } + fn mark(l: *const Get) Mark { + return .{ .made = l.made.items.len, .customs = l.customs.items.len }; + } + + /// Load's `letGo`: what was made since `at`, freed, nothing holding it. + fn letGo(l: *Get, at: Mark) void { + for (l.customs.items[at.customs..]) |c| c.free(c.ptr, c.gpa); + l.customs.shrinkRetainingCapacity(at.customs); + for (l.made.items[at.made..]) |*m| { + if (m.bytes.len > 0) m.gpa.rawFree(m.bytes, m.alignment, @returnAddress()); + m.bytes = &.{}; + m.dead = true; + } + } + pub fn note(l: *Get, comptime fmt: []const u8, args: anytype) void { if (l.notes.items.len > 4096) return; l.notes.print(l.own, fmt ++ "\n", args) catch {}; @@ -2461,9 +2502,11 @@ pub const Get = struct { while (it.next() catch return error.BadDump) |at| : (i += 1) { if (i >= out.len) break; try l.fresh(E, &out[i]); + const before = l.mark(); l.value(E, at, &out[i]) catch |err| { if (err != error.Incompatible) return err; if (droppable) { + l.letGo(before); out[i] = null; l.note("{s}[{d}] left out", .{ l.field, i }); } else failed = true; @@ -2494,10 +2537,13 @@ pub const Get = struct { found.set(at); inline for (names, 0..) |field, i| if (i == at) { l.field = field; + const before = l.mark(); if (l.oneField(T, field, kv.value_cursor, out, existing)) { if (comptime pairAt(T, field)) |p| held[p] = l.filled; } else |err| { if (err != error.Incompatible) return err; + // What the read made before it failed: nothing holds it. + l.letGo(before); const attr = comptime @typeInfo(T).@"struct".field_attrs[std.meta.fieldIndex(T, field).?]; if (comptime attr.default_value_ptr) |dp| { @field(out, field) = @as(*const @FieldType(T, field), @ptrCast(@alignCast(dp))).*; @@ -2874,6 +2920,40 @@ test "a bitset's count read back is held to the bytes left, read or passed over, try std.testing.expectError(error.BadDump, get.get(B, (try Get.member(db.rootCursor().readOnly(), "b")).?)); } +test "what a value left out had made is freed: a pane another build cannot give leaks nothing, from a dump or an image" { + const gpa = std.testing.allocator; + const Old = struct { + const Pane = struct { name: []const u8, n: u32 = 0 }; + panes: [2]?*Pane = @splat(null), + }; + // The pane's `n` changed kind, with no default: the pane is left out, + // after its name was made. + const New = struct { + const Pane = struct { name: []const u8, n: struct { a: u8 } }; + panes: [2]?*Pane = @splat(null), + }; + var one: Old.Pane = .{ .name = "one", .n = 1 }; + const old: Old = .{ .panes = .{ &one, null } }; + const bytes = try save(Old, gpa, &old); + defer gpa.free(bytes); + try std.testing.expect((try load(New, gpa, bytes, null)).panes[0] == null); + var image: std.Io.Writer.Allocating = .init(gpa); + defer image.deinit(); + var db = try Mem.init(std.Io.failing, .{ .buffer = &image, .file_id = 0 }); + var put: Put = .{ .gpa = gpa }; + defer put.deinit(); + try db.runContext(struct { + put: *Put, + old: *const Old, + pub fn run(ctx: @This(), cursor: *Mem.Cursor(.read_write)) !void { + try ctx.put.member(try Mem.HashMap(.read_write).init(cursor.*), "old", Old, ctx.old); + } + }{ .put = &put, .old = &old }); + var get: Get = .init(gpa); + defer get.deinit(); + try std.testing.expect((try get.get(New, (try Get.member(db.rootCursor().readOnly(), "old")).?)).panes[0] == null); +} + test "a dump that is not one, or is cut short, is refused" { const gpa = std.testing.allocator; const S = struct { a: u32, b: []const u8 }; |
