summaryrefslogtreecommitdiff
path: root/abi
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-23 17:58:08 -0300
committerGabriel Schneider <[email protected]>2026-11-23 18:05:08 -0300
commit2d54f7ff16f353ec8267133a9159591fbeca9f8d (patch)
treece5c00e973ad49f18cbd6183efa5f3662ce4b41c /abi
parent4eb608b0c1f231005237b0f57283d22bfccee0a9 (diff)
downloadpardes-2d54f7ff16f353ec8267133a9159591fbeca9f8d.tar.gz
pardes-2d54f7ff16f353ec8267133a9159591fbeca9f8d.zip
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 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'abi')
-rw-r--r--abi/addon.zig23
-rw-r--r--abi/pardes_addon.h9
2 files changed, 26 insertions, 6 deletions
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