Avoid polluting cygwin namespace.
[libsigsegv/ericb.git] / tests / stackoverflow1.c
blobb5c80f22fccb6536e300ed171b58e264f8397bb8
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)
8 any later version.
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. */
19 #ifndef _MSC_VER
20 # include <config.h>
21 #endif
23 #include "sigsegv.h"
24 #include <stdio.h>
25 #include <limits.h>
27 #if HAVE_STACK_OVERFLOW_RECOVERY
29 #if defined _WIN32 && !defined __CYGWIN__
30 /* Windows doesn't have sigset_t. */
31 typedef int sigset_t;
32 # define sigemptyset(set)
33 # define sigprocmask(how,set,oldset)
34 #endif
36 #include <stddef.h> /* needed for NULL on SunOS4 */
37 #include <stdlib.h> /* for abort, exit */
38 #include <signal.h>
39 #include <setjmp.h>
40 #if HAVE_SETRLIMIT
41 # include <sys/types.h>
42 # include <sys/time.h>
43 # include <sys/resource.h>
44 #endif
45 #include "altstack.h"
47 jmp_buf mainloop;
48 sigset_t mainsigset;
50 volatile int pass = 0;
52 volatile char *stack_lower_bound;
53 volatile char *stack_upper_bound;
55 static void
56 stackoverflow_handler_continuation (void *arg1, void *arg2, void *arg3)
58 int arg = (int) (long) arg1;
59 longjmp (mainloop, arg);
62 void
63 stackoverflow_handler (int emergency, stackoverflow_context_t scp)
65 char dummy;
66 volatile char *addr = &dummy;
67 if (!(addr >= stack_lower_bound && addr <= stack_upper_bound))
68 abort ();
69 pass++;
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);
76 volatile int *
77 recurse_1 (int n, volatile int *p)
79 if (n < INT_MAX)
80 *recurse_1 (n + 1, p) += n;
81 return p;
84 int
85 recurse (volatile int n)
87 return *recurse_1 (n, &n);
90 int
91 main ()
93 sigset_t emptyset;
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. */
99 struct rlimit rl;
100 rl.rlim_cur = rl.rlim_max = 0x100000; /* 1 MB */
101 setrlimit (RLIMIT_STACK, &rl);
102 #endif
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, SIGSTKSZ)
110 < 0)
111 exit (2);
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))
122 case -1:
123 printf ("emergency exit\n"); exit (1);
124 case 0: case 1:
125 printf ("Starting recursion pass %d.\n", pass + 1);
126 recurse (0);
127 printf ("no endless recursion?!\n"); exit (1);
128 case 2:
129 break;
130 default:
131 abort ();
134 /* Validate that the alternate stack did not overflow. */
135 check_alternate_stack_no_overflow ();
137 printf ("Test passed.\n");
138 exit (0);
141 #else
144 main ()
146 return 77;
149 #endif