tst-longjmp_chk.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. static int do_test(void);
  13. #define TEST_FUNCTION do_test ()
  14. #include "../test-skeleton.c"
  15. static jmp_buf b;
  16. static void
  17. __attribute__ ((noinline))
  18. f (void)
  19. {
  20. char buf[1000];
  21. asm volatile ("" : "=m" (buf));
  22. if (setjmp (b) != 0)
  23. {
  24. puts ("second longjmp succeeded");
  25. exit (1);
  26. }
  27. }
  28. static bool expected_to_fail;
  29. static void
  30. handler (int sig)
  31. {
  32. if (expected_to_fail)
  33. _exit (0);
  34. else
  35. {
  36. static const char msg[] = "unexpected longjmp failure\n";
  37. TEMP_FAILURE_RETRY (write (STDOUT_FILENO, msg, sizeof (msg) - 1));
  38. _exit (1);
  39. }
  40. }
  41. static int
  42. do_test (void)
  43. {
  44. set_fortify_handler (handler);
  45. expected_to_fail = false;
  46. if (setjmp (b) == 0)
  47. {
  48. longjmp (b, 1);
  49. /* NOTREACHED */
  50. printf ("first longjmp returned\n");
  51. return 1;
  52. }
  53. expected_to_fail = true;
  54. f ();
  55. longjmp (b, 1);
  56. puts ("second longjmp returned");
  57. return 1;
  58. }