diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-23 16:13:35 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-23 16:28:34 -0300 |
| commit | 2aa977777fca277eba401ac0c1ebbd6e9049d6f9 (patch) | |
| tree | d2180e8d48ddf0b182397962cd72745b2d5835f7 /core/tape.zig | |
| parent | 8ee2339f0224000c48d23eca94e31d7c59fedd4c (diff) | |
| download | pardes-2aa977777fca277eba401ac0c1ebbd6e9049d6f9.tar.gz pardes-2aa977777fca277eba401ac0c1ebbd6e9049d6f9.zip | |
the tape's reader holds every record to its type: a unit a client replays (from its session, a far link, a recorded tape) is cut, flipped or hostile, and each read of it is checked, the client diverging (and joining again) in words where it panicked: a record's kind byte (core/tape.zig Tape.next, peek, nextRecord, describe: `@fromBackingInt`: `invalid enum value` at tape.zig:458 on main, the fuzz's client side), a payload cut short after its head (replayUnit, finishInput: `next().?`), a host record under two bytes (finishInput, expectHost: new Tape.hostId), an input's tag (apply), and in core/walk.zig's decode, the tape's decoder for every type: an enum's bytes (new walk.enumFrom: its number held to a name, as std.enums.fromInt holds it), a run of enums copied whole (`named`), a slice's count held to the bytes left before it is allocated (`least`, the fewest bytes encode writes for a type, derived from the same walk); the one generic decoder checked, every type the tape carries is, and every type added; a user could hit it only by a corrupt or hostile peer
Co-Authored-By: Claude Opus <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core/tape.zig')
| -rw-r--r-- | core/tape.zig | 77 |
1 files changed, 54 insertions, 23 deletions
diff --git a/core/tape.zig b/core/tape.zig index 5f98238d..1552d65c 100644 --- a/core/tape.zig +++ b/core/tape.zig @@ -271,7 +271,9 @@ pub const Tape = struct { replica: bool = false, skips_render: bool = false, digests: u64 = 0, + /// What `diverge` said first, in `diverged_buf`: nothing to free. diverged: ?[]const u8 = null, + diverged_buf: [1024]u8 = undefined, /// The input being applied, for a divergence's report. applying: []const u8 = "", /// PARDES_RECORD_EVERY: a digest after every input, a nested one's too @@ -436,30 +438,50 @@ pub const Tape = struct { const Record = struct { kind: Kind, payload: walk.Reader }; + /// The next record, or null at the tape's end; a record the tape cannot + /// read (cut short, of no kind, by a hash not here) diverges it. fn next(t: *Tape) ?Record { const head = t.in.take(5) catch return null; const len = std.mem.bytesToValue(u32, head[1..5]); - const payload = t.in.take(len) catch return null; - const kind: Kind = @fromBackingInt(@intCast(head[0])); + const payload = t.in.take(len) catch { + t.diverge("the tape ends inside a record", .{}); + return null; + }; + const kind = std.enums.fromInt(Kind, head[0]) orelse { + t.diverge("a record of no kind ({d})", .{head[0]}); + return null; + }; if (kind != .blob) return .{ .kind = kind, .payload = .{ .bytes = payload } }; if (payload.len != 34) { t.diverge("a record by hash is short", .{}); return null; } + const of = std.enums.fromInt(Kind, payload[0]) orelse { + t.diverge("a record by hash of no kind ({d})", .{payload[0]}); + return null; + }; const bytes = t.byHash(payload[1..33], payload[33] != 0) orelse { t.diverge("a record by hash {x} is not here", .{payload[1..9]}); return null; }; - return .{ .kind = @fromBackingInt(@intCast(payload[0])), .payload = .{ .bytes = bytes } }; + return .{ .kind = of, .payload = .{ .bytes = bytes } }; } + /// The next record's kind, or null at the tape's end and for a byte of + /// no kind (which `next` diverges on). fn peek(t: *const Tape) ?Kind { if (t.in.bytes.len - t.in.at < 5) return null; - const kind: Kind = @fromBackingInt(@intCast(t.in.bytes[t.in.at])); - if (kind == .blob and t.in.bytes.len - t.in.at > 5) return @fromBackingInt(@intCast(t.in.bytes[t.in.at + 5])); + const kind = std.enums.fromInt(Kind, t.in.bytes[t.in.at]) orelse return null; + if (kind == .blob and t.in.bytes.len - t.in.at > 5) return std.enums.fromInt(Kind, t.in.bytes[t.in.at + 5]); return kind; } + /// A host record's call (its first two bytes), 0 for one too short to + /// name it: what a divergence says. + fn hostId(payload: []const u8) u16 { + return if (payload.len < 2) 0 else std.mem.bytesToValue(u16, payload[0..2]); + } + /// The bytes of a record by hash: sent, or read here (a file read's /// answer, on the session's machine), each checked against its hash. fn byHash(t: *Tape, hash: *const [32]u8, is_path: bool) ?[]const u8 { @@ -485,7 +507,7 @@ pub const Tape = struct { /// first said): its replica joins again. pub fn diverge(t: *Tape, comptime why: []const u8, args: anytype) void { if (t.diverged != null) return; - t.diverged = std.fmt.allocPrint(t.gpa, why ++ " (tape byte {d}, in {s})", args ++ .{ t.in.at, t.applying }) catch why; + t.diverged = std.fmt.bufPrint(&t.diverged_buf, why ++ " (tape byte {d}, in {s})", args ++ .{ t.in.at, t.applying }) catch why; } /// A host call a replica that skips render does not make (it draws no frame, @@ -526,11 +548,11 @@ pub const Tape = struct { fn finishInput(t: *Tape) void { while (t.diverged == null) { const kind = t.peek() orelse return t.diverge("the tape ends inside an input", .{}); - var rec = t.next().?; + var rec = t.next() orelse return t.diverge("the tape ends inside an input", .{}); switch (kind) { .done => return, .set => t.applySet(&rec.payload), - .host => if (t.skips_render) t.skipHost() else return t.diverge("the core did not make the host call {d} the tape has", .{std.mem.bytesToValue(u16, rec.payload.bytes[0..2])}), + .host => if (t.skips_render) t.skipHost() else return t.diverge("the core did not make the host call {d} the tape has", .{hostId(rec.payload.bytes)}), .input => t.apply(&rec.payload), .digest => t.check(rec.payload.bytes), .ret, .blob => return t.diverge("the core's input ended before the recorded one", .{}), @@ -569,8 +591,8 @@ pub const Tape = struct { t.diverge("the core called " ++ what ++ " past the tape's end", .{}); return false; }; - if (rec.kind != .host or std.mem.bytesToValue(u16, rec.payload.bytes[0..2]) != id) { - const other: u16 = if (rec.kind == .host) std.mem.bytesToValue(u16, rec.payload.bytes[0..2]) else 0; + if (rec.kind != .host or rec.payload.bytes.len < 2 or hostId(rec.payload.bytes) != id) { + const other: u16 = if (rec.kind == .host) hostId(rec.payload.bytes) else 0; t.diverge("the core called " ++ what ++ " where the tape has a {t} ({d})", .{ rec.kind, other }); return false; } @@ -644,7 +666,7 @@ pub const Tape = struct { fn apply(t: *Tape, r: *walk.Reader) void { // A session's options are kept by its core (`opts`): theirs for good. const Tag = std.meta.Tag(Input); - const tag = std.mem.bytesToValue(Tag, (r.take(@sizeOf(Tag)) catch return t.diverge("a short input", .{}))); + const tag = walk.enumFrom(Tag, (r.take(@sizeOf(Tag)) catch return t.diverge("a short input", .{}))[0..@sizeOf(Tag)]) orelse return t.diverge("an input of no kind", .{}); r.at -= @sizeOf(Tag); const kept = tag == .init or tag == .init_from_dump or tag == .restore; const keep = if (t.kept_arena) |*k| k.allocator() else t.gpa; @@ -1392,8 +1414,12 @@ pub fn replayUnit(t: *Tape, bytes: []const u8) ?[]const u8 { defer t.replay = false; t.in = .{ .bytes = bytes }; while (t.diverged == null) { - const kind = t.peek() orelse break; - var rec = t.next().?; + const kind = t.peek() orelse { + // Bytes left that are no record's head: cut, or of no kind. + if (t.in.at < t.in.bytes.len and t.next() == null and t.diverged == null) t.diverge("the tape ends inside a record's head", .{}); + break; + }; + var rec = t.next() orelse break; switch (kind) { .input => { t.apply(&rec.payload); @@ -1450,7 +1476,7 @@ fn nextRecord(r: *walk.Reader) ?struct { kind: Kind, payload: []const u8 } { while (true) { const head = r.take(5) catch return null; const payload = r.take(std.mem.bytesToValue(u32, head[1..5])) catch return null; - const kind: Kind = @fromBackingInt(@intCast(head[0])); + const kind = std.enums.fromInt(Kind, head[0]) orelse return null; if (kind != .digest) return .{ .kind = kind, .payload = payload }; } } @@ -1463,9 +1489,9 @@ pub fn describe(bytes: []const u8, w: *std.Io.Writer) void { while (true) { const head = r.take(5) catch return; const payload = r.take(std.mem.bytesToValue(u32, head[1..5])) catch return; - const raw: Kind = @fromBackingInt(@intCast(head[0])); + const raw = std.enums.fromInt(Kind, head[0]) orelse return w.print("a record of no kind ({d})\n", .{head[0]}) catch {}; // A record by hash, as the kind it stands for (its bytes are not here). - const kind: Kind = if (raw == .blob and payload.len > 0) @fromBackingInt(@intCast(payload[0])) else raw; + const kind: Kind = if (raw == .blob and payload.len > 0) std.enums.fromInt(Kind, payload[0]) orelse .blob else raw; if (raw == .blob) { w.splatByteAll(' ', depth * 2) catch {}; w.print("{t} by hash\n", .{kind}) catch {}; @@ -1477,9 +1503,15 @@ pub fn describe(bytes: []const u8, w: *std.Io.Writer) void { w.splatByteAll(' ', depth * 2) catch {}; switch (kind) { .input => { - const tag = std.mem.bytesToValue(std.meta.Tag(Input), payload[0..@sizeOf(std.meta.Tag(Input))]); - w.print("input {t}", .{tag}) catch {}; - if (tag == .update) w.print(" {t}", .{std.mem.bytesToValue(std.meta.Tag(pardes.Event), payload[@sizeOf(std.meta.Tag(Input))..][0..@sizeOf(std.meta.Tag(pardes.Event))])}) catch {}; + // Its tags read as the replay reads them: one of no kind is said, not cast. + const Tag = std.meta.Tag(Input); + const EventTag = std.meta.Tag(pardes.Event); + const tag: ?Tag = if (payload.len < @sizeOf(Tag)) null else walk.enumFrom(Tag, payload[0..@sizeOf(Tag)]); + w.print("input {s}", .{if (tag) |g| @tagName(g) else "of no kind"}) catch {}; + if (tag == .update and payload.len >= @sizeOf(Tag) + @sizeOf(EventTag)) { + const ev = walk.enumFrom(EventTag, payload[@sizeOf(Tag)..][0..@sizeOf(EventTag)]); + w.print(" {s}", .{if (ev) |e| @tagName(e) else "of no kind"}) catch {}; + } // A key's or a pointer's own words: which one a divergence // starts at. var room: [4096]u8 = undefined; @@ -1492,8 +1524,9 @@ pub fn describe(bytes: []const u8, w: *std.Io.Writer) void { } else |_| {}; }, .host => { - const id = std.mem.bytesToValue(u16, payload[0..2]); - w.print("host {s}", .{if (id < host_fns.len) host_fns[id] else @tagName(@as(Site, @fromBackingInt(@intCast(id - host_fns.len))))}) catch {}; + const id = Tape.hostId(payload); + const of = if (id < host_fns.len) null else std.enums.fromInt(Site, id - host_fns.len); + w.print("host {s}", .{if (id < host_fns.len) host_fns[id] else if (of) |s| @tagName(s) else "of no kind"}) catch {}; }, // Its bytes when traced: which field of equal length differs. .ret => if (payload.len <= 8 or libc.getenv("PARDES_REPLICA_TRACE") != null) w.print("ret {x}", .{payload}) catch {} else w.print("ret ({d} bytes)", .{payload.len}) catch {}, @@ -1579,7 +1612,6 @@ test "a syscall that is no site, made replaying, diverges the replay and poisons var t: Tape = .{ .gpa = gpa, .replay = true, .scratch = .init(gpa), .arena = .init(gpa) }; defer t.scratch.deinit(); defer t.arena.deinit(); - defer if (t.diverged) |why| gpa.free(why); const since = pardes.turn.yields; t.stray(since); try std.testing.expect(t.diverged == null); @@ -1600,7 +1632,6 @@ test "a replay that finds no answer where its core reads one diverges, and the r var t: Tape = .{ .gpa = gpa, .replay = true, .scratch = .init(gpa), .arena = .init(gpa) }; defer t.scratch.deinit(); defer t.arena.deinit(); - defer if (t.diverged) |why| gpa.free(why); // The input's end where the core reads a value of the host's (a // window client's step that took a turn the session's had not, H9 of // 2026-10-30: it aborted the client). |
