1 /* Test that the handler can be exited multiple times.
2 Copyright (C) 2002-2021 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 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/>. */
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)
34 # include "mmap-anon-util.h"
35 # include <stdlib.h> /* for abort, exit */
39 # if SIGSEGV_FAULT_ADDRESS_ALIGNMENT > 1UL
41 # define SIGSEGV_FAULT_ADDRESS_ROUNDOFF_BITS (getpagesize () - 1)
43 # define SIGSEGV_FAULT_ADDRESS_ROUNDOFF_BITS 0
49 volatile int pass = 0;
52 volatile int handler_called = 0;
55 handler_continuation (void *arg1, void *arg2, void *arg3)
57 longjmp (mainloop, pass);
61 handler (void *fault_address, int serious)
64 if (handler_called > 10)
67 != (void *)((page + 0x678 + 8 * pass) & ~SIGSEGV_FAULT_ADDRESS_ROUNDOFF_BITS))
70 printf ("Fault %d caught.\n", pass);
71 sigprocmask (SIG_SETMASK, &mainsigset, NULL);
72 return sigsegv_leave_handler (handler_continuation, NULL, NULL, NULL);
78 *(volatile int *) (p + 0x678 + 8 * pass) = 42;
89 # if !HAVE_MAP_ANONYMOUS
90 zero_fd = open ("/dev/zero", O_RDONLY, 0644);
93 # if defined __linux__ && defined __sparc__
94 /* On Linux 2.6.26/SPARC64, PROT_READ has the same effect as
95 PROT_READ | PROT_WRITE. */
96 prot_unwritable = PROT_NONE;
98 prot_unwritable = PROT_READ;
101 /* Setup some mmaped memory. */
102 p = mmap_zeromap ((void *) 0x12340000, 0x4000);
103 if (p == (void *)(-1))
105 fprintf (stderr, "mmap_zeromap failed.\n");
108 page = (uintptr_t) p;
110 /* Make it read-only. */
111 if (mprotect ((void *) page, 0x4000, prot_unwritable) < 0)
113 fprintf (stderr, "mprotect failed.\n");
117 /* Install the SIGSEGV handler. */
118 if (sigsegv_install_handler (&handler) < 0)
121 /* Save the current signal mask. */
122 sigemptyset (&emptyset);
123 sigprocmask (SIG_BLOCK, &emptyset, &mainsigset);
125 /* Provoke two SIGSEGVs in a row. */
126 switch (setjmp (mainloop))
129 printf ("Doing SIGSEGV pass %d.\n", pass + 1);
131 printf ("no SIGSEGV?!\n"); exit (1);
139 printf ("Test passed.\n");