From 391c1383011f4fb84743f824c6cc29e68cc4beb7 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 20 Nov 2026 19:49:24 -0300 Subject: ninep: a 9P open record past fs.opens_len is nothing, never read or freed: a replica client's rollback copy takes the opens only that far (faults-4, zlomnsus), so a record a session's open made over a copy taken ahead (a client idle, a script's pty/run opened and its command printed, the units kept over the copy) was left as it was when the copy went back, its line and output freed by the epoch, and the open replayed after the rollback (the client's key into the shell stopped at its pty write) freed them again in tree.open (`if (o.node != 0) o.deinit`): the client's C heap corrupt, `malloc(): unaligned tcache chunk detected` and SIGABRT at its next delta (daemon.py --clients' client C, since pardes-merge40; a user's terminal or window client typing while a script runs pty/run in the session could hit it); the line goes and open writes the record over, Namespace.deinit and forget free opens[0..opens_len]; the test, a client's key into a shell over a copy taken ahead with a pty/run kept since, aborted on the base with `free(): double free detected in tcache 2` and passes; gated with the next change on it (its description has the suites): daemon.py --clients 39 of 40 alone (the one a client D that did not say it joined again within 60 s, at a load of 6.7, no core dumped, not seen again in 77 runs of the scene) and 20 of 20 pinned to one CPU, plain 5 of 5 Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- core/fs.zig | 10 +++++--- core/ninep/tree.zig | 4 +-- core/replica.zig | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 7 deletions(-) diff --git a/core/fs.zig b/core/fs.zig index a64487d6..6b43ba67 100644 --- a/core/fs.zig +++ b/core/fs.zig @@ -884,8 +884,10 @@ pub const Namespace = struct { os_paths: std.ArrayList(OsPath) = .empty, /// What each open holds between its open and its release. opens: [tree.open_slots]tree.Open = @splat(.{}), - /// The records past it are free: a digest and a dump take the opens - /// this far (an idle session's none). + /// The records past it are nothing: a digest, a dump and a replica's + /// rollback copy take the opens this far (an idle session's none), and + /// nothing reads or frees one past it (a rollback leaves there what + /// its epoch freed). opens_len: u8 = 0, out: std.ArrayList(u8) = .empty, listeners: u16 = 0, @@ -1024,7 +1026,7 @@ pub const Namespace = struct { st.mounts.deinit(gpa); for (st.os_paths.items) |entry| gpa.free(entry.path); st.os_paths.deinit(gpa); - for (&st.opens) |*o| o.deinit(gpa); + for (st.opens[0..st.opens_len]) |*o| o.deinit(gpa); for (st.pages.items) |*page| page.bytes.deinit(gpa); st.pages.deinit(gpa); for (st.closed_lines.items) |*line| line.bytes.deinit(gpa); @@ -1057,7 +1059,7 @@ pub const Namespace = struct { /// What the requests of connections gone made, let go: a session /// revived from a dump has none of the connections that held its opens. pub fn forget(st: *Namespace, gpa: std.mem.Allocator) void { - for (&st.opens) |*o| o.deinit(gpa); + for (st.opens[0..st.opens_len]) |*o| o.deinit(gpa); st.opens_len = 0; st.results_len = 0; st.batch.bytes.deinit(gpa); diff --git a/core/ninep/tree.zig b/core/ninep/tree.zig index 3eeb5d3f..2c0ce1b0 100644 --- a/core/ninep/tree.zig +++ b/core/ninep/tree.zig @@ -957,9 +957,7 @@ fn open(p: *Pardes, req: Req, target: Target) Reply { // record would be another than the session's. const i = p.tape.authority(freeOpen, .{p}) orelse return Reply.fail(req.tag, E.NFILE); const o = &p.fs.opens[i]; - // Past `opens_len` a replica's record may hold what a rollback did not - // put back (rollback.zig saves records as far as it says). - if (o.node != 0) o.deinit(p.gpa); + // Past `opens_len` a record is nothing: written over, never freed. o.* = .{ .node = req.node, .what = what }; p.fs.opens_len = @max(p.fs.opens_len, @as(u8, @intCast(i + 1))); if (resultsFile(target)) { diff --git a/core/replica.zig b/core/replica.zig index 0b554ed5..4ba08849 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -7050,6 +7050,80 @@ test "a client of screen-state terminals takes a 9P pty/run in a shell as the se try agrees(a, late); } +// daemon.py --clients' client C (2026-11-20): idle, a copy taken ahead; the +// session's script opens pty/run and its command prints, replayed over the +// copy and kept; the client types into the shell, its speculation stops at +// the pty write, and the rollback replays the open again over a record the +// copy never held (it holds the opens as far as `opens_len`), whose line +// and output the epoch had freed: freed again there, the client's C heap +// was corrupt. +test "a 9P open the session made over a copy taken ahead, replayed after a rollback: the record past the opens the copy holds is written over, never freed again" { + if (pardes.Term.ops == null) return error.SkipZigTest; // a shell + const gpa = std.testing.allocator; + const RunHost = struct { + fn taken(_: ?*anyopaque, _: pardes.Pane.Id) bool { + return false; + } + fn write(_: ?*anyopaque, _: pardes.Pane.Id, _: []const u8) void {} + const vtable: pardes.Host.VTable = .{ .watched = TestHost.watched, .tty_taken = taken, .pty_write = write }; + }; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .boot = .tty }, .{ .vtable = &RunHost.vtable }, true); + defer a.deinit(); + var screens: Screens = .init(gpa); + defer screens.deinit(a.core); + const peer = try screens.addPeer(); + const r = try Replica.init(gpa, 1, try a.unit(0, 0), true); + defer r.deinit(); + const shell: u8 = for (a.core.panes, 0..) |slot, id| { + if (slot) |pane| if (pane.isTerminal()) break @intCast(id); + } else return error.NoShell; + const prompt = "\x1b]133;A;cl=line;aid=pardes\x07$ \x1b]133;B;aid=pardes\x07"; + a.output(.{ .output = .{ .pane = .at(shell), .bytes = prompt } }); + r.confirm((try a.pending()).?); + try deliver(a, &screens, peer, r, .flush); + r.ready(); + try std.testing.expect(r.copy.held and r.core().fs.opens_len == 0); + const node = pardes.ctlfs.Node.of(a.core.panes[shell].?.serial, .pty_run); + const Serve = struct { + fn serve(s: *Authority, x: *Replica, req: pardes.ctlfs.Req) !pardes.ctlfs.Reply { + const was = Stepping.begin(&s.t, null); + const reply = s.core.serveFs(req); + s.core.pump(s.host) catch {}; + was.end(); + s.seq += 1; + const u = try std.testing.allocator.dupe(u8, try s.unit(0, s.seq)); + defer std.testing.allocator.free(u); + x.confirm(u); + x.settle(); + return reply; + } + }; + const h = (try Serve.serve(a, r, .{ .tag = 1, .op = .open, .node = node, .omode = 2 })).handle; + _ = try Serve.serve(a, r, .{ .tag = 2, .op = .write, .node = node, .handle = h, .data = "echo x\n" }); + a.output(.{ .output = .{ .pane = .at(shell), .bytes = "echo x\r\n\x1b]133;C;aid=pardes\x07x\r\n\x1b]133;D;0;aid=pardes\x07" ++ prompt } }); + r.confirm((try a.pending()).?); + r.settle(); + // Kept over the copy: the record past its opens holds the run's output. + try std.testing.expect(r.copy.held and r.since.items.len > 0); + try std.testing.expectEqualStrings("x\n", r.core().fs.opens[h - 1].what.run.output); + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| gpa.free(m); + sent.deinit(gpa); + } + // A key into the shell: its speculation stops at the pty write, and + // the copy is put back, the units since replayed (the open again). + try key(r, 'e', &sent); + try std.testing.expect(r.applied == 0 and r.stats.stopped > 0); + r.confirm(try a.take(sent.items[0])); + r.settle(); + try deliver(a, &screens, peer, r, .flush); + _ = try Serve.serve(a, r, .{ .tag = 3, .op = .release, .node = node, .handle = h, .opened = true }); + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null and r.quiet()); + try agrees(a, r); +} + // Del with a pane above and below asks which way the space goes, its // question shown once a delay is up (builtins.zig): its time was the // host's clock read in the step, an answering entry a speculation stops at. -- cgit v1.3 From 1ae6addf4590d3b9c1070f6546c29e807d272491 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 20 Nov 2026 19:50:04 -0300 Subject: rollback: the copy walks a type by its fields when it says what they are (`replica`), whatever digest of its own it has: it skipped every type with one as if it were a terminal, so since faults-4 the 9P state (fs.Namespace, a pane's ninep State) was saved only as the bytes of its records, never the buffers they own, and a unit kept over a copy that wrote one in place came back other than it was (a script's exec held "Ta", the next write ran `Tab 8` and moved "Un" to the front of the same buffer, and the rollback's replay ran `Unb 8`: the client's digest other than the session's); now only a type with a digest and no `replica` (Term.State, the authority's emulator; locations.Row, made again and never written) is left out (`unwalked`); the test, that exec write over a copy taken ahead and a rollback by another's key, differed on the base (digest 12509739004356058903 against 4039275215734315918) and passes; the tree gated whole (load 1-8): tui core-test 875/1033 (158 skipped), shell-test 1075/1078, term-test 17/17, pane-test 183/184, kitty-test 45/45, behaviour-test 275/275 and --replay 275/275, process-test 0 of 145 failed; both soaks diverged 0, unequal 0, unlisted 0, quiet checks 597/0 673/0 779/0 and 693/0 586/0 829/0, the corpus 22 of 22, no leak; daemon.py --clients 39 of 40 alone and 20 of 20 pinned, plain 5 of 5, --far, --far-find, --relink, --launch, --pair, --restore, --games (addon-test 5/5), --window-clients (its PDF case skipped: no pdf.so in a tui prefix), --gui pass; the cross-builds (macos, macos arm, freebsd) build; the frame and perf gate against tree2: 77/77 frames, perfab +0.043%, 121 per-operation counts within 0.1% (largest client-key-down on medium +0.0749%), gui unit-test 1671/1675 with no failure the base has not Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- core/replica.zig | 31 +++++++++++++++++++++++++++++++ core/rollback.zig | 16 ++++++++++++---- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/core/replica.zig b/core/replica.zig index 4ba08849..c895618d 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -7124,6 +7124,37 @@ test "a 9P open the session made over a copy taken ahead, replayed after a rollb try agrees(a, r); } +test "a script's half line held over a copy taken ahead, its next write run and its rest moved to the front in place, put back by a rollback: the line replayed is the session's" { + var n = try NinepJoin.init("one\n", TestHost.host()); + defer n.deinit(); + try n.join(); + const r = n.late.?; + const exec = n.node(.exec); + const h = (try n.serve(.{ .tag = 1, .op = .open, .node = exec, .omode = 1 }, true)).handle; + _ = try n.serve(.{ .tag = 2, .op = .write, .node = exec, .handle = h, .data = "Ta" }, true); + r.ready(); + // Replayed over the copy and kept: `Tab 8` runs, `Un` is held where + // `Ta` was, in the buffer the copy holds. + _ = try n.serve(.{ .tag = 3, .op = .write, .node = exec, .handle = h, .data = "b 8\nUn" }, true); + try std.testing.expect(r.copy.held and r.since.items.len > 0); + var sent: std.ArrayListUnmanaged([]u8) = .empty; + defer { + for (sent.items) |m| std.testing.allocator.free(m); + sent.deinit(std.testing.allocator); + } + try key(r, 'l', &sent); + try std.testing.expectEqual(@as(usize, 1), r.applied); + // The session's key first: a rollback, the write replayed again. + try n.send(try n.a.local(.{ .key = .{ .cp = 'j', .text = "j" } })); + try std.testing.expect(r.stats.foreign > 0); + try n.send(try n.a.take(sent.items[0])); + _ = try n.serve(.{ .tag = 4, .op = .write, .node = exec, .handle = h, .data = "do\n" }, true); + _ = try n.serve(.{ .tag = 5, .op = .release, .node = exec, .handle = h, .opened = true }, true); + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null and r.quiet()); + try n.agree(); +} + // Del with a pane above and below asks which way the space goes, its // question shown once a delay is up (builtins.zig): its time was the // host's clock read in the step, an answering entry a speculation stops at. diff --git a/core/rollback.zig b/core/rollback.zig index c7d0a4e8..a55d3c87 100644 --- a/core/rollback.zig +++ b/core/rollback.zig @@ -404,9 +404,7 @@ pub const Copy = struct { }, .@"struct" => |s| { if (comptime walk.machinery(T)) return; - // A terminal: the authority's (its emulator is never written - // by a speculation: terminal.zig's guards). - if (comptime @hasDecl(T, "digest") and @TypeOf(T.digest) == fn (*const T, *walk.Hasher) void) return; + if (comptime unwalked(T)) return; if (s.layout == .@"packed") return; // std's lists (a safe build's have a third field, a lock). if (comptime walk.isList(T)) { @@ -467,7 +465,7 @@ pub const Copy = struct { if (comptime walk.machinery(P)) return; const addr = @intFromPtr(ptr); if (try c.reached(P, addr)) return; - if (comptime @hasDecl(P, "digest") and @TypeOf(P.digest) == fn (*const P, *walk.Hasher) void) return; + if (comptime unwalked(P)) return; if (c.epoch.?.owns(addr) != null) { _ = try c.reached(void, addr); try c.fields(P, ptr); @@ -513,6 +511,16 @@ pub const Copy = struct { } }; +/// A type that hashes itself and says nothing of its fields (`replica`): +/// a terminal, the authority's (its emulator is never written by a +/// speculation: terminal.zig's guards), or a location row, made again and +/// never written. One that says its fields' kinds is walked by them, +/// whatever digest it has (the 9P state, fs.Namespace and a pane's: a +/// replayed request writes its buffers in place). +fn unwalked(comptime T: type) bool { + return @hasDecl(T, "digest") and @TypeOf(T.digest) == fn (*const T, *walk.Hasher) void and !@hasDecl(T, "replica"); +} + fn annotatedStruct(comptime T: type) bool { return @typeInfo(T) == .@"struct" and @hasDecl(T, "replica"); } -- cgit v1.3