(root)/
glibc-2.38/
debug/
tst-longjmp_chk.c
       1  /* Basic test to make sure doing a longjmp to a jmpbuf with an invalid sp
       2     is caught by the fortification code.  */
       3  #include <errno.h>
       4  #include <fcntl.h>
       5  #include <paths.h>
       6  #include <setjmp.h>
       7  #include <signal.h>
       8  #include <stdbool.h>
       9  #include <stdio.h>
      10  #include <stdlib.h>
      11  #include <unistd.h>
      12  
      13  
      14  static int do_test(void);
      15  #define TEST_FUNCTION do_test ()
      16  #include "../test-skeleton.c"
      17  
      18  
      19  static jmp_buf b;
      20  
      21  
      22  static void
      23  __attribute__ ((noinline))
      24  f (void)
      25  {
      26    char buf[1000];
      27    asm volatile ("" : "=m" (buf));
      28  
      29    if (setjmp (b) != 0)
      30      {
      31        puts ("second longjmp succeeded");
      32        exit (1);
      33      }
      34  }
      35  
      36  
      37  static bool expected_to_fail;
      38  
      39  
      40  static void
      41  handler (int sig)
      42  {
      43    if (expected_to_fail)
      44      _exit (0);
      45    else
      46      {
      47        static const char msg[] = "unexpected longjmp failure\n";
      48        TEMP_FAILURE_RETRY (write (STDOUT_FILENO, msg, sizeof (msg) - 1));
      49        _exit (1);
      50      }
      51  }
      52  
      53  
      54  static int
      55  do_test (void)
      56  {
      57    set_fortify_handler (handler);
      58  
      59  
      60    expected_to_fail = false;
      61  
      62    if (setjmp (b) == 0)
      63      {
      64        longjmp (b, 1);
      65        /* NOTREACHED */
      66        printf ("first longjmp returned\n");
      67        return 1;
      68      }
      69  
      70  
      71    expected_to_fail = true;
      72  
      73    f ();
      74    longjmp (b, 1);
      75  
      76    puts ("second longjmp returned");
      77    return 1;
      78  }