1 /* Check setcontext on the context from makecontext.
2 Copyright (C) 2018-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/>. */
23 #include <stdatomic.h>
25 static ucontext_t ctx
[5];
26 static atomic_int done
;
29 __attribute__((noinline
, noclone
))
33 puts ("swap contexts in f2");
34 if (swapcontext (&ctx
[4], &ctx
[2]) != 0)
36 printf ("%s: setcontext: %m\n", __FUNCTION__
);
40 exit (done
== 2 ? EXIT_SUCCESS
: EXIT_FAILURE
);
48 puts ("set context in f1b");
49 if (setcontext (&ctx
[3]) != 0)
51 printf ("%s: setcontext: %m\n", __FUNCTION__
);
61 static char st2
[32768];
63 if (getcontext (&ctx
[2]) != 0)
65 printf ("%s: getcontext: %m\n", __FUNCTION__
);
68 ctx
[2].uc_stack
.ss_sp
= st2
;
69 ctx
[2].uc_stack
.ss_size
= sizeof st2
;
70 ctx
[2].uc_link
= &ctx
[0];
71 makecontext (&ctx
[2], (void (*) (void)) f1b
, 0);
75 /* The execution path through the test looks like this:
79 f1a (via swapcontext to ctx[1], with alternate stack)
82 -> "swap contexts in f2"
83 f1b (via swapcontext to ctx[2], with alternate stack)
84 -> "set context in f1b"
85 do_test (via setcontext to ctx[3], main stack)
87 f2 (via setcontext to ctx[4], with alternate stack)
90 We must use an alternate stack for f1b, because if we don't then the
91 result of executing an earlier caller may overwrite registers
92 spilled to the stack in f2. */
96 static char st1
[32768];
97 puts ("making contexts");
98 if (getcontext (&ctx
[0]) != 0)
100 printf ("%s: getcontext: %m\n", __FUNCTION__
);
103 if (getcontext (&ctx
[1]) != 0)
105 printf ("%s: getcontext: %m\n", __FUNCTION__
);
108 ctx
[1].uc_stack
.ss_sp
= st1
;
109 ctx
[1].uc_stack
.ss_size
= sizeof st1
;
110 ctx
[1].uc_link
= &ctx
[0];
111 makecontext (&ctx
[1], (void (*) (void)) f1a
, 0);
112 puts ("swap contexts");
113 if (swapcontext (&ctx
[3], &ctx
[1]) != 0)
115 printf ("%s: setcontext: %m\n", __FUNCTION__
);
121 puts ("set context");
122 if (setcontext (&ctx
[4]) != 0)
124 printf ("%s: setcontext: %m\n", __FUNCTION__
);
130 #include <support/test-driver.c>