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