Bump version number.
[libsigsegv/ericb.git] / tests / sigsegv3.c
blob1ea0ec405abe9358f62f1bae1c856d13d5130307
1 /* Test that the handler can be exited multiple times.
2 Copyright (C) 2002-2006, 2008 Bruno Haible <bruno@clisp.org>
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, or (at your option)
7 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, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 #ifndef _MSC_VER
19 # include <config.h>
20 #endif
22 #include "sigsegv.h"
23 #include <stdio.h>
25 #if HAVE_SIGSEGV_RECOVERY
27 #if defined _WIN32 && !defined __CYGWIN__
28 /* Windows doesn't have sigset_t. */
29 typedef int sigset_t;
30 # define sigemptyset(set)
31 # define sigprocmask(how,set,oldset)
32 #endif
34 #include "mmaputil.h"
35 #include <stdlib.h> /* for abort, exit */
36 #include <signal.h>
37 #include <setjmp.h>
39 jmp_buf mainloop;
40 sigset_t mainsigset;
42 volatile int pass = 0;
43 unsigned long page;
45 volatile int handler_called = 0;
47 static void
48 handler_continuation (void *arg1, void *arg2, void *arg3)
50 longjmp (mainloop, pass);
53 int
54 handler (void *fault_address, int serious)
56 handler_called++;
57 if (handler_called > 10)
58 abort ();
59 if (fault_address != (void *)(page + 0x678 + 8 * pass))
60 abort ();
61 pass++;
62 printf ("Stack overflow %d caught.\n", pass);
63 sigprocmask (SIG_SETMASK, &mainsigset, NULL);
64 return sigsegv_leave_handler (handler_continuation, NULL, NULL, NULL);
67 void
68 crasher (unsigned long p)
70 *(volatile int *) (p + 0x678 + 8 * pass) = 42;
73 int
74 main ()
76 sigset_t emptyset;
77 void *p;
79 /* Preparations. */
80 #if !HAVE_MMAP_ANON && !HAVE_MMAP_ANONYMOUS && HAVE_MMAP_DEVZERO
81 zero_fd = open ("/dev/zero", O_RDONLY, 0644);
82 #endif
84 /* Setup some mmaped memory. */
85 p = mmap_zeromap ((void *) 0x12340000, 0x4000);
86 if (p == (void *)(-1))
88 fprintf (stderr, "mmap_zeromap failed.\n");
89 exit (2);
91 page = (unsigned long) p;
93 /* Make it read-only. */
94 if (mprotect ((void *) page, 0x4000, PROT_READ) < 0)
96 fprintf (stderr, "mprotect failed.\n");
97 exit (2);
100 /* Install the SIGSEGV handler. */
101 if (sigsegv_install_handler (&handler) < 0)
102 exit (2);
104 /* Save the current signal mask. */
105 sigemptyset (&emptyset);
106 sigprocmask (SIG_BLOCK, &emptyset, &mainsigset);
108 /* Provoke two SIGSEGVs in a row. */
109 switch (setjmp (mainloop))
111 case 0: case 1:
112 printf ("Doing SIGSEGV pass %d.\n", pass + 1);
113 crasher (page);
114 printf ("no SIGSEGV?!\n"); exit (1);
115 case 2:
116 break;
117 default:
118 abort ();
121 /* Test passed! */
122 printf ("Test passed.\n");
123 return 0;
126 #else
129 main ()
131 return 77;
134 #endif