summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-25 03:06:50 -0300
committerGabriel Schneider <[email protected]>2026-11-25 03:18:54 -0300
commitbdbe524d8ea6b08689b2eec77d2a2db316ae32d2 (patch)
treeb9abed7580d05fdddb54ecb6dd546a792b5a20a6
parent3845a3eb8dd009ac1f2d184e6d15439e27065b66 (diff)
downloadpardes-bdbe524d8ea6b08689b2eec77d2a2db316ae32d2.tar.gz
pardes-bdbe524d8ea6b08689b2eec77d2a2db316ae32d2.zip
walk: a struct read in place holds a length read back without its array to what the array held before the read, so a damaged dump whose messages' `live_len` came back without their `live` is refused, not walked over undefined messages (walk UI M4 item 2, the fuzz panic G2's gate met: join seed 4135 on G2's tree, 6356 on this one; G2's run without this line's session lines met it at 33733): RUN: the dump fuzz at join seed 6356 panicked in its digest (tape.digestOf after the load), `index out of bounds: index 43690, len 96` at walk.zig hash: Messages.live[0]'s `src` (a Kind.Buf(96)) said 0xAAAA bytes; the load said live_len 1 and had not read `live`; THE CAUSE: a load reads the core in place (dump.zig loadKnowing, walk.Loader.into: `fields(Pardes, ..., existing = true)`, and each struct field under it in place too), and Loader.fields (walk.zig, the `held` it begins with) and Get.fields (the image's) took an in-place struct's arrays as holding ALL their slots (`maxInt`) until they were read, so pastHeld refused no length whose array the dump did not carry: a damaged dump whose Messages kept `live_len` and lost `live` (a damaged name, or a kind that changed, "left as it was") left `live_len` 1 over the fresh core's `live: [max_live]Said = undefined` (0xAA in Debug, any bytes in a release build), and the digest, the notice row, the marks and every reader of a live message read past it; THE FIX (heldNow, both readers): an in-place array holds, before the read, as far as its length said then (heldOf: a fresh core's none), and a read array as far as it was read, as before; so the length read back alone is refused when it says more (pastHeld's note: `live_len` says 1, past the 0 its buffer holds), and a length the array held before stands; a same-build dump reads every field and is not changed by it; a user restoring or joining from a damaged dump could have hit it (a release build reads that garbage as messages); TEST FIRST: walk.zig "a struct read in place holds a length read back without its array to what the array held: past it, the dump is refused" (fails before: no error), fuzz.zig's join seed 6356 pinned beside 19224 and 31554 (panics before); the fuzz at 40,000 join and 1,779 image seeds over this line's session (kept picker, recipe, search word, Help): pass (3 min 17 s); core-test 1062/1232 (170 skipped), shell-test 1279/1282 (3 skipped)
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBFhdevVu48bvL9kCE
-rw-r--r--core/fuzz.zig4
-rw-r--r--core/walk.zig44
2 files changed, 44 insertions, 4 deletions
diff --git a/core/fuzz.zig b/core/fuzz.zig
index 2d589054..091eca63 100644
--- a/core/fuzz.zig
+++ b/core/fuzz.zig
@@ -812,6 +812,10 @@ test "a dump and an image cut, flipped and grown: refused or loaded, never a pan
// Join seed 31554: the namespace's opens_len past its 64 slots, refused,
// and the rollback's teardown of the namespace read that far.
try loadSeed(bytes, .join, 31554);
+ // Join seed 6356: the messages' `live_len` read back without their
+ // `live` (the core read in place, its own `live` undefined), a live
+ // message's `src` length 0xAAAA when the digest walked it.
+ try loadSeed(bytes, .join, 6356);
for ([_][]const u8{ bytes, image }, [_]Road{ .join, .restore }, [_]u64{ 2048, 1024 }) |whole, road, n| {
try loadSeed(whole, road, 0);
for (1..n + 1) |seed| try loadSeed(whole, road, seed);
diff --git a/core/walk.zig b/core/walk.zig
index 6734c0b7..309842f1 100644
--- a/core/walk.zig
+++ b/core/walk.zig
@@ -236,6 +236,17 @@ fn pairAt(comptime T: type, comptime name: []const u8) ?usize {
return null;
}
+/// How far each of `out`'s arrays a length cuts holds before a read:
+/// nothing for a new value; for one read in place (`existing`), as far as
+/// its length says now.
+fn heldNow(comptime T: type, out: *const T, existing: bool) [pairs(T)]usize {
+ var held: [pairs(T)]usize = @splat(0);
+ if (existing) inline for (0..comptime pairs(T)) |i| {
+ held[i] = heldOf(T, out, T.replica.len[i][0]);
+ };
+ return held;
+}
+
/// A length that says more than its buffer holds.
const Past = struct { name: []const u8, says: usize, holds: usize };
@@ -1904,9 +1915,11 @@ pub const Loader = struct {
const outer = l.field;
defer l.field = outer;
const dropped = l.dropped;
- // How far each array a length cuts was read: none yet (one read in
- // place holds what it held).
- var held: [pairs(T)]usize = @splat(if (existing) std.math.maxInt(usize) else 0);
+ // How far each array a length cuts was read: none yet, or for one
+ // read in place as far as its length said before the read (a fresh
+ // core's `undefined` past it), so a length read back without its
+ // array holds no more than that.
+ var held = heldNow(T, out, existing);
if (l.same) {
if (s.fields.len != names.len) return error.BadDump;
inline for (names, 0..) |field, k| {
@@ -2819,7 +2832,7 @@ pub const Get = struct {
var found: std.StaticBitSet(@max(1, names.len)) = .empty;
var failed = false;
// How far each array a length cuts was read (Load's `fields`).
- var held: [pairs(T)]usize = @splat(if (existing) std.math.maxInt(usize) else 0);
+ var held = heldNow(T, out, existing);
var it = map.iterator() catch return error.BadDump;
while (it.next() catch return error.BadDump) |kv_at| {
const kv = kv_at.readKeyValuePair() catch return error.BadDump;
@@ -3038,6 +3051,29 @@ test "a dump comes back whole: shared things shared, lists and runs as long as t
try std.testing.expectEqualSlices(u8, bytes, again);
}
+test "a struct read in place holds a length read back without its array to what the array held: past it, the dump is refused" {
+ const gpa = std.testing.allocator;
+ const Old = struct { n: u8 = 0 };
+ const New = struct {
+ pub const replica = .{ .len = .{.{ "buf", "n" }} };
+ buf: [4]u32 = undefined,
+ n: u8 = 0,
+ };
+ const bytes = try save(Old, gpa, &.{ .n = 3 });
+ defer gpa.free(bytes);
+ // Holding none: the 3 read back name what was never written.
+ var l = try Load(&.{New}).init(gpa, bytes);
+ defer l.deinit();
+ var out: New = .{};
+ try std.testing.expectError(error.BadDump, l.into(New, &out));
+ // Holding 3 as it stood: they stand.
+ var again = try Load(&.{New}).init(gpa, bytes);
+ defer again.deinit();
+ var held: New = .{ .buf = .{ 7, 8, 9, 0 }, .n = 3 };
+ try again.into(New, &held);
+ try std.testing.expectEqualSlices(u32, &.{ 7, 8, 9 }, held.buf[0..held.n]);
+}
+
test "a dump crosses builds: a field added takes its default, one removed is passed over, one changed is left out" {
const gpa = std.testing.allocator;
const Old = struct {