(root)/
m4-1.4.19/
tests/
test-sigsegv-catch-stackoverflow1.c
       1  /* Test the stack overflow handler.
       2     Copyright (C) 2002-2021  Bruno Haible <bruno@clisp.org>
       3     Copyright (C) 2010 Eric Blake <eblake@redhat.com>
       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 2 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  #include <config.h>
      19  
      20  /* Specification.  */
      21  #include "sigsegv.h"
      22  
      23  #include <stdio.h>
      24  #include <limits.h>
      25  
      26  #if HAVE_STACK_OVERFLOW_RECOVERY
      27  
      28  # if defined _WIN32 && !defined __CYGWIN__
      29    /* Windows doesn't have sigset_t.  */
      30    typedef int sigset_t;
      31  #  define sigemptyset(set)
      32  #  define sigprocmask(how,set,oldset)
      33  # endif
      34  
      35  # include <stddef.h> /* needed for NULL on SunOS4 */
      36  # include <stdlib.h> /* for abort, exit */
      37  # include <signal.h>
      38  # include <setjmp.h>
      39  # if HAVE_SETRLIMIT
      40  #  include <sys/types.h>
      41  #  include <sys/time.h>
      42  #  include <sys/resource.h>
      43  # endif
      44  # include "altstack-util.h"
      45  
      46  jmp_buf mainloop;
      47  sigset_t mainsigset;
      48  
      49  volatile int pass = 0;
      50  
      51  volatile char *stack_lower_bound;
      52  volatile char *stack_upper_bound;
      53  
      54  static void
      55  stackoverflow_handler_continuation (void *arg1, void *arg2, void *arg3)
      56  {
      57    int arg = (int) (long) arg1;
      58    longjmp (mainloop, arg);
      59  }
      60  
      61  void
      62  stackoverflow_handler (int emergency, stackoverflow_context_t scp)
      63  {
      64    char dummy;
      65    volatile char *addr = &dummy;
      66    if (!(addr >= stack_lower_bound && addr <= stack_upper_bound))
      67      abort ();
      68    pass++;
      69    printf ("Stack overflow %d caught.\n", pass);
      70    sigprocmask (SIG_SETMASK, &mainsigset, NULL);
      71    sigsegv_leave_handler (stackoverflow_handler_continuation,
      72                           (void *) (long) (emergency ? -1 : pass), NULL, NULL);
      73  }
      74  
      75  volatile int *
      76  recurse_1 (int n, volatile int *p)
      77  {
      78    if (n < INT_MAX)
      79      *recurse_1 (n + 1, p) += n;
      80    return p;
      81  }
      82  
      83  int
      84  recurse (volatile int n)
      85  {
      86    return *recurse_1 (n, &n);
      87  }
      88  
      89  int
      90  main ()
      91  {
      92    sigset_t emptyset;
      93  
      94  # if HAVE_SETRLIMIT && defined RLIMIT_STACK
      95    /* Before starting the endless recursion, try to be friendly to the user's
      96       machine.  On some Linux 2.2.x systems, there is no stack limit for user
      97       processes at all.  We don't want to kill such systems.  */
      98    struct rlimit rl;
      99    rl.rlim_cur = rl.rlim_max = 0x100000; /* 1 MB */
     100    setrlimit (RLIMIT_STACK, &rl);
     101  # endif
     102  
     103    /* Prepare the storage for the alternate stack.  */
     104    prepare_alternate_stack ();
     105  
     106    /* Install the stack overflow handler.  */
     107    if (stackoverflow_install_handler (&stackoverflow_handler,
     108                                       mystack, MYSTACK_SIZE)
     109        < 0)
     110      exit (2);
     111    stack_lower_bound = mystack;
     112    stack_upper_bound = mystack + MYSTACK_SIZE - 1;
     113  
     114    /* Save the current signal mask.  */
     115    sigemptyset (&emptyset);
     116    sigprocmask (SIG_BLOCK, &emptyset, &mainsigset);
     117  
     118    /* Provoke two stack overflows in a row.  */
     119    switch (setjmp (mainloop))
     120      {
     121      case -1:
     122        printf ("emergency exit\n"); exit (1);
     123      case 0: case 1:
     124        printf ("Starting recursion pass %d.\n", pass + 1);
     125        recurse (0);
     126        printf ("no endless recursion?!\n"); exit (1);
     127      case 2:
     128        break;
     129      default:
     130        abort ();
     131      }
     132  
     133    /* Validate that the alternate stack did not overflow.  */
     134    check_alternate_stack_no_overflow ();
     135  
     136    printf ("Test passed.\n");
     137    exit (0);
     138  }
     139  
     140  #else
     141  
     142  int
     143  main ()
     144  {
     145    return 77;
     146  }
     147  
     148  #endif