1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
//! 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 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. 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");
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 {
const object = c.SDL_LoadObject(path.ptr) orelse return error.LoadLibrary;
errdefer c.SDL_UnloadObject(object);
var sketch: abi.Sketch = undefined;
inline for (comptime std.meta.fieldNames(abi.Sketch)) |name| {
const symbol = @field(abi.Sketch.symbols, name);
const address = c.SDL_LoadFunction(object, symbol) orelse return error.MissingEntryPoint;
@field(sketch, name) = @ptrCast(address);
}
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 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();
const file = try cwd.openFile(io, source, .{});
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}", .{fd});
var buf: [std.fs.max_path_bytes]u8 = undefined;
const link = try linkName(&buf, scratch_dir, serial);
try cwd.symLink(io, target, link, .{});
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);
}
};
// ponytail: Zig 0.17.1's new ELF linker (which `-fincremental` selects)
// writes DT_VERSYM without DT_VERDEF or DT_VERNEED when nothing is
// versioned, and glibc's dlopen then reads a null version table and
// faults. Such a library has nothing versioned, so a copy without
// DT_VERSYM, in an anonymous memory file, loads instead (the build's own
// file is never written: the watch would see the write). Never fires on a
// correct library; dead once Zig has commit 6d27011edb (0.18): delete it.
fn withoutVersym(fd: i32) !?i32 {
const elf = std.elf;
var ehdr: elf.Elf64.Ehdr = undefined;
try pread(fd, std.mem.asBytes(&ehdr), 0);
if (!std.mem.eql(u8, &ehdr.ident.magic, elf.MAGIC) or ehdr.ident.class != .@"64" or ehdr.phnum > 32) return null;
var phdrs: [32]elf.Elf64.Phdr = undefined;
try pread(fd, std.mem.sliceAsBytes(phdrs[0..ehdr.phnum]), ehdr.phoff);
for (phdrs[0..ehdr.phnum]) |ph| {
if (ph.type != .DYNAMIC) continue;
var dyn: [64]elf.Elf64_Dyn = undefined;
const len = @min(dyn.len, ph.filesz / @sizeOf(elf.Elf64_Dyn));
try pread(fd, std.mem.sliceAsBytes(dyn[0..len]), ph.offset);
var versym: ?usize = null;
var end: usize = 0;
while (end < len and dyn[end].d_tag != elf.DT_NULL) : (end += 1) switch (dyn[end].d_tag) {
elf.DT_VERSYM => versym = end,
elf.DT_VERDEF, elf.DT_VERNEED => return null,
else => {},
};
const at = versym orelse return null;
if (end == len) return null;
const copy: i32 = @intCast(@as(isize, @bitCast(linux.memfd_create("sketchbook", linux.MFD.CLOEXEC))));
if (copy < 0) return error.MemfdFailed;
errdefer _ = linux.close(copy);
var offset: i64 = 0;
while (true) {
const rc = linux.sendfile(copy, fd, &offset, 1 << 30);
if (linux.errno(rc) != .SUCCESS) return error.CopyFailed;
if (rc == 0) break;
}
std.mem.copyForwards(elf.Elf64_Dyn, dyn[at..end], dyn[at + 1 .. end + 1]);
const bytes = std.mem.sliceAsBytes(dyn[at..end]);
if (linux.pwrite(copy, bytes.ptr, bytes.len, @intCast(ph.offset + at * @sizeOf(elf.Elf64_Dyn))) != bytes.len) return error.WriteFailed;
return copy;
}
return null;
}
fn pread(fd: i32, buf: []u8, offset: u64) !void {
if (linux.pread(fd, buf.ptr, buf.len, @intCast(offset)) != buf.len) return error.ShortRead;
}
/// inotify on a library's directory, filtered to its file name. Builds
/// replace files by renaming (`MOVED_TO`) or rewrite them (`CLOSE_WRITE`).
pub const Watch = struct {
fd: i32,
name: []const u8,
pub fn init(path: []const u8) !Watch {
const fd: i32 = @intCast(@as(isize, @bitCast(linux.inotify_init1(linux.IN.NONBLOCK | linux.IN.CLOEXEC))));
if (fd < 0) return error.WatchFailed;
errdefer _ = linux.close(fd);
var buf: [std.fs.max_path_bytes]u8 = undefined;
const dir = std.fs.path.dirname(path) orelse ".";
const dir_z = try std.mem.printSentinel(&buf, "{s}", .{dir}, 0);
if (@as(isize, @bitCast(linux.inotify_add_watch(fd, dir_z, linux.IN.MOVED_TO | linux.IN.CLOSE_WRITE))) < 0) return error.WatchFailed;
return .{ .fd = fd, .name = std.fs.path.basename(path) };
}
pub fn deinit(self: Watch) void {
_ = linux.close(self.fd);
}
/// Drains pending events; true if any named the library.
pub fn changed(self: Watch) bool {
var found = false;
var buf: [4096]u8 align(@alignOf(linux.inotify_event)) = undefined;
while (true) {
const rc = linux.read(self.fd, &buf, buf.len);
const len: isize = @bitCast(rc);
if (len <= 0) return found;
var off: usize = 0;
while (off + @sizeOf(linux.inotify_event) <= @as(usize, @intCast(len))) {
const event: *const linux.inotify_event = @ptrCast(@alignCast(&buf[off]));
off += @sizeOf(linux.inotify_event) + event.len;
if (event.getName()) |name| {
if (std.mem.eql(u8, name, self.name)) found = true;
}
}
}
}
};
|