summaryrefslogtreecommitdiff
path: root/src/host/library.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/host/library.zig')
-rw-r--r--src/host/library.zig45
1 files changed, 33 insertions, 12 deletions
diff --git a/src/host/library.zig b/src/host/library.zig
index 6958eec..91b2296 100644
--- a/src/host/library.zig
+++ b/src/host/library.zig
@@ -1,9 +1,12 @@
//! Loading sketch libraries, and noticing when a build replaces one.
//!
//! `dlopen` returns the handle it already has for a path it loaded before, so
-//! every load goes through a private copy with a fresh name. The copy is
-//! deleted right after loading: the mapping keeps the code alive, and nothing
-//! is left behind if the host dies.
+//! 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.
const std = @import("std");
const linux = std.os.linux;
const c = @import("c");
@@ -13,7 +16,8 @@ pub const Library = struct {
object: *c.SDL_SharedObject,
sketch: abi.Sketch,
- pub fn open(path: [:0]const u8) !Library {
+ /// Loads the library at `path`; `source` names it in messages.
+ pub fn open(path: [:0]const u8, source: []const u8) !Library {
const object = c.SDL_LoadObject(path.ptr) orelse return error.LoadLibrary;
errdefer c.SDL_UnloadObject(object);
var sketch: abi.Sketch = undefined;
@@ -22,18 +26,35 @@ pub const Library = struct {
const address = c.SDL_LoadFunction(object, symbol) orelse return error.MissingEntryPoint;
@field(sketch, name) = @ptrCast(address);
}
- if (sketch.info().abi != abi.hash) return error.AbiMismatch;
+ if (sketch.info().abi != abi.hash) {
+ const address = c.SDL_LoadFunction(object, "sketch_build") orelse {
+ std.log.err("{s}: built by an older sketchbook: rebuild it", .{source});
+ return error.AbiMismatch;
+ };
+ const build = std.mem.span(@as([*:0]const u8, @ptrFromInt(@intFromPtr(address))));
+ if (std.mem.eql(u8, build, abi.build)) {
+ std.log.err("{s}: built against other abi types: restart the host to load it", .{source});
+ } else {
+ const mode = build[std.mem.lastIndexOfScalar(u8, build, ' ').? + 1 ..];
+ std.log.err("{s}: built by zig {s}, this host by zig {s}; Zig calls cross the boundary, so both need the same compiler, backend and mode: build the host like the library (`zig build run -Doptimize={s}`) or the library like the host", .{ source, build, abi.build, mode });
+ }
+ return error.AbiMismatch;
+ }
return .{ .object = object, .sketch = sketch };
}
- /// Loads `source` through a uniquely named copy in `scratch_dir`.
- pub fn openCopy(io: std.Io, source: []const u8, scratch_dir: []const u8, serial: usize) !Library {
- var buf: [std.fs.max_path_bytes]u8 = undefined;
- const copy = try std.mem.printSentinel(&buf, "{s}/sketchbook-{d}-{d}.so", .{ scratch_dir, linux.getpid(), serial }, 0);
+ /// Loads `source` through a uniquely named symlink in `scratch_dir`.
+ pub fn openFresh(io: std.Io, source: []const u8, scratch_dir: []const u8, serial: usize) !Library {
const cwd = std.Io.Dir.cwd();
- try cwd.copyFile(source, cwd, copy, io, .{});
- defer cwd.deleteFile(io, copy) catch {};
- return open(copy);
+ const file = try cwd.openFile(io, source, .{});
+ defer file.close(io);
+ var target_buf: [32]u8 = undefined;
+ const target = try std.mem.print(&target_buf, "/proc/self/fd/{d}", .{file.handle});
+ 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);
+ try cwd.symLink(io, target, link, .{});
+ defer cwd.deleteFile(io, link) catch {};
+ return open(link, source);
}
pub fn close(self: *Library) void {