tst-longjmp_chk.c 1.2 KB

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