diff options
| -rw-r--r-- | core/fs.zig | 10 | ||||
| -rw-r--r-- | core/ninep/tree.zig | 4 | ||||
| -rw-r--r-- | core/replica.zig | 105 | ||||
| -rw-r--r-- | core/rollback.zig | 16 |
4 files changed, 124 insertions, 11 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 de58da94..e2ecc796 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -7050,6 +7050,111 @@ 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); +} + +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"); } |
