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)
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. */
25 #if HAVE_SIGSEGV_RECOVERY
27 #if defined _WIN32 && !defined __CYGWIN__
28 /* Windows doesn't have sigset_t. */
30 # define sigemptyset(set)
31 # define sigprocmask(how,set,oldset)
35 #include <stdlib.h> /* for abort, exit */
42 volatile int pass
= 0;
45 volatile int handler_called
= 0;
48 handler_continuation (void *arg1
, void *arg2
, void *arg3
)
50 longjmp (mainloop
, pass
);
54 handler (void *fault_address
, int serious
)
57 if (handler_called
> 10)
59 if (fault_address
!= (void *)(page
+ 0x678 + 8 * 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
);
68 crasher (unsigned long p
)
70 *(volatile int *) (p
+ 0x678 + 8 * pass
) = 42;
80 #if !HAVE_MMAP_ANON && !HAVE_MMAP_ANONYMOUS && HAVE_MMAP_DEVZERO
81 zero_fd
= open ("/dev/zero", O_RDONLY
, 0644);
84 /* Setup some mmaped memory. */
85 p
= mmap_zeromap ((void *) 0x12340000, 0x4000);
86 if (p
== (void *)(-1))
88 fprintf (stderr
, "mmap_zeromap failed.\n");
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");
100 /* Install the SIGSEGV handler. */
101 if (sigsegv_install_handler (&handler
) < 0)
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
))
112 printf ("Doing SIGSEGV pass %d.\n", pass
+ 1);
114 printf ("no SIGSEGV?!\n"); exit (1);
122 printf ("Test passed.\n");