From 8b28a0a5101a62fd55c2d55433e0fa899d4623f9 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 10 Nov 2026 12:52:12 -0300 Subject: 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 --- src/agent/agent.zig | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'src/agent/agent.zig') diff --git a/src/agent/agent.zig b/src/agent/agent.zig index 2a32a05..0e23de9 100644 --- a/src/agent/agent.zig +++ b/src/agent/agent.zig @@ -45,6 +45,7 @@ pub fn init() void { if (state.load(.acquire) != 0 or state.cmpxchgStrong(0, 1, .acquire, .monotonic) != null) return; defer state.store(2, .release); const r = client.get() orelse return; + signal.keys.detect(); var buf: [1 << 16]u8 = undefined; const env = client.environ(&buf); cut = if (client.getenv(env, "ZTRACE_CUT")) |v| std.fmt.parseInt(u64, v, 0) catch 0 else 0; @@ -161,14 +162,17 @@ pub fn mark(label: []const u8, value: u64) void { /// Names `memory` (a `variable` event), records it, and keeps recording what /// changes in it: only the bytes that changed, before each event that ends a -/// moment. At most `max_region` bytes, which must stay mapped. Watched from -/// inside a hooked call, it is forgotten when that call returns. +/// moment. At most `max_region` bytes, which must stay mapped; a protection +/// key on it does not hide it. Watched from inside a hooked call, it is +/// forgotten when that call returns. pub fn watch(memory: []const u8, name: []const u8) void { init(); if (ring == null or tooLarge(memory, "watch")) return; const was = sud.selector; sud.selector = sud.allow; defer sud.selector = was; + const rights = signal.keys.open(); + defer if (rights) |r| signal.keys.set(r); const id = stringId(name); emit(.string, 0, .{ id, 0, 0, 0, 0, 0 }, text(name, max_payload)); emit(.variable, 0, .{ @intFromPtr(memory.ptr), memory.len, id, 0, 0, 0 }, ""); @@ -180,6 +184,8 @@ pub fn watch(memory: []const u8, name: []const u8) void { pub fn snapshot(memory: []const u8) void { init(); if (ring == null or tooLarge(memory, "snapshot")) return; + const rights = signal.keys.open(); + defer if (rights) |r| signal.keys.set(r); write(@intFromPtr(memory.ptr), memory); } -- cgit v1.3