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.
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/>. */
27 static ucontext_t ctx
[3];
28 static jmp_buf jmpbuf
;
32 static int longjmp_called
;
34 static char st2
[32768];
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");
47 if (swapcontext (&ctx
[1], &ctx
[2]) != 0)
49 printf ("%s: swapcontext: %m\n", __FUNCTION__
);
57 __attribute__ ((noinline
, noclone
))
69 if (setjmp (jmpbuf
) == 0)
74 if (swapcontext (&ctx
[2], &ctx
[1]) != 0)
76 printf ("%s: swapcontext: %m\n", __FUNCTION__
);
84 static int back_in_main
;
89 if (back_in_main
== 0)
91 puts ("program did not reach main again");
99 atexit (check_called
);
103 puts ("making contexts");
104 if (getcontext (&ctx
[1]) != 0)
112 printf ("%s: getcontext: %m\n", __FUNCTION__
);
116 /* Play some tricks with this context. */
118 if (setcontext (&ctx
[1]) != 0)
120 printf ("%s: setcontext: %m\n", __FUNCTION__
);
125 printf ("%s: 'global' not incremented twice\n", __FUNCTION__
);
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");
144 if (getcontext (&ctx
[2]) != 0)
146 printf ("%s: second getcontext: %m\n", __FUNCTION__
);
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__
);
160 puts ("back at main program");
165 puts ("didn't reach f1");
170 puts ("didn't reach f2");
174 puts ("test succeeded");
178 #include <support/test-driver.c>