aboutsummaryrefslogtreecommitdiff
path: root/lib/mlibc/tests/posix/sigaltstack.c
blob: bebcc30794172aadd1d965fa057c105647d9fa44 (plain)
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
#include <assert.h>
#include <setjmp.h>
#include <signal.h>
#include <stdlib.h>

static jmp_buf env;
static char *sigStack;

static void sig_handler(int sig, siginfo_t *info, void *ctx) {
	(void)sig;
	(void)info;
	(void)ctx;

	longjmp(env, 1);
}

int main() {
	if (setjmp(env)) {
		free(sigStack);
		return 0;
	}

	sigStack = malloc(SIGSTKSZ);

	stack_t ss;
	ss.ss_sp = sigStack;
	ss.ss_size = SIGSTKSZ;
	ss.ss_flags = 0;

	assert(!sigaltstack(&ss, NULL));

	struct sigaction sa;
	sigemptyset(&sa.sa_mask);
	sa.sa_flags = SA_SIGINFO | SA_ONSTACK;
	sa.sa_sigaction = sig_handler;

	assert(!sigaction(SIGSEGV, &sa, NULL));

	// This is used to trash the stack to ensure sigaltstack actually switched stacks.
#if defined(__x86_64__)
	asm volatile ("mov $0, %rsp\n"
			"\t" "push $0");
#elif defined(__i386__)
	asm volatile ("mov $0, %esp\n"
			"\t" "push $0");
#elif defined(__aarch64__)
	asm volatile ("mov sp, %0\n"
			"\t" "stp x0, x1, [sp, #-16]!" :: "r"((uint64_t)0));
#elif defined(__riscv) && __riscv_xlen == 64
	asm volatile ("li sp, 0\n"
			"\t" "sd zero, 0(sp)");
#else
#	error Unknown architecture
#endif
}