Fix unwind info in x86 strcmp-sse4.S
[glibc.git] / stdlib / tst-setcontext.c
blob9a6d79ba9d618706f00ab382ea4ae571eb77d06e
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, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <errno.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <ucontext.h>
24 #include <unistd.h>
26 static ucontext_t ctx[3];
28 static int was_in_f1;
29 static int was_in_f2;
31 static char st2[32768];
33 static void
34 f1 (int a0, int a1, int a2, int a3)
36 printf ("start f1(a0=%x,a1=%x,a2=%x,a3=%x)\n", a0, a1, a2, a3);
38 if (a0 != 1 || a1 != 2 || a2 != 3 || a3 != -4)
40 puts ("arg mismatch");
41 exit (-1);
44 if (swapcontext (&ctx[1], &ctx[2]) != 0)
46 printf ("%s: swapcontext: %m\n", __FUNCTION__);
47 exit (1);
49 puts ("finish f1");
50 was_in_f1 = 1;
53 static void
54 f2 (void)
56 char on_stack[1];
58 puts ("start f2");
60 printf ("&on_stack=%p\n", on_stack);
61 if (on_stack < st2 || on_stack >= st2 + sizeof (st2))
63 printf ("%s: memory stack is not where it belongs!", __FUNCTION__);
64 exit (1);
67 if (swapcontext (&ctx[2], &ctx[1]) != 0)
69 printf ("%s: swapcontext: %m\n", __FUNCTION__);
70 exit (1);
72 puts ("finish f2");
73 was_in_f2 = 1;
76 void
77 test_stack(volatile int a, volatile int b,
78 volatile int c, volatile int d)
80 volatile int e = 5;
81 volatile int f = 6;
82 ucontext_t uc;
84 /* Test for cases where getcontext is clobbering the callers
85 stack, including parameters. */
86 getcontext(&uc);
88 if (a != 1)
90 printf ("%s: getcontext clobbers parm a\n", __FUNCTION__);
91 exit (1);
94 if (b != 2)
96 printf ("%s: getcontext clobbers parm b\n", __FUNCTION__);
97 exit (1);
100 if (c != 3)
102 printf ("%s: getcontext clobbers parm c\n", __FUNCTION__);
103 exit (1);
106 if (d != 4)
108 printf ("%s: getcontext clobbers parm d\n", __FUNCTION__);
109 exit (1);
112 if (e != 5)
114 printf ("%s: getcontext clobbers varible e\n", __FUNCTION__);
115 exit (1);
118 if (f != 6)
120 printf ("%s: getcontext clobbers variable f\n", __FUNCTION__);
121 exit (1);
125 volatile int global;
128 static int back_in_main;
131 static void
132 check_called (void)
134 if (back_in_main == 0)
136 puts ("program did not reach main again");
137 _exit (1);
143 main (void)
145 atexit (check_called);
147 char st1[32768];
149 puts ("making contexts");
150 if (getcontext (&ctx[1]) != 0)
152 if (errno == ENOSYS)
154 back_in_main = 1;
155 exit (0);
158 printf ("%s: getcontext: %m\n", __FUNCTION__);
159 exit (1);
162 test_stack (1, 2, 3, 4);
164 /* Play some tricks with this context. */
165 if (++global == 1)
166 if (setcontext (&ctx[1]) != 0)
168 printf ("%s: setcontext: %m\n", __FUNCTION__);
169 exit (1);
171 if (global != 2)
173 printf ("%s: 'global' not incremented twice\n", __FUNCTION__);
174 exit (1);
177 ctx[1].uc_stack.ss_sp = st1;
178 ctx[1].uc_stack.ss_size = sizeof st1;
179 ctx[1].uc_link = &ctx[0];
181 ucontext_t tempctx = ctx[1];
182 makecontext (&ctx[1], (void (*) (void)) f1, 4, 1, 2, 3, -4);
184 /* Without this check, a stub makecontext can make us spin forever. */
185 if (memcmp (&tempctx, &ctx[1], sizeof ctx[1]) == 0)
187 puts ("makecontext was a no-op, presuming not implemented");
188 return 0;
192 if (getcontext (&ctx[2]) != 0)
194 printf ("%s: second getcontext: %m\n", __FUNCTION__);
195 exit (1);
197 ctx[2].uc_stack.ss_sp = st2;
198 ctx[2].uc_stack.ss_size = sizeof st2;
199 ctx[2].uc_link = &ctx[1];
200 makecontext (&ctx[2], f2, 0);
202 puts ("swapping contexts");
203 if (swapcontext (&ctx[0], &ctx[2]) != 0)
205 printf ("%s: swapcontext: %m\n", __FUNCTION__);
206 exit (1);
208 puts ("back at main program");
209 back_in_main = 1;
211 if (was_in_f1 == 0)
213 puts ("didn't reach f1");
214 exit (1);
216 if (was_in_f2 == 0)
218 puts ("didn't reach f2");
219 exit (1);
222 puts ("test succeeded");
223 return 0;