1 // commit: 6871fd773dcedbf056317d5d5e87b4859e97c4a4 2011-03-10
2 // commit: 9505bfbc40fec217820abad7142663eda60cd6be 2014-03-18
3 // catching stackoverflow SIGSEGV using sigaltstack
4 // mips stack_t is inconsistent with other archs
5 #define _XOPEN_SOURCE 700
13 #define T(f) ((f)==0 || (t_error(#f " failed: %s\n", strerror(errno)),0))
15 static char stack[SIGSTKSZ];
16 static sigjmp_buf sbuf;
17 static volatile sig_atomic_t state;
19 static void handler(int sig)
25 t_error("handler is not invoked in fillstack (after sigsetjmp returned 0), "
26 "state is %d, want 1\n", state);
30 if (i < (uintptr_t)stack || i >= (uintptr_t)stack+SIGSTKSZ)
31 t_error("signal handler was not invoked on the altstack\n");
33 T(sigaltstack(0, &ss));
34 if (ss.ss_flags != SS_ONSTACK)
35 t_error("ss_flags is not SS_ONSTACK in the signal handler\n");
40 static unsigned fillstack(unsigned x)
44 t_error("wrap around\n");
47 return fillstack(fillstack(x)) + 1;
56 ss.ss_size = sizeof stack;
58 sa.sa_handler = handler;
59 sa.sa_flags = SA_ONSTACK;
61 T(sigaltstack(&ss, 0));
62 T(sigfillset(&sa.sa_mask));
63 T(sigaction(SIGSEGV, &sa, 0));
64 if (sigsetjmp(sbuf,0) == 0) {
67 t_error("infinite recursion finished\n");
70 t_error("sigsetjmp returned non-zero before running handler, "
71 "state is %d, want 2\n", state);
74 ss.ss_size = MINSIGSTKSZ-1;
75 if (sigaltstack(&ss, 0) != -1 || errno != ENOMEM)
76 t_error("sigaltstack with stack size < MINSIGSTKSZ should have failed with ENOMEM, "
77 "got %s\n", strerror(errno));
80 ss.ss_size = MINSIGSTKSZ;
81 if (sigaltstack(&ss, 0) != -1 || errno != EINVAL)
82 t_error("sigaltstack with bad ss_flags should have failed with EINVAL, "
83 "got %s\n", strerror(errno));