1 | /* Copyright (C) 2001,02, 2004 Free Software Foundation, Inc.
|
---|
2 | This file is part of the GNU C Library.
|
---|
3 |
|
---|
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.
|
---|
8 |
|
---|
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.
|
---|
13 |
|
---|
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. */
|
---|
18 |
|
---|
19 | #include <errno.h>
|
---|
20 | #include <stdio.h>
|
---|
21 | #include <stdlib.h>
|
---|
22 | #include <string.h>
|
---|
23 | #include <ucontext.h>
|
---|
24 |
|
---|
25 | static ucontext_t ctx[3];
|
---|
26 |
|
---|
27 | static int was_in_f1;
|
---|
28 | static int was_in_f2;
|
---|
29 |
|
---|
30 | static char st2[32768];
|
---|
31 |
|
---|
32 | static void
|
---|
33 | f1 (long a0, long a1, long a2, long a3)
|
---|
34 | {
|
---|
35 | printf ("start f1(a0=%lx,a1=%lx,a2=%lx,a3=%lx)\n", a0, a1, a2, a3);
|
---|
36 |
|
---|
37 | if (a0 != 1 || a1 != 2 || a2 != 3 || a3 != -4)
|
---|
38 | {
|
---|
39 | puts ("arg mismatch");
|
---|
40 | exit (-1);
|
---|
41 | }
|
---|
42 |
|
---|
43 | if (swapcontext (&ctx[1], &ctx[2]) != 0)
|
---|
44 | {
|
---|
45 | printf ("%s: swapcontext: %m\n", __FUNCTION__);
|
---|
46 | exit (1);
|
---|
47 | }
|
---|
48 | puts ("finish f1");
|
---|
49 | was_in_f1 = 1;
|
---|
50 | }
|
---|
51 |
|
---|
52 | static void
|
---|
53 | f2 (void)
|
---|
54 | {
|
---|
55 | char on_stack[1];
|
---|
56 |
|
---|
57 | puts ("start f2");
|
---|
58 |
|
---|
59 | printf ("&on_stack=%p\n", on_stack);
|
---|
60 | if (on_stack < st2 || on_stack >= st2 + sizeof (st2))
|
---|
61 | {
|
---|
62 | printf ("%s: memory stack is not where it belongs!", __FUNCTION__);
|
---|
63 | exit (1);
|
---|
64 | }
|
---|
65 |
|
---|
66 | if (swapcontext (&ctx[2], &ctx[1]) != 0)
|
---|
67 | {
|
---|
68 | printf ("%s: swapcontext: %m\n", __FUNCTION__);
|
---|
69 | exit (1);
|
---|
70 | }
|
---|
71 | puts ("finish f2");
|
---|
72 | was_in_f2 = 1;
|
---|
73 | }
|
---|
74 |
|
---|
75 | void
|
---|
76 | test_stack(volatile int a, volatile int b,
|
---|
77 | volatile int c, volatile int d)
|
---|
78 | {
|
---|
79 | volatile int e = 5;
|
---|
80 | volatile int f = 6;
|
---|
81 | ucontext_t uc;
|
---|
82 |
|
---|
83 | /* Test for cases where getcontext is clobbering the callers
|
---|
84 | stack, including parameters. */
|
---|
85 | getcontext(&uc);
|
---|
86 |
|
---|
87 | if (a != 1)
|
---|
88 | {
|
---|
89 | printf ("%s: getcontext clobbers parm a\n", __FUNCTION__);
|
---|
90 | exit (1);
|
---|
91 | }
|
---|
92 |
|
---|
93 | if (b != 2)
|
---|
94 | {
|
---|
95 | printf ("%s: getcontext clobbers parm b\n", __FUNCTION__);
|
---|
96 | exit (1);
|
---|
97 | }
|
---|
98 |
|
---|
99 | if (c != 3)
|
---|
100 | {
|
---|
101 | printf ("%s: getcontext clobbers parm c\n", __FUNCTION__);
|
---|
102 | exit (1);
|
---|
103 | }
|
---|
104 |
|
---|
105 | if (d != 4)
|
---|
106 | {
|
---|
107 | printf ("%s: getcontext clobbers parm d\n", __FUNCTION__);
|
---|
108 | exit (1);
|
---|
109 | }
|
---|
110 |
|
---|
111 | if (e != 5)
|
---|
112 | {
|
---|
113 | printf ("%s: getcontext clobbers varible e\n", __FUNCTION__);
|
---|
114 | exit (1);
|
---|
115 | }
|
---|
116 |
|
---|
117 | if (f != 6)
|
---|
118 | {
|
---|
119 | printf ("%s: getcontext clobbers variable f\n", __FUNCTION__);
|
---|
120 | exit (1);
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | volatile int global;
|
---|
125 |
|
---|
126 | int
|
---|
127 | main (void)
|
---|
128 | {
|
---|
129 | char st1[32768];
|
---|
130 |
|
---|
131 | puts ("making contexts");
|
---|
132 | if (getcontext (&ctx[1]) != 0)
|
---|
133 | {
|
---|
134 | if (errno == ENOSYS)
|
---|
135 | exit (0);
|
---|
136 |
|
---|
137 | printf ("%s: getcontext: %m\n", __FUNCTION__);
|
---|
138 | exit (1);
|
---|
139 | }
|
---|
140 |
|
---|
141 | test_stack (1, 2, 3, 4);
|
---|
142 |
|
---|
143 | /* Play some tricks with this context. */
|
---|
144 | if (++global == 1)
|
---|
145 | if (setcontext (&ctx[1]) != 0)
|
---|
146 | {
|
---|
147 | printf ("%s: setcontext: %m\n", __FUNCTION__);
|
---|
148 | exit (1);
|
---|
149 | }
|
---|
150 | if (global != 2)
|
---|
151 | {
|
---|
152 | printf ("%s: 'global' not incremented twice\n", __FUNCTION__);
|
---|
153 | exit (1);
|
---|
154 | }
|
---|
155 |
|
---|
156 | ctx[1].uc_stack.ss_sp = st1;
|
---|
157 | ctx[1].uc_stack.ss_size = sizeof st1;
|
---|
158 | ctx[1].uc_link = &ctx[0];
|
---|
159 | {
|
---|
160 | ucontext_t tempctx = ctx[1];
|
---|
161 | makecontext (&ctx[1], (void (*) (void)) f1, 4, 1, 2, 3, -4);
|
---|
162 |
|
---|
163 | /* Without this check, a stub makecontext can make us spin forever. */
|
---|
164 | if (memcmp (&tempctx, &ctx[1], sizeof ctx[1]) == 0)
|
---|
165 | {
|
---|
166 | puts ("makecontext was a no-op, presuming not implemented");
|
---|
167 | return 0;
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | if (getcontext (&ctx[2]) != 0)
|
---|
172 | {
|
---|
173 | printf ("%s: second getcontext: %m\n", __FUNCTION__);
|
---|
174 | exit (1);
|
---|
175 | }
|
---|
176 | ctx[2].uc_stack.ss_sp = st2;
|
---|
177 | ctx[2].uc_stack.ss_size = sizeof st2;
|
---|
178 | ctx[2].uc_link = &ctx[1];
|
---|
179 | makecontext (&ctx[2], f2, 0);
|
---|
180 |
|
---|
181 | puts ("swapping contexts");
|
---|
182 | if (swapcontext (&ctx[0], &ctx[2]) != 0)
|
---|
183 | {
|
---|
184 | printf ("%s: swapcontext: %m\n", __FUNCTION__);
|
---|
185 | exit (1);
|
---|
186 | }
|
---|
187 | puts ("back at main program");
|
---|
188 |
|
---|
189 | if (was_in_f1 == 0)
|
---|
190 | {
|
---|
191 | puts ("didn't reach f1");
|
---|
192 | exit (1);
|
---|
193 | }
|
---|
194 | if (was_in_f2 == 0)
|
---|
195 | {
|
---|
196 | puts ("didn't reach f2");
|
---|
197 | exit (1);
|
---|
198 | }
|
---|
199 |
|
---|
200 | puts ("test succeeded");
|
---|
201 | return 0;
|
---|
202 | }
|
---|