1 /* Test the stack overflow handler.
2 Copyright (C) 2002-2006, 2008, 2010 Bruno Haible <bruno@clisp.org>
3 Copyright (C) 2010 Eric Blake <eblake@redhat.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
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>
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
,
112 stack_lower_bound
= mystack
;
113 stack_upper_bound
= mystack
+ SIGSTKSZ
- 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");