summaryrefslogtreecommitdiff
path: root/core/walk.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-24 05:55:02 -0300
committerGabriel Schneider <[email protected]>2026-11-24 06:20:21 -0300
commitc90db8c04787d923bbef500eb78c68fd65715cf0 (patch)
treeb4fd8184d077ada92113485f94c4e7ac4d459085 /core/walk.zig
parent9ebe0ab174b25d488dfbf20a9b9d9827fc564b6c (diff)
downloadpardes-c90db8c04787d923bbef500eb78c68fd65715cf0.tar.gz
pardes-c90db8c04787d923bbef500eb78c68fd65715cf0.zip
walk, views, Core: what the 40,000-seed dump fuzz reached once a view's geometry is in the dump again (its seeds land elsewhere; and the dump holds its tree's path, so each tree's seeds damage bytes of their own), each a run first: (1) THE WORKSPACE'S RUN (my gate's fuzz40k, the join road): `views.undumped` took `bit(d.k)` of a view's shares whose `k` was past any view (`integer does not fit in destination type` at views.zig `bit`), and `Shares.k` declared an index (`slots`) did NOT hold it: the `[]Shares` was a REFERENCE (walk.zig `Loader.ref`) to bytes made for another type, read as shares without being decoded (no-panic-1's open item, "references read back as another type", reached): every allocation a load makes records what it holds (`Made.of`, `typeWord` of its element), and a reference read back is to a value of its own type or the dump is refused (`Loader.ref`, `Get.again`, at a pointer's and a slice's, in both readers: a damaged one had one type's bytes read as another's, pointers and alignment and all; the writers share a reference between values of one type only, so no session's dump is refused); the skips' placeholders hold no type, so a reference to one is refused; `Shares.k` stays declared (a slot read back is held); (3) THE WORKSPACE'S RUN with (1) (my gate h2's fuzz40k): leaked 1 allocation, 7 bytes, a byte dump's theme (dump.zig `loadKnowing`: `l.get([]const u8)` failed after its slice was made, and the load's `defer` cut its books back to before the mounts, dropping the half-made slice unfreed: a leak at every such damaged join dump or Restore): a root read that fails lets go of what it had made (`Loader.get`, `Get.get`: `mark`/`letGo`, merge65's mechanism), so no caller's books hold half of a value; (2) MY SECOND TREE'S RUN (the join road): `Pardes.resize_count` read back as the most a usize holds, `+= 1` at the next resize (`integer overflow`, Core.zig `updateInner`, the fuzz's `use` resizing to 40x12): a count that only grows saturates (`+|=`); the fuzz with them: 40000 join and 1779 image seeds green in my second tree three times (212 seeds refused by `.within` each: 211 a pane's rect off its screen, 1 a column; 210 on the join road, 2 on the image's); in the workspace 40000 join seeds green with no leak (206 refused by `.within`), the image road green to seed 1366: seed 1367 there reaches xitdb's own reader (`switch on corrupt value`, xitdb lib.zig `readBytesObjectAlloc`, from Db.zig `Read.open` in dump.check: the last agent's seed 1780 in another tree, a dependency's, not here)
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.zig55
1 files changed, 34 insertions, 21 deletions
diff --git a/core/walk.zig b/core/walk.zig
index 51b0a384..7a77fdb6 100644
--- a/core/walk.zig
+++ b/core/walk.zig
@@ -1398,8 +1398,11 @@ fn countOf(n: u64) DecodeError!usize {
}
/// An allocation a load made: what a reference to it finds, and what a
-/// failed load frees.
-const Made = struct { bytes: []u8, alignment: std.mem.Alignment, gpa: std.mem.Allocator, dead: bool = false };
+/// failed load frees. `of`: what it holds (`typeWord` of its element); a
+/// reference read back is to a value of its own type, or the dump is
+/// refused (a damaged one would have one type's bytes read as another's,
+/// pointers and all).
+const Made = struct { bytes: []u8, alignment: std.mem.Alignment, gpa: std.mem.Allocator, dead: bool = false, of: u64 = 0 };
/// How far a reader had made things before a value: what that value made
/// is everything after it (`letGo`).
@@ -1509,8 +1512,8 @@ pub const Loader = struct {
}
}
- 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 });
+ fn made(l: *Self, bytes: []u8, alignment: std.mem.Alignment, of: u64) DecodeError!usize {
+ try l.shared.append(l.own, .{ .bytes = bytes, .alignment = alignment, .gpa = l.gpa, .of = of });
return l.shared.items.len - 1;
}
@@ -1609,6 +1612,11 @@ pub const Loader = struct {
/// The next root, made: what it points at is `gpa`'s.
pub fn get(l: *Self, comptime T: type) DecodeError!T {
+ // A root that fails lets go of what it had made: its caller's books
+ // (a load's, cut back past what it frees itself) never hold half
+ // of a value.
+ const at = l.mark();
+ errdefer l.letGo(at);
var out: T = undefined;
try l.fresh(T, &out);
try l.value(T, try l.root(), &out);
@@ -1750,12 +1758,12 @@ pub const Loader = struct {
},
.one => {
const C = @typeInfo(T).pointer.child;
- if (try l.ref()) |again| {
+ if (try l.ref(C)) |again| {
out.* = @ptrCast(@alignCast(again.ptr));
return;
}
const one = try l.gpa.create(C);
- const at = l.made(std.mem.asBytes(one), .of(C)) catch |err| {
+ const at = l.made(std.mem.asBytes(one), .of(C), typeWord(C)) catch |err| {
l.gpa.destroy(one);
return err;
};
@@ -1821,22 +1829,23 @@ pub const Loader = struct {
/// A reference to a pointer or slice made before, or null for a
/// new one (its length then read).
- fn ref(l: *Self) DecodeError!?[]u8 {
+ fn ref(l: *Self, comptime E: type) DecodeError!?[]u8 {
const n = try l.scalar(u32);
if (n == 0) return null;
if (n > l.shared.items.len) return error.BadDump;
const got = l.shared.items[n - 1];
if (got.dead) return error.Incompatible;
+ if (got.of != typeWord(E)) return error.BadDump;
return got.bytes;
}
fn slice(l: *Self, comptime E: type, s: SrcType) DecodeError![]E {
- if (try l.ref()) |again| return @as([*]E, @ptrCast(@alignCast(again.ptr)))[0 .. again.len / @max(1, @sizeOf(E))];
+ if (try l.ref(E)) |again| return @as([*]E, @ptrCast(@alignCast(again.ptr)))[0 .. again.len / @max(1, @sizeOf(E))];
const n = try l.count();
if (n == 0) return &.{};
if (n > l.bytes.len - l.at) return error.EndOfDump;
const many = try l.gpa.alloc(E, n);
- const at = l.made(std.mem.sliceAsBytes(many), .of(E)) catch |err| {
+ const at = l.made(std.mem.sliceAsBytes(many), .of(E), typeWord(E)) catch |err| {
l.gpa.free(many);
return err;
};
@@ -1994,14 +2003,14 @@ pub const Loader = struct {
.float => _ = try l.take(s.bits / 8),
.optional => if ((try l.take(1))[0] != 0) try l.skip(s.child),
.one => if (try l.scalar(u32) == 0) {
- _ = try l.made(&.{}, .@"1");
+ _ = try l.made(&.{}, .@"1", 0);
l.shared.items[l.shared.items.len - 1].dead = true;
try l.skip(s.child);
},
.slice => if (try l.scalar(u32) == 0) {
const n = try l.scalar(u64);
if (n > 0) {
- _ = try l.made(&.{}, .@"1");
+ _ = try l.made(&.{}, .@"1", 0);
l.shared.items[l.shared.items.len - 1].dead = true;
}
try l.skipMany(s.child, n);
@@ -2390,6 +2399,9 @@ pub const Get = struct {
/// The value at `c`, made: what it points at is `gpa`'s.
pub fn get(l: *Get, comptime T: type, c: Cursor) Error!T {
+ // As `Loader.get`: a value that fails leaves nothing made.
+ const at = l.mark();
+ errdefer l.letGo(at);
var out: T = undefined;
try l.fresh(T, &out);
try l.value(T, c, &out);
@@ -2441,8 +2453,8 @@ pub const Get = struct {
/// An allocation made for the value at `slot`, kept: what a value met
/// again is, and what `rollback` frees.
- fn keep(l: *Get, bytes: []u8, alignment: std.mem.Alignment, slot: xitdb.Slot) Error!usize {
- try l.made.append(l.own, .{ .bytes = bytes, .alignment = alignment, .gpa = l.gpa });
+ fn keep(l: *Get, bytes: []u8, alignment: std.mem.Alignment, slot: xitdb.Slot, of: u64) Error!usize {
+ try l.made.append(l.own, .{ .bytes = bytes, .alignment = alignment, .gpa = l.gpa, .of = of });
const at = l.made.items.len - 1;
// Not kept after all: the caller frees it.
if (placed(slot)) l.memo.put(l.own, slotWord(slot), at) catch |err| {
@@ -2453,13 +2465,14 @@ pub const Get = struct {
}
/// The value made before for the same place, when there was one.
- fn again(l: *Get, slot: xitdb.Slot) Error!?[]u8 {
+ fn again(l: *Get, slot: xitdb.Slot, comptime E: type) Error!?[]u8 {
if (!placed(slot)) return null;
const at = l.memo.get(slotWord(slot)) orelse return null;
// Made, then let go of by the reader's caller (a dump's mounts).
if (at >= l.made.items.len) return null;
const m = l.made.items[at];
if (m.dead) return error.Incompatible;
+ if (m.of != typeWord(E)) return error.BadDump;
return m.bytes;
}
@@ -2539,12 +2552,12 @@ pub const Get = struct {
},
.one => {
const C = @typeInfo(T).pointer.child;
- if (try l.again(slot)) |bytes| {
+ if (try l.again(slot, C)) |bytes| {
out.* = @ptrCast(@alignCast(bytes.ptr));
return;
}
const one = try l.gpa.create(C);
- const at = l.keep(std.mem.asBytes(one), .of(C), slot) catch |err| {
+ const at = l.keep(std.mem.asBytes(one), .of(C), slot, typeWord(C)) catch |err| {
l.gpa.destroy(one);
return err;
};
@@ -2674,7 +2687,7 @@ pub const Get = struct {
/// A slice of `E` at `c`: numbers' bytes, a blob's text by its hash, or
/// a list; one met again is the same.
fn slice(l: *Get, comptime E: type, c: Cursor, slot: xitdb.Slot) Error![]E {
- if (try l.again(slot)) |bytes| return @as([*]E, @ptrCast(@alignCast(bytes.ptr)))[0 .. bytes.len / @max(1, @sizeOf(E))];
+ if (try l.again(slot, E)) |bytes| return @as([*]E, @ptrCast(@alignCast(bytes.ptr)))[0 .. bytes.len / @max(1, @sizeOf(E))];
if (comptime numbers(E)) {
switch (slot.tag) {
.bytes, .short_bytes => {},
@@ -2692,7 +2705,7 @@ pub const Get = struct {
l.gpa.free(got.bytes);
return &.{};
}
- _ = l.keep(got.bytes, .@"1", slot) catch |err| {
+ _ = l.keep(got.bytes, .@"1", slot, typeWord(u8)) catch |err| {
l.gpa.free(got.bytes);
return err;
};
@@ -2704,7 +2717,7 @@ pub const Get = struct {
const n = got.bytes.len / width;
if (n == 0) return &.{};
const many = try l.gpa.alloc(E, n);
- const at = l.keep(std.mem.sliceAsBytes(many), .of(E), slot) catch |err| {
+ const at = l.keep(std.mem.sliceAsBytes(many), .of(E), slot, typeWord(E)) catch |err| {
l.gpa.free(many);
return err;
};
@@ -2721,7 +2734,7 @@ pub const Get = struct {
// sizes nothing.
if (n > (c.db.core.length() catch return error.BadDump) / (@bitSizeOf(xitdb.Slot) / 8)) return error.BadDump;
const many = try l.gpa.alloc(E, n);
- const at = l.keep(std.mem.sliceAsBytes(many), .of(E), slot) catch |err| {
+ const at = l.keep(std.mem.sliceAsBytes(many), .of(E), slot, typeWord(E)) catch |err| {
l.gpa.free(many);
return err;
};
@@ -2742,7 +2755,7 @@ pub const Get = struct {
const at = (texts.getCursor(std.mem.readInt(u256, got.value[0..32], .big)) catch return error.BadDump) orelse return error.BadDump;
const text = at.readBytesAlloc(l.gpa, most(at)) catch |err| return if (err == error.OutOfMemory) error.OutOfMemory else error.BadDump;
const bytes: []u8 = @constCast(text);
- _ = l.keep(bytes, .@"1", slot) catch |err| {
+ _ = l.keep(bytes, .@"1", slot, typeWord(u8)) catch |err| {
l.gpa.free(bytes);
return err;
};