1 /* Test that the handler is called, with the right fault address.
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
32 volatile int handler_called
= 0;
35 handler (void *fault_address
, int serious
)
38 if (handler_called
> 10)
40 if (fault_address
!= (void *)(page
+ 0x678))
42 if (mprotect ((void *) page
, 0x4000, PROT_READ_WRITE
) == 0)
48 crasher (unsigned long p
)
50 *(volatile int *) (p
+ 0x678) = 42;
59 #if !HAVE_MMAP_ANON && !HAVE_MMAP_ANONYMOUS && HAVE_MMAP_DEVZERO
60 zero_fd
= open ("/dev/zero", O_RDONLY
, 0644);
63 /* Setup some mmaped memory. */
64 p
= mmap_zeromap ((void *) 0x12340000, 0x4000);
65 if (p
== (void *)(-1))
67 fprintf (stderr
, "mmap_zeromap failed.\n");
70 page
= (unsigned long) p
;
72 /* Make it read-only. */
73 if (mprotect ((void *) page
, 0x4000, PROT_READ
) < 0)
75 fprintf (stderr
, "mprotect failed.\n");
78 /* Test whether it's possible to make it read-write after it was read-only.
79 This is not possible on Cygwin. */
80 if (mprotect ((void *) page
, 0x4000, PROT_READ_WRITE
) < 0
81 || mprotect ((void *) page
, 0x4000, PROT_READ
) < 0)
83 fprintf (stderr
, "mprotect failed.\n");
87 /* Install the SIGSEGV handler. */
88 sigsegv_install_handler (&handler
);
90 /* The first write access should invoke the handler and then complete. */
92 /* The second write access should not invoke the handler. */
95 /* Check that the handler was called only once. */
96 if (handler_called
!= 1)
99 printf ("Test passed.\n");