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
|
//! Function hooks: `call` and `ret` events for chosen functions, with the
//! registers at entry (the first six integer arguments among them) and the
//! return value.
//!
//! Entry is a breakpoint: the function's first byte becomes int3, and the
//! SIGTRAP handler runs here, in the target, with every register in hand. It
//! needs no instruction decoding, so it works on any function, whatever its
//! prologue. The handler then lets the replaced instruction run: emulated
//! when it is `push %rbp` or `endbr64` (how compiled functions start), else
//! the byte is put back for a single step. Return needs no trap: the entry
//! handler swaps the return address on the stack for `returned`, which emits
//! the event and jumps to the real one.
//!
//! shortcut: SIGTRAP is forced; a target that calls a hooked function with
//! SIGTRAP blocked is killed by the kernel. Under --syscalls the agent keeps
//! the target's masks clear of it; without, nothing does.
//!
//! shortcut: a signal per call, about a microsecond. For hooks on hot
//! functions, copy the prologue to a trampoline and patch a jump in (this
//! needs an x86-64 instruction-length decoder, to move whole instructions and
//! fix their rip-relative operands).
const std = @import("std");
const linux = std.os.linux;
const agent = @import("agent.zig");
const signal = @import("signal.zig");
const sud = @import("sud.zig");
const watch = @import("watch.zig");
const Hook = struct { addr: usize, first: [4]u8 };
var hooks: [64]Hook = undefined;
var count: usize = 0;
/// A hooked call that has not returned: where its return address lives, and
/// the real one.
const Frame = struct { slot: usize, ret: usize, callee: usize };
// shortcut: calls nested deeper than this are not reported; grow the stack on demand if a target recurses further.
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;
}
/// `addr` must be a function's first instruction, and nothing may jump back
/// to it from inside the function.
pub fn add(addr: usize) void {
if (count == hooks.len) return;
for (hooks[0..count]) |h| {
if (h.addr == addr) return;
}
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;
const code: *[4]u8 = @ptrFromInt(addr);
hooks[count] = .{ .addr = addr, .first = code.* };
count += 1;
code[0] = 0xcc;
}
fn onTrap(_: i32, info: *const linux.siginfo_t, uc: *signal.Ucontext) callconv(.c) void {
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;
uc.eflags &= ~@as(u64, signal.trap_flag);
return;
}
const hook = for (hooks[0..count]) |*h| {
if (h.addr == uc.rip - 1) break h;
} else {
// A breakpoint of the target's own: again, without us.
signal.install(.TRAP, null);
uc.rip -= 1;
return;
};
uc.rip = hook.addr;
if (depth < frames.len) {
const slot: *usize = @ptrFromInt(uc.rsp);
agent.emit(.call, 0, .{ hook.addr, slot.*, uc.rsp, 0, 0, 0 }, std.mem.asBytes(&uc.regs()));
frames[depth] = .{ .slot = uc.rsp, .ret = slot.*, .callee = hook.addr };
depth += 1;
slot.* = @intFromPtr(&returned);
}
if (hook.first[0] == 0x55) {
// push %rbp
uc.rsp -= 8;
@as(*u64, @ptrFromInt(uc.rsp)).* = uc.rbp;
uc.rip += 1;
} else if (std.mem.eql(u8, &hook.first, &.{ 0xf3, 0x0f, 0x1e, 0xfa })) {
// endbr64: nothing to do while indirect-branch tracking is off, as it is in user space.
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];
uc.eflags |= signal.trap_flag;
}
}
/// Where a hooked function returns to, in place of its caller. rax and rdx
/// and xmm0 and xmm1 hold what it returns: kept. (A 256-bit return value
/// would lose its upper half to the code called here.)
fn returned() callconv(.naked) void {
asm volatile (
\\ pushq %%rax
\\ pushq %%rax
\\ pushq %%rdx
\\ subq $40, %%rsp
\\ movdqu %%xmm0, (%%rsp)
\\ movdqu %%xmm1, 16(%%rsp)
\\ movq %%rax, %%rdi
\\ leaq 64(%%rsp), %%rsi
\\ callq %[onReturn:P]
\\ movq %%rax, 56(%%rsp)
\\ movdqu (%%rsp), %%xmm0
\\ movdqu 16(%%rsp), %%xmm1
\\ addq $40, %%rsp
\\ popq %%rdx
\\ popq %%rax
\\ retq
:
: [onReturn] "X" (&onReturn),
);
}
/// `sp` is the stack pointer the function returned with. Returns where it
/// meant to return to.
fn onReturn(value: u64, sp: usize) callconv(.c) usize {
const was = sud.selector;
sud.selector = sud.allow;
defer sud.selector = was;
while (true) {
depth -= 1;
const frame = frames[depth];
// Frames below `sp` were left without returning (longjmp): they end here too.
const ours = frame.slot + 8 == sp or depth == 0;
agent.emit(.ret, 0, .{ frame.callee, if (ours) value else 0, 0, 0, 0, 0 }, "");
if (ours) {
watch.drop(agent.threadId(), depth);
return frame.ret;
}
}
}
|