1 /* Test that the handler is called, with the right fault address.
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 # include "mmap-anon-util.h"
30 # if SIGSEGV_FAULT_ADDRESS_ALIGNMENT > 1UL
32 # define SIGSEGV_FAULT_ADDRESS_ROUNDOFF_BITS (getpagesize () - 1)
34 # define SIGSEGV_FAULT_ADDRESS_ROUNDOFF_BITS 0
39 volatile int handler_called = 0;
42 handler (void *fault_address, int serious)
45 if (handler_called > 10)
48 != (void *)((page + 0x678) & ~SIGSEGV_FAULT_ADDRESS_ROUNDOFF_BITS))
50 if (mprotect ((void *) page, 0x4000, PROT_READ_WRITE) == 0)
58 *(volatile int *) (p + 0x678) = 42;
68 # if !HAVE_MAP_ANONYMOUS
69 zero_fd = open ("/dev/zero", O_RDONLY, 0644);
72 # if defined __linux__ && defined __sparc__
73 /* On Linux 2.6.26/SPARC64, PROT_READ has the same effect as
74 PROT_READ | PROT_WRITE. */
75 prot_unwritable = PROT_NONE;
77 prot_unwritable = PROT_READ;
80 /* Setup some mmaped memory. */
81 p = mmap_zeromap ((void *) 0x12340000, 0x4000);
82 if (p == (void *)(-1))
84 fprintf (stderr, "mmap_zeromap failed.\n");
89 /* Make it read-only. */
90 if (mprotect ((void *) page, 0x4000, prot_unwritable) < 0)
92 fprintf (stderr, "mprotect failed.\n");
95 /* Test whether it's possible to make it read-write after it was read-only.
96 This is not possible on Cygwin. */
97 if (mprotect ((void *) page, 0x4000, PROT_READ_WRITE) < 0
98 || mprotect ((void *) page, 0x4000, prot_unwritable) < 0)
100 fprintf (stderr, "mprotect failed.\n");
104 /* Install the SIGSEGV handler. */
105 sigsegv_install_handler (&handler);
107 /* The first write access should invoke the handler and then complete. */
109 /* The second write access should not invoke the handler. */
112 /* Check that the handler was called only once. */
113 if (handler_called != 1)
116 printf ("Test passed.\n");