sigsegv.h 9.7 KB

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