summaryrefslogtreecommitdiff
path: root/src/agent/signal.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/agent/signal.zig')
-rw-r--r--src/agent/signal.zig56
1 files changed, 52 insertions, 4 deletions
diff --git a/src/agent/signal.zig b/src/agent/signal.zig
index 99aead0..66baf2d 100644
--- a/src/agent/signal.zig
+++ b/src/agent/signal.zig
@@ -65,8 +65,8 @@ pub fn bit(sig: linux.SIG) u64 {
comptime {
asm (
\\ .pushsection .text.ztrace_gate, "ax", @progbits
- \\ .globl ztrace_gate_sigreturn, ztrace_gate_restorer, ztrace_gate_syscall, ztrace_gate_end
- \\ .hidden ztrace_gate_sigreturn, ztrace_gate_restorer, ztrace_gate_syscall, ztrace_gate_end
+ \\ .globl ztrace_gate_sigreturn, ztrace_gate_restorer, ztrace_gate_syscall, ztrace_gate_clone, ztrace_gate_end
+ \\ .hidden ztrace_gate_sigreturn, ztrace_gate_restorer, ztrace_gate_syscall, ztrace_gate_clone, ztrace_gate_end
\\ ztrace_gate_sigreturn:
\\ movq %rdi, %rsp
\\ ztrace_gate_restorer:
@@ -82,6 +82,46 @@ comptime {
\\ movq 8(%rsp), %r9
\\ syscall
\\ retq
+ \\ ztrace_gate_clone:
+ \\ pushq %rbx
+ \\ pushq %r12
+ \\ movq 32(%rsp), %rbx
+ \\ movq 40(%rsp), %r12
+ \\ movq %rdi, %rax
+ \\ movq %rsi, %rdi
+ \\ movq %rdx, %rsi
+ \\ movq %rcx, %rdx
+ \\ movq %r8, %r10
+ \\ movq %r9, %r8
+ \\ movq 24(%rsp), %r9
+ \\ syscall
+ \\ testq %rax, %rax
+ \\ jz 1f
+ \\ popq %r12
+ \\ popq %rbx
+ \\ retq
+ \\ 1: movq %rsp, %rdx
+ \\ andq $-16, %rsp
+ \\ subq $112, %rsp
+ \\ movq %rbx, %rdi
+ \\ movq %rsp, %rsi
+ \\ callq *%r12
+ \\ popq %r8
+ \\ popq %r9
+ \\ popq %r10
+ \\ popq %r12
+ \\ popq %r13
+ \\ popq %r14
+ \\ popq %r15
+ \\ popq %rdi
+ \\ popq %rsi
+ \\ popq %rbp
+ \\ popq %rbx
+ \\ popq %rdx
+ \\ popq %rcx
+ \\ popq %rsp
+ \\ xorl %eax, %eax
+ \\ jmpq *%rcx
\\ ztrace_gate_end:
\\ .popsection
);
@@ -93,6 +133,14 @@ pub extern fn ztrace_gate_sigreturn(uc: u64) callconv(.c) noreturn;
extern fn ztrace_gate_restorer() callconv(.c) void;
/// Any syscall, by number.
pub extern fn ztrace_gate_syscall(number: u64, a0: u64, a1: u64, a2: u64, a3: u64, a4: u64, a5: u64) callconv(.c) u64;
+/// A clone-family syscall whose child cannot come back the way its parent
+/// does (another stack, or its parent's own). The parent returns like
+/// `ztrace_gate_syscall`. The child calls `start(context, regs, sp)` on the
+/// stack it was given, then continues with the fourteen registers `start`
+/// wrote (r8 r9 r10 r12 r13 r14 r15 rdi rsi rbp rbx rdx, then the
+/// instruction pointer and the stack pointer) and 0 in rax: where the
+/// target's own clone would have returned.
+pub extern fn ztrace_gate_clone(number: u64, a0: u64, a1: u64, a2: u64, a3: u64, a4: u64, a5: u64, context: *anyopaque, start: *const anyopaque) callconv(.c) u64;
extern const ztrace_gate_end: u8;
/// Where the gate is: start and length.
@@ -104,11 +152,11 @@ pub const Handler = *const fn (i32, *const linux.siginfo_t, *Ucontext) callconv(
/// Null puts the default action back. The handler runs with no signal
/// blocked, its own included: inside it the thread is still the target, with
-/// the target's mask.
+/// the target's mask. On the alternate stack, where the thread has one.
pub fn install(sig: linux.SIG, handler: ?Handler) void {
const act: linux.k_sigaction = .{
.handler = @ptrCast(handler),
- .flags = linux.SA.SIGINFO | linux.SA.RESTORER | linux.SA.NODEFER,
+ .flags = linux.SA.SIGINFO | linux.SA.RESTORER | linux.SA.NODEFER | linux.SA.ONSTACK,
.restorer = @ptrCast(&ztrace_gate_restorer),
.mask = linux.sigemptyset(),
};