1 /* Test that the handler is called, with the right fault address.
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 # include "mmap-anon-util.h"
32 # if SIGSEGV_FAULT_ADDRESS_ALIGNMENT > 1UL
34 # define SIGSEGV_FAULT_ADDRESS_ROUNDOFF_BITS (getpagesize () - 1)
36 # define SIGSEGV_FAULT_ADDRESS_ROUNDOFF_BITS 0
41 volatile int handler_called
= 0;
44 handler (void *fault_address
, int serious
)
47 if (handler_called
> 10)
50 != (void *)((page
+ 0x678) & ~SIGSEGV_FAULT_ADDRESS_ROUNDOFF_BITS
))
52 if (mprotect ((void *) page
, 0x4000, PROT_READ_WRITE
) == 0)
60 *(volatile int *) (p
+ 0x678) = 42;
70 # if !HAVE_MAP_ANONYMOUS
71 zero_fd
= open ("/dev/zero", O_RDONLY
, 0644);
74 # if defined __linux__ && defined __sparc__
75 /* On Linux 2.6.26/SPARC64, PROT_READ has the same effect as
76 PROT_READ | PROT_WRITE. */
77 prot_unwritable
= PROT_NONE
;
79 prot_unwritable
= PROT_READ
;
82 /* Setup some mmapped memory. */
83 p
= mmap_zeromap ((void *) 0x12340000, 0x4000);
84 if (p
== (void *)(-1))
86 fprintf (stderr
, "mmap_zeromap failed.\n");
91 /* Make it read-only. */
92 if (mprotect ((void *) page
, 0x4000, prot_unwritable
) < 0)
94 fprintf (stderr
, "mprotect failed.\n");
97 /* Test whether it's possible to make it read-write after it was read-only.
98 This is not possible on Cygwin. */
99 if (mprotect ((void *) page
, 0x4000, PROT_READ_WRITE
) < 0
100 || mprotect ((void *) page
, 0x4000, prot_unwritable
) < 0)
102 fprintf (stderr
, "mprotect failed.\n");
106 /* Install the SIGSEGV handler. */
107 sigsegv_install_handler (&handler
);
109 /* The first write access should invoke the handler and then complete. */
111 /* The second write access should not invoke the handler. */
114 /* Check that the handler was called only once. */
115 if (handler_called
!= 1)
118 printf ("Test passed.\n");