|
| 1 | +/* DO NOT EDIT! GENERATED AUTOMATICALLY! */ |
| 2 | +/* Page fault handling library. |
| 3 | + Copyright (C) 1998-2021 Bruno Haible <bruno@clisp.org> |
| 4 | +
|
| 5 | + This program is free software: you can redistribute it and/or modify |
| 6 | + it under the terms of the GNU General Public License as published by |
| 7 | + the Free Software Foundation; either version 3 of the License, or |
| 8 | + (at your option) any later version. |
| 9 | +
|
| 10 | + This program is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + GNU General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU General Public License |
| 16 | + along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 17 | + |
| 18 | +#ifndef _SIGSEGV_H |
| 19 | +#define _SIGSEGV_H |
| 20 | + |
| 21 | +/* Get size_t. */ |
| 22 | +#include <stddef.h> |
| 23 | + |
| 24 | +/* Define the fault context structure. */ |
| 25 | +#if defined __linux__ || defined __ANDROID__ \ |
| 26 | + || (defined __FreeBSD__ && (defined __arm__ || defined __armhf__ || defined __arm64__)) \ |
| 27 | + || defined __NetBSD__ \ |
| 28 | + || defined _AIX || defined __sun \ |
| 29 | + || defined __CYGWIN__ |
| 30 | +/* Linux, FreeBSD, NetBSD, AIX, Solaris, Cygwin */ |
| 31 | +# include <ucontext.h> |
| 32 | +#elif (defined __APPLE__ && defined __MACH__) |
| 33 | +/* macOS */ |
| 34 | +# include <sys/ucontext.h> |
| 35 | +#elif defined __HAIKU__ |
| 36 | +/* Haiku */ |
| 37 | +# include <signal.h> |
| 38 | +#endif |
| 39 | + |
| 40 | +/* Correct the value of SIGSTKSZ on some systems. |
| 41 | + glibc >= 2.34: When _GNU_SOURCE is defined, SIGSTKSZ is no longer a |
| 42 | + compile-time constant. But most programs need a simple constant. |
| 43 | + AIX 64-bit: original value 4096 is too small. |
| 44 | + HP-UX: original value 8192 is too small. |
| 45 | + Solaris 11/x86_64: original value 8192 is too small. */ |
| 46 | +#include <signal.h> |
| 47 | +#if __GLIBC__ >= 2 |
| 48 | +# undef SIGSTKSZ |
| 49 | +# if defined __ia64__ |
| 50 | +# define SIGSTKSZ 262144 |
| 51 | +# else |
| 52 | +# define SIGSTKSZ 65536 |
| 53 | +# endif |
| 54 | +#endif |
| 55 | +#if defined _AIX && defined _ARCH_PPC64 |
| 56 | +# undef SIGSTKSZ |
| 57 | +# define SIGSTKSZ 8192 |
| 58 | +#endif |
| 59 | +#if defined __hpux || (defined __sun && (defined __x86_64__ || defined __amd64__)) |
| 60 | +# undef SIGSTKSZ |
| 61 | +# define SIGSTKSZ 16384 |
| 62 | +#endif |
| 63 | + |
| 64 | +/* HAVE_SIGSEGV_RECOVERY |
| 65 | + is defined if the system supports catching SIGSEGV. */ |
| 66 | +#if defined __linux__ || defined __ANDROID__ || defined __GNU__ \ |
| 67 | + || defined __FreeBSD_kernel__ || (defined __FreeBSD__ && !(defined __sparc__ || defined __sparc64__)) || defined __DragonFly__ \ |
| 68 | + || defined __NetBSD__ \ |
| 69 | + || defined __OpenBSD__ \ |
| 70 | + || (defined __APPLE__ && defined __MACH__) \ |
| 71 | + || defined _AIX || defined __sgi || defined __sun \ |
| 72 | + || defined __CYGWIN__ || defined __HAIKU__ |
| 73 | +/* Linux, Hurd, GNU/kFreeBSD, FreeBSD, NetBSD, OpenBSD, macOS, AIX, IRIX, Solaris, Cygwin, Haiku */ |
| 74 | +# define HAVE_SIGSEGV_RECOVERY 1 |
| 75 | +#endif |
| 76 | + |
| 77 | +/* HAVE_STACK_OVERFLOW_RECOVERY |
| 78 | + is defined if stack overflow can be caught. */ |
| 79 | +#if defined __linux__ || defined __ANDROID__ || defined __GNU__ \ |
| 80 | + || defined __FreeBSD_kernel__ || (defined __FreeBSD__ && !(defined __sparc__ || defined __sparc64__)) || defined __DragonFly__ \ |
| 81 | + || (defined __NetBSD__ && !(defined __sparc__ || defined __sparc64__)) \ |
| 82 | + || defined __OpenBSD__ \ |
| 83 | + || (defined __APPLE__ && defined __MACH__) \ |
| 84 | + || defined _AIX || defined __sgi || defined __sun \ |
| 85 | + || defined __CYGWIN__ || defined __HAIKU__ |
| 86 | +/* Linux, Hurd, GNU/kFreeBSD, FreeBSD, NetBSD, OpenBSD, macOS, AIX, IRIX, Solaris, Cygwin, Haiku */ |
| 87 | +# define HAVE_STACK_OVERFLOW_RECOVERY 1 |
| 88 | +#endif |
| 89 | + |
| 90 | + |
| 91 | +#ifdef __cplusplus |
| 92 | +extern "C" { |
| 93 | +#endif |
| 94 | + |
| 95 | +#define LIBSIGSEGV_VERSION 0x020D /* version number: (major<<8) + minor */ |
| 96 | +extern int libsigsegv_version; /* Likewise */ |
| 97 | + |
| 98 | +/* -------------------------------------------------------------------------- */ |
| 99 | + |
| 100 | +#if 1 /* really only HAVE_SIGSEGV_RECOVERY */ |
| 101 | + |
| 102 | +/* |
| 103 | + * The mask of bits that are set to zero in a fault address that gets passed |
| 104 | + * to a global SIGSEGV handler. |
| 105 | + * On some platforms, the precise fault address is not known, only the memory |
| 106 | + * page into which the fault address falls. This is apparently allowed by POSIX: |
| 107 | + * <http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/signal.h.html> |
| 108 | + * says: "For some implementations, the value of si_addr may be inaccurate." |
| 109 | + * In this case, the returned fault address is rounded down to a multiple of |
| 110 | + * getpagesize() = sysconf(_SC_PAGESIZE). |
| 111 | + * On such platforms, we define SIGSEGV_FAULT_ADDRESS_ALIGNMENT to be an upper |
| 112 | + * bound for getpagesize() (and, like getpagesize(), also a power of 2). |
| 113 | + * On the platforms where the returned fault address is the precise one, we |
| 114 | + * define SIGSEGV_FAULT_ADDRESS_ALIGNMENT to 1. |
| 115 | + */ |
| 116 | +# if defined __NetBSD__ && (defined __sparc__ || defined __sparc64__) |
| 117 | + /* getpagesize () is 0x1000 or 0x2000, depending on hardware. */ |
| 118 | +# define SIGSEGV_FAULT_ADDRESS_ALIGNMENT 0x2000UL |
| 119 | +# elif defined __linux__ && (defined __s390__ || defined __s390x__) |
| 120 | + /* getpagesize () is 0x1000. */ |
| 121 | +# define SIGSEGV_FAULT_ADDRESS_ALIGNMENT 0x1000UL |
| 122 | +# else |
| 123 | +# define SIGSEGV_FAULT_ADDRESS_ALIGNMENT 1UL |
| 124 | +# endif |
| 125 | + |
| 126 | +/* |
| 127 | + * The type of a global SIGSEGV handler. |
| 128 | + * The fault address, with the bits (SIGSEGV_FAULT_ADDRESS_ALIGNMENT - 1) |
| 129 | + * cleared, is passed as argument. |
| 130 | + * The access type (read access or write access) is not passed; your handler |
| 131 | + * has to know itself how to distinguish these two cases. |
| 132 | + * The second argument is 0, meaning it could also be a stack overflow, or 1, |
| 133 | + * meaning the handler should seriously try to fix the fault. |
| 134 | + * The return value should be nonzero if the handler has done its job |
| 135 | + * and no other handler should be called, or 0 if the handler declines |
| 136 | + * responsibility for the given address. |
| 137 | + * |
| 138 | + * The handler is run at a moment when nothing about the global state of the |
| 139 | + * program is known. Therefore it cannot use facilities that manipulate global |
| 140 | + * variables or locks. In particular, it cannot use malloc(); use mmap() |
| 141 | + * instead. It cannot use fopen(); use open() instead. Etc. All global |
| 142 | + * variables that are accessed by the handler should be marked 'volatile'. |
| 143 | + */ |
| 144 | +typedef int (*sigsegv_handler_t) (void* fault_address, int serious); |
| 145 | + |
| 146 | +/* |
| 147 | + * Installs a global SIGSEGV handler. |
| 148 | + * This should be called once only, and it ignores any previously installed |
| 149 | + * SIGSEGV handler. |
| 150 | + * Returns 0 on success, or -1 if the system doesn't support catching SIGSEGV. |
| 151 | + */ |
| 152 | +extern int sigsegv_install_handler (sigsegv_handler_t handler); |
| 153 | + |
| 154 | +/* |
| 155 | + * Deinstalls the global SIGSEGV handler. |
| 156 | + * This goes back to the state where no SIGSEGV handler is installed. |
| 157 | + */ |
| 158 | +extern void sigsegv_deinstall_handler (void); |
| 159 | + |
| 160 | +/* |
| 161 | + * Prepares leaving a SIGSEGV handler (through longjmp or similar means). |
| 162 | + * Control is transferred by calling CONTINUATION with CONT_ARG1, CONT_ARG2, |
| 163 | + * CONT_ARG3 as arguments. |
| 164 | + * CONTINUATION must not return. |
| 165 | + * The sigsegv_leave_handler function may return if called from a SIGSEGV |
| 166 | + * handler; its return value should be used as the handler's return value. |
| 167 | + * The sigsegv_leave_handler function does not return if called from a |
| 168 | + * stack overflow handler. |
| 169 | + */ |
| 170 | +extern int sigsegv_leave_handler (void (*continuation) (void*, void*, void*), void* cont_arg1, void* cont_arg2, void* cont_arg3); |
| 171 | + |
| 172 | +#endif /* HAVE_SIGSEGV_RECOVERY */ |
| 173 | + |
| 174 | +#if 1 /* really only HAVE_STACK_OVERFLOW_RECOVERY */ |
| 175 | + |
| 176 | +/* |
| 177 | + * The type of a context passed to a stack overflow handler. |
| 178 | + * This type is system dependent; on some platforms it is an 'ucontext_t *', |
| 179 | + * on some platforms it is a 'struct sigcontext *', on others merely an |
| 180 | + * opaque 'void *'. |
| 181 | + */ |
| 182 | +# if defined __linux__ || defined __ANDROID__ \ |
| 183 | + || (defined __FreeBSD__ && (defined __arm__ || defined __armhf__ || defined __arm64__)) \ |
| 184 | + || defined __NetBSD__ \ |
| 185 | + || (defined __APPLE__ && defined __MACH__) \ |
| 186 | + || defined _AIX || defined __sun \ |
| 187 | + || defined __CYGWIN__ || defined __HAIKU__ |
| 188 | +typedef ucontext_t *stackoverflow_context_t; |
| 189 | +# elif defined __GNU__ \ |
| 190 | + || defined __FreeBSD_kernel__ || (defined __FreeBSD__ && !(defined __sparc__ || defined __sparc64__)) \ |
| 191 | + || defined __OpenBSD__ || defined __sgi |
| 192 | +typedef struct sigcontext *stackoverflow_context_t; |
| 193 | +# else |
| 194 | +typedef void *stackoverflow_context_t; |
| 195 | +# endif |
| 196 | + |
| 197 | +/* |
| 198 | + * The type of a stack overflow handler. |
| 199 | + * Such a handler should perform a longjmp call in order to reduce the amount |
| 200 | + * of stack needed. It must not return. |
| 201 | + * The emergency argument is 0 when the stack could be repared, or 1 if the |
| 202 | + * application should better save its state and exit now. |
| 203 | + * |
| 204 | + * The handler is run at a moment when nothing about the global state of the |
| 205 | + * program is known. Therefore it cannot use facilities that manipulate global |
| 206 | + * variables or locks. In particular, it cannot use malloc(); use mmap() |
| 207 | + * instead. It cannot use fopen(); use open() instead. Etc. All global |
| 208 | + * variables that are accessed by the handler should be marked 'volatile'. |
| 209 | + */ |
| 210 | +typedef void (*stackoverflow_handler_t) (int emergency, stackoverflow_context_t scp); |
| 211 | + |
| 212 | +/* |
| 213 | + * Installs a stack overflow handler. |
| 214 | + * The extra_stack argument is a pointer to a pre-allocated area used as a |
| 215 | + * stack for executing the handler. It typically comes from a static variable |
| 216 | + * or from heap-allocated memoty; placing it on the main stack may fail on |
| 217 | + * some operating systems. |
| 218 | + * Its size, passed in extra_stack_size, should be sufficiently large. The |
| 219 | + * following code determines an appropriate size: |
| 220 | + * #include <signal.h> |
| 221 | + * #ifndef SIGSTKSZ / * glibc defines SIGSTKSZ for this purpose * / |
| 222 | + * # define SIGSTKSZ 16384 / * on most platforms, 16 KB are sufficient * / |
| 223 | + * #endif |
| 224 | + * Returns 0 on success, or -1 if the system doesn't support catching stack |
| 225 | + * overflow. |
| 226 | + */ |
| 227 | +extern int stackoverflow_install_handler (stackoverflow_handler_t handler, |
| 228 | + void* extra_stack, size_t extra_stack_size); |
| 229 | + |
| 230 | +/* |
| 231 | + * Deinstalls the stack overflow handler. |
| 232 | + */ |
| 233 | +extern void stackoverflow_deinstall_handler (void); |
| 234 | + |
| 235 | +#endif /* HAVE_STACK_OVERFLOW_RECOVERY */ |
| 236 | + |
| 237 | +/* -------------------------------------------------------------------------- */ |
| 238 | + |
| 239 | +#ifdef __cplusplus |
| 240 | +} |
| 241 | +#endif |
| 242 | + |
| 243 | +#endif /* _SIGSEGV_H */ |
0 commit comments