|
Packit |
6c4009 |
/* Copyright (C) 2001-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License as published by the Free Software Foundation; either
|
|
Packit |
6c4009 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6c4009 |
Lesser General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License along with the GNU C Library; if not, see
|
|
Packit |
6c4009 |
<http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <errno.h>
|
|
Packit |
6c4009 |
#include <signal.h>
|
|
Packit |
6c4009 |
#include <stdio.h>
|
|
Packit |
6c4009 |
#include <stdlib.h>
|
|
Packit |
6c4009 |
#include <string.h>
|
|
Packit |
6c4009 |
#include <ucontext.h>
|
|
Packit |
6c4009 |
#include <unistd.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static ucontext_t ctx[3];
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static int was_in_f1;
|
|
Packit |
6c4009 |
static int was_in_f2;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static char st2[32768];
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static void
|
|
Packit |
6c4009 |
f1 (int a0, int a1, int a2, int a3)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("start f1(a0=%x,a1=%x,a2=%x,a3=%x)\n", a0, a1, a2, a3);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (a0 != 1 || a1 != 2 || a2 != 3 || a3 != -4)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
puts ("arg mismatch");
|
|
Packit |
6c4009 |
exit (-1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (swapcontext (&ctx[1], &ctx[2]) != 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("%s: swapcontext: %m\n", __FUNCTION__);
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
puts ("finish f1");
|
|
Packit |
6c4009 |
was_in_f1 = 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static void
|
|
Packit |
6c4009 |
f2 (void)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
char on_stack[1];
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
puts ("start f2");
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
printf ("&on_stack=%p\n", on_stack);
|
|
Packit |
6c4009 |
if (on_stack < st2 || on_stack >= st2 + sizeof (st2))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("%s: memory stack is not where it belongs!", __FUNCTION__);
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (swapcontext (&ctx[2], &ctx[1]) != 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("%s: swapcontext: %m\n", __FUNCTION__);
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
puts ("finish f2");
|
|
Packit |
6c4009 |
was_in_f2 = 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
void
|
|
Packit |
6c4009 |
test_stack (volatile int a, volatile int b,
|
|
Packit |
6c4009 |
volatile int c, volatile int d)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
volatile int e = 5;
|
|
Packit |
6c4009 |
volatile int f = 6;
|
|
Packit |
6c4009 |
ucontext_t uc;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Test for cases where getcontext is clobbering the callers
|
|
Packit |
6c4009 |
stack, including parameters. */
|
|
Packit |
6c4009 |
getcontext (&uc);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (a != 1)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("%s: getcontext clobbers parm a\n", __FUNCTION__);
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (b != 2)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("%s: getcontext clobbers parm b\n", __FUNCTION__);
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (c != 3)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("%s: getcontext clobbers parm c\n", __FUNCTION__);
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (d != 4)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("%s: getcontext clobbers parm d\n", __FUNCTION__);
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (e != 5)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("%s: getcontext clobbers varible e\n", __FUNCTION__);
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (f != 6)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("%s: getcontext clobbers variable f\n", __FUNCTION__);
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
volatile int global;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static int back_in_main;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static void
|
|
Packit |
6c4009 |
check_called (void)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (back_in_main == 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
puts ("program did not reach main again");
|
|
Packit |
6c4009 |
_exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
int
|
|
Packit |
6c4009 |
main (void)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
atexit (check_called);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
char st1[32768];
|
|
Packit |
6c4009 |
stack_t stack_before, stack_after;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
sigaltstack (NULL, &stack_before);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
puts ("making contexts");
|
|
Packit |
6c4009 |
if (getcontext (&ctx[1]) != 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (errno == ENOSYS)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
back_in_main = 1;
|
|
Packit |
6c4009 |
exit (0);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
printf ("%s: getcontext: %m\n", __FUNCTION__);
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
test_stack (1, 2, 3, 4);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Play some tricks with this context. */
|
|
Packit |
6c4009 |
if (++global == 1)
|
|
Packit |
6c4009 |
if (setcontext (&ctx[1]) != 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("%s: setcontext: %m\n", __FUNCTION__);
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
if (global != 2)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("%s: 'global' not incremented twice\n", __FUNCTION__);
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
ctx[1].uc_stack.ss_sp = st1;
|
|
Packit |
6c4009 |
ctx[1].uc_stack.ss_size = sizeof st1;
|
|
Packit |
6c4009 |
ctx[1].uc_link = &ctx[0];
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
ucontext_t tempctx = ctx[1];
|
|
Packit |
6c4009 |
makecontext (&ctx[1], (void (*) (void)) f1, 4, 1, 2, 3, -4);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Without this check, a stub makecontext can make us spin forever. */
|
|
Packit |
6c4009 |
if (memcmp (&tempctx, &ctx[1], sizeof ctx[1]) == 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
puts ("makecontext was a no-op, presuming not implemented");
|
|
Packit |
6c4009 |
return 0;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (getcontext (&ctx[2]) != 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("%s: second getcontext: %m\n", __FUNCTION__);
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
ctx[2].uc_stack.ss_sp = st2;
|
|
Packit |
6c4009 |
ctx[2].uc_stack.ss_size = sizeof st2;
|
|
Packit |
6c4009 |
ctx[2].uc_link = &ctx[1];
|
|
Packit |
6c4009 |
makecontext (&ctx[2], f2, 0);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
puts ("swapping contexts");
|
|
Packit |
6c4009 |
if (swapcontext (&ctx[0], &ctx[2]) != 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("%s: swapcontext: %m\n", __FUNCTION__);
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
puts ("back at main program");
|
|
Packit |
6c4009 |
back_in_main = 1;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
sigaltstack (NULL, &stack_after);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (was_in_f1 == 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
puts ("didn't reach f1");
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
if (was_in_f2 == 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
puts ("didn't reach f2");
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Check sigaltstack state is not clobbered as in BZ #16629. */
|
|
Packit |
6c4009 |
if (stack_before.ss_sp != stack_after.ss_sp)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("stack ss_sp mismatch: %p %p\n",
|
|
Packit |
6c4009 |
stack_before.ss_sp, stack_after.ss_sp);
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (stack_before.ss_size != stack_after.ss_size)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("stack ss_size mismatch: %zd %zd\n",
|
|
Packit |
6c4009 |
stack_before.ss_size, stack_after.ss_size);
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
puts ("test succeeded");
|
|
Packit |
6c4009 |
return 0;
|
|
Packit |
6c4009 |
}
|