1 /* Copyright (C) 2001-2022 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 <https://www.gnu.org/licenses/>. */
26 static ucontext_t ctx
[3];
31 static char st2
[32768];
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");
44 if (swapcontext (&ctx
[1], &ctx
[2]) != 0)
46 printf ("%s: swapcontext: %m\n", __FUNCTION__
);
60 printf ("&on_stack=%p\n", on_stack
);
61 if (&on_stack
[0] < &st2
[0] || &on_stack
[0] >= st2
+ sizeof (st2
))
63 printf ("%s: memory stack is not where it belongs!", __FUNCTION__
);
67 if (swapcontext (&ctx
[2], &ctx
[1]) != 0)
69 printf ("%s: swapcontext: %m\n", __FUNCTION__
);
77 test_stack (volatile int a
, volatile int b
,
78 volatile int c
, volatile int d
)
84 /* Test for cases where getcontext is clobbering the callers
85 stack, including parameters. */
90 printf ("%s: getcontext clobbers parm a\n", __FUNCTION__
);
96 printf ("%s: getcontext clobbers parm b\n", __FUNCTION__
);
102 printf ("%s: getcontext clobbers parm c\n", __FUNCTION__
);
108 printf ("%s: getcontext clobbers parm d\n", __FUNCTION__
);
114 printf ("%s: getcontext clobbers varible e\n", __FUNCTION__
);
120 printf ("%s: getcontext clobbers variable f\n", __FUNCTION__
);
128 static int back_in_main
;
134 if (back_in_main
== 0)
136 puts ("program did not reach main again");
145 atexit (check_called
);
148 stack_t stack_before
, stack_after
;
150 sigaltstack (NULL
, &stack_before
);
152 puts ("making contexts");
153 if (getcontext (&ctx
[1]) != 0)
161 printf ("%s: getcontext: %m\n", __FUNCTION__
);
165 test_stack (1, 2, 3, 4);
167 /* Play some tricks with this context. */
169 if (setcontext (&ctx
[1]) != 0)
171 printf ("%s: setcontext: %m\n", __FUNCTION__
);
176 printf ("%s: 'global' not incremented twice\n", __FUNCTION__
);
180 ctx
[1].uc_stack
.ss_sp
= st1
;
181 ctx
[1].uc_stack
.ss_size
= sizeof st1
;
182 ctx
[1].uc_link
= &ctx
[0];
184 ucontext_t tempctx
= ctx
[1];
185 makecontext (&ctx
[1], (void (*) (void)) f1
, 4, 1, 2, 3, -4);
187 /* Without this check, a stub makecontext can make us spin forever. */
188 if (memcmp (&tempctx
, &ctx
[1], sizeof ctx
[1]) == 0)
190 puts ("makecontext was a no-op, presuming not implemented");
195 if (getcontext (&ctx
[2]) != 0)
197 printf ("%s: second getcontext: %m\n", __FUNCTION__
);
200 ctx
[2].uc_stack
.ss_sp
= st2
;
201 ctx
[2].uc_stack
.ss_size
= sizeof st2
;
202 ctx
[2].uc_link
= &ctx
[1];
203 makecontext (&ctx
[2], f2
, 0);
205 puts ("swapping contexts");
206 if (swapcontext (&ctx
[0], &ctx
[2]) != 0)
208 printf ("%s: swapcontext: %m\n", __FUNCTION__
);
211 puts ("back at main program");
214 sigaltstack (NULL
, &stack_after
);
218 puts ("didn't reach f1");
223 puts ("didn't reach f2");
227 /* Check sigaltstack state is not clobbered as in BZ #16629. */
228 if (stack_before
.ss_sp
!= stack_after
.ss_sp
)
230 printf ("stack ss_sp mismatch: %p %p\n",
231 stack_before
.ss_sp
, stack_after
.ss_sp
);
235 if (stack_before
.ss_size
!= stack_after
.ss_size
)
237 printf ("stack ss_size mismatch: %zd %zd\n",
238 stack_before
.ss_size
, stack_after
.ss_size
);
242 puts ("test succeeded");