Fix the order of my ChangeLog entries
[glibc.git] / stdlib / tst-setcontext.c
blob5b012b98e112924a53493b9c910ab01a495d13de
1 /* Copyright (C) 2001,2002,2004,2006,2007,2008 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
18 #include <errno.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <ucontext.h>
23 #include <unistd.h>
25 static ucontext_t ctx[3];
27 static int was_in_f1;
28 static int was_in_f2;
30 static char st2[32768];
32 static void
33 f1 (int a0, int a1, int a2, int a3)
35 printf ("start f1(a0=%x,a1=%x,a2=%x,a3=%x)\n", a0, a1, a2, a3);
37 if (a0 != 1 || a1 != 2 || a2 != 3 || a3 != -4)
39 puts ("arg mismatch");
40 exit (-1);
43 if (swapcontext (&ctx[1], &ctx[2]) != 0)
45 printf ("%s: swapcontext: %m\n", __FUNCTION__);
46 exit (1);
48 puts ("finish f1");
49 was_in_f1 = 1;
52 static void
53 f2 (void)
55 char on_stack[1];
57 puts ("start f2");
59 printf ("&on_stack=%p\n", on_stack);
60 if (on_stack < st2 || on_stack >= st2 + sizeof (st2))
62 printf ("%s: memory stack is not where it belongs!", __FUNCTION__);
63 exit (1);
66 if (swapcontext (&ctx[2], &ctx[1]) != 0)
68 printf ("%s: swapcontext: %m\n", __FUNCTION__);
69 exit (1);
71 puts ("finish f2");
72 was_in_f2 = 1;
75 void
76 test_stack(volatile int a, volatile int b,
77 volatile int c, volatile int d)
79 volatile int e = 5;
80 volatile int f = 6;
81 ucontext_t uc;
83 /* Test for cases where getcontext is clobbering the callers
84 stack, including parameters. */
85 getcontext(&uc);
87 if (a != 1)
89 printf ("%s: getcontext clobbers parm a\n", __FUNCTION__);
90 exit (1);
93 if (b != 2)
95 printf ("%s: getcontext clobbers parm b\n", __FUNCTION__);
96 exit (1);
99 if (c != 3)
101 printf ("%s: getcontext clobbers parm c\n", __FUNCTION__);
102 exit (1);
105 if (d != 4)
107 printf ("%s: getcontext clobbers parm d\n", __FUNCTION__);
108 exit (1);
111 if (e != 5)
113 printf ("%s: getcontext clobbers varible e\n", __FUNCTION__);
114 exit (1);
117 if (f != 6)
119 printf ("%s: getcontext clobbers variable f\n", __FUNCTION__);
120 exit (1);
124 volatile int global;
127 static int back_in_main;
130 static void
131 check_called (void)
133 if (back_in_main == 0)
135 puts ("program did not reach main again");
136 _exit (1);
142 main (void)
144 atexit (check_called);
146 char st1[32768];
148 puts ("making contexts");
149 if (getcontext (&ctx[1]) != 0)
151 if (errno == ENOSYS)
153 back_in_main = 1;
154 exit (0);
157 printf ("%s: getcontext: %m\n", __FUNCTION__);
158 exit (1);
161 test_stack (1, 2, 3, 4);
163 /* Play some tricks with this context. */
164 if (++global == 1)
165 if (setcontext (&ctx[1]) != 0)
167 printf ("%s: setcontext: %m\n", __FUNCTION__);
168 exit (1);
170 if (global != 2)
172 printf ("%s: 'global' not incremented twice\n", __FUNCTION__);
173 exit (1);
176 ctx[1].uc_stack.ss_sp = st1;
177 ctx[1].uc_stack.ss_size = sizeof st1;
178 ctx[1].uc_link = &ctx[0];
180 ucontext_t tempctx = ctx[1];
181 makecontext (&ctx[1], (void (*) (void)) f1, 4, 1, 2, 3, -4);
183 /* Without this check, a stub makecontext can make us spin forever. */
184 if (memcmp (&tempctx, &ctx[1], sizeof ctx[1]) == 0)
186 puts ("makecontext was a no-op, presuming not implemented");
187 return 0;
191 if (getcontext (&ctx[2]) != 0)
193 printf ("%s: second getcontext: %m\n", __FUNCTION__);
194 exit (1);
196 ctx[2].uc_stack.ss_sp = st2;
197 ctx[2].uc_stack.ss_size = sizeof st2;
198 ctx[2].uc_link = &ctx[1];
199 makecontext (&ctx[2], f2, 0);
201 puts ("swapping contexts");
202 if (swapcontext (&ctx[0], &ctx[2]) != 0)
204 printf ("%s: swapcontext: %m\n", __FUNCTION__);
205 exit (1);
207 puts ("back at main program");
208 back_in_main = 1;
210 if (was_in_f1 == 0)
212 puts ("didn't reach f1");
213 exit (1);
215 if (was_in_f2 == 0)
217 puts ("didn't reach f2");
218 exit (1);
221 puts ("test succeeded");
222 return 0;