2012-06-20 15:33:13 +08:00
|
|
|
/* Make sure that the stackaddr returned by pthread_getattr_np is
|
|
|
|
reachable.
|
|
|
|
|
2021-01-03 03:32:25 +08:00
|
|
|
Copyright (C) 2012-2021 Free Software Foundation, Inc.
|
2012-06-20 15:33:13 +08:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with the GNU C Library; if not, see
|
Prefer https to http for gnu.org and fsf.org URLs
Also, change sources.redhat.com to sourceware.org.
This patch was automatically generated by running the following shell
script, which uses GNU sed, and which avoids modifying files imported
from upstream:
sed -ri '
s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g
s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g
' \
$(find $(git ls-files) -prune -type f \
! -name '*.po' \
! -name 'ChangeLog*' \
! -path COPYING ! -path COPYING.LIB \
! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \
! -path manual/texinfo.tex ! -path scripts/config.guess \
! -path scripts/config.sub ! -path scripts/install-sh \
! -path scripts/mkinstalldirs ! -path scripts/move-if-change \
! -path INSTALL ! -path locale/programs/charmap-kw.h \
! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \
! '(' -name configure \
-execdir test -f configure.ac -o -f configure.in ';' ')' \
! '(' -name preconfigure \
-execdir test -f preconfigure.ac ';' ')' \
-print)
and then by running 'make dist-prepare' to regenerate files built
from the altered files, and then executing the following to cleanup:
chmod a+x sysdeps/unix/sysv/linux/riscv/configure
# Omit irrelevant whitespace and comment-only changes,
# perhaps from a slightly-different Autoconf version.
git checkout -f \
sysdeps/csky/configure \
sysdeps/hppa/configure \
sysdeps/riscv/configure \
sysdeps/unix/sysv/linux/csky/configure
# Omit changes that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines
git checkout -f \
sysdeps/powerpc/powerpc64/ppc-mcount.S \
sysdeps/unix/sysv/linux/s390/s390-64/syscall.S
# Omit change that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline
git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 13:40:42 +08:00
|
|
|
<https://www.gnu.org/licenses/>. */
|
2012-06-20 15:33:13 +08:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/resource.h>
|
2012-07-28 15:55:00 +08:00
|
|
|
#include <sys/param.h>
|
2012-06-20 15:33:13 +08:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <alloca.h>
|
2012-07-28 15:55:00 +08:00
|
|
|
#include <assert.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
|
|
|
/* There is an obscure bug in the kernel due to which RLIMIT_STACK is sometimes
|
|
|
|
returned as unlimited when it is not, which may cause this test to fail.
|
|
|
|
There is also the other case where RLIMIT_STACK is intentionally set as
|
|
|
|
unlimited or very high, which may result in a vma that is too large and again
|
|
|
|
results in a test case failure. To avoid these problems, we cap the stack
|
|
|
|
size to one less than 8M. See the following mailing list threads for more
|
|
|
|
information about this problem:
|
2017-11-16 14:19:26 +08:00
|
|
|
<https://sourceware.org/ml/libc-alpha/2012-06/msg00599.html>
|
|
|
|
<https://sourceware.org/ml/libc-alpha/2012-06/msg00713.html>. */
|
2012-07-28 15:55:00 +08:00
|
|
|
#define MAX_STACK_SIZE (8192 * 1024 - 1)
|
|
|
|
|
|
|
|
static size_t pagesize;
|
|
|
|
|
2019-07-30 16:35:08 +08:00
|
|
|
/* Test that the page in which TARGET lies is accessible. This will
|
|
|
|
segfault if the write fails. This function has only half a page
|
|
|
|
of thread stack left and so should not do anything and immediately
|
|
|
|
return the address to which the stack reached. */
|
|
|
|
static volatile uintptr_t
|
2012-07-28 15:55:00 +08:00
|
|
|
allocate_and_test (char *target)
|
2012-06-20 15:33:13 +08:00
|
|
|
{
|
2012-07-28 15:55:00 +08:00
|
|
|
volatile char *mem = (char *) &mem;
|
|
|
|
/* FIXME: mem >= target for _STACK_GROWSUP. */
|
|
|
|
mem = alloca ((size_t) (mem - target));
|
|
|
|
|
|
|
|
*mem = 42;
|
2019-07-30 16:35:08 +08:00
|
|
|
return (uintptr_t) mem;
|
2012-06-20 15:33:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
get_self_pthread_attr (const char *id, void **stackaddr, size_t *stacksize)
|
|
|
|
{
|
|
|
|
pthread_attr_t attr;
|
|
|
|
int ret;
|
|
|
|
pthread_t me = pthread_self ();
|
|
|
|
|
2012-07-28 15:55:00 +08:00
|
|
|
if ((ret = pthread_getattr_np (me, &attr)) < 0)
|
2012-06-20 15:33:13 +08:00
|
|
|
{
|
|
|
|
printf ("%s: pthread_getattr_np failed: %s\n", id, strerror (ret));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-07-28 15:55:00 +08:00
|
|
|
if ((ret = pthread_attr_getstack (&attr, stackaddr, stacksize)) < 0)
|
2012-06-20 15:33:13 +08:00
|
|
|
{
|
|
|
|
printf ("%s: pthread_attr_getstack returned error: %s\n", id,
|
|
|
|
strerror (ret));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify that the stack size returned by pthread_getattr_np is usable when
|
|
|
|
the returned value is subject to rlimit. */
|
|
|
|
static int
|
|
|
|
check_stack_top (void)
|
|
|
|
{
|
|
|
|
struct rlimit stack_limit;
|
|
|
|
void *stackaddr;
|
|
|
|
size_t stacksize = 0;
|
|
|
|
int ret;
|
2012-07-28 15:55:00 +08:00
|
|
|
uintptr_t pagemask = ~(pagesize - 1);
|
2012-06-20 15:33:13 +08:00
|
|
|
|
|
|
|
puts ("Verifying that stack top is accessible");
|
|
|
|
|
|
|
|
ret = getrlimit (RLIMIT_STACK, &stack_limit);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
perror ("getrlimit failed");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-07-28 15:55:00 +08:00
|
|
|
printf ("current rlimit_stack is %zu\n", (size_t) stack_limit.rlim_cur);
|
|
|
|
|
2012-06-20 15:33:13 +08:00
|
|
|
if (get_self_pthread_attr ("check_stack_top", &stackaddr, &stacksize))
|
|
|
|
return 1;
|
|
|
|
|
2012-07-28 15:55:00 +08:00
|
|
|
/* Reduce the rlimit to a page less that what is currently being returned
|
|
|
|
(subject to a maximum of MAX_STACK_SIZE) so that we ensure that
|
|
|
|
pthread_getattr_np uses rlimit. The figure is intentionally unaligned so
|
|
|
|
to verify that pthread_getattr_np returns an aligned stacksize that
|
|
|
|
correctly fits into the rlimit. We don't bother about the case where the
|
|
|
|
stack is limited by the vma below it and not by the rlimit because the
|
|
|
|
stacksize returned in that case is computed from the end of that vma and is
|
|
|
|
hence safe. */
|
|
|
|
stack_limit.rlim_cur = MIN (stacksize - pagesize + 1, MAX_STACK_SIZE);
|
|
|
|
printf ("Adjusting RLIMIT_STACK to %zu\n", (size_t) stack_limit.rlim_cur);
|
|
|
|
if ((ret = setrlimit (RLIMIT_STACK, &stack_limit)) < 0)
|
2012-06-20 15:33:13 +08:00
|
|
|
{
|
|
|
|
perror ("setrlimit failed");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (get_self_pthread_attr ("check_stack_top2", &stackaddr, &stacksize))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
printf ("Adjusted rlimit: stacksize=%zu, stackaddr=%p\n", stacksize,
|
|
|
|
stackaddr);
|
2012-07-28 15:55:00 +08:00
|
|
|
|
|
|
|
/* A lot of targets tend to write stuff on top of the user stack during
|
|
|
|
context switches, so we cannot possibly safely go up to the very top of
|
|
|
|
stack and test access there. It is however sufficient to simply check if
|
|
|
|
the top page is accessible, so we target our access halfway up the top
|
|
|
|
page. Thanks Chris Metcalf for this idea. */
|
2019-07-30 16:35:08 +08:00
|
|
|
uintptr_t mem = allocate_and_test (stackaddr + pagesize / 2);
|
2012-07-28 15:55:00 +08:00
|
|
|
|
|
|
|
/* Before we celebrate, make sure we actually did test the same page. */
|
2019-07-30 16:35:08 +08:00
|
|
|
if (((uintptr_t) stackaddr & pagemask) != (mem & pagemask))
|
2012-07-28 15:55:00 +08:00
|
|
|
{
|
|
|
|
printf ("We successfully wrote into the wrong page.\n"
|
|
|
|
"Expected %#" PRIxPTR ", but got %#" PRIxPTR "\n",
|
2019-07-30 16:35:08 +08:00
|
|
|
(uintptr_t) stackaddr & pagemask, mem & pagemask);
|
2012-07-28 15:55:00 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2012-06-20 15:33:13 +08:00
|
|
|
|
|
|
|
puts ("Stack top tests done");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: Similar check for thread stacks once the thread stack sizes are
|
|
|
|
fixed. */
|
|
|
|
static int
|
|
|
|
do_test (void)
|
|
|
|
{
|
2012-07-28 15:55:00 +08:00
|
|
|
pagesize = sysconf (_SC_PAGESIZE);
|
2012-06-20 15:33:13 +08:00
|
|
|
return check_stack_top ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define TEST_FUNCTION do_test ()
|
|
|
|
#include "../test-skeleton.c"
|