summaryrefslogtreecommitdiff
path: root/src/agent/check.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-10 12:52:12 -0300
committerGabriel Schneider <[email protected]>2026-11-10 13:00:24 -0300
commit8b28a0a5101a62fd55c2d55433e0fa899d4623f9 (patch)
treede233408fc45f93e19bbcf637ed66242d569fc56 /src/agent/check.zig
parent36d7f642e71207d04f5da80530da552e06447337 (diff)
downloadcomputeranim-8b28a0a5101a62fd55c2d55433e0fa899d4623f9.tar.gz
computeranim-8b28a0a5101a62fd55c2d55433e0fa899d4623f9.zip
agent: its handlers act with the thread's protection keys and float modes, not a handler's
The kernel starts a signal handler with default PKRU (key 0 only) and default MXCSR and x87 control word, the thread's own saved in the frame. The SIGSYS handler makes the thread's syscalls and clones its threads from there, so under --syscalls: - a pkey_alloc's new rights were undone when the handler returned; - a thread was born with no right to keyed memory (V8's compiler threads fault reading the code space: d8 died of SEGV_PKUERR, and the 35 seconds were its core dump, not a spin); - a buffer in keyed memory was EFAULT to the kernel, and a fault to the agent reading it for the event; - a thread started without its maker's float modes (flush-to-zero). Both handlers now take the thread's PKRU and control words from the frame first (signal.adopt), and the SIGSYS one writes PKRU back to the frame after the syscall. Watched memory is read, and hooked code patched, with every key open, so a key the thread itself cannot use hides nothing. test-agent: agent_pkey (a keyed page read, given to syscalls, watched, read by a later thread; skips itself without keys), and agent_sys's thread checks its MXCSR. README: --alloc and programs with their own malloc; the core dump. Co-Authored-By: Claude Fable 5.2 <[email protected]>
Diffstat (limited to 'src/agent/check.zig')
-rw-r--r--src/agent/check.zig35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/agent/check.zig b/src/agent/check.zig
index 8657665..348f497 100644
--- a/src/agent/check.zig
+++ b/src/agent/check.zig
@@ -89,6 +89,8 @@ pub fn main(init: std.process.Init) !void {
try annotated(r);
} else if (std.mem.eql(u8, scenario, "syscalls")) {
try syscalls(r);
+ } else if (std.mem.eql(u8, scenario, "pkey")) {
+ try protected(r);
} else return error.NoSuchScenario;
}
@@ -207,6 +209,39 @@ fn annotated(r: Recording) !void {
try sorted(r, "numbers");
}
+/// agent_pkey: syscalls into memory behind a protection key did what they
+/// do natively, from the thread that made the key and from one made after;
+/// the page was seen while its thread could not read it.
+fn protected(r: Recording) !void {
+ const SYS = std.os.linux.SYS;
+ var writers: std.AutoArrayHashMapUnmanaged(u32, void) = .empty;
+ var reads: std.ArrayList(i64) = .empty;
+ var key: i64 = -1;
+ var writing = false;
+ for (r.items) |item| switch (item.ev.kind) {
+ .syscall_enter => writing = item.ev.aux == @intFromEnum(SYS.write) and std.mem.eql(u8, item.payload, "protected\n"),
+ .syscall_exit => if (item.ev.aux == @intFromEnum(SYS.pkey_alloc)) {
+ key = @bitCast(item.ev.a[0]);
+ } else if (item.ev.aux == @intFromEnum(SYS.read) and key > 0) {
+ try reads.append(r.arena, @bitCast(item.ev.a[0]));
+ } else if (writing) {
+ try expectEqual(@as(u64, 10), item.ev.a[0]);
+ try writers.put(r.arena, item.ev.tid, {});
+ writing = false;
+ },
+ else => {},
+ };
+ // No protection keys on this machine: nothing to hold the agent to.
+ if (key < 0) return;
+ try expectEqual(@as(usize, 2), writers.count());
+ try std.testing.expectEqualSlices(i64, &.{ 16, -@as(i64, @intFromEnum(std.os.linux.E.FAULT)) }, reads.items);
+ const page = try r.variable("page");
+ var text: [10]u8 = undefined;
+ try (try r.trace.moment(r.trace.len - 1)).read(page.ev.a[0], &text, null);
+ try expectEqualStrings("protected\n", &text);
+ try expect(r.count(.mark) == 1 and r.count(.call) == 1);
+}
+
/// agent_sys: what it wrote, from which thread, and every syscall entered
/// is left, innermost first.
fn syscalls(r: Recording) !void {