3 * Test getcontext, setcontext, makecontext, and swapcontext system calls.
5 * Part of this test is somewhat based on the GNU GCC ucontext test set.
17 _PROTOTYPE( void do_calcs
, (void) );
18 _PROTOTYPE( void do_child
, (void) );
19 _PROTOTYPE( void do_parent
, (void) );
20 _PROTOTYPE( void err
, (int subtest
, int error_no
) );
21 _PROTOTYPE( void func1
, (int a
, int b
, int c
, int d
, int e
, int f
, int g
,
22 int h
, int i
, int j
, int k
, int l
, int m
, int n
,
23 int o
, int p
, int q
, int r
, int s
, int t
, int u
,
24 int v
, int w
, int x
, int y
, int z
, int aa
, int bb
));
25 _PROTOTYPE( void func2
, (void) );
26 _PROTOTYPE( void just_exit
, (void) );
27 _PROTOTYPE( void test_brk
, (void) );
28 _PROTOTYPE( void verify_main_reenter
, (void) );
37 int entered_func1
, entered_func2
, reentered_main
, entered_overflow
;
39 static char st_stack
[SSIZE
];
40 static volatile int shift
, global
;
54 foo
= a
* b
; /* 2.42 */
55 bar
= c
* d
; /* 14.52 */
60 foo
*= d
; /* 25.168 */
61 foo
/= e
; /* 4.5760 */
63 bar
*= b
; /* 29.524 */
64 bar
/= e
; /* 5.3680 */
77 if(fabs(foo
- (a
* b
)) > 0.0001) err(8, 1);
78 if(fabs(bar
- (c
* d
)) > 0.0001) err(8, 2);
86 /* Initialize FPU context and verify it's set to round to nearest. */
87 if (fegetround() != FE_TONEAREST
) err(9, 1);
89 /* Now we change the rounding to something else, and this should be preserved
90 between context swaps. */
91 if (fesetround(FE_DOWNWARD
) != 0) err(9, 2);
95 if (swapcontext(&ctx
[2], &ctx
[1]) == -1) err(9, 2);
97 if (fegetround() != FE_DOWNWARD
) err(9, 4);
106 /* Initialize FPU context and verify it's set to round to nearest. */
107 if (fegetround() != FE_TONEAREST
) err(10, 1);
109 /* Now we change the rounding to something else, and this should be preserved
110 between context swaps. */
111 if (fesetround(FE_UPWARD
) != 0) err(10, 2);
115 if (fegetround() != FE_UPWARD
) err(10, 3);
117 if (swapcontext(&ctx
[1], &ctx
[2]) == -1) err(10, 4);
119 /* Returning to main thread through uc_link */
124 /* Shouldn't get here */
128 void func1(int a
, int b
, int c
, int d
, int e
, int f
, int g
,
129 int h
, int i
, int j
, int k
, int l
, int m
, int n
,
130 int o
, int p
, int q
, int r
, int s
, int t
, int u
,
131 int v
, int w
, int x
, int y
, int z
, int aa
, int bb
)
133 if ( a
!= (0x0000001 << shift
) || b
!= (0x0000004 << shift
) ||
134 c
!= (0x0000010 << shift
) || d
!= (0x0000040 << shift
) ||
135 e
!= (0x0000100 << shift
) || f
!= (0x0000400 << shift
) ||
136 g
!= (0x0001000 << shift
) || h
!= (0x0004000 << shift
) ||
137 i
!= (0x0010000 << shift
) || j
!= (0x0040000 << shift
) ||
138 k
!= (0x0100000 << shift
) || l
!= (0x0400000 << shift
) ||
139 m
!= (0x1000000 << shift
) || n
!= (0x4000000 << shift
) ||
140 o
!= (0x0000002 << shift
) || p
!= (0x0000008 << shift
) ||
141 q
!= (0x0000020 << shift
) || r
!= (0x0000080 << shift
) ||
142 s
!= (0x0000200 << shift
) || t
!= (0x0000800 << shift
) ||
143 u
!= (0x0002000 << shift
) || v
!= (0x0008000 << shift
) ||
144 w
!= (0x0020000 << shift
) || x
!= (0x0080000 << shift
) ||
145 y
!= (0x0200000 << shift
) || z
!= (0x0800000 << shift
) ||
146 aa
!= (0x2000000 << shift
) || bb
!= (0x8000000 << shift
) ) {
150 if (shift
&& swapcontext (&ctx
[1], &ctx
[2]) != 0) err(2, 2);
157 if (swapcontext(&ctx
[2], &ctx
[1]) != 0) err(3, 1);
163 if (errct
== 0) printf("ok\n");
171 big_stack
= malloc(16 * 1024 * 1024); /* 16 MB */
172 /* If this fails, it is likely brk system call failed due stack/data segments
173 collision detection. Unless the system really is low on memory, this is an
175 if (big_stack
== NULL
) err(7, 1);
178 void verify_main_reenter(void)
180 if (reentered_main
== 0) err(4, 1);
181 if (errct
== 0) printf("ok\n");
188 printf("Test 51 (GCC) ");
190 printf("Test 51 (ACK) ");
194 atexit(verify_main_reenter
);
196 /* Save current context in ctx[0] */
197 if (getcontext(&ctx
[0]) != 0) {
198 /* Don't verify reentering main, not going to happen */
204 ctx
[1].uc_stack
.ss_sp
= st_stack
;
205 ctx
[1].uc_stack
.ss_size
= SSIZE
;
206 ctx
[1].uc_link
= &ctx
[0]; /* When done running, return here */
208 /* ctx[1] is going to run func1 and then return here (uc_link). */
209 /* We'll see later on whether makecontext worked. */
210 makecontext(&ctx
[1], (void (*) (void)) func1
, 28,
211 (0x0000001 << shift
), (0x0000004 << shift
),
212 (0x0000010 << shift
), (0x0000040 << shift
),
213 (0x0000100 << shift
), (0x0000400 << shift
),
214 (0x0001000 << shift
), (0x0004000 << shift
),
215 (0x0010000 << shift
), (0x0040000 << shift
),
216 (0x0100000 << shift
), (0x0400000 << shift
),
217 (0x1000000 << shift
), (0x4000000 << shift
),
218 (0x0000002 << shift
), (0x0000008 << shift
),
219 (0x0000020 << shift
), (0x0000080 << shift
),
220 (0x0000200 << shift
), (0x0000800 << shift
),
221 (0x0002000 << shift
), (0x0008000 << shift
),
222 (0x0020000 << shift
), (0x0080000 << shift
),
223 (0x0200000 << shift
), (0x0800000 << shift
),
224 (0x2000000 << shift
), (0x8000000 << shift
));
227 /* First time we're here. Let's run ctx[1] and return to ctx[0] when
228 * we're done. Note that we return to above the 'makecontext' call. */
229 if (setcontext(&ctx
[1]) != 0) err(1, 2);
232 /* When ++global was 1 we let ctx[1] run and returned to ctx[0], so
233 above ++global is executed again and should've become 2. */
237 /* Setup ctx[2] to run func2 */
238 if (getcontext(&ctx
[2]) != 0) err(1, 4);
239 ctx
[2].uc_stack
.ss_sp
= malloc(SSIZE
);
240 ctx
[2].uc_stack
.ss_size
= SSIZE
;
241 ctx
[2].uc_link
= &ctx
[1];
242 makecontext(&ctx
[2], (void (*) (void)) func2
, 0);
244 /* Now things become tricky. ctx[2] is set up such that when it finishes
245 running, and starts ctx[1] again. However, func1 swaps back to func2. Then,
246 when func2 has finished running, we continue with ctx[1] and, finally, we
248 if (swapcontext(&ctx
[0], &ctx
[2]) != 0) err(1, 5); /* makecontext failed? */
251 /* The call graph is as follows:
255 * 7 /----########----\
259 * #########----/ #########
260 * # func1 #<-------4-------# func2 #
261 * #########--------5------>#########
264 * \---------6--------/
266 * Main calls func1, func1 increases entered_func1, and returns to main. Main
267 * calls func2, swaps to func1, swaps to func2, which increases entered_func2,
268 * continues with func1, which increases entered_func1 again, continues to
269 * main, where reentered_main is set to 1. In effect, entered_func1 == 2,
270 * entered_func2 == 1, reentered_main == 1. Verify that. */
272 if (entered_func1
!= 2) err(1, 6);
273 if (entered_func2
!= 1) err(1, 7);
274 /* reentered_main == 1 is verified upon exit */
276 /* Try to allocate too small a stack */
277 free(ctx
[2].uc_stack
.ss_sp
); /* Deallocate stack space first */
278 if (getcontext(&ctx
[2]) != 0) err(1, 8);
279 ctx
[2].uc_stack
.ss_sp
= malloc(MINSIGSTKSZ
-1);
280 ctx
[2].uc_stack
.ss_size
= MINSIGSTKSZ
-1;
281 ctx
[2].uc_link
= &ctx
[0];
282 makecontext(&ctx
[2], (void (*) (void)) fail
, 0);
283 /* Because makecontext is void, we can only detect an error by trying to use
284 the invalid context */
285 if (swapcontext(&ctx
[0], &ctx
[2]) == 0) err(1, 9);
287 /* Try to allocate a huge stack to force the usage of brk/sbrk system call
288 to enlarge the data segment. Because we are fiddling with the stack
289 pointer, the OS might think the stack segment and data segment have
290 collided and kill us. This is wrong and therefore the following should
292 free(ctx
[2].uc_stack
.ss_sp
); /* Deallocate stack space first */
293 if (getcontext(&ctx
[2]) != 0) err(1, 14);
294 ctx
[2].uc_stack
.ss_sp
= malloc(8 * 1024 * 1024); /* 8 MB */
295 ctx
[2].uc_stack
.ss_size
= 8 * 1024 * 1024;
296 ctx
[2].uc_link
= &ctx
[0];
297 makecontext(&ctx
[2], (void (*) (void)) test_brk
, 0);
298 if (swapcontext(&ctx
[0], &ctx
[2]) != 0) err(1, 15);
300 ctx
[1].uc_link
= &ctx
[0];
301 ctx
[2].uc_link
= NULL
;
302 makecontext(&ctx
[1], (void (*) (void)) do_parent
, 0);
303 makecontext(&ctx
[2], (void (*) (void)) do_child
, 0);
304 if (swapcontext(&ctx
[0], &ctx
[2]) == -1) err(1, 16);
309 /* We can't use a global subtest variable reliably, because threads might
310 change the value when we reenter a thread (i.e., report wrong subtest
312 void err(int subtest
, int error_no
)
314 printf("Subtest %d, error %d\n", subtest
, error_no
);
316 if (errct
++ > ERROR_MAX
) {
317 printf("Too many errors, test aborted\n");