1 /* Test that the handler can be exited multiple times.
2 Copyright (C) 2002-2024 Free Software Foundation, Inc.
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/>. */
17 /* Written by Bruno Haible. */
27 #if HAVE_SIGSEGV_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 "mmap-anon-util.h"
37 # include <stdlib.h> /* for abort, exit */
41 # if SIGSEGV_FAULT_ADDRESS_ALIGNMENT > 1UL
43 # define SIGSEGV_FAULT_ADDRESS_ROUNDOFF_BITS (getpagesize () - 1)
45 # define SIGSEGV_FAULT_ADDRESS_ROUNDOFF_BITS 0
51 volatile int pass
= 0;
54 volatile int handler_called
= 0;
57 handler_continuation (void *arg1
, void *arg2
, void *arg3
)
59 longjmp (mainloop
, pass
);
63 handler (void *fault_address
, int serious
)
66 if (handler_called
> 10)
69 != (void *)((page
+ 0x678 + 8 * pass
) & ~SIGSEGV_FAULT_ADDRESS_ROUNDOFF_BITS
))
72 printf ("Fault %d caught.\n", pass
);
73 sigprocmask (SIG_SETMASK
, &mainsigset
, NULL
);
74 return sigsegv_leave_handler (handler_continuation
, NULL
, NULL
, NULL
);
80 *(volatile int *) (p
+ 0x678 + 8 * pass
) = 42;
91 # if !HAVE_MAP_ANONYMOUS
92 zero_fd
= open ("/dev/zero", O_RDONLY
, 0644);
95 # if defined __linux__ && defined __sparc__
96 /* On Linux 2.6.26/SPARC64, PROT_READ has the same effect as
97 PROT_READ | PROT_WRITE. */
98 prot_unwritable
= PROT_NONE
;
100 prot_unwritable
= PROT_READ
;
103 /* Setup some mmapped memory. */
104 p
= mmap_zeromap ((void *) 0x12340000, 0x4000);
105 if (p
== (void *)(-1))
107 fprintf (stderr
, "mmap_zeromap failed.\n");
110 page
= (uintptr_t) p
;
112 /* Make it read-only. */
113 if (mprotect ((void *) page
, 0x4000, prot_unwritable
) < 0)
115 fprintf (stderr
, "mprotect failed.\n");
119 /* Install the SIGSEGV handler. */
120 if (sigsegv_install_handler (&handler
) < 0)
123 /* Save the current signal mask. */
124 sigemptyset (&emptyset
);
125 sigprocmask (SIG_BLOCK
, &emptyset
, &mainsigset
);
127 /* Provoke two SIGSEGVs in a row. */
128 switch (setjmp (mainloop
))
131 printf ("Doing SIGSEGV pass %d.\n", pass
+ 1);
133 printf ("no SIGSEGV?!\n"); exit (1);
141 printf ("Test passed.\n");