From 7dbcaa13cc3bebd5470ce1ac3f5e112357564063 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Mon, 23 Nov 2026 09:37:41 -0300 Subject: upgrade: the test that an Upgrade's outcome is said in every view: runtime/upgrade.zig:449 (`load`'s end) set "Upgrade: now ..." or "... it goes on as ..." at `p.view().active`, the live view at the dump, the one whose input came last before the Upgrade's step, not the one that asked (the pair round's `--with-pair --upgrade` ReleaseSafe to Debug 1 of 3: the message on pane 5, a pane of client f's view, which the session's /screen does not show); the unit test (two views, the second's keyboard on a New pane of its own and its input last, dumped and loaded as an upgrade loads it: each view's pane at its keyboard must say "Upgrade: now pardes ") fails at upgrade.zig:493 (logs/t7-upgrade-unfixed.log); and test/daemon.py's refused-upgrade row (two replica clients, the Upgrade written over 9P to the shell's pane) now waits for each client's own screen to say "Upgrade: the new build" Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- test/daemon.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'test') diff --git a/test/daemon.py b/test/daemon.py index dc1abcbc..b240b887 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -3561,6 +3561,9 @@ def upgrades(old, new): raise TimeoutError(f'the client joined again: not {[c.stem for _, c in owners if c not in joined]} ' f'{time.monotonic() - t0:.1f} s after the Upgrade, the session back at {back:.1f} s') from None assert again.poll() is None and again5.poll() is None + # The session's news: each client's own screen says it, not only + # the view whose input came last before the Upgrade's step. + wait_for("each client's own screen saying the Upgrade's outcome", lambda: all(b'Upgrade: the new build' in t.text() for t, _ in owners), timeout=15) for (t, check), pane, theirs in zip(owners, mine, reversed(mine)): in_own_view('its own view again', lambda: made_live(t, check), client, pane, theirs) for c, t in ((again, term4), (again5, term5)): -- cgit v1.3 From 3ea7524ba026f03052e8861c9d4cb18eed8102cb Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Mon, 23 Nov 2026 10:04:38 -0300 Subject: test/daemon.py: the refused-upgrade row's new check reads the outcome's end, `it goes on as`, where it read its head: a message longer than its pane shows its end (each client's screen in the kept evidence: "...ssion (refused by PARDES_TEST_UPGRADE_REFUSE); it goes on as pardes 0.24 (e22d77e95959)"), so `Upgrade: the new build` never reached a client's bytes and the row failed at the check of `ulztktwm` (3 of 3 `--upgrade` from the installed build, its session's log saying the outcome on every view's pane: `msg 3`, `msg 4`, `msg 5`); a change of its own on top, since the coordinator's integrations already stand on `ulztktwm` Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- test/daemon.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'test') diff --git a/test/daemon.py b/test/daemon.py index b240b887..c96425c2 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -3561,9 +3561,10 @@ def upgrades(old, new): raise TimeoutError(f'the client joined again: not {[c.stem for _, c in owners if c not in joined]} ' f'{time.monotonic() - t0:.1f} s after the Upgrade, the session back at {back:.1f} s') from None assert again.poll() is None and again5.poll() is None - # The session's news: each client's own screen says it, not only - # the view whose input came last before the Upgrade's step. - wait_for("each client's own screen saying the Upgrade's outcome", lambda: all(b'Upgrade: the new build' in t.text() for t, _ in owners), timeout=15) + # The session's news: each client's own screen says it (a long + # message shows its end), not only the view whose input came + # last before the Upgrade's step. + wait_for("each client's own screen saying the Upgrade's outcome", lambda: all(b'it goes on as' in t.text() for t, _ in owners), timeout=15) for (t, check), pane, theirs in zip(owners, mine, reversed(mine)): in_own_view('its own view again', lambda: made_live(t, check), client, pane, theirs) for c, t in ((again, term4), (again5, term5)): -- cgit v1.3 From 6de26a20a5fb547f3d032f6ae2062066f356a659 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Mon, 23 Nov 2026 17:26:17 -0300 Subject: numbers from outside are checked where they are read: a diff's hunk header, a place's line and column, a PDF's page size (the failing runs first, kept in the scratch): core/fuzz.zig gains two fuzzes, "a diff's hunk headers with numbers past any file's: drawn, formatted and looked at, never a panic" (each seed a diff whose `@@` numbers are drawn from around and past every int width, drawn as a `.diff`, formatted as the patch view, looked at on every row) and "a language server's rows and a typed Look with numbers past any file's: landed, stepped and drawn, never a panic" (rows as lsp.sourceSpanRow writes them for seven kinds of answer, landed, stepped with n N j k Enter, drawn; the same places typed to Look in its three spellings); on the base the first panicked at core/diff.zig:134 (`w.new_at += 1`, integer overflow: a `.diff`, or a `cat` of one in a terminal, with `@@ -1 +18446744073709551615 @@` killed the session and every client that drew it) and the second at core/look.zig:751 (`@intCast(at.col - 1)`, integer does not fit: `Look /test.txt:1:3000000000` typed, or such text B3'd anywhere, a server's row with a column past i32 looked at); the language server's own rows land through Query's landing, which clamped already (its client validates each coordinate to a u32); FIXES: core/diff.zig `number` reads a header's number as a u32 (a line or a count of lines: no file pardes opens has more), one past it makes the row no header, drawn as text as patch(1) refuses it, so the walk's counts, hunks.zig `moved`'s i64 and jj.zig's u32 casts all hold; core/look.zig `focusPaneLine` takes a line past an i32 as no line and lands a column past its line's end at the end (`columnOn`, as a search's landing and Text.clampCursor have it: `Look x:2:9` on a 5-byte line lands at its end, where it stood past it before; a terminal's column, a jump's, as it was), the newly opened file's the same, and `selectSpan` saturates each number into cellRange, which clamps to the text; the pdf addon: a page's size is held where the bridge reads it (addons/pdf/pdf_bridge.c `pardes_pdf_get_page_size`, every size the layout sees comes through it) to `PARDES_PDF_MAX_PAGE_POINTS` (2^24 pt, 5.9 km, on a side; MuPDF bounds neither a MediaBox nor a UserUnit, nor a float's infinity) and the addon says so ("a page past 16777216 pt on a side: laid out at that size") when one lands: on the base a page 1e9 pt wide panicked the addon's paint (`@intFromFloat`, out of bounds, pdf.zig:413: the session and every client that drew it), test/pdf-fixtures/huge-pages.pdf (a 1e9 pt side, a side past a float, a UserUnit past a float, a page as any) opened, drawn, stepped and ended in the addon's script (addons/pdf/pdf.script, pdf.out); a test in core/look.zig of the landing itself; core-test Debug 950/1116 (166 skipped; base 947/1113), shell-test Debug 1162/1165 (3 skipped), addon-test Debug whole Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- addons/pdf/pdf.out | 33 ++++++++++++++ addons/pdf/pdf.script | 8 ++++ addons/pdf/pdf.zig | 9 ++++ addons/pdf/pdf_bridge.c | 2 + addons/pdf/pdf_bridge.h | 7 ++- core/diff.zig | 7 ++- core/fuzz.zig | 98 ++++++++++++++++++++++++++++++++++++++++ core/look.zig | 40 +++++++++++++--- test/pdf-fixtures/huge-pages.pdf | 61 +++++++++++++++++++++++++ 9 files changed, 256 insertions(+), 9 deletions(-) create mode 100644 test/pdf-fixtures/huge-pages.pdf (limited to 'test') diff --git a/addons/pdf/pdf.out b/addons/pdf/pdf.out index 49ba2a88..2d4566b1 100644 --- a/addons/pdf/pdf.out +++ b/addons/pdf/pdf.out @@ -201,3 +201,36 @@ tag [3/16] petscii:on palette:commodore ascii:on docs/design.pdf |the native macOS host watches with debounced DispatchSourc |instead, then restats the exact path under a pane-generati |guard. All three macOS watchers mark the same two things f +> open test/pdf-fixtures/huge-pages.pdf +window 58x1 +tag [1/4] petscii:on palette:commodore ascii:on test/pdf-fixtures/huge-pages.pdf +| a page past 16777216 pt on a side: laid out at that size +| +> graphics on +tag [1/4] petscii:off palette:commodore ascii:on test/pdf-fixtures/huge-pages.pdf +| a page past 16777216 pt on a side: laid out at that size +| +placement 1x1 src 0,0 1x1 over 0,1 58x1 key fb85a0e4117bdb75 at 0,0 464x1 +placement 1200x1200 src 0,0 1200x19 over 0,1 58x1 key a44f40e9b170318e at 0,9 464x7 +> size 60 20 +window 58x1 +tag [1/4] petscii:off palette:commodore ascii:on test/pdf-fixtures/huge-pages.pdf +| a page past 16777216 pt on a side: laid out at that size +| +placement 1x1 src 0,0 1x1 over 0,1 58x1 key fb85a0e4117bdb75 at 0,0 464x1 +placement 1200x1200 src 0,0 1200x19 over 0,1 58x1 key a44f40e9b170318e at 0,9 464x7 +> key j +tag [2/4] petscii:off palette:commodore ascii:on test/pdf-fixtures/huge-pages.pdf +| a page past 16777216 pt on a side: laid out at that size +| +placement 1200x1200 src 0,18 1200x42 over 0,1 58x1 key a44f40e9b170318e at 0,0 464x16 +> key 2j +tag [2/4] petscii:off palette:commodore ascii:on test/pdf-fixtures/huge-pages.pdf +| a page past 16777216 pt on a side: laid out at that size +| +placement 1200x1200 src 0,100 1200x43 over 0,1 58x1 key a44f40e9b170318e at 0,0 464x16 +> key G +tag [4/4] petscii:off palette:commodore ascii:on test/pdf-fixtures/huge-pages.pdf +| a page past 16777216 pt on a side: laid out at that size +| +placement 816x64 src 0,0 816x29 over 0,1 58x1 key aa6de78f475e58f7 at 0,0 464x16 diff --git a/addons/pdf/pdf.script b/addons/pdf/pdf.script index 60e4bfca..62629ded 100644 --- a/addons/pdf/pdf.script +++ b/addons/pdf/pdf.script @@ -23,3 +23,11 @@ key 3gj key gk key 999gj key gj +# pages past any size (a side of 1e9 pt, one past a float, a UserUnit +# past a float): laid out and drawn at the bridge's bound, never a panic +open test/pdf-fixtures/huge-pages.pdf +graphics on +size 60 20 +key j +key 2j +key G diff --git a/addons/pdf/pdf.zig b/addons/pdf/pdf.zig index 79d79219..5694e8c8 100644 --- a/addons/pdf/pdf.zig +++ b/addons/pdf/pdf.zig @@ -270,6 +270,7 @@ pub const Pdf = extern struct { return addon.say("not a PDF pardes can read", .err); } o.d = d; + sayHuge(o.size[0..1]); st.pages = @intCast(pages); // every page the first's size until a lane measures them o.measured = st.pages == 1; @@ -812,6 +813,7 @@ pub const Pdf = extern struct { if (res.n != st.pages or sizes.len != st.pages) return; for (o.size, sizes) |*to, from| to.* = from; o.measured = true; + sayHuge(o.size); // laid again where a page is not the first's size for (o.size) |s| if (!std.meta.eql(s, o.size[0])) { for (&st.views) |*l| l.laid = false; @@ -958,6 +960,13 @@ fn ownOf(st: *const Pdf) *Own { } /// Page `p`'s size as the strip is laid with it. +/// A page the bridge held to its bound (a crafted MediaBox) is said. +fn sayHuge(sizes: []const m.pardes_pdf_page_size) void { + const most: f32 = m.PARDES_PDF_MAX_PAGE_POINTS; + for (sizes) |s| if (s.width >= most or s.height >= most) + return addon.say(std.fmt.comptimePrint("a page past {d} pt on a side: laid out at that size", .{m.PARDES_PDF_MAX_PAGE_POINTS}), .note); +} + fn sizeOf(st: *const Pdf, o: *Own, p: usize) m.pardes_pdf_page_size { if (st.sizes != .measured) return o.size[0]; // A client joined after the lane measured them: measured here. diff --git a/addons/pdf/pdf_bridge.c b/addons/pdf/pdf_bridge.c index 3b326655..e1279f51 100644 --- a/addons/pdf/pdf_bridge.c +++ b/addons/pdf/pdf_bridge.c @@ -591,6 +591,8 @@ pardes_pdf_get_page_size( out->height = bounds.y1 - bounds.y0; if (!(out->width > 0.0f) || !(out->height > 0.0f)) fz_throw(ctx, FZ_ERROR_FORMAT, "PDF page has empty bounds"); + out->width = fminf(out->width, PARDES_PDF_MAX_PAGE_POINTS); + out->height = fminf(out->height, PARDES_PDF_MAX_PAGE_POINTS); } fz_catch(ctx) { diff --git a/addons/pdf/pdf_bridge.h b/addons/pdf/pdf_bridge.h index fd5d05d0..0e52c505 100644 --- a/addons/pdf/pdf_bridge.h +++ b/addons/pdf/pdf_bridge.h @@ -203,7 +203,12 @@ enum { PARDES_PDF_MAX_LINK_URI_BYTES = 64 * 1024, /* No page raster is taller than this, whatever it is shown at. */ - PARDES_PDF_MAX_RASTER_ROWS = 1 << 20 + PARDES_PDF_MAX_RASTER_ROWS = 1 << 20, + + /* No page is laid out wider or taller than this, in points (5.9 km): a + * crafted MediaBox or UserUnit has any size, a float's infinity too, and + * MuPDF bounds neither; pdf.zig counts a page's pixels in u32s. */ + PARDES_PDF_MAX_PAGE_POINTS = 1 << 24 }; pardes_pdf_document *pardes_pdf_open_memory(const unsigned char *bytes, size_t length, int *page_count); diff --git a/core/diff.zig b/core/diff.zig index 659576d3..edf037fe 100644 --- a/core/diff.zig +++ b/core/diff.zig @@ -294,11 +294,14 @@ fn range(line: []const u8, at: *usize) ?[2]usize { return .{ start, 1 }; } +/// A header's number: a line of a file, or a count of them, so a u32 (no +/// file pardes opens has more lines); one past it makes the line no header, +/// where the walk's count would overflow (a crafted `+18446744073709551615`). fn number(line: []const u8, at: *usize) ?usize { const from = at.*; - var n: usize = 0; + var n: u32 = 0; while (at.* < line.len and std.ascii.isDigit(line[at.*])) : (at.* += 1) - n = std.math.add(usize, std.math.mul(usize, n, 10) catch return null, line[at.*] - '0') catch return null; + n = std.math.add(u32, std.math.mul(u32, n, 10) catch return null, line[at.*] - '0') catch return null; return if (at.* > from) n else null; } diff --git a/core/fuzz.zig b/core/fuzz.zig index f324a48e..23d007ac 100644 --- a/core/fuzz.zig +++ b/core/fuzz.zig @@ -771,3 +771,101 @@ test "9P: a rows style past the syntax's kinds is refused, never drawn" { _ = try p.render(p.frame_arena.allocator()); } } + +// --------------------------------------------------------------------------- +// (v, vi) Numbers in a file's text and in another program's answer: a +// diff's hunk headers (a `.diff` drawn, the patch view, a look's target) +// and a language server's rows (each kind's answer landed, stepped, drawn; +// the same places typed to Look). + +const diff = @import("diff.zig"); +const hunks = @import("hunks.zig"); +const lsp = pardes.lsp; + +/// Around what an int of each width holds, and past it. +const numbers = [_][]const u8{ "0", "1", "2", "3", "5", "65535", "2147483647", "2147483648", "3000000000", "4294967295", "4294967296", "9223372036854775807", "9223372036854775808", "18446744073709551615", "18446744073709551616", "99999999999999999999999" }; + +fn draw(p: *Pardes) !void { + p.sync(); + _ = p.frame_arena.reset(.retain_capacity); + _ = try p.render(p.frame_arena.allocator()); +} + +fn diffSeed(gpa: std.mem.Allocator, seed: u64) !void { + var prng: Random.DefaultPrng = .init(seed); + const r = prng.random(); + var text: std.ArrayList(u8) = .empty; + defer text.deinit(gpa); + try text.appendSlice(gpa, "diff --git a/x.zig b/x.zig\n--- a/x.zig\n+++ b/x.zig\n"); + for (0..1 + r.uintLessThan(usize, 3)) |_| { + try text.print(gpa, "@@ -{s}", .{pick(r, numbers)}); + if (chance(r, 0.7)) try text.print(gpa, ",{s}", .{pick(r, numbers)}); + try text.print(gpa, " +{s}", .{pick(r, numbers)}); + if (chance(r, 0.7)) try text.print(gpa, ",{s}", .{pick(r, numbers)}); + try text.appendSlice(gpa, " @@ fn x()\n"); + const lines = [_][]const u8{ " const a = 1;\n", "-const b = 2;\n", "+const b = 3;\n", "\\ No newline at end of file\n", "\n", "--- y\n", "+++ y\n", "text\n" }; + for (0..r.uintLessThan(usize, 9)) |_| try text.appendSlice(gpa, pick(r, lines)); + } + // A `.diff` drawn. + const p = try th.withFile(gpa, text.items); + defer p.deinit(); + const pane = p.panes[p.view().active].?; + gpa.free(pane.file.?.path); + pane.file.?.path = try gpa.dupe(u8, "/change.diff"); + pane.file.?.syntax_dirty = true; + try draw(p); + // The patch view. + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + const ctx: hunks.Ctx = .{ .arena = arena.allocator() }; + _ = hunks.format(&ctx, "", text.items) catch {}; + // A look's target, at every row. + var rows: std.ArrayList([]const u8) = .empty; + defer rows.deinit(gpa); + var it = std.mem.splitScalar(u8, text.items, '\n'); + while (it.next()) |row| try rows.append(gpa, row); + for (0..rows.items.len) |row| _ = diff.targetAt(rows.items, row, false); +} + +test "a diff's hunk headers with numbers past any file's: drawn, formatted and looked at, never a panic" { + for (seeds) |seed| for (0..4) |k| try diffSeed(testing.allocator, seed *% 31 +% k); +} + +fn lspSeed(gpa: std.mem.Allocator, seed: u64) !void { + var prng: Random.DefaultPrng = .init(seed); + const r = prng.random(); + const p = try th.withFile(gpa, "alpha one\n\tbeta\n\t\tgamma \xe6\x97\xa5\n\n"); + defer p.deinit(); + var rows: std.ArrayList(u8) = .empty; + defer rows.deinit(gpa); + for (0..1 + r.uintLessThan(usize, 4)) |_| { + // As lsp.sourceSpanRow writes them; the file's own name. + try rows.print(gpa, "/test.txt:{s}:{s}", .{ pick(r, numbers), pick(r, numbers) }); + if (chance(r, 0.5)) try rows.print(gpa, "-{s}", .{pick(r, numbers)}); + try rows.appendSlice(gpa, " alpha\n"); + } + const kinds = [_]lsp.Kind{ .definition, .declaration, .implementation, .references, .diagnostics, .document_symbols, .workspace_symbols }; + const kind = pick(r, kinds); + if (lsp.supports.contains(kind)) { + p.lspRequest(0, kind, ""); + while (p.nextEffect()) |_| {} + if (p.lsp_ask.wait) |wait| p.update(.{ .lsp_resp = .{ .id = wait.id, .rows = rows.items } }); + try draw(p); + for ("nNjk\r") |key| { + p.update(.{ .key = .{ .cp = if (key == '\r') pardes.Key.enter else key } }); + try draw(p); + } + } + // The same places typed. + var look: [96]u8 = undefined; + p.update(.{ .command = try std.fmt.bufPrint(&look, "Look /test.txt:{s}:{s}", .{ pick(r, numbers), pick(r, numbers) }) }); + try draw(p); + p.update(.{ .command = try std.fmt.bufPrint(&look, "Look /test.txt:{s}:{s}-{s}:{s}", .{ pick(r, numbers), pick(r, numbers), pick(r, numbers), pick(r, numbers) }) }); + try draw(p); + p.update(.{ .command = try std.fmt.bufPrint(&look, "Look /test.txt:{s}-{s}", .{ pick(r, numbers), pick(r, numbers) }) }); + try draw(p); +} + +test "a language server's rows and a typed Look with numbers past any file's: landed, stepped and drawn, never a panic" { + for (seeds) |seed| for (0..4) |k| try lspSeed(testing.allocator, seed *% 31 +% k); +} diff --git a/core/look.zig b/core/look.zig index 53c26943..41b48393 100644 --- a/core/look.zig +++ b/core/look.zig @@ -747,20 +747,29 @@ pub fn focusPaneLine(p: *Pardes, id: usize, at: Spot, landing: enum { center, ke } } } - pane.body.cur_row = @intCast(at.line - 1); - pane.body.cur_col = if (at.col > 0) @intCast(at.col - 1) else 0; + pane.body.cur_row = std.math.cast(i32, at.line - 1) orelse return; + pane.body.cur_col = columnOn(pane, pane.body.cur_row, at.col); pane.body.cur_pinned = true; if (at.end_line != 0) return selectSpan(p, pane, at); pane.ensureCursorVisible(p.settings.tab); } +/// A place's column (from 1) as a cursor's on `row`: past the line's end (a +/// server's answer, a typed number: any) it is the end, as a search's +/// landing and clampCursor have it. A terminal's (a jump's) as it was. +fn columnOn(pane: *const Pane, row: i32, col: usize) i32 { + if (pane.file == null) return std.math.lossyCast(i32, col -| 1); + return @intCast(@min(col -| 1, panes.File.sourceLine(pane, row).len)); +} + fn selectSpan(p: *Pardes, pane: *Pane, at: Spot) void { const text = edit.flatSurface(p.editor(), &pane.body) catch |err| return p.reportError(.at(p.view().active), "look", err); // saturating, not `- 1`: `f.zig:0-5` is a legal thing to type and a // 1-based zero is the same nothing an absent number is - const acol: i32 = @intCast(at.col -| 1); - const ecol: i32 = if (at.end_col > 0) @intCast(at.end_col - 1) else std.math.maxInt(i32); - const r = panes.Text.cellRange(text, @intCast(at.line -| 1), acol, @intCast(at.end_line -| 1), ecol); + // Any number a place has: cellRange clamps each to the text. + const acol = std.math.lossyCast(i32, at.col -| 1); + const ecol: i32 = if (at.end_col > 0) std.math.lossyCast(i32, at.end_col - 1) else std.math.maxInt(i32); + const r = panes.Text.cellRange(text, std.math.lossyCast(i32, at.line -| 1), acol, std.math.lossyCast(i32, at.end_line -| 1), ecol); pane.body.setRange(p.settings.tab, text, 0, r, true); } @@ -1590,7 +1599,7 @@ pub fn lookAt(p: *Pardes, id: usize, operand_raw: []const u8) void { const free = p.freeSlot() orelse return p.reportError(.at(id), "look", error.NoPaneSlots); const nt = panes.File.open(p, free, target.path, target.at.line) catch |err| return lookOpenFailed(p, id, target.path, err); - if (target.at.col > 0) nt.body.cur_col = @intCast(target.at.col - 1); + nt.body.cur_col = columnOn(nt, nt.body.cur_row, target.at.col); const failures = p.fs.failures; layout.placeNew(p, id, id, free, .doc); // Refused a place: that is the look's one failure, not a @@ -2010,6 +2019,25 @@ test "Look copies its operand before releasing pointer selection storage" { try std.testing.expectEqual(.none, p.view().drag); } +test "a Look whose column is past its line's end lands at the end, however big; a line past the file's is a miss" { + const p = try Pardes.init(std.testing.allocator, .{ .cols = 60, .rows = 12 }); + defer p.deinit(); + const pane = try p.setTestFile("alpha\n\tbeta\n"); + // Up to 2026-11-23 a column past i32 panicked the session (`@intCast` + // in focusPaneLine): any text a B3 or Look takes, a server's row. + for ([_][]const u8{ "/test.txt:2:9", "/test.txt:2:3000000000", "/test.txt:2:18446744073709551615" }) |place| { + lookAt(p, 0, place); + try std.testing.expectEqual(@as(i32, 1), pane.body.cur_row); + try std.testing.expectEqual(@as(i32, 5), pane.body.cur_col); + } + // A span past the text: selected to its end. + lookAt(p, 0, "/test.txt:1:2-3000000000:3000000000"); + try std.testing.expect(pane.body.vsel.active); + pane.body.cur_row = 0; + lookAt(p, 0, "/test.txt:3000000000:1"); + try std.testing.expectEqual(@as(i32, 0), pane.body.cur_row); +} + test "@p names a pane by its serial, as /index does" { const p = try Pardes.init(std.testing.allocator, .{ .cols = 60, .rows = 12 }); defer p.deinit(); diff --git a/test/pdf-fixtures/huge-pages.pdf b/test/pdf-fixtures/huge-pages.pdf new file mode 100644 index 00000000..f2d73a14 --- /dev/null +++ b/test/pdf-fixtures/huge-pages.pdf @@ -0,0 +1,61 @@ +%PDF-1.4 +1 0 obj +<< /Type /Catalog /Pages 2 0 R >> +endobj +2 0 obj +<< /Type /Pages /Kids [3 0 R 5 0 R 7 0 R 9 0 R] /Count 4 >> +endobj +3 0 obj +<< /Type /Page /Parent 2 0 R /MediaBox [0 0 1000000000 792] /Contents 4 0 R >> +endobj +4 0 obj +<< /Length 17 >> +stream +0 0 m 100 100 l S +endstream +endobj +5 0 obj +<< /Type /Page /Parent 2 0 R /MediaBox [0 0 1000000000000000000000000000000000000000000000 1000000000000000000000000000000000000000000000] /Contents 6 0 R >> +endobj +6 0 obj +<< /Length 17 >> +stream +0 0 m 100 100 l S +endstream +endobj +7 0 obj +<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /UserUnit 1000000000000000000000000000000000000 /Contents 8 0 R >> +endobj +8 0 obj +<< /Length 17 >> +stream +0 0 m 100 100 l S +endstream +endobj +9 0 obj +<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Contents 10 0 R >> +endobj +10 0 obj +<< /Length 17 >> +stream +0 0 m 100 100 l S +endstream +endobj +xref +0 11 +0000000000 65535 f +0000000009 00000 n +0000000058 00000 n +0000000133 00000 n +0000000227 00000 n +0000000294 00000 n +0000000467 00000 n +0000000534 00000 n +0000000669 00000 n +0000000736 00000 n +0000000824 00000 n +trailer +<< /Size 11 /Root 1 0 R >> +startxref +892 +%%EOF -- cgit v1.3 From 2d54f7ff16f353ec8267133a9159591fbeca9f8d Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Mon, 23 Nov 2026 17:58:08 -0300 Subject: addons: a Zig addon's panic is pardes's panic, and a segmentation fault leaves a crash record: RUN on the base (a throwaway Debug build whose snake addon panics at its open, a tui session in a pty of its own, Snake sent over its 9P; .scratch kept/i5-addon-panic-throw): the session died with the trace on the terminal, NO CRASH RECORD, the terminal LEFT RAW (`-isig -icanon -iexten -echo -opost`, the alternate screen and the mouse modes still on: the shell after it took no line), `pardes new` exited 0, the daemon kept the shell ("shells no session holds"); the cause: a library kind is its own root, so its `@panic` ran std's handler, never runtime/main.zig's (replica.dying, vaxis.recover, crash.record); THE CHANGE: the ABI's pa_api gains `panic` (3.3, at its end, checked by api->size as `draw` is; an addon of 3.2 loads as ever, a 3.3 addon on an older pardes keeps std's handler), the core's entry (core/Kind.zig) `@panic`s with the addon's words ("an addon's panic: ...") so the host's root handler runs, and abi/addon.zig's `panic` is a handler that calls it, which every Zig addon's root names (`pub const panic = addon.panic;`: pdf, image, life, snake, snake_shader, test/liar, test/veil, and the example in addon.zig's head); runtime/main.zig `debug.handleSegfault` writes the crash record too (a fault in C code: MuPDF, stb, tree-sitter, an addon's library); RUN on the change (kept/i5-addon-panic-fixed): the record written ("panic: an addon's panic: addon panic: ... in addon snake, open"), the terminal put back (`isig icanon iexten echo`, the alternate screen and the mouse modes left), the daemon keeping the shell as before; NOT BUILT, the user's to rule: recovery (an addon's panic ending its pane and not the session); the behaviour tests read the addons' ABI as 3.3; addon-test Debug whole Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- abi/addon.zig | 23 +++++++++++++++++++---- abi/pardes_addon.h | 9 +++++++-- addons/image/image.zig | 1 + addons/life/life.zig | 1 + addons/pdf/pdf.zig | 1 + addons/snake/snake.zig | 1 + addons/snake_shader/snake_shader.zig | 1 + core/Kind.zig | 11 ++++++++++- runtime/main.zig | 3 +++ test/behaviour/addons.zig | 8 ++++---- test/behaviour/addons_pdf.zig | 2 +- test/liar.zig | 1 + test/veil.zig | 1 + 13 files changed, 51 insertions(+), 12 deletions(-) (limited to 'test') diff --git a/abi/addon.zig b/abi/addon.zig index 059e016d..53227fbb 100644 --- a/abi/addon.zig +++ b/abi/addon.zig @@ -20,9 +20,10 @@ //! //! and pardes's side (pa_api) as the functions below: `arena` and `heap` //! as Allocators, `seed`, `say`, `job`, `draw`, `cancel`, `look`, `text`, `exec`, -//! `select` and `found`. A body written is at most `body_max` bytes. +//! `select`, `found` and `panic`. A body written is at most `body_max` bytes. //! //! pub const pardes_addon = addon.ops(Snake); +//! pub const panic = addon.panic; //! comptime { //! addon.exportLib(&pardes_addon); //! } @@ -36,9 +37,9 @@ pub const c = @import("pardes_addon"); const Allocator = std.mem.Allocator; /// The ABI this file speaks: a header of another needs it brought along. -pub const abi = 0x30002; +pub const abi = 0x30003; comptime { - if (c.PA_ABI != abi) @compileError("addon.zig speaks ABI 3.2 and the header another"); + if (c.PA_ABI != abi) @compileError("addon.zig speaks ABI 3.3 and the header another"); } /// A word the kind brings: `opens` a pane of it, else goes to the pane's @@ -51,6 +52,19 @@ pub const Head = struct { name: []const u8, version: u32, exts: [:0]const u8 = " /// pardes's side, kept from init: about the pane of the current call. pub var api: *const c.pa_api = undefined; +/// pardes's panic (pa_api.panic), from init, when its pardes has one. +var host_panic: ?*const fn ([*c]const u8, u32) callconv(.c) void = null; + +/// A library kind's panic handler (its root's `pub const panic = +/// addon.panic;`): pardes's own, so the panic ends the process as one of +/// pardes's does (its crash record, its terminal put back, its clients +/// told); std's where its pardes is older than ABI 3.3, or before init. +pub const panic = std.debug.FullPanic(struct { + fn call(msg: []const u8, ret_addr: ?usize) noreturn { + if (host_panic) |f| f(msg.ptr, @intCast(@min(msg.len, std.math.maxInt(u32)))); + std.debug.defaultPanic(msg, ret_addr); + } +}.call); /// The pane's arena: 16-aligned zeroed memory, all of it freed at the /// pane's close (a free frees nothing). @@ -91,7 +105,7 @@ pub fn job(in: []const u8) u32 { /// of the process that shows the pane, its answer to `jobDone` there alone /// (`token & drawn`), which keeps it for `paint` only. 0 when refused. pub fn draw(in: []const u8) u32 { - if (api.size < @sizeOf(c.pa_api)) return 0; + if (api.size < @offsetOf(c.pa_api, "draw") + @sizeOf(@TypeOf(api.draw))) return 0; return api.draw.?(in.ptr, @intCast(in.len)); } pub const drawn = c.PA_DRAWN; @@ -283,6 +297,7 @@ pub fn ops(comptime T: type) c.pa_ops { } fn init(a: [*c]const c.pa_api) callconv(.c) c_int { api = a; + if (a.*.size >= @offsetOf(c.pa_api, "panic") + @sizeOf(@TypeOf(a.*.panic))) host_panic = a.*.panic; return if (!@hasDecl(T, "init") or T.init()) c.PA_OK else 1; } fn open(path: [*c]const u8, bytes: [*c]const u8, len: u64) callconv(.c) ?*anyopaque { diff --git a/abi/pardes_addon.h b/abi/pardes_addon.h index 2f5c53de..d8257a0b 100644 --- a/abi/pardes_addon.h +++ b/abi/pardes_addon.h @@ -37,7 +37,7 @@ /* Major in the high half: an addon of another major is refused. */ #define PA_ABI_MAJOR 3u -#define PA_ABI_MINOR 2u +#define PA_ABI_MINOR 3u #define PA_ABI ((PA_ABI_MAJOR << 16) | PA_ABI_MINOR) #define PA_OK 0 #define PA_RASTER_MAX (32u << 20) /* a placement's w*h*4 above this is dropped, said once */ @@ -234,6 +234,11 @@ typedef struct pa_api { * PA_PLAIN kind's block, nor its token) `paint` alone reads; no other * call reads it, or anything made from it. 0: refused. */ uint32_t (*draw)(const void *in, uint32_t n); + /* Since 3.3 (check api->size): the addon panicked (a Zig addon's + * `pub const panic = addon.panic;`, a C addon's fatal error). pardes ends + * as at a panic of its own: its crash record written, its terminal put + * back, its clients told; it does not return. */ + void (*panic)(const char *msg, uint32_t n); } pa_api; /* A token of api->draw's. */ #define PA_DRAWN 0x80000000u @@ -390,7 +395,7 @@ _Static_assert(sizeof(pa_rect) == 16 && sizeof(pa_cells) == 8, "pa_rect"); _Static_assert(sizeof(pa_placement) == 80, "pa_placement"); _Static_assert(sizeof(pa_paint) == 48 && sizeof(pa_view) == 32 && sizeof(pa_tick) == 128, "pa_paint"); _Static_assert(sizeof(pa_key) == 8 && sizeof(pa_mouse) == 16, "pa_key"); -_Static_assert(sizeof(pa_api) == 120 && sizeof(pa_out) == 16, "pa_api"); +_Static_assert(sizeof(pa_api) == 128 && sizeof(pa_out) == 16, "pa_api"); _Static_assert(sizeof(pa_word) == 32 && sizeof(pa_head) == 56, "pa_head"); _Static_assert(sizeof(pa_ops) == 208, "pa_ops"); #endif diff --git a/addons/image/image.zig b/addons/image/image.zig index 8e2cb32a..5b7976ac 100644 --- a/addons/image/image.zig +++ b/addons/image/image.zig @@ -128,6 +128,7 @@ pub const Image = extern struct { }; pub const pardes_addon = addon.ops(Image); +pub const panic = addon.panic; comptime { addon.exportLib(&pardes_addon); } diff --git a/addons/life/life.zig b/addons/life/life.zig index 254f248d..c49878cd 100644 --- a/addons/life/life.zig +++ b/addons/life/life.zig @@ -5,6 +5,7 @@ const std = @import("std"); const addon = @import("addon"); pub const pardes_addon = addon.ops(Life); +pub const panic = addon.panic; comptime { addon.exportLib(&pardes_addon); } diff --git a/addons/pdf/pdf.zig b/addons/pdf/pdf.zig index 5694e8c8..e1539eae 100644 --- a/addons/pdf/pdf.zig +++ b/addons/pdf/pdf.zig @@ -35,6 +35,7 @@ const m = @import("pdf_bridge"); const c = addon.c; pub const pardes_addon = addon.ops(Pdf); +pub const panic = addon.panic; comptime { addon.exportLib(&pardes_addon); } diff --git a/addons/snake/snake.zig b/addons/snake/snake.zig index ef2283ac..9f2bdd4e 100644 --- a/addons/snake/snake.zig +++ b/addons/snake/snake.zig @@ -131,6 +131,7 @@ pub const Snake = extern struct { }; pub const pardes_addon = addon.ops(Snake); +pub const panic = addon.panic; comptime { addon.exportLib(&pardes_addon); } diff --git a/addons/snake_shader/snake_shader.zig b/addons/snake_shader/snake_shader.zig index 039e07b0..65e1dd44 100644 --- a/addons/snake_shader/snake_shader.zig +++ b/addons/snake_shader/snake_shader.zig @@ -372,6 +372,7 @@ pub const Snake = extern struct { }; pub const pardes_addon = addon.ops(Snake); +pub const panic = addon.panic; comptime { addon.exportLib(&pardes_addon); } diff --git a/core/Kind.zig b/core/Kind.zig index 5d2dcdb0..764b6f06 100644 --- a/core/Kind.zig +++ b/core/Kind.zig @@ -41,7 +41,7 @@ comptime { if (@sizeOf(usize) == 8) for (.{ .{ c.pa_cell, 20 }, .{ c.pa_window, 24 }, .{ c.pa_rect, 16 }, .{ c.pa_cells, 8 }, .{ c.pa_placement, 80 }, .{ c.pa_paint, 48 }, .{ c.pa_key, 8 }, .{ c.pa_mouse, 16 }, - .{ c.pa_api, 120 }, .{ c.pa_out, 16 }, .{ c.pa_word, 32 }, .{ c.pa_head, 56 }, + .{ c.pa_api, 128 }, .{ c.pa_out, 16 }, .{ c.pa_word, 32 }, .{ c.pa_head, 56 }, .{ c.pa_ops, 208 }, .{ c.pa_view, 32 }, .{ c.pa_tick, 128 }, }) |r| if (@sizeOf(r[0]) != r[1]) @compileError(std.fmt.comptimePrint("{s} is {d} bytes, the header says {d}", .{ @typeName(r[0]), @sizeOf(r[0]), r[1] })); } @@ -1229,6 +1229,15 @@ pub const api: c.pa_api = .{ return token; } }.f, + .panic = struct { + /// An addon's panic is pardes's (its root's handler: the crash + /// record, the terminal, the clients): a call into it never returns. + fn f(msg: [*c]const u8, n: u32) callconv(.c) void { + var buf: [512]u8 = undefined; + const said = cSlice(msg, n); + @panic(std.fmt.bufPrint(&buf, "an addon's panic: {s}", .{said}) catch said); + } + }.f, .found = struct { /// Gathered in the call: the search's answer, or onto its +Search after. fn f(rows: [*c]const u8, n: u32) callconv(.c) void { diff --git a/runtime/main.zig b/runtime/main.zig index 5cdf5ee0..e428faa2 100644 --- a/runtime/main.zig +++ b/runtime/main.zig @@ -52,6 +52,9 @@ pub const debug = struct { pardes.replica.dying(); @import("vaxis").recover(); } + // A fault in C code (an addon's library, MuPDF, tree-sitter) leaves + // a record as a panic does. + @import("runtime").crash.record(name); return std.debug.defaultHandleSegfault(addr, name, opt_ctx); } }; diff --git a/test/behaviour/addons.zig b/test/behaviour/addons.zig index e36b088c..1b284f8d 100644 --- a/test/behaviour/addons.zig +++ b/test/behaviour/addons.zig @@ -37,7 +37,7 @@ test "addons: Addon load takes a library in, /addons lists it with the kinds com defer unload(&s, "fake"); // ` `. const listed = try s.read("/addons"); - try std.testing.expect(std.mem.startsWith(u8, listed, "fake 1 3.2 0 /")); + try std.testing.expect(std.mem.startsWith(u8, listed, "fake 1 3.3 0 /")); var fields = std.mem.tokenizeScalar(u8, listed[0..std.mem.indexOfScalar(u8, listed, '\n').?], ' '); for (0..5) |_| _ = fields.next(); try std.testing.expectEqual(@as(usize, 64), fields.next().?.len); @@ -51,7 +51,7 @@ test "addons: Addon load takes a library in, /addons lists it with the kinds com // A pane of it open, it stays; closed, it goes. try s.write("/ctl", "Fake\n"); const pane = try paneOf(&s, "fake"); - try std.testing.expect(std.mem.startsWith(u8, try s.read("/addons"), "fake 1 3.2 1 /")); + try std.testing.expect(std.mem.startsWith(u8, try s.read("/addons"), "fake 1 3.3 1 /")); try std.testing.expect(try s.refused("/ctl", "Addon unload fake\n") != 0); try std.testing.expect(has(s.said, "1 fake pane is open")); try s.write(s.at("/pane/{d}/ctl", .{pane}), "Del\n"); @@ -68,7 +68,7 @@ test "addons: a library of another ABI, one with no pardes_addon, and one whose try std.testing.expect(std.mem.startsWith(u8, listed, "fakeabi 1 2.2 0 /")); try std.testing.expect(has(listed, " refused: wants abi 2, this pardes speaks 3\n")); try std.testing.expect(has(listed, " refused: it has no pardes_addon\n")); - try std.testing.expect(has(listed, "\nfakeword 1 3.2 0 /")); + try std.testing.expect(has(listed, "\nfakeword 1 3.3 0 /")); try std.testing.expect(has(listed, " refused: word Save is the core's\n")); // A file that is no library at all: listed with what the loader said. var tmp = std.testing.tmpDir(.{}); @@ -120,7 +120,7 @@ test "addons: the image addon opens the pictures a Look names, fitted, with the defer s.deinit(); try load(&s, Session.image_addon); const listed = try s.read("/addons"); - try std.testing.expect(std.mem.startsWith(u8, listed, "image 1 3.2 0 /") and has(listed, " loaded\n")); + try std.testing.expect(std.mem.startsWith(u8, listed, "image 1 3.3 0 /") and has(listed, " loaded\n")); var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); var buf: [4096]u8 = undefined; diff --git a/test/behaviour/addons_pdf.zig b/test/behaviour/addons_pdf.zig index 18fa503c..0c7982d1 100644 --- a/test/behaviour/addons_pdf.zig +++ b/test/behaviour/addons_pdf.zig @@ -38,7 +38,7 @@ test "addons: a PDF is the pdf addon's pane, its pages pictures, its page in its s.graphics(); const n = try s.lookFile("docs/design.pdf"); try std.testing.expect(has(try s.read("/index"), " pdf ")); - try std.testing.expect(std.mem.startsWith(u8, try s.read("/addons"), "pdf 1 3.2 1 /")); + try std.testing.expect(std.mem.startsWith(u8, try s.read("/addons"), "pdf 1 3.3 1 /")); const pages = (try s.pdfPage(n))[1]; try std.testing.expectEqual(@as(u32, 16), pages); // Its first page, a picture over its body. diff --git a/test/liar.zig b/test/liar.zig index f564614b..e7bc2763 100644 --- a/test/liar.zig +++ b/test/liar.zig @@ -32,6 +32,7 @@ pub const Liar = extern struct { }; pub const pardes_addon = addon.ops(Liar); +pub const panic = addon.panic; comptime { addon.exportLib(&pardes_addon); } diff --git a/test/veil.zig b/test/veil.zig index 0be5d218..ac51de1c 100644 --- a/test/veil.zig +++ b/test/veil.zig @@ -32,6 +32,7 @@ pub const Veil = extern struct { }; pub const pardes_addon = addon.ops(Veil); +pub const panic = addon.panic; comptime { addon.exportLib(&pardes_addon); } -- cgit v1.3