This source file includes following definitions.
- handler_continuation
- handler
- crasher
- main
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #include <config.h>
20
21
22 #include "sigsegv.h"
23
24 #include <stdint.h>
25 #include <stdio.h>
26
27 #if HAVE_SIGSEGV_RECOVERY
28
29 # if defined _WIN32 && !defined __CYGWIN__
30
31 typedef int sigset_t;
32 # define sigemptyset(set)
33 # define sigprocmask(how,set,oldset)
34 # endif
35
36 # include "mmap-anon-util.h"
37 # include <stdlib.h>
38 # include <signal.h>
39 # include <setjmp.h>
40
41 # if SIGSEGV_FAULT_ADDRESS_ALIGNMENT > 1UL
42 # include <unistd.h>
43 # define SIGSEGV_FAULT_ADDRESS_ROUNDOFF_BITS (getpagesize () - 1)
44 # else
45 # define SIGSEGV_FAULT_ADDRESS_ROUNDOFF_BITS 0
46 # endif
47
48 jmp_buf mainloop;
49 sigset_t mainsigset;
50
51 volatile int pass = 0;
52 uintptr_t page;
53
54 volatile int handler_called = 0;
55
56 static void
57 handler_continuation (void *arg1, void *arg2, void *arg3)
58 {
59 longjmp (mainloop, pass);
60 }
61
62 int
63 handler (void *fault_address, int serious)
64 {
65 handler_called++;
66 if (handler_called > 10)
67 abort ();
68 if (fault_address
69 != (void *)((page + 0x678 + 8 * pass) & ~SIGSEGV_FAULT_ADDRESS_ROUNDOFF_BITS))
70 abort ();
71 pass++;
72 printf ("Fault %d caught.\n", pass);
73 sigprocmask (SIG_SETMASK, &mainsigset, NULL);
74 return sigsegv_leave_handler (handler_continuation, NULL, NULL, NULL);
75 }
76
77 void
78 crasher (uintptr_t p)
79 {
80 *(volatile int *) (p + 0x678 + 8 * pass) = 42;
81 }
82
83 int
84 main ()
85 {
86 int prot_unwritable;
87 void *p;
88 sigset_t emptyset;
89
90
91 # if !HAVE_MAP_ANONYMOUS
92 zero_fd = open ("/dev/zero", O_RDONLY, 0644);
93 # endif
94
95 # if defined __linux__ && defined __sparc__
96
97
98 prot_unwritable = PROT_NONE;
99 # else
100 prot_unwritable = PROT_READ;
101 # endif
102
103
104 p = mmap_zeromap ((void *) 0x12340000, 0x4000);
105 if (p == (void *)(-1))
106 {
107 fprintf (stderr, "mmap_zeromap failed.\n");
108 exit (2);
109 }
110 page = (uintptr_t) p;
111
112
113 if (mprotect ((void *) page, 0x4000, prot_unwritable) < 0)
114 {
115 fprintf (stderr, "mprotect failed.\n");
116 exit (2);
117 }
118
119
120 if (sigsegv_install_handler (&handler) < 0)
121 exit (2);
122
123
124 sigemptyset (&emptyset);
125 sigprocmask (SIG_BLOCK, &emptyset, &mainsigset);
126
127
128 switch (setjmp (mainloop))
129 {
130 case 0: case 1:
131 printf ("Doing SIGSEGV pass %d.\n", pass + 1);
132 crasher (page);
133 printf ("no SIGSEGV?!\n"); exit (1);
134 case 2:
135 break;
136 default:
137 abort ();
138 }
139
140
141 printf ("Test passed.\n");
142 return 0;
143 }
144
145 #else
146
147 int
148 main ()
149 {
150 return 77;
151 }
152
153 #endif