(root)/
m4-1.4.19/
lib/
sigsegv.in.h
       1  /* Page fault handling library.
       2     Copyright (C) 1998-2021  Bruno Haible <bruno@clisp.org>
       3  
       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  
       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.
      13  
      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/>.  */
      16  
      17  #ifndef _SIGSEGV_H
      18  #define _SIGSEGV_H
      19  
      20  /* Get size_t.  */
      21  #include <stddef.h>
      22  
      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 \
      28      || defined __CYGWIN__
      29  /* Linux, FreeBSD, NetBSD, AIX, Solaris, Cygwin */
      30  # include <ucontext.h>
      31  #elif (defined __APPLE__ && defined __MACH__)
      32  /* macOS */
      33  # include <sys/ucontext.h>
      34  #elif defined __HAIKU__
      35  /* Haiku */
      36  # include <signal.h>
      37  #endif
      38  
      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.  */
      45  #include <signal.h>
      46  #if __GLIBC__ >= 2
      47  # undef SIGSTKSZ
      48  # if defined __ia64__
      49  #  define SIGSTKSZ 262144
      50  # else
      51  #  define SIGSTKSZ 65536
      52  # endif
      53  #endif
      54  #if defined _AIX && defined _ARCH_PPC64
      55  # undef SIGSTKSZ
      56  # define SIGSTKSZ 8192
      57  #endif
      58  #if defined __hpux || (defined __sun && (defined __x86_64__ || defined __amd64__))
      59  # undef SIGSTKSZ
      60  # define SIGSTKSZ 16384
      61  #endif
      62  
      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
      74  #endif
      75  
      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
      87  #endif
      88  
      89  
      90  #ifdef __cplusplus
      91  extern "C" {
      92  #endif
      93  
      94  #define LIBSIGSEGV_VERSION 0x020D    /* version number: (major<<8) + minor */
      95  extern int libsigsegv_version;       /* Likewise */
      96  
      97  /* -------------------------------------------------------------------------- */
      98  
      99  #if 1 /* really only HAVE_SIGSEGV_RECOVERY */
     100  
     101  /*
     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.
     114   */
     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
     121  # else
     122  #  define SIGSEGV_FAULT_ADDRESS_ALIGNMENT 1UL
     123  # endif
     124  
     125  /*
     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.
     136   *
     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'.
     142   */
     143  typedef int (*sigsegv_handler_t) (void* fault_address, int serious);
     144  
     145  /*
     146   * Installs a global SIGSEGV handler.
     147   * This should be called once only, and it ignores any previously installed
     148   * SIGSEGV handler.
     149   * Returns 0 on success, or -1 if the system doesn't support catching SIGSEGV.
     150   */
     151  extern int sigsegv_install_handler (sigsegv_handler_t handler);
     152  
     153  /*
     154   * Deinstalls the global SIGSEGV handler.
     155   * This goes back to the state where no SIGSEGV handler is installed.
     156   */
     157  extern void sigsegv_deinstall_handler (void);
     158  
     159  /*
     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.
     168   */
     169  extern int sigsegv_leave_handler (void (*continuation) (void*, void*, void*), void* cont_arg1, void* cont_arg2, void* cont_arg3);
     170  
     171  #endif /* HAVE_SIGSEGV_RECOVERY */
     172  
     173  #if 1 /* really only HAVE_STACK_OVERFLOW_RECOVERY */
     174  
     175  /*
     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
     179   * opaque 'void *'.
     180   */
     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;
     192  # else
     193  typedef void *stackoverflow_context_t;
     194  # endif
     195  
     196  /*
     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.
     202   *
     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'.
     208   */
     209  typedef void (*stackoverflow_handler_t) (int emergency, stackoverflow_context_t scp);
     210  
     211  /*
     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 * /
     222   *   #endif
     223   * Returns 0 on success, or -1 if the system doesn't support catching stack
     224   * overflow.
     225   */
     226  extern int stackoverflow_install_handler (stackoverflow_handler_t handler,
     227                                            void* extra_stack, size_t extra_stack_size);
     228  
     229  /*
     230   * Deinstalls the stack overflow handler.
     231   */
     232  extern void stackoverflow_deinstall_handler (void);
     233  
     234  #endif /* HAVE_STACK_OVERFLOW_RECOVERY */
     235  
     236  /* -------------------------------------------------------------------------- */
     237  
     238  #ifdef __cplusplus
     239  }
     240  #endif
     241  
     242  #endif /* _SIGSEGV_H */