]> git.saurik.com Git - apple/xnu.git/blame - tests/signal_stack.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / tests / signal_stack.c
CommitLineData
f427ee49
A
1#include <signal.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <unistd.h>
5#include <errno.h>
6
7#include <darwintest.h>
8
9static uint64_t stack_base, stack_end;
10
11static void
12signal_handler(int __unused signum, struct __siginfo * __unused info, void * __unused uap)
13{
14 T_LOG("In signal handler\n");
15 uint64_t signal_stack = (uint64_t)__builtin_frame_address(0);
16 T_ASSERT_LE(stack_base, signal_stack, NULL);
17 T_ASSERT_LE(signal_stack, stack_end, NULL);
18 T_END;
19}
20
21T_DECL(signalstack, "Check that the signal stack is set up correctly", T_META_ASROOT(YES))
22{
23 void* stack_allocation = malloc(SIGSTKSZ);
24
25 stack_base = (uint64_t)stack_allocation;
26 stack_end = stack_base + SIGSTKSZ;
27
28 T_LOG("stack base = 0x%llx\n", stack_base);
29 T_LOG("stack end = 0x%llx\n", stack_end);
30
31 stack_t alt_stack;
32 alt_stack.ss_sp = stack_allocation;
33 alt_stack.ss_size = SIGSTKSZ;
34 alt_stack.ss_flags = 0;
35
36 if (sigaltstack(&alt_stack, NULL) < 0) {
37 T_FAIL("error: sigaltstack failed\n");
38 }
39
40 sigset_t signal_mask;
41 sigemptyset(&signal_mask);
42
43 struct sigaction sig_action;
44 sig_action.sa_sigaction = signal_handler;
45 sig_action.sa_mask = signal_mask;
46 sig_action.sa_flags = SA_ONSTACK;
47
48 if (sigaction(SIGUSR1, &sig_action, NULL) != 0) {
49 T_FAIL("error: sigaction failed\n");
50 }
51
52 T_LOG("Sending a SIGUSR1\n");
53 kill(getpid(), SIGUSR1);
54
55 return;
56}