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)
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
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. */
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]))
46 if (!((unsigned long)fault_address
>= area
47 && (unsigned long)fault_address
- area
< 0x4000))
49 if (mprotect ((void *) area
, 0x4000, PROT_READ_WRITE
) == 0)
55 handler (void *fault_address
, int serious
)
57 return sigsegv_dispatch (&dispatcher
, fault_address
);
74 #if !HAVE_MMAP_ANON && !HAVE_MMAP_ANONYMOUS && HAVE_MMAP_DEVZERO
75 zero_fd
= open ("/dev/zero", O_RDONLY
, 0644);
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");
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");
96 p
= mmap_zeromap ((void *) 0x0BEE0000, 0x4000);
97 if (p
== (void *)(-1))
99 fprintf (stderr
, "mmap_zeromap failed.\n");
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");
109 if (mprotect ((void *) area2
, 0x4000, PROT_READ_WRITE
) < 0
110 || mprotect ((void *) area2
, 0x4000, PROT_READ
) < 0)
112 fprintf (stderr
, "mprotect failed.\n");
116 p
= mmap_zeromap ((void *) 0x06990000, 0x4000);
117 if (p
== (void *)(-1))
119 fprintf (stderr
, "mmap_zeromap failed.\n");
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;
137 /* Check that the handler was called three times. */
140 if (!(logdata
[0] == area2
&& logdata
[1] == area3
&& logdata
[2] == area1
))
142 printf ("Test passed.\n");