Avoid polluting cygwin namespace.
[libsigsegv/ericb.git] / tests / sigsegv2.c
blob775a56a60967c9b5ef1ed9ef55263b8763894fff
1 /* Test the dispatcher.
2 Copyright (C) 2002-2006, 2008, 2011 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 static sigsegv_dispatcher dispatcher;
32 static volatile unsigned int logcount = 0;
33 static volatile unsigned long logdata[10];
35 /* Note about SIGSEGV_FAULT_ADDRESS_ALIGNMENT: It does not matter whether
36 fault_address is rounded off here because all intervals that we pass to
37 sigsegv_register are page-aligned. */
39 static int
40 area_handler (void *fault_address, void *user_arg)
42 unsigned long area = *(unsigned long *)user_arg;
43 logdata[logcount++] = area;
44 if (logcount >= sizeof (logdata) / sizeof (logdata[0]))
45 abort ();
46 if (!((unsigned long)fault_address >= area
47 && (unsigned long)fault_address - area < 0x4000))
48 abort ();
49 if (mprotect ((void *) area, 0x4000, PROT_READ_WRITE) == 0)
50 return 1;
51 return 0;
54 static int
55 handler (void *fault_address, int serious)
57 return sigsegv_dispatch (&dispatcher, fault_address);
60 static void
61 barrier ()
65 int
66 main ()
68 void *p;
69 unsigned long area1;
70 unsigned long area2;
71 unsigned long area3;
73 /* Preparations. */
74 #if !HAVE_MMAP_ANON && !HAVE_MMAP_ANONYMOUS && HAVE_MMAP_DEVZERO
75 zero_fd = open ("/dev/zero", O_RDONLY, 0644);
76 #endif
77 sigsegv_init (&dispatcher);
78 sigsegv_install_handler (&handler);
80 /* Setup some mmaped memory. */
82 p = mmap_zeromap ((void *) 0x12340000, 0x4000);
83 if (p == (void *)(-1))
85 fprintf (stderr, "mmap_zeromap failed.\n");
86 exit (2);
88 area1 = (unsigned long) p;
89 sigsegv_register (&dispatcher, (void *) area1, 0x4000, &area_handler, &area1);
90 if (mprotect ((void *) area1, 0x4000, PROT_NONE) < 0)
92 fprintf (stderr, "mprotect failed.\n");
93 exit (2);
96 p = mmap_zeromap ((void *) 0x0BEE0000, 0x4000);
97 if (p == (void *)(-1))
99 fprintf (stderr, "mmap_zeromap failed.\n");
100 exit (2);
102 area2 = (unsigned long) p;
103 sigsegv_register (&dispatcher, (void *) area2, 0x4000, &area_handler, &area2);
104 if (mprotect ((void *) area2, 0x4000, PROT_READ) < 0)
106 fprintf (stderr, "mprotect failed.\n");
107 exit (2);
109 if (mprotect ((void *) area2, 0x4000, PROT_READ_WRITE) < 0
110 || mprotect ((void *) area2, 0x4000, PROT_READ) < 0)
112 fprintf (stderr, "mprotect failed.\n");
113 exit (2);
116 p = mmap_zeromap ((void *) 0x06990000, 0x4000);
117 if (p == (void *)(-1))
119 fprintf (stderr, "mmap_zeromap failed.\n");
120 exit (2);
122 area3 = (unsigned long) p;
123 sigsegv_register (&dispatcher, (void *) area3, 0x4000, &area_handler, &area3);
124 mprotect ((void *) area3, 0x4000, PROT_READ);
126 /* This access should call the handler. */
127 ((volatile int *)area2)[230] = 22;
128 /* This access should call the handler. */
129 ((volatile int *)area3)[412] = 33;
130 /* This access should not give a signal. */
131 ((volatile int *)area2)[135] = 22;
132 /* This access should call the handler. */
133 ((volatile int *)area1)[612] = 11;
135 barrier();
137 /* Check that the handler was called three times. */
138 if (logcount != 3)
139 exit (1);
140 if (!(logdata[0] == area2 && logdata[1] == area3 && logdata[2] == area1))
141 exit (1);
142 printf ("Test passed.\n");
143 return 0;
146 #else
149 main ()
151 return 77;
154 #endif