powerpc: Update libm-test-ulps
[glibc.git] / stdlib / tst-setcontext9.c
blob6e541ebd1b85187bdd6579918e5d1dad153604e7
1 /* Check setcontext on the context from makecontext.
2 Copyright (C) 2018-2021 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 <stdio.h>
20 #include <stdlib.h>
21 #include <ucontext.h>
22 #include <unistd.h>
23 #include <stdatomic.h>
25 static ucontext_t ctx[5];
26 static atomic_int done;
28 static void
29 __attribute__((noinline, noclone))
30 f2 (void)
32 done++;
33 puts ("swap contexts in f2");
34 if (swapcontext (&ctx[4], &ctx[2]) != 0)
36 printf ("%s: setcontext: %m\n", __FUNCTION__);
37 exit (EXIT_FAILURE);
39 puts ("end f2");
40 exit (done == 2 ? EXIT_SUCCESS : EXIT_FAILURE);
43 static void
44 f1b (void)
46 if (done)
48 puts ("set context in f1b");
49 if (setcontext (&ctx[3]) != 0)
51 printf ("%s: setcontext: %m\n", __FUNCTION__);
52 exit (EXIT_FAILURE);
55 exit (EXIT_FAILURE);
58 static void
59 f1a (void)
61 static char st2[32768];
62 puts ("start f1a");
63 if (getcontext (&ctx[2]) != 0)
65 printf ("%s: getcontext: %m\n", __FUNCTION__);
66 exit (EXIT_FAILURE);
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);
72 f2 ();
75 /* The execution path through the test looks like this:
76 do_test (call)
77 -> "making contexts"
78 -> "swap contexts"
79 f1a (via swapcontext to ctx[1], with alternate stack)
80 -> "start f1a"
81 f2 (call)
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)
86 -> "setcontext"
87 f2 (via setcontext to ctx[4], with alternate stack)
88 -> "end f2"
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. */
93 static int
94 do_test (void)
96 static char st1[32768];
97 puts ("making contexts");
98 if (getcontext (&ctx[0]) != 0)
100 printf ("%s: getcontext: %m\n", __FUNCTION__);
101 exit (EXIT_FAILURE);
103 if (getcontext (&ctx[1]) != 0)
105 printf ("%s: getcontext: %m\n", __FUNCTION__);
106 exit (EXIT_FAILURE);
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__);
116 exit (EXIT_FAILURE);
118 if (done != 1)
119 exit (EXIT_FAILURE);
120 done++;
121 puts ("set context");
122 if (setcontext (&ctx[4]) != 0)
124 printf ("%s: setcontext: %m\n", __FUNCTION__);
125 exit (EXIT_FAILURE);
127 exit (EXIT_FAILURE);
130 #include <support/test-driver.c>