t40c term[] count fix
[minix.git] / test / test51.c
blobab19902523c3951f2eff085e557b98dd54a3ba03
1 /* Test51.c
3 * Test getcontext, setcontext, makecontext, and swapcontext system calls.
5 * Part of this test is somewhat based on the GNU GCC ucontext test set.
7 */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <errno.h>
13 #include <string.h>
14 #include <ucontext.h>
15 #include <math.h>
16 #include <fenv.h>
18 #include <sys/signal.h>
20 void do_calcs(void);
21 void do_child(void);
22 void do_parent(void);
23 void err(int subtest, int error_no);
24 void func1(int a, int b, int c, int d, int e, int f, int g, int h, int
25 i, int j, int k, int l, int m, int n, int o, int p, int q, int r, int s,
26 int t, int u, int v, int w, int x, int y, int z, int aa, int bb);
27 void func2(void);
28 void just_exit(void);
29 void test_brk(void);
30 void verify_main_reenter(void);
32 #define MAX_ERROR 5
33 #define SSIZE 32768
34 #define ROUNDS 10
35 #define SWAPS 10
37 #include "common.c"
39 int subtest;
40 ucontext_t ctx[3];
41 int entered_func1, entered_func2, reentered_main, entered_overflow;
43 static char st_stack[SSIZE];
44 static volatile int shift, global;
46 void do_calcs(void)
48 float a, b, c, d, e;
49 float foo, bar;
50 int i;
52 a = 1.1;
53 b = 2.2;
54 c = 3.3;
55 d = 4.4;
56 e = 5.5;
58 foo = a * b; /* 2.42 */
59 bar = c * d; /* 14.52 */
61 i = 0;
62 while(i < ROUNDS) {
63 foo += c; /* 5.72 */
64 foo *= d; /* 25.168 */
65 foo /= e; /* 4.5760 */
66 bar -= a; /* 13.42 */
67 bar *= b; /* 29.524 */
68 bar /= e; /* 5.3680 */
70 /* Undo */
71 foo *= e;
72 foo /= d;
73 foo -= c;
75 bar *= e;
76 bar /= b;
77 bar += a;
79 i++;
81 if(fabs(foo - (a * b)) > 0.0001) err(8, 1);
82 if(fabs(bar - (c * d)) > 0.0001) err(8, 2);
85 void do_child(void)
87 int s;
88 s = 1;
90 /* Initialize FPU context and verify it's set to round to nearest. */
91 if (fegetround() != FE_TONEAREST) err(9, 1);
93 /* Now we change the rounding to something else, and this should be preserved
94 between context swaps. */
95 if (fesetround(FE_DOWNWARD) != 0) err(9, 2);
97 while(s < SWAPS) {
98 s++;
99 if (swapcontext(&ctx[2], &ctx[1]) == -1) err(9, 2);
100 do_calcs();
101 if (fegetround() != FE_DOWNWARD) err(9, 4);
103 quit();
106 void do_parent(void)
108 ucontext_t dummy;
109 int s;
110 s = 1;
112 /* Initialize FPU context and verify it's set to round to nearest. */
113 if (fegetround() != FE_TONEAREST) err(10, 1);
115 /* Now we change the rounding to something else, and this should be preserved
116 between context swaps. */
117 if (fesetround(FE_UPWARD) != 0) err(10, 2);
119 /* Quick check to make sure that getcontext does not reset the FPU state. */
120 getcontext(&dummy);
122 if (fegetround() != FE_UPWARD) err(10, 3);
124 while(s < SWAPS) {
125 do_calcs();
126 if (fegetround() != FE_UPWARD) err(10, 4);
127 s++;
128 if (swapcontext(&ctx[1], &ctx[2]) == -1) err(10, 5);
130 /* Returning to main thread through uc_link */
133 void fail(void)
135 /* Shouldn't get here */
136 err(5, 1);
139 void func1(int a, int b, int c, int d, int e, int f, int g,
140 int h, int i, int j, int k, int l, int m, int n,
141 int o, int p, int q, int r, int s, int t, int u,
142 int v, int w, int x, int y, int z, int aa, int bb)
144 if ( a != (0x0000001 << shift) || b != (0x0000004 << shift) ||
145 c != (0x0000010 << shift) || d != (0x0000040 << shift) ||
146 e != (0x0000100 << shift) || f != (0x0000400 << shift) ||
147 g != (0x0001000 << shift) || h != (0x0004000 << shift) ||
148 i != (0x0010000 << shift) || j != (0x0040000 << shift) ||
149 k != (0x0100000 << shift) || l != (0x0400000 << shift) ||
150 m != (0x1000000 << shift) || n != (0x4000000 << shift) ||
151 o != (0x0000002 << shift) || p != (0x0000008 << shift) ||
152 q != (0x0000020 << shift) || r != (0x0000080 << shift) ||
153 s != (0x0000200 << shift) || t != (0x0000800 << shift) ||
154 u != (0x0002000 << shift) || v != (0x0008000 << shift) ||
155 w != (0x0020000 << shift) || x != (0x0080000 << shift) ||
156 y != (0x0200000 << shift) || z != (0x0800000 << shift) ||
157 aa != (0x2000000 << shift) || bb != (0x8000000 << shift) ) {
158 err(2, 1);
161 if (shift && swapcontext (&ctx[1], &ctx[2]) != 0) err(2, 2);
162 shift++;
163 entered_func1++;
166 void func2(void)
168 if (swapcontext(&ctx[2], &ctx[1]) != 0) err(3, 1);
169 entered_func2++;
172 void just_exit(void)
174 if (errct == 0) printf("ok\n");
175 _exit(1);
178 void test_brk(void)
180 char *big_stack;
182 big_stack = malloc(16 * 1024 * 1024); /* 16 MB */
183 /* If this fails, it is likely brk system call failed due stack/data segments
184 collision detection. Unless the system really is low on memory, this is an
185 error. */
186 if (big_stack == NULL) err(7, 1);
189 void verify_main_reenter(void)
191 if (reentered_main == 0) err(4, 1);
195 int main(void)
197 start(51);
199 atexit(verify_main_reenter);
201 /* Save current context in ctx[0] */
202 if (getcontext(&ctx[0]) != 0) {
203 /* Don't verify reentering main, not going to happen */
204 atexit(just_exit);
205 err(1, 1);
208 ctx[1] = ctx[0];
209 ctx[1].uc_stack.ss_sp = st_stack;
210 ctx[1].uc_stack.ss_size = SSIZE;
211 ctx[1].uc_link = &ctx[0]; /* When done running, return here */
213 /* ctx[1] is going to run func1 and then return here (uc_link). */
214 /* We'll see later on whether makecontext worked. */
215 makecontext(&ctx[1], (void (*) (void)) func1, 28,
216 (0x0000001 << shift), (0x0000004 << shift),
217 (0x0000010 << shift), (0x0000040 << shift),
218 (0x0000100 << shift), (0x0000400 << shift),
219 (0x0001000 << shift), (0x0004000 << shift),
220 (0x0010000 << shift), (0x0040000 << shift),
221 (0x0100000 << shift), (0x0400000 << shift),
222 (0x1000000 << shift), (0x4000000 << shift),
223 (0x0000002 << shift), (0x0000008 << shift),
224 (0x0000020 << shift), (0x0000080 << shift),
225 (0x0000200 << shift), (0x0000800 << shift),
226 (0x0002000 << shift), (0x0008000 << shift),
227 (0x0020000 << shift), (0x0080000 << shift),
228 (0x0200000 << shift), (0x0800000 << shift),
229 (0x2000000 << shift), (0x8000000 << shift));
231 if (++global == 1) {
232 /* First time we're here. Let's run ctx[1] and return to ctx[0] when
233 * we're done. Note that we return to above the 'makecontext' call. */
234 if (setcontext(&ctx[1]) != 0) err(1, 2);
236 if (global != 2) {
237 /* When ++global was 1 we let ctx[1] run and returned to ctx[0], so
238 above ++global is executed again and should've become 2. */
239 err(1, 3);
242 /* Setup ctx[2] to run func2 */
243 if (getcontext(&ctx[2]) != 0) err(1, 4);
244 ctx[2].uc_stack.ss_sp = malloc(SSIZE);
245 ctx[2].uc_stack.ss_size = SSIZE;
246 ctx[2].uc_link = &ctx[1];
247 makecontext(&ctx[2], (void (*) (void)) func2, 0);
249 /* Now things become tricky. ctx[2] is set up such that when it finishes
250 running, and starts ctx[1] again. However, func1 swaps back to func2. Then,
251 when func2 has finished running, we continue with ctx[1] and, finally, we
252 return to ctx[0]. */
253 if (swapcontext(&ctx[0], &ctx[2]) != 0) err(1, 5); /* makecontext failed? */
254 reentered_main = 1;
256 /* The call graph is as follows:
258 * ########
259 * /--------># main #
260 * 7 /----########----\
261 * | | ^ |
262 * | 1 2 3
263 * | V | V
264 * #########----/ #########
265 * # func1 #<-------4-------# func2 #
266 * #########--------5------>#########
267 * ^ |
268 * | |
269 * \---------6--------/
271 * Main calls func1, func1 increases entered_func1, and returns to main. Main
272 * calls func2, swaps to func1, swaps to func2, which increases entered_func2,
273 * continues with func1, which increases entered_func1 again, continues to
274 * main, where reentered_main is set to 1. In effect, entered_func1 == 2,
275 * entered_func2 == 1, reentered_main == 1. Verify that. */
277 if (entered_func1 != 2) err(1, 6);
278 if (entered_func2 != 1) err(1, 7);
279 /* reentered_main == 1 is verified upon exit */
281 /* Try to allocate too small a stack */
282 free(ctx[2].uc_stack.ss_sp); /* Deallocate stack space first */
283 if (getcontext(&ctx[2]) != 0) err(1, 8);
284 ctx[2].uc_stack.ss_sp = malloc(MINSIGSTKSZ-1);
285 ctx[2].uc_stack.ss_size = MINSIGSTKSZ-1;
286 ctx[2].uc_link = &ctx[0];
287 makecontext(&ctx[2], (void (*) (void)) fail, 0);
288 /* Because makecontext is void, we can only detect an error by trying to use
289 the invalid context */
290 if (swapcontext(&ctx[0], &ctx[2]) == 0) err(1, 9);
292 /* Try to allocate a huge stack to force the usage of brk/sbrk system call
293 to enlarge the data segment. Because we are fiddling with the stack
294 pointer, the OS might think the stack segment and data segment have
295 collided and kill us. This is wrong and therefore the following should
296 work. */
297 free(ctx[2].uc_stack.ss_sp); /* Deallocate stack space first */
298 if (getcontext(&ctx[2]) != 0) err(1, 14);
299 ctx[2].uc_stack.ss_sp = malloc(8 * 1024 * 1024); /* 8 MB */
300 ctx[2].uc_stack.ss_size = 8 * 1024 * 1024;
301 ctx[2].uc_link = &ctx[0];
302 makecontext(&ctx[2], (void (*) (void)) test_brk, 0);
303 if (swapcontext(&ctx[0], &ctx[2]) != 0) err(1, 15);
305 ctx[1].uc_link = &ctx[0];
306 ctx[2].uc_link = NULL;
307 makecontext(&ctx[1], (void (*) (void)) do_parent, 0);
308 makecontext(&ctx[2], (void (*) (void)) do_child, 0);
309 if (swapcontext(&ctx[0], &ctx[2]) == -1) err(1, 16);
311 quit();
312 return(-1);
315 /* We can't use a global subtest variable reliably, because threads might
316 change the value when we reenter a thread (i.e., report wrong subtest
317 number). */
318 void err(int sub, int error_no)
320 subtest = sub;
321 e(error_no);