1 /* Test the stack overflow handler.
2 Copyright (C) 2002-2021 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible and Eric Blake. */
27 #if HAVE_STACK_OVERFLOW_RECOVERY
29 # if defined _WIN32 && !defined __CYGWIN__
30 /* Windows doesn't have sigset_t. */
32 # define sigemptyset(set)
33 # define sigprocmask(how,set,oldset)
36 # include <stddef.h> /* needed for NULL on SunOS4 */
37 # include <stdlib.h> /* for abort, exit */
41 # include <sys/types.h>
42 # include <sys/time.h>
43 # include <sys/resource.h>
45 # include "altstack-util.h"
50 volatile int pass = 0;
52 volatile char *stack_lower_bound;
53 volatile char *stack_upper_bound;
56 stackoverflow_handler_continuation (void *arg1, void *arg2, void *arg3)
58 int arg = (int) (long) arg1;
59 longjmp (mainloop, arg);
63 stackoverflow_handler (int emergency, stackoverflow_context_t scp)
66 volatile char *addr = &dummy;
67 if (!(addr >= stack_lower_bound && addr <= stack_upper_bound))
70 printf ("Stack overflow %d caught.\n", pass);
71 sigprocmask (SIG_SETMASK, &mainsigset, NULL);
72 sigsegv_leave_handler (stackoverflow_handler_continuation,
73 (void *) (long) (emergency ? -1 : pass), NULL, NULL);
77 recurse_1 (int n, volatile int *p)
80 *recurse_1 (n + 1, p) += n;
85 recurse (volatile int n)
87 return *recurse_1 (n, &n);
95 # if HAVE_SETRLIMIT && defined RLIMIT_STACK
96 /* Before starting the endless recursion, try to be friendly to the user's
97 machine. On some Linux 2.2.x systems, there is no stack limit for user
98 processes at all. We don't want to kill such systems. */
100 rl.rlim_cur = rl.rlim_max = 0x100000; /* 1 MB */
101 setrlimit (RLIMIT_STACK, &rl);
104 /* Prepare the storage for the alternate stack. */
105 prepare_alternate_stack ();
107 /* Install the stack overflow handler. */
108 if (stackoverflow_install_handler (&stackoverflow_handler,
109 mystack, MYSTACK_SIZE)
112 stack_lower_bound = mystack;
113 stack_upper_bound = mystack + MYSTACK_SIZE - 1;
115 /* Save the current signal mask. */
116 sigemptyset (&emptyset);
117 sigprocmask (SIG_BLOCK, &emptyset, &mainsigset);
119 /* Provoke two stack overflows in a row. */
120 switch (setjmp (mainloop))
123 printf ("emergency exit\n"); exit (1);
125 printf ("Starting recursion pass %d.\n", pass + 1);
127 printf ("no endless recursion?!\n"); exit (1);
134 /* Validate that the alternate stack did not overflow. */
135 check_alternate_stack_no_overflow ();
137 printf ("Test passed.\n");