1 | /* Check longjmp from user context to main context. |
2 | Copyright (C) 2023-2024 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | /* When _FORTIFY_SOURCE is defined to 2, ____longjmp_chk is called, |
20 | instead of longjmp. ____longjmp_chk compares the relative stack |
21 | values to decide if it is called from a stack frame which called |
22 | setjmp. If not, ____longjmp_chk assumes that an alternate signal |
23 | stack is used. Since comparing the relative stack values isn't |
24 | reliable with user context, when there is no signal, ____longjmp_chk |
25 | will fail. Undefine _FORTIFY_SOURCE to avoid ____longjmp_chk. */ |
26 | #undef _FORTIFY_SOURCE |
27 | |
28 | #include <stdio.h> |
29 | #include <stdlib.h> |
30 | #include <setjmp.h> |
31 | #include <ucontext.h> |
32 | #include <unistd.h> |
33 | |
34 | static jmp_buf jmpbuf; |
35 | static ucontext_t ctx; |
36 | |
37 | static void f2 (void); |
38 | |
39 | static void |
40 | __attribute__ ((noinline, noclone)) |
41 | f1 (void) |
42 | { |
43 | printf (format: "start f1\n" ); |
44 | f2 (); |
45 | } |
46 | |
47 | static void |
48 | __attribute__ ((noinline, noclone)) |
49 | f2 (void) |
50 | { |
51 | printf (format: "start f2\n" ); |
52 | if (setcontext (&ctx) != 0) |
53 | { |
54 | printf (format: "%s: setcontext: %m\n" , __FUNCTION__); |
55 | exit (EXIT_FAILURE); |
56 | } |
57 | } |
58 | |
59 | static void |
60 | f3 (void) |
61 | { |
62 | printf (format: "start f3\n" ); |
63 | longjmp (jmpbuf, 1); |
64 | } |
65 | |
66 | static int |
67 | __attribute__ ((noinline, noclone)) |
68 | do_test_1 (void) |
69 | { |
70 | char st1[32768]; |
71 | |
72 | if (setjmp (jmpbuf) != 0) |
73 | return 0; |
74 | |
75 | puts (s: "making contexts" ); |
76 | if (getcontext (ucp: &ctx) != 0) |
77 | { |
78 | printf (format: "%s: getcontext: %m\n" , __FUNCTION__); |
79 | exit (EXIT_FAILURE); |
80 | } |
81 | ctx.uc_stack.ss_sp = st1; |
82 | ctx.uc_stack.ss_size = sizeof st1; |
83 | ctx.uc_link = NULL; |
84 | makecontext (ucp: &ctx, func: (void (*) (void)) f3, argc: 0); |
85 | f1 (); |
86 | puts (s: "FAIL: returned from f1 ()" ); |
87 | exit (EXIT_FAILURE); |
88 | } |
89 | |
90 | static int |
91 | do_test (void) |
92 | { |
93 | return do_test_1 (); |
94 | } |
95 | |
96 | #include <support/test-driver.c> |
97 | |