Bump version number.
[libsigsegv/ericb.git] / tests / sigsegv1.c
blobce00642809af57c63ec8c235e5925f257d6ca79c
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)
7 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, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 #ifndef _MSC_VER
19 # include <config.h>
20 #endif
22 #include "sigsegv.h"
23 #include <stdio.h>
25 #if HAVE_SIGSEGV_RECOVERY
27 #include "mmaputil.h"
28 #include <stdlib.h>
30 unsigned long page;
32 volatile int handler_called = 0;
34 int
35 handler (void *fault_address, int serious)
37 handler_called++;
38 if (handler_called > 10)
39 abort ();
40 if (fault_address != (void *)(page + 0x678))
41 abort ();
42 if (mprotect ((void *) page, 0x4000, PROT_READ_WRITE) == 0)
43 return 1;
44 return 0;
47 void
48 crasher (unsigned long p)
50 *(volatile int *) (p + 0x678) = 42;
53 int
54 main ()
56 void *p;
58 /* Preparations. */
59 #if !HAVE_MMAP_ANON && !HAVE_MMAP_ANONYMOUS && HAVE_MMAP_DEVZERO
60 zero_fd = open ("/dev/zero", O_RDONLY, 0644);
61 #endif
63 /* Setup some mmaped memory. */
64 p = mmap_zeromap ((void *) 0x12340000, 0x4000);
65 if (p == (void *)(-1))
67 fprintf (stderr, "mmap_zeromap failed.\n");
68 exit (2);
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");
76 exit (2);
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");
84 exit (2);
87 /* Install the SIGSEGV handler. */
88 sigsegv_install_handler (&handler);
90 /* The first write access should invoke the handler and then complete. */
91 crasher (page);
92 /* The second write access should not invoke the handler. */
93 crasher (page);
95 /* Check that the handler was called only once. */
96 if (handler_called != 1)
97 exit (1);
98 /* Test passed! */
99 printf ("Test passed.\n");
100 return 0;
103 #else
106 main ()
108 return 77;
111 #endif