diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-16 23:37:24 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 12:15:01 -0300 |
| commit | 233f811bba051de380457183ee41c51a034ecd4c (patch) | |
| tree | 29e0232de2dde5c383ac670d22529648757bfad8 | |
| parent | 341882fc1a655137c7a68cdf45c77d5d53c0e522 (diff) | |
| download | sketchbook-233f811bba051de380457183ee41c51a034ecd4c.tar.gz sketchbook-233f811bba051de380457183ee41c51a034ecd4c.zip | |
Keep a loaded sketch library's name valid, for panic traces
A panic's stack trace finds a library's debug info by the name dlopen was
given, and the load link was deleted right after loading: every frame in a
sketch printed as ??? (a sketch that links libc; one that does not is not
seen by Zig's own dl_iterate_phdr at all). Link and fd now live as long as
the library is loaded, and Library.close removes them; a host that dies
leaves its dangling links in $XDG_RUNTIME_DIR.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
| -rw-r--r-- | src/host/library.zig | 44 |
1 files changed, 32 insertions, 12 deletions
diff --git a/src/host/library.zig b/src/host/library.zig index 6516704..6619d5b 100644 --- a/src/host/library.zig +++ b/src/host/library.zig @@ -3,10 +3,11 @@ //! `dlopen` returns the handle it already has for a path it loaded before, so //! every load goes through a symlink with a fresh name to an fd open on the //! library: what loads is the file that was opened, whatever a build renames -//! over it meanwhile. The link is deleted right after loading: the mapping -//! keeps the code alive, and nothing is left behind if the host dies. A -//! library rewritten in place instead (same inode) would not load again, and -//! would change the code under the running host. +//! over it meanwhile. Link and fd live as long as the library is loaded: the +//! name dlopen was given is where a panic's trace reads the sketch's debug +//! info, so a host that dies leaves its dangling links behind. A library +//! rewritten in place instead (same inode) would not load again, and would +//! change the code under the running host. const std = @import("std"); const linux = std.os.linux; const c = @import("c"); @@ -15,6 +16,11 @@ const abi = @import("abi"); pub const Library = struct { object: *c.SDL_SharedObject, sketch: abi.Sketch, + /// What the link names (the library, or its fixed copy) and where the + /// link is, for `openFresh`'s libraries; -1 for `open`'s. + fd: i32 = -1, + scratch_dir: []const u8 = "", + serial: usize = 0, /// Loads the library at `path`; `source` names it in messages. pub fn open(path: [:0]const u8, source: []const u8) !Library { @@ -47,22 +53,36 @@ pub const Library = struct { pub fn openFresh(io: std.Io, source: []const u8, scratch_dir: []const u8, serial: usize) !Library { const cwd = std.Io.Dir.cwd(); const file = try cwd.openFile(io, source, .{}); - defer file.close(io); - const fixed = try withoutVersym(file.handle); - defer if (fixed) |fd| { - _ = linux.close(fd); + const fixed = withoutVersym(file.handle) catch |err| { + file.close(io); + return err; }; + if (fixed != null) file.close(io); + const fd = fixed orelse file.handle; + errdefer _ = linux.close(fd); var target_buf: [32]u8 = undefined; - const target = try std.mem.print(&target_buf, "/proc/self/fd/{d}", .{fixed orelse file.handle}); + const target = try std.mem.print(&target_buf, "/proc/self/fd/{d}", .{fd}); var buf: [std.fs.max_path_bytes]u8 = undefined; - const link = try std.mem.printSentinel(&buf, "{s}/sketchbook-{d}-{d}.so", .{ scratch_dir, linux.getpid(), serial }, 0); + const link = try linkName(&buf, scratch_dir, serial); try cwd.symLink(io, target, link, .{}); - defer cwd.deleteFile(io, link) catch {}; - return open(link, source); + errdefer cwd.deleteFile(io, link) catch {}; + var library = try open(link, source); + library.fd = fd; + library.scratch_dir = scratch_dir; + library.serial = serial; + return library; } pub fn close(self: *Library) void { c.SDL_UnloadObject(self.object); + if (self.fd < 0) return; + var buf: [std.fs.max_path_bytes]u8 = undefined; + _ = linux.unlink(linkName(&buf, self.scratch_dir, self.serial) catch unreachable); + _ = linux.close(self.fd); + } + + fn linkName(buf: []u8, scratch_dir: []const u8, serial: usize) ![:0]u8 { + return std.mem.printSentinel(buf, "{s}/sketchbook-{d}-{d}.so", .{ scratch_dir, linux.getpid(), serial }, 0); } }; |
