]>
Commit | Line | Data |
---|---|---|
49b4de21 L |
1 | /* Check setjmp/longjmp within user context. |
2 | Copyright (C) 2023 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 ("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 ("arg mismatch"); | |
44 | exit (EXIT_FAILURE); | |
45 | } | |
46 | ||
47 | if (swapcontext (&ctx[1], &ctx[2]) != 0) | |
48 | { | |
49 | printf ("%s: swapcontext: %m\n", __FUNCTION__); | |
50 | exit (EXIT_FAILURE); | |
51 | } | |
52 | puts ("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 ("start f2"); | |
74 | if (swapcontext (&ctx[2], &ctx[1]) != 0) | |
75 | { | |
76 | printf ("%s: swapcontext: %m\n", __FUNCTION__); | |
77 | exit (EXIT_FAILURE); | |
78 | } | |
79 | puts ("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 ("program did not reach main again"); | |
92 | _exit (EXIT_FAILURE); | |
93 | } | |
94 | } | |
95 | ||
96 | static int | |
97 | do_test (void) | |
98 | { | |
99 | atexit (check_called); | |
100 | ||
101 | char st1[32768]; | |
102 | ||
103 | puts ("making contexts"); | |
104 | if (getcontext (&ctx[1]) != 0) | |
105 | { | |
106 | if (errno == ENOSYS) | |
107 | { | |
108 | back_in_main = 1; | |
109 | exit (EXIT_SUCCESS); | |
110 | } | |
111 | ||
112 | printf ("%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 ("%s: setcontext: %m\n", __FUNCTION__); | |
121 | exit (EXIT_FAILURE); | |
122 | } | |
123 | if (global != 2) | |
124 | { | |
125 | printf ("%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 (&ctx[1], (void (*) (void)) f1, 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 ("makecontext was a no-op, presuming not implemented"); | |
140 | return 0; | |
141 | } | |
142 | } | |
143 | ||
144 | if (getcontext (&ctx[2]) != 0) | |
145 | { | |
146 | printf ("%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 (&ctx[2], f2, 0); | |
153 | ||
154 | puts ("swapping contexts"); | |
155 | if (swapcontext (&ctx[0], &ctx[2]) != 0) | |
156 | { | |
157 | printf ("%s: swapcontext: %m\n", __FUNCTION__); | |
158 | exit (EXIT_FAILURE); | |
159 | } | |
160 | puts ("back at main program"); | |
161 | back_in_main = 1; | |
162 | ||
163 | if (was_in_f1 == 0) | |
164 | { | |
165 | puts ("didn't reach f1"); | |
166 | exit (EXIT_FAILURE); | |
167 | } | |
168 | if (was_in_f2 == 0) | |
169 | { | |
170 | puts ("didn't reach f2"); | |
171 | exit (EXIT_FAILURE); | |
172 | } | |
173 | ||
174 | puts ("test succeeded"); | |
175 | return 0; | |
176 | } | |
177 | ||
178 | #include <support/test-driver.c> |