usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-sigsegv-catch-segv1.c
blob8c04ef0888adf456cf657b1c3b3b81ba04608ab1
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. */
19 #include <config.h>
21 /* Specification. */
22 #include "sigsegv.h"
24 #include <stdint.h>
25 #include <stdio.h>
27 #if HAVE_SIGSEGV_RECOVERY
29 # include "mmap-anon-util.h"
30 # include <stdlib.h>
32 # if SIGSEGV_FAULT_ADDRESS_ALIGNMENT > 1UL
33 # include <unistd.h>
34 # define SIGSEGV_FAULT_ADDRESS_ROUNDOFF_BITS (getpagesize () - 1)
35 # else
36 # define SIGSEGV_FAULT_ADDRESS_ROUNDOFF_BITS 0
37 # endif
39 uintptr_t page;
41 volatile int handler_called = 0;
43 static int
44 handler (void *fault_address, int serious)
46 handler_called++;
47 if (handler_called > 10)
48 abort ();
49 if (fault_address
50 != (void *)((page + 0x678) & ~SIGSEGV_FAULT_ADDRESS_ROUNDOFF_BITS))
51 abort ();
52 if (mprotect ((void *) page, 0x4000, PROT_READ_WRITE) == 0)
53 return 1;
54 return 0;
57 static void
58 crasher (uintptr_t p)
60 *(volatile int *) (p + 0x678) = 42;
63 int
64 main ()
66 int prot_unwritable;
67 void *p;
69 /* Preparations. */
70 # if !HAVE_MAP_ANONYMOUS
71 zero_fd = open ("/dev/zero", O_RDONLY, 0644);
72 # endif
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;
78 # else
79 prot_unwritable = PROT_READ;
80 # endif
82 /* Setup some mmapped memory. */
83 p = mmap_zeromap ((void *) 0x12340000, 0x4000);
84 if (p == (void *)(-1))
86 fprintf (stderr, "mmap_zeromap failed.\n");
87 exit (2);
89 page = (uintptr_t) p;
91 /* Make it read-only. */
92 if (mprotect ((void *) page, 0x4000, prot_unwritable) < 0)
94 fprintf (stderr, "mprotect failed.\n");
95 exit (2);
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");
103 exit (2);
106 /* Install the SIGSEGV handler. */
107 sigsegv_install_handler (&handler);
109 /* The first write access should invoke the handler and then complete. */
110 crasher (page);
111 /* The second write access should not invoke the handler. */
112 crasher (page);
114 /* Check that the handler was called only once. */
115 if (handler_called != 1)
116 exit (1);
117 /* Test passed! */
118 printf ("Test passed.\n");
119 return 0;
122 #else
125 main ()
127 return 77;
130 #endif