|
Packit |
6c4009 |
/* Copyright (C) 2003-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
|
|
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 <error.h>
|
|
Packit |
6c4009 |
#include <limits.h>
|
|
Packit |
6c4009 |
#include <pthread.h>
|
|
Packit |
6c4009 |
#include <stdio.h>
|
|
Packit |
6c4009 |
#include <stdlib.h>
|
|
Packit |
6c4009 |
#include <ucontext.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#define N 4
|
|
Packit |
6c4009 |
#if __WORDSIZE == 64
|
|
Packit |
6c4009 |
#define GUARD_PATTERN 0xdeadbeafdeadbeaf
|
|
Packit |
6c4009 |
#else
|
|
Packit |
6c4009 |
#define GUARD_PATTERN 0xdeadbeaf
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
typedef struct {
|
|
Packit |
6c4009 |
ucontext_t uctx;
|
|
Packit |
6c4009 |
unsigned long guard[3];
|
|
Packit |
6c4009 |
} tst_context_t;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static char stacks[N][2 * PTHREAD_STACK_MIN];
|
|
Packit |
6c4009 |
static tst_context_t ctx[N][2];
|
|
Packit |
6c4009 |
static volatile int failures;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static void
|
|
Packit |
6c4009 |
fct (long int n)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
char on_stack[1];
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Just to use the thread local descriptor. */
|
|
Packit |
6c4009 |
printf ("%ld: in %s now, on_stack = %p\n", n, __FUNCTION__, on_stack);
|
|
Packit |
6c4009 |
errno = 0;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (ctx[n][1].uctx.uc_link != &ctx[n][0].uctx)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("context[%ld][1] uc_link damaged, = %p\n", n,
|
|
Packit |
6c4009 |
ctx[n][1].uctx.uc_link);
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if ((ctx[n][0].guard[0] != GUARD_PATTERN)
|
|
Packit |
6c4009 |
|| (ctx[n][0].guard[1] != GUARD_PATTERN)
|
|
Packit |
6c4009 |
|| (ctx[n][0].guard[2] != GUARD_PATTERN))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("%ld: %s context[0] overflow detected!\n", n, __FUNCTION__);
|
|
Packit |
6c4009 |
++failures;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if ((ctx[n][1].guard[0] != GUARD_PATTERN)
|
|
Packit |
6c4009 |
|| (ctx[n][1].guard[1] != GUARD_PATTERN)
|
|
Packit |
6c4009 |
|| (ctx[n][1].guard[2] != GUARD_PATTERN))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("%ld: %s context[1] overflow detected!\n", n, __FUNCTION__);
|
|
Packit |
6c4009 |
++failures;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (n < 0 || n >= N)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("%ld out of range\n", n);
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (on_stack < stacks[n] || on_stack >= stacks[n] + sizeof (stacks[0]))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("%ld: on_stack not on appropriate stack\n", n);
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static void *
|
|
Packit |
6c4009 |
tf (void *arg)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
int n = (int) (long int) arg;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
ctx[n][0].guard[0] = GUARD_PATTERN;
|
|
Packit |
6c4009 |
ctx[n][0].guard[1] = GUARD_PATTERN;
|
|
Packit |
6c4009 |
ctx[n][0].guard[2] = GUARD_PATTERN;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
ctx[n][1].guard[0] = GUARD_PATTERN;
|
|
Packit |
6c4009 |
ctx[n][1].guard[1] = GUARD_PATTERN;
|
|
Packit |
6c4009 |
ctx[n][1].guard[2] = GUARD_PATTERN;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (getcontext (&ctx[n][1].uctx) != 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("%d: cannot get context: %m\n", n);
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
printf ("%d: %s: before makecontext\n", n, __FUNCTION__);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
ctx[n][1].uctx.uc_stack.ss_sp = stacks[n];
|
|
Packit |
6c4009 |
ctx[n][1].uctx.uc_stack.ss_size = sizeof (stacks[n]);
|
|
Packit |
6c4009 |
ctx[n][1].uctx.uc_link = &ctx[n][0].uctx;
|
|
Packit |
6c4009 |
makecontext (&ctx[n][1].uctx, (void (*) (void)) fct, 1, (long int) n);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
printf ("%d: %s: before swapcontext\n", n, __FUNCTION__);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (swapcontext (&ctx[n][0].uctx, &ctx[n][1].uctx) != 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
++failures;
|
|
Packit |
6c4009 |
printf ("%d: %s: swapcontext failed\n", n, __FUNCTION__);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
printf ("%d: back in %s\n", n, __FUNCTION__);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return NULL;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static volatile int global;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static int
|
|
Packit |
6c4009 |
do_test (void)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
int n;
|
|
Packit |
6c4009 |
pthread_t th[N];
|
|
Packit |
6c4009 |
ucontext_t mctx;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
puts ("making contexts");
|
|
Packit |
6c4009 |
if (getcontext (&mctx) != 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (errno == ENOSYS)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
puts ("context handling not supported");
|
|
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 |
/* Play some tricks with this context. */
|
|
Packit |
6c4009 |
if (++global == 1)
|
|
Packit |
6c4009 |
if (setcontext (&mctx) != 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
puts ("setcontext failed");
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
if (global != 2)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
puts ("global not incremented twice");
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
puts ("global OK");
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
pthread_attr_t at;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (pthread_attr_init (&at) != 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
puts ("attr_init failed");
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (pthread_attr_setstacksize (&at, 1 * 1024 * 1024) != 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
puts ("attr_setstacksize failed");
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
for (n = 0; n < N; ++n)
|
|
Packit |
6c4009 |
if (pthread_create (&th[n], &at, tf, (void *) (long int) n) != 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
puts ("create failed");
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (pthread_attr_destroy (&at) != 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
puts ("attr_destroy failed");
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
for (n = 0; n < N; ++n)
|
|
Packit |
6c4009 |
if (pthread_join (th[n], NULL) != 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
puts ("join failed");
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return failures;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#define TEST_FUNCTION do_test ()
|
|
Packit |
6c4009 |
#include "../test-skeleton.c"
|