1 | /* Check setjmp/longjmp within user 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 | #include <errno.h> |
20 | #include <stdio.h> |
21 | #include <stdlib.h> |
22 | #include <string.h> |
23 | #include <setjmp.h> |
24 | #include <ucontext.h> |
25 | #include <unistd.h> |
26 | |
27 | static ucontext_t ctx[3]; |
28 | static jmp_buf jmpbuf; |
29 | |
30 | static int was_in_f1; |
31 | static int was_in_f2; |
32 | static int longjmp_called; |
33 | |
34 | static char st2[32768]; |
35 | |
36 | static void |
37 | f1 (int a0, int a1, int a2, int a3) |
38 | { |
39 | printf (format: "start f1(a0=%x,a1=%x,a2=%x,a3=%x)\n" , a0, a1, a2, a3); |
40 | |
41 | if (a0 != 1 || a1 != 2 || a2 != 3 || a3 != -4) |
42 | { |
43 | puts (s: "arg mismatch" ); |
44 | exit (EXIT_FAILURE); |
45 | } |
46 | |
47 | if (swapcontext (oucp: &ctx[1], ucp: &ctx[2]) != 0) |
48 | { |
49 | printf (format: "%s: swapcontext: %m\n" , __FUNCTION__); |
50 | exit (EXIT_FAILURE); |
51 | } |
52 | puts (s: "finish f1" ); |
53 | was_in_f1 = 1; |
54 | } |
55 | |
56 | static void |
57 | __attribute__ ((noinline, noclone)) |
58 | call_longjmp (void) |
59 | { |
60 | longjmp_called = 1; |
61 | longjmp (jmpbuf, 1); |
62 | } |
63 | |
64 | static void |
65 | f2 (void) |
66 | { |
67 | if (!longjmp_called) |
68 | { |
69 | if (setjmp (jmpbuf) == 0) |
70 | call_longjmp (); |
71 | } |
72 | |
73 | puts (s: "start f2" ); |
74 | if (swapcontext (oucp: &ctx[2], ucp: &ctx[1]) != 0) |
75 | { |
76 | printf (format: "%s: swapcontext: %m\n" , __FUNCTION__); |
77 | exit (EXIT_FAILURE); |
78 | } |
79 | puts (s: "finish f2" ); |
80 | was_in_f2 = 1; |
81 | } |
82 | |
83 | volatile int global; |
84 | static int back_in_main; |
85 | |
86 | static void |
87 | check_called (void) |
88 | { |
89 | if (back_in_main == 0) |
90 | { |
91 | puts (s: "program did not reach main again" ); |
92 | _exit (EXIT_FAILURE); |
93 | } |
94 | } |
95 | |
96 | static int |
97 | do_test (void) |
98 | { |
99 | atexit (func: check_called); |
100 | |
101 | char st1[32768]; |
102 | |
103 | puts (s: "making contexts" ); |
104 | if (getcontext (ucp: &ctx[1]) != 0) |
105 | { |
106 | if (errno == ENOSYS) |
107 | { |
108 | back_in_main = 1; |
109 | exit (EXIT_SUCCESS); |
110 | } |
111 | |
112 | printf (format: "%s: getcontext: %m\n" , __FUNCTION__); |
113 | exit (EXIT_FAILURE); |
114 | } |
115 | |
116 | /* Play some tricks with this context. */ |
117 | if (++global == 1) |
118 | if (setcontext (&ctx[1]) != 0) |
119 | { |
120 | printf (format: "%s: setcontext: %m\n" , __FUNCTION__); |
121 | exit (EXIT_FAILURE); |
122 | } |
123 | if (global != 2) |
124 | { |
125 | printf (format: "%s: 'global' not incremented twice\n" , __FUNCTION__); |
126 | exit (EXIT_FAILURE); |
127 | } |
128 | |
129 | ctx[1].uc_stack.ss_sp = st1; |
130 | ctx[1].uc_stack.ss_size = sizeof st1; |
131 | ctx[1].uc_link = &ctx[0]; |
132 | { |
133 | ucontext_t tempctx = ctx[1]; |
134 | makecontext (ucp: &ctx[1], func: (void (*) (void)) f1, argc: 4, 1, 2, 3, -4); |
135 | |
136 | /* Without this check, a stub makecontext can make us spin forever. */ |
137 | if (memcmp (&tempctx, &ctx[1], sizeof ctx[1]) == 0) |
138 | { |
139 | puts (s: "makecontext was a no-op, presuming not implemented" ); |
140 | return 0; |
141 | } |
142 | } |
143 | |
144 | if (getcontext (ucp: &ctx[2]) != 0) |
145 | { |
146 | printf (format: "%s: second getcontext: %m\n" , __FUNCTION__); |
147 | exit (EXIT_FAILURE); |
148 | } |
149 | ctx[2].uc_stack.ss_sp = st2; |
150 | ctx[2].uc_stack.ss_size = sizeof st2; |
151 | ctx[2].uc_link = &ctx[1]; |
152 | makecontext (ucp: &ctx[2], func: f2, argc: 0); |
153 | |
154 | puts (s: "swapping contexts" ); |
155 | if (swapcontext (oucp: &ctx[0], ucp: &ctx[2]) != 0) |
156 | { |
157 | printf (format: "%s: swapcontext: %m\n" , __FUNCTION__); |
158 | exit (EXIT_FAILURE); |
159 | } |
160 | puts (s: "back at main program" ); |
161 | back_in_main = 1; |
162 | |
163 | if (was_in_f1 == 0) |
164 | { |
165 | puts (s: "didn't reach f1" ); |
166 | exit (EXIT_FAILURE); |
167 | } |
168 | if (was_in_f2 == 0) |
169 | { |
170 | puts (s: "didn't reach f2" ); |
171 | exit (EXIT_FAILURE); |
172 | } |
173 | |
174 | puts (s: "test succeeded" ); |
175 | return 0; |
176 | } |
177 | |
178 | #include <support/test-driver.c> |
179 | |