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

source code of glibc/stdlib/tst-setcontext9.c