diff options
Diffstat (limited to 'src/agent/hook.zig')
| -rw-r--r-- | src/agent/hook.zig | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/src/agent/hook.zig b/src/agent/hook.zig index fa44cc4..88e187c 100644 --- a/src/agent/hook.zig +++ b/src/agent/hook.zig @@ -61,15 +61,24 @@ pub fn add(addr: usize) void { const code: *[4]u8 = @ptrFromInt(addr); hooks[count] = .{ .addr = addr, .first = code.* }; count += 1; - code[0] = 0xcc; + poke(addr, 0xcc); +} + +/// One byte of code changed, whatever protection key the code is behind (a JIT's is). +fn poke(addr: usize, byte: u8) void { + const rights = signal.keys.open(); + @as(*u8, @ptrFromInt(addr)).* = byte; + if (rights) |was| signal.keys.set(was); } fn onTrap(_: i32, info: *const linux.siginfo_t, uc: *signal.Ucontext) callconv(.c) void { + // As the thread, not with a handler's defaults: its stack and what is watched are read from here. + signal.adopt(uc); const TRAP_TRACE = 2; if (info.code == TRAP_TRACE) { // 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; + for (hooks[0..count]) |h| poke(h.addr, 0xcc); uc.eflags &= ~@as(u64, signal.trap_flag); return; } @@ -99,7 +108,7 @@ fn onTrap(_: i32, info: *const linux.siginfo_t, uc: *signal.Ucontext) callconv(. uc.rip += 4; } else { // shortcut: while the byte is back another thread can enter unseen; a relocated copy of the instruction would close that. - @as(*u8, @ptrFromInt(hook.addr)).* = hook.first[0]; + poke(hook.addr, hook.first[0]); uc.eflags |= signal.trap_flag; } } |
