From 2be93397533432020413d6e0bb5021e2f565ef9c Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 10 Nov 2026 11:50:30 -0300 Subject: agent: syscalls on a stack of the agent's, clones made inside the handler; what a recording may hold Syscall User Dispatch now runs its handler on an alternate stack a thread (a thread's own stack may not hold a signal frame; node makes such threads), and makes clone, clone3 and vfork itself: the child leaves through the gate with the target's registers instead of being stepped into with the trap flag. Paths and written buffers are cut at 256 bytes (--capture), a watch or snapshot above 1 MiB is refused, and ztrace run ends a recording that is full (--max-size) with the target killed and the file closed. Co-Authored-By: Claude Fable 5.2 --- src/agent/hook.zig | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) (limited to 'src/agent/hook.zig') diff --git a/src/agent/hook.zig b/src/agent/hook.zig index d11ddbd..57a374c 100644 --- a/src/agent/hook.zig +++ b/src/agent/hook.zig @@ -33,6 +33,11 @@ const Frame = struct { slot: usize, ret: usize, callee: usize }; threadlocal var frames: [1024]Frame = @splat(.{ .slot = 0, .ret = 0, .callee = 0 }); threadlocal var depth: u32 = 0; +/// SIGTRAP is the agent's. +pub fn active() bool { + return count != 0; +} + /// Hooked calls the calling thread has open. pub fn open() u32 { return depth; @@ -45,7 +50,7 @@ pub fn add(addr: usize) void { for (hooks[0..count]) |h| { if (h.addr == addr) return; } - listen(); + if (count == 0) signal.install(.TRAP, onTrap); // Left writable: a single step puts the first byte back for a moment. const page = addr & ~@as(usize, 4095); if (linux.errno(linux.mprotect(@ptrFromInt(page), 4096, .{ .READ = true, .WRITE = true, .EXEC = true })) != .SUCCESS) return; @@ -55,22 +60,13 @@ pub fn add(addr: usize) void { code[0] = 0xcc; } -var listening = false; - -/// SIGTRAP is the agent's from here on: breakpoints, and single steps (its own and sud.zig's). -pub fn listen() void { - if (!listening) signal.install(.TRAP, onTrap); - listening = true; -} - fn onTrap(_: i32, info: *const linux.siginfo_t, uc: *signal.Ucontext) callconv(.c) void { const TRAP_TRACE = 2; if (info.code == TRAP_TRACE) { - // A single step ended: over a hooked function's first instruction, - // or over a syscall the dispatcher let through. + // The step over a hooked function's first instruction ended. Whose, + // nobody wrote down: all the breakpoints go back in. for (hooks[0..count]) |h| @as(*u8, @ptrFromInt(h.addr)).* = 0xcc; uc.eflags &= ~@as(u64, signal.trap_flag); - sud.stepped(uc); return; } const hook = for (hooks[0..count]) |*h| { -- cgit v1.3