socket: Improve fortify with clang
[glibc.git] / stdlib / tst-setcontext11.c
blob1883c44b760d9fe9a41e54a6f59d818aef856b3d
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.
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.
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.
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/>. */
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>
27 static ucontext_t ctx[3];
28 static jmp_buf jmpbuf;
30 static int was_in_f1;
31 static int was_in_f2;
32 static int longjmp_called;
34 static char st2[32768];
36 static void
37 f1 (int a0, int a1, int a2, int a3)
39 printf ("start f1(a0=%x,a1=%x,a2=%x,a3=%x)\n", a0, a1, a2, a3);
41 if (a0 != 1 || a1 != 2 || a2 != 3 || a3 != -4)
43 puts ("arg mismatch");
44 exit (EXIT_FAILURE);
47 if (swapcontext (&ctx[1], &ctx[2]) != 0)
49 printf ("%s: swapcontext: %m\n", __FUNCTION__);
50 exit (EXIT_FAILURE);
52 puts ("finish f1");
53 was_in_f1 = 1;
56 static void
57 __attribute__ ((noinline, noclone))
58 call_longjmp (void)
60 longjmp_called = 1;
61 longjmp (jmpbuf, 1);
64 static void
65 f2 (void)
67 if (!longjmp_called)
69 if (setjmp (jmpbuf) == 0)
70 call_longjmp ();
73 puts ("start f2");
74 if (swapcontext (&ctx[2], &ctx[1]) != 0)
76 printf ("%s: swapcontext: %m\n", __FUNCTION__);
77 exit (EXIT_FAILURE);
79 puts ("finish f2");
80 was_in_f2 = 1;
83 volatile int global;
84 static int back_in_main;
86 static void
87 check_called (void)
89 if (back_in_main == 0)
91 puts ("program did not reach main again");
92 _exit (EXIT_FAILURE);
96 static int
97 do_test (void)
99 atexit (check_called);
101 char st1[32768];
103 puts ("making contexts");
104 if (getcontext (&ctx[1]) != 0)
106 if (errno == ENOSYS)
108 back_in_main = 1;
109 exit (EXIT_SUCCESS);
112 printf ("%s: getcontext: %m\n", __FUNCTION__);
113 exit (EXIT_FAILURE);
116 /* Play some tricks with this context. */
117 if (++global == 1)
118 if (setcontext (&ctx[1]) != 0)
120 printf ("%s: setcontext: %m\n", __FUNCTION__);
121 exit (EXIT_FAILURE);
123 if (global != 2)
125 printf ("%s: 'global' not incremented twice\n", __FUNCTION__);
126 exit (EXIT_FAILURE);
129 ctx[1].uc_stack.ss_sp = st1;
130 ctx[1].uc_stack.ss_size = sizeof st1;
131 ctx[1].uc_link = &ctx[0];
133 ucontext_t tempctx = ctx[1];
134 makecontext (&ctx[1], (void (*) (void)) f1, 4, 1, 2, 3, -4);
136 /* Without this check, a stub makecontext can make us spin forever. */
137 if (memcmp (&tempctx, &ctx[1], sizeof ctx[1]) == 0)
139 puts ("makecontext was a no-op, presuming not implemented");
140 return 0;
144 if (getcontext (&ctx[2]) != 0)
146 printf ("%s: second getcontext: %m\n", __FUNCTION__);
147 exit (EXIT_FAILURE);
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);
154 puts ("swapping contexts");
155 if (swapcontext (&ctx[0], &ctx[2]) != 0)
157 printf ("%s: swapcontext: %m\n", __FUNCTION__);
158 exit (EXIT_FAILURE);
160 puts ("back at main program");
161 back_in_main = 1;
163 if (was_in_f1 == 0)
165 puts ("didn't reach f1");
166 exit (EXIT_FAILURE);
168 if (was_in_f2 == 0)
170 puts ("didn't reach f2");
171 exit (EXIT_FAILURE);
174 puts ("test succeeded");
175 return 0;
178 #include <support/test-driver.c>