1 /* Check setcontext on the context from makecontext.
2    Copyright (C) 2018-2022 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 <stdio.h>
20 #include <stdlib.h>
21 #include <ucontext.h>
22 #include <unistd.h>
23 #include <stdatomic.h>
24 
25 static ucontext_t ctx[5];
26 static atomic_int done;
27 
28 static void
29 __attribute__((noinline, noclone))
f2(void)30 f2 (void)
31 {
32   done++;
33   puts ("swap contexts in f2");
34   if (swapcontext (&ctx[4], &ctx[2]) != 0)
35     {
36       printf ("%s: setcontext: %m\n", __FUNCTION__);
37       exit (EXIT_FAILURE);
38     }
39   puts ("end f2");
40   exit (done == 2 ? EXIT_SUCCESS : EXIT_FAILURE);
41 }
42 
43 static void
f1b(void)44 f1b (void)
45 {
46   if (done)
47     {
48       puts ("set context in f1b");
49       if (setcontext (&ctx[3]) != 0)
50 	{
51 	  printf ("%s: setcontext: %m\n", __FUNCTION__);
52 	  exit (EXIT_FAILURE);
53 	}
54     }
55   exit (EXIT_FAILURE);
56 }
57 
58 static void
f1a(void)59 f1a (void)
60 {
61   static char st2[32768];
62   puts ("start f1a");
63   if (getcontext (&ctx[2]) != 0)
64     {
65       printf ("%s: getcontext: %m\n", __FUNCTION__);
66       exit (EXIT_FAILURE);
67     }
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 ();
73 }
74 
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"
89 
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
do_test(void)94 do_test (void)
95 {
96   static char st1[32768];
97   puts ("making contexts");
98   if (getcontext (&ctx[0]) != 0)
99     {
100       printf ("%s: getcontext: %m\n", __FUNCTION__);
101       exit (EXIT_FAILURE);
102     }
103   if (getcontext (&ctx[1]) != 0)
104     {
105       printf ("%s: getcontext: %m\n", __FUNCTION__);
106       exit (EXIT_FAILURE);
107     }
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)
114     {
115       printf ("%s: setcontext: %m\n", __FUNCTION__);
116       exit (EXIT_FAILURE);
117     }
118   if (done != 1)
119     exit (EXIT_FAILURE);
120   done++;
121   puts ("set context");
122   if (setcontext (&ctx[4]) != 0)
123     {
124       printf ("%s: setcontext: %m\n", __FUNCTION__);
125       exit (EXIT_FAILURE);
126     }
127   exit (EXIT_FAILURE);
128 }
129 
130 #include <support/test-driver.c>
131