From 3e1677f526baa02179c5646223f48b316e5152e0 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 6 Nov 2026 18:31:03 -0300 Subject: kinds #40, rasters as bounded messages on the wire (ยง17.3 C): a placement over PA_RASTER_MAX is dropped at paint and said once, a kind's frame names its placements by key and their pixels cross before it as `raster` strips of at most 4 MiB over the rows its `src` shows (only those the peer was not sent with the frame before), the peer assembles them by name in a 64 MiB cache let go by least recent use but for what a Shadow shows, a frame naming one it lacks draws its cells and asks once for its pane again, the sender checks every message against wire.max_payload (Debug's panic, release's drop and log) and a reader names an oversized one (said on the client as it joins again, logged at the session) instead of reading it as the link's end, so kinds #21's byte budget goes; wire 23; and pdf.c lays out for a view with pictures over a glyph view until an input chooses (one layout until 7c), so the 110-column window of the stall's repro shows its page MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/replica.zig | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 83 insertions(+), 9 deletions(-) (limited to 'src/replica.zig') diff --git a/src/replica.zig b/src/replica.zig index 29904fa0..5b2facb4 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -299,10 +299,13 @@ pub const Authority = struct { // to a replica: kind 1, a delta; kind 2, rows (encodeRows): asked for, // or a step read them; kind 4, a pane of a kind's frame // (a Shadow, screen_state.encodeShadow), at the size the -// peer's view gives the pane; +// peer's view gives the pane; kind 5, a strip of one of +// its rasters (encodeStrip), before the frame naming it; // to the authority: kind 1, an acknowledgement (u32 seq); kind 2, a // request for rows (u64 lo, u64 hi, absolute); kind 3, a -// delta refused: start this terminal afresh. +// delta refused: start this terminal afresh; kind 4, a +// kind's frame naming a raster the replica lacks: all +// of that pane again. // In the authority's stream to a peer, before it steps any input but a // pty's output, and once a frame: its own steps since the last unit, as one // (`Authority.pending`), then what the peer is owed of the terminals @@ -314,8 +317,10 @@ pub const Authority = struct { const kind_delta = 1; const kind_rows = 2; const kind_shadow = 4; +const kind_raster = 5; const kind_ack = 1; const kind_reset = 3; +const kind_want = 4; const ScreenMsg = struct { pane: u8, serial: u32, kind: u8, payload: []const u8 }; @@ -442,14 +447,26 @@ pub const Screens = struct { if (cols == 0 or rows == 0) continue; var frame: pardes.Kind.Frame = .{ .cols = cols, .rows = rows }; frame.cells = pardes.Kind.paint(p, k, id, arena.allocator(), cols, rows, &frame) orelse continue; + // Its rasters' strips over what it shows that the peer + // lacks, each a message before the frame naming them. + for (frame.places[0..@min(frame.n, 64)]) |*pl| { + // A row past a strip's bound cannot cross: not shown there. + if (@as(usize, pl.w) * 4 > screen_state.strip_max) continue; + var owed = try pp.shadow.owed(x.s.gpa, pl); + while (owed != 0) : (owed &= owed - 1) { + x.s.out.clearRetainingCapacity(); + try writeScreenHead(&x.s.out, x.s.gpa, @intCast(id), pane.serial, kind_raster); + var w: std.Io.Writer.Allocating = .fromArrayList(x.s.gpa, &x.s.out); + defer x.s.out = w.toArrayList(); + try screen_state.encodeStrip(pl, @ctz(owed), &w.writer); + try x.out.append(x.s.gpa, try x.s.gpa.dupe(u8, w.written())); + } + } x.s.out.clearRetainingCapacity(); try writeScreenHead(&x.s.out, x.s.gpa, @intCast(id), pane.serial, kind_shadow); var w: std.Io.Writer.Allocating = .fromArrayList(x.s.gpa, &x.s.out); defer x.s.out = w.toArrayList(); - // Within the wire's bound, its head (6 bytes) aside: a page of a - // PDF in a wide window is more, and a message past it ends - // the peer's link (its reader stops at it). - if (!try screen_state.encodeShadow(x.s.gpa, &pp.shadow, &frame, k.pointer, wire.max_payload - 6, &w.writer)) continue; + if (!try screen_state.encodeShadow(x.s.gpa, &pp.shadow, &frame, k.pointer, &w.writer)) continue; try x.out.append(x.s.gpa, try x.s.gpa.dupe(u8, w.written())); }; } @@ -512,6 +529,8 @@ pub const Screens = struct { const m = parseScreen(msg) orelse return null; const pane = core.panes[m.pane] orelse return null; if (pane.serial != m.serial) return null; + // A kind's frame whose raster the peer lacks: its pane sent afresh. + if (m.kind == kind_want and pane.addon != null) s.peers.items[peer][m.pane].shadow.deinit(s.gpa); const state = pane.terminal orelse return null; const pp = &s.peers.items[peer][m.pane]; if (pp.serial != m.serial) return null; @@ -659,6 +678,8 @@ pub const Replica = struct { /// speculation may move a view, so a rollback puts these back. views: [pardes.MAX_PANES]View = @splat(.{}), screen_out: std.ArrayListUnmanaged(u8) = .empty, + /// Its Shadows' rasters, from the session's strips (17.3). + blobs: screen_state.Blobs = .{}, /// Its predicted echo (src/echo.zig), drawn over its core's terminals: /// fed by its keys, its units and its deltas here, and by `echoFrame`. echo: pardes.echo.Echo = .{}, @@ -744,6 +765,7 @@ pub const Replica = struct { c.deinit(); was.end(); } + r.blobs.deinit(r.gpa); r.copy.deinit(); for (r.pending.items) |p| r.freePending(p); r.pending.deinit(r.gpa); @@ -1109,6 +1131,14 @@ pub const Replica = struct { const c = r.t.core orelse return null; const pane = c.panes[m.pane] orelse return null; if (pane.serial != m.serial) return null; // a pane gone: its slot's next is another + // A strip of a raster of a pane of a kind, kept until a frame shows it. + if (m.kind == kind_raster and m.payload.len >= 8) { + var shown: std.ArrayList([*]const u8) = .empty; + defer shown.deinit(r.gpa); + for (c.panes) |slot| if (slot) |p| if (p.addon) |k| if (k.shadow) |sh| for (sh.places.items) |pl| shown.append(r.gpa, pl.rgba) catch {}; + r.blobs.strip(r.gpa, std.hash.Wyhash.hash(m.serial, m.payload[0..8]), m.payload, shown.items) catch r.fail("a kind's raster did not apply"); + return null; + } // A pane of a kind: the authority's frame of it, drawn as it is. if (m.kind == kind_shadow) if (pane.addon) |k| { const sh = k.shadow orelse sh: { @@ -1117,8 +1147,11 @@ pub const Replica = struct { k.shadow = made; break :sh made; }; - sh.apply(k.gpa, m.payload) catch r.fail("a kind's frame did not apply"); - return null; + const lacking = sh.apply(k.gpa, m.payload, m.serial, &r.blobs) catch { + r.fail("a kind's frame did not apply"); + return null; + }; + return if (lacking != 0) r.screenMessage(m.pane, m.serial, kind_want, &.{}) else null; }; const state = pane.terminal orelse return null; const slot = &r.screens[m.pane]; @@ -1374,6 +1407,13 @@ pub const Framer = struct { f.z = null; } + /// The length a message past the wire's bound said it had, when that + /// is where `run` stopped: an error to name, never a hangup (17.3). + pub fn overlong(fr: *const Framer) ?u32 { + for ([_][]const u8{ fr.buf.items[0..fr.len], fr.inner.items }) |b| if (wire.framed(b)) |_| {} else |_| return std.mem.readInt(u32, b[1..5], .little); + return null; + } + /// Framed messages off `fd` (wire.zig) handed to `take` until /// the stream ends, `take` says stop, or a message is malformed. An /// envelope's are handed on as if they had come bare. @@ -1627,6 +1667,14 @@ pub const Peer = struct { peer.lock.lockUncancelable(io); defer peer.lock.unlock(io); if (peer.cut or peer.closing) return; + // The wire's bound, as the message goes (17.3): one past it would + // be the link's end at the reader. + const len = std.mem.readInt(u32, parts[0][1..5], .little); + if (len > wire.max_payload) { + log.err("a {d} MB message to client {d}, over the {d} MiB bound: dropped", .{ len >> 20, peer.id, wire.max_payload >> 20 }); + if (checked) std.debug.panic("a {d}-byte message to client {d}, over the wire's bound", .{ len, peer.id }); + return; + } var n: usize = peer.out.items.len; for (parts) |p| n += p.len; if (n > peer.limit) { @@ -1748,6 +1796,7 @@ pub const Peer = struct { // The framer goes before `gone`, after which the peer may be freed. var fr: Framer = .{}; fr.run(peer.peers.gpa, peer.fd, peer, take); + if (fr.overlong()) |n| log.err("client {d} sent a {d} MB message, over the {d} MiB bound: let go", .{ peer.id, n >> 20, wire.max_payload >> 20 }); fr.deinit(peer.peers.gpa); peer.peers.post(.{ .gone = peer }); } @@ -1882,7 +1931,11 @@ pub const Peers = struct { }, .screen => |in| { defer ps.gpa.free(in.msg); - const reply = (ps.screens.take(core, in.peer.sid, in.msg) catch null) orelse continue; + const reply = (ps.screens.take(core, in.peer.sid, in.msg) catch null) orelse { + // A raster asked for again: its pane's frame now. + if (in.msg.len > 5 and in.msg[5] == kind_want) ps.screensTo(core, in.peer, .flush); + continue; + }; defer ps.gpa.free(reply); const head = wire.header(@backingInt(wire.ServerTag.screen), reply.len); in.peer.send(&.{ &head, reply }); @@ -2648,6 +2701,10 @@ pub fn readLink(gpa: Allocator, fd: c_int, rest: *Framer, ctx: anytype, comptime } }; rest.run(gpa, fd, ctx, Take.take); + // A message past the wire's bound: said, never a hangup. + const n = rest.overlong() orelse return; + const words = std.fmt.allocPrint(std.heap.c_allocator, "the session sent a {d} MB message, over the {d} MiB bound", .{ n >> 20, wire.max_payload >> 20 }) catch return; + if (!post(ctx, .{ .refused = words })) std.heap.c_allocator.free(words); } /// The words a client goes with when its session runs another build now. @@ -2952,6 +3009,23 @@ pub const Link = struct { } }; +test "a message past the wire's bound is named, never read as the link's end" { + var fds: [2]c_int = undefined; + try std.testing.expectEqual(0, libc.pipe(&fds)); + const head = wire.header(@backingInt(wire.ServerTag.unit), wire.max_payload + 1); + _ = libc.write(fds[1], &head, head.len); + _ = libc.close(fds[1]); + var fr: Framer = .{}; + defer fr.deinit(std.testing.allocator); + fr.run(std.testing.allocator, fds[0], {}, struct { + fn take(_: void, _: u8, _: []const u8) bool { + return true; + } + }.take); + _ = libc.close(fds[0]); + try std.testing.expectEqual(wire.max_payload + 1, fr.overlong().?); +} + test "a client joining again after a divergence is told at once that its session is gone" { var why: [600]u8 = undefined; const t0 = nowNs(); -- cgit v1.3