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
14static int do_test(void);
15#define TEST_FUNCTION do_test ()
16#include "../test-skeleton.c"
17
18
19static jmp_buf b;
20
21
22static void
23__attribute__ ((noinline))
24f (void)
25{
26 char buf[1000];
27 asm volatile ("" : "=m" (buf));
28
29 if (setjmp (b) != 0)
30 {
31 puts (s: "second longjmp succeeded");
32 exit (1);
33 }
34}
35
36
37static bool expected_to_fail;
38
39
40static void
41handler (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
54static int
55do_test (void)
56{
57 set_fortify_handler (handler);
58
59
60 expected_to_fail = false;
61
62 if (setjmp (b) == 0)
63 {
64 longjmp (env: b, val: 1);
65 /* NOTREACHED */
66 printf (format: "first longjmp returned\n");
67 return 1;
68 }
69
70
71 expected_to_fail = true;
72
73 f ();
74 longjmp (env: b, val: 1);
75
76 puts (s: "second longjmp returned");
77 return 1;
78}
79

source code of glibc/debug/tst-longjmp_chk.c