27 #if HAVE_STACK_OVERFLOW_RECOVERY && HAVE_SIGSEGV_RECOVERY
29 #if defined _WIN32 && !defined __CYGWIN__
32 # define sigemptyset(set)
33 # define sigprocmask(how,set,oldset)
42 # include <sys/types.h>
43 # include <sys/time.h>
44 # include <sys/resource.h>
51 volatile int pass = 0;
55 stackoverflow_handler_continuation (
void *arg1,
void *arg2,
void *arg3)
57 int arg = (int) (
long) arg1;
58 longjmp (mainloop, arg);
62 stackoverflow_handler (
int emergency, stackoverflow_context_t scp)
66 printf (
"Stack overflow %d caught.\n", pass);
69 printf (
"Segmentation violation misdetected as stack overflow.\n");
72 sigprocmask (SIG_SETMASK, &mainsigset, NULL);
74 (
void *) (
long) (emergency ? -1 : pass), NULL, NULL);
78 sigsegv_handler (
void *address,
int emergency)
87 printf (
"Stack overflow %d missed.\n", pass);
91 printf (
"Segmentation violation correctly detected.\n");
92 sigprocmask (SIG_SETMASK, &mainsigset, NULL);
94 (
void *) (
long) pass, NULL, NULL);
98 recurse_1 (
int n,
volatile int *p)
101 *recurse_1 (n + 1, p) += n;
106 recurse (
volatile int n)
108 return *recurse_1 (n, &n);
117 #if HAVE_SETRLIMIT && defined RLIMIT_STACK
122 rl.rlim_cur = rl.rlim_max = 0x100000;
123 setrlimit (RLIMIT_STACK, &rl);
127 prepare_alternate_stack ();
136 #if !HAVE_MMAP_ANON && !HAVE_MMAP_ANONYMOUS && HAVE_MMAP_DEVZERO
137 zero_fd = open (
"/dev/zero", O_RDONLY, 0644);
141 p = mmap_zeromap ((
void *) 0x12340000, 0x4000);
142 if (p == (
void *)(-1))
144 fprintf (stderr,
"mmap_zeromap failed.\n");
147 page = (
unsigned long) p;
150 if (mprotect ((
void *) page, 0x4000, PROT_READ) < 0)
152 fprintf (stderr,
"mprotect failed.\n");
161 sigemptyset (&emptyset);
162 sigprocmask (SIG_BLOCK, &emptyset, &mainsigset);
165 switch (setjmp (mainloop))
168 printf (
"emergency exit\n"); exit (1);
170 printf (
"Starting recursion pass %d.\n", pass + 1);
172 printf (
"no endless recursion?!\n"); exit (1);
174 *(
volatile int *) (page + 0x678) = 42;
177 *(
volatile int *) 0 = 42;
186 check_alternate_stack_no_overflow ();
188 printf (
"Test passed.\n");