1 /* Licensed under Apache License v2.0 - see LICENSE file for details */
14 static __thread struct altstack_state {
15 char ebuf[ALTSTACK_ERR_MAXLEN];
25 (state.elen += snprintf(state.ebuf + state.elen, \
26 sizeof(state.ebuf) - state.elen, \
27 "%s(altstack@%d) %s%s%s", \
28 state.elen ? "; " : "", __LINE__, (x), \
30 errno ? strerror(errno) : ""))
32 void altstack_perror(void)
34 fprintf(stderr, "%s\n", state.ebuf);
37 char *altstack_geterr(void)
42 static void segvjmp(int signum)
44 longjmp(state.jmp, 1);
48 rlim_t altstack_max(void) {
52 static ptrdiff_t rsp_save(unsigned i) {
54 asm volatile ("movq %%rsp, %0" : "=g" (state.rsp_save[i]));
55 return (char *) state.rsp_save[0] - (char *) state.rsp_save[i];
58 void altstack_rsp_save(void) {
62 ptrdiff_t altstack_used(void) {
66 int altstack(rlim_t max, void *(*fn)(void *), void *arg, void **out)
68 long pgsz = sysconf(_SC_PAGESIZE);
69 int ret = -1, undo = 0;
71 struct rlimit rl_save;
72 struct sigaction sa_save;
76 assert(max > 0 && fn);
77 #define ok(x, y) ({ long __r = (long) (x); if (__r == -1) { bang(#x); if (y) goto out; } __r; })
83 state.ebuf[state.elen = 0] = '\0';
86 // if the first page below the mapping is in use, we get max-pgsz usable bytes
87 // add pgsz to max to guarantee at least max usable bytes
90 ok(getrlimit(RLIMIT_STACK, &rl_save), 1);
91 ok(setrlimit(RLIMIT_STACK, &(struct rlimit) { state.max, rl_save.rlim_max }), 1);
94 ok(m = mmap(NULL, max, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_GROWSDOWN|MAP_NORESERVE, -1, 0), 1);
97 if (setjmp(state.jmp) == 0) {
98 unsigned char sigstk[SIGSTKSZ];
99 stack_t ss = { .ss_sp = sigstk, .ss_size = sizeof(sigstk) };
100 struct sigaction sa = { .sa_handler = segvjmp, .sa_flags = SA_NODEFER|SA_RESETHAND|SA_ONSTACK };
102 ok(sigaltstack(&ss, &ss_save), 1);
105 sigemptyset(&sa.sa_mask);
106 ok(sigaction(SIGSEGV, &sa, &sa_save), 1);
110 "mov %%rsp, %%r10\n\t"
114 : "=r" (state.rsp_save[0])
115 : "0" (m + max) : "r10", "memory");
116 state.out = state.fn(state.arg);
117 asm volatile ("pop %%rsp"
120 if (out) *out = state.out;
124 bang("SIGSEGV caught");
133 ok(sigaction(SIGSEGV, &sa_save, NULL), 0);
135 ok(sigaltstack(&ss_save, NULL), 0);
137 ok(munmap(m, max), 0);
139 ok(setrlimit(RLIMIT_STACK, &rl_save), 0);
144 return !ret && state.elen ? 1 : ret;