diff options
| -rw-r--r-- | core/fs.zig | 10 | ||||
| -rw-r--r-- | core/ninep/tree.zig | 4 | ||||
| -rw-r--r-- | 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. |
