1 /* Page fault handling library.
2 Copyright (C) 1998-2021 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 3 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/>. */
23 /* Define the fault context structure. */
24 #if defined __linux__ || defined __ANDROID__ \
25 || (defined __FreeBSD__ && (defined __arm__ || defined __armhf__ || defined __arm64__)) \
26 || defined __NetBSD__ \
27 || defined _AIX || defined __sun \
29 /* Linux, FreeBSD, NetBSD, AIX, Solaris, Cygwin */
30 # include <ucontext.h>
31 #elif (defined __APPLE__ && defined __MACH__)
33 # include <sys/ucontext.h>
34 #elif defined __HAIKU__
39 /* Correct the value of SIGSTKSZ on some systems.
40 glibc >= 2.34: When _GNU_SOURCE is defined, SIGSTKSZ is no longer a
41 compile-time constant. But most programs need a simple constant.
42 AIX 64-bit: original value 4096 is too small.
43 HP-UX: original value 8192 is too small.
44 Solaris 11/x86_64: original value 8192 is too small. */
49 # define SIGSTKSZ 262144
51 # define SIGSTKSZ 65536
54 #if defined _AIX && defined _ARCH_PPC64
56 # define SIGSTKSZ 8192
58 #if defined __hpux || (defined __sun && (defined __x86_64__ || defined __amd64__))
60 # define SIGSTKSZ 16384
63 /* HAVE_SIGSEGV_RECOVERY
64 is defined if the system supports catching SIGSEGV. */
65 #if defined __linux__ || defined __ANDROID__ || defined __GNU__ \
66 || defined __FreeBSD_kernel__ || (defined __FreeBSD__ && !(defined __sparc__ || defined __sparc64__)) || defined __DragonFly__ \
67 || defined __NetBSD__ \
68 || defined __OpenBSD__ \
69 || (defined __APPLE__ && defined __MACH__) \
70 || defined _AIX || defined __sgi || defined __sun \
71 || defined __CYGWIN__ || defined __HAIKU__
72 /* Linux, Hurd, GNU/kFreeBSD, FreeBSD, NetBSD, OpenBSD, macOS, AIX, IRIX, Solaris, Cygwin, Haiku */
73 # define HAVE_SIGSEGV_RECOVERY 1
76 /* HAVE_STACK_OVERFLOW_RECOVERY
77 is defined if stack overflow can be caught. */
78 #if defined __linux__ || defined __ANDROID__ || defined __GNU__ \
79 || defined __FreeBSD_kernel__ || (defined __FreeBSD__ && !(defined __sparc__ || defined __sparc64__)) || defined __DragonFly__ \
80 || (defined __NetBSD__ && !(defined __sparc__ || defined __sparc64__)) \
81 || defined __OpenBSD__ \
82 || (defined __APPLE__ && defined __MACH__) \
83 || defined _AIX || defined __sgi || defined __sun \
84 || defined __CYGWIN__ || defined __HAIKU__
85 /* Linux, Hurd, GNU/kFreeBSD, FreeBSD, NetBSD, OpenBSD, macOS, AIX, IRIX, Solaris, Cygwin, Haiku */
86 # define HAVE_STACK_OVERFLOW_RECOVERY 1
94 #define LIBSIGSEGV_VERSION 0x020D /* version number: (major<<8) + minor */
95 extern int libsigsegv_version; /* Likewise */
97 /* -------------------------------------------------------------------------- */
99 #if 1 /* really only HAVE_SIGSEGV_RECOVERY */
102 * The mask of bits that are set to zero in a fault address that gets passed
103 * to a global SIGSEGV handler.
104 * On some platforms, the precise fault address is not known, only the memory
105 * page into which the fault address falls. This is apparently allowed by POSIX:
106 * <http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/signal.h.html>
107 * says: "For some implementations, the value of si_addr may be inaccurate."
108 * In this case, the returned fault address is rounded down to a multiple of
109 * getpagesize() = sysconf(_SC_PAGESIZE).
110 * On such platforms, we define SIGSEGV_FAULT_ADDRESS_ALIGNMENT to be an upper
111 * bound for getpagesize() (and, like getpagesize(), also a power of 2).
112 * On the platforms where the returned fault address is the precise one, we
113 * define SIGSEGV_FAULT_ADDRESS_ALIGNMENT to 1.
115 # if defined __NetBSD__ && (defined __sparc__ || defined __sparc64__)
116 /* getpagesize () is 0x1000 or 0x2000, depending on hardware. */
117 # define SIGSEGV_FAULT_ADDRESS_ALIGNMENT 0x2000UL
118 # elif defined __linux__ && (defined __s390__ || defined __s390x__)
119 /* getpagesize () is 0x1000. */
120 # define SIGSEGV_FAULT_ADDRESS_ALIGNMENT 0x1000UL
122 # define SIGSEGV_FAULT_ADDRESS_ALIGNMENT 1UL
126 * The type of a global SIGSEGV handler.
127 * The fault address, with the bits (SIGSEGV_FAULT_ADDRESS_ALIGNMENT - 1)
128 * cleared, is passed as argument.
129 * The access type (read access or write access) is not passed; your handler
130 * has to know itself how to distinguish these two cases.
131 * The second argument is 0, meaning it could also be a stack overflow, or 1,
132 * meaning the handler should seriously try to fix the fault.
133 * The return value should be nonzero if the handler has done its job
134 * and no other handler should be called, or 0 if the handler declines
135 * responsibility for the given address.
137 * The handler is run at a moment when nothing about the global state of the
138 * program is known. Therefore it cannot use facilities that manipulate global
139 * variables or locks. In particular, it cannot use malloc(); use mmap()
140 * instead. It cannot use fopen(); use open() instead. Etc. All global
141 * variables that are accessed by the handler should be marked 'volatile'.
143 typedef int (*sigsegv_handler_t) (void* fault_address, int serious);
146 * Installs a global SIGSEGV handler.
147 * This should be called once only, and it ignores any previously installed
149 * Returns 0 on success, or -1 if the system doesn't support catching SIGSEGV.
151 extern int sigsegv_install_handler (sigsegv_handler_t handler);
154 * Deinstalls the global SIGSEGV handler.
155 * This goes back to the state where no SIGSEGV handler is installed.
157 extern void sigsegv_deinstall_handler (void);
160 * Prepares leaving a SIGSEGV handler (through longjmp or similar means).
161 * Control is transferred by calling CONTINUATION with CONT_ARG1, CONT_ARG2,
162 * CONT_ARG3 as arguments.
163 * CONTINUATION must not return.
164 * The sigsegv_leave_handler function may return if called from a SIGSEGV
165 * handler; its return value should be used as the handler's return value.
166 * The sigsegv_leave_handler function does not return if called from a
167 * stack overflow handler.
169 extern int sigsegv_leave_handler (void (*continuation) (void*, void*, void*), void* cont_arg1, void* cont_arg2, void* cont_arg3);
171 #endif /* HAVE_SIGSEGV_RECOVERY */
173 #if 1 /* really only HAVE_STACK_OVERFLOW_RECOVERY */
176 * The type of a context passed to a stack overflow handler.
177 * This type is system dependent; on some platforms it is an 'ucontext_t *',
178 * on some platforms it is a 'struct sigcontext *', on others merely an
181 # if defined __linux__ || defined __ANDROID__ \
182 || (defined __FreeBSD__ && (defined __arm__ || defined __armhf__ || defined __arm64__)) \
183 || defined __NetBSD__ \
184 || (defined __APPLE__ && defined __MACH__) \
185 || defined _AIX || defined __sun \
186 || defined __CYGWIN__ || defined __HAIKU__
187 typedef ucontext_t *stackoverflow_context_t;
188 # elif defined __GNU__ \
189 || defined __FreeBSD_kernel__ || (defined __FreeBSD__ && !(defined __sparc__ || defined __sparc64__)) \
190 || defined __OpenBSD__ || defined __sgi
191 typedef struct sigcontext *stackoverflow_context_t;
193 typedef void *stackoverflow_context_t;
197 * The type of a stack overflow handler.
198 * Such a handler should perform a longjmp call in order to reduce the amount
199 * of stack needed. It must not return.
200 * The emergency argument is 0 when the stack could be repared, or 1 if the
201 * application should better save its state and exit now.
203 * The handler is run at a moment when nothing about the global state of the
204 * program is known. Therefore it cannot use facilities that manipulate global
205 * variables or locks. In particular, it cannot use malloc(); use mmap()
206 * instead. It cannot use fopen(); use open() instead. Etc. All global
207 * variables that are accessed by the handler should be marked 'volatile'.
209 typedef void (*stackoverflow_handler_t) (int emergency, stackoverflow_context_t scp);
212 * Installs a stack overflow handler.
213 * The extra_stack argument is a pointer to a pre-allocated area used as a
214 * stack for executing the handler. It typically comes from a static variable
215 * or from heap-allocated memoty; placing it on the main stack may fail on
216 * some operating systems.
217 * Its size, passed in extra_stack_size, should be sufficiently large. The
218 * following code determines an appropriate size:
219 * #include <signal.h>
220 * #ifndef SIGSTKSZ / * glibc defines SIGSTKSZ for this purpose * /
221 * # define SIGSTKSZ 16384 / * on most platforms, 16 KB are sufficient * /
223 * Returns 0 on success, or -1 if the system doesn't support catching stack
226 extern int stackoverflow_install_handler (stackoverflow_handler_t handler,
227 void* extra_stack, size_t extra_stack_size);
230 * Deinstalls the stack overflow handler.
232 extern void stackoverflow_deinstall_handler (void);
234 #endif /* HAVE_STACK_OVERFLOW_RECOVERY */
236 /* -------------------------------------------------------------------------- */
242 #endif /* _SIGSEGV_H */