2017-11-17 18:04:29 +00:00
|
|
|
/* Test for i386 sigaction sa_restorer handling (BZ#21269)
|
2023-01-06 21:08:04 +00:00
|
|
|
Copyright (C) 2017-2023 Free Software Foundation, Inc.
|
2017-11-17 18:04:29 +00: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 05:40:42 +00:00
|
|
|
<https://www.gnu.org/licenses/>. */
|
2017-11-17 18:04:29 +00:00
|
|
|
|
|
|
|
/* This is based on Linux test tools/testing/selftests/x86/ldt_gdt.c,
|
|
|
|
more specifically in do_multicpu_tests function. The main changes
|
|
|
|
are:
|
|
|
|
|
|
|
|
- C11 atomics instead of plain access.
|
|
|
|
- Remove x86_64 support which simplifies the syscall handling
|
|
|
|
and fallbacks.
|
|
|
|
- Replicate only the test required to trigger the issue for the
|
|
|
|
BZ#21269. */
|
|
|
|
|
|
|
|
#include <stdatomic.h>
|
|
|
|
|
|
|
|
#include <asm/ldt.h>
|
|
|
|
#include <linux/futex.h>
|
|
|
|
|
|
|
|
#include <setjmp.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
|
|
|
#include <support/xunistd.h>
|
|
|
|
#include <support/check.h>
|
|
|
|
#include <support/xthread.h>
|
|
|
|
|
|
|
|
static int
|
|
|
|
xset_thread_area (struct user_desc *u_info)
|
|
|
|
{
|
|
|
|
long ret = syscall (SYS_set_thread_area, u_info);
|
|
|
|
TEST_VERIFY_EXIT (ret == 0);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
xmodify_ldt (int func, const void *ptr, unsigned long bytecount)
|
|
|
|
{
|
2023-08-04 23:04:33 +00:00
|
|
|
long ret = syscall (SYS_modify_ldt, func, ptr, bytecount);
|
|
|
|
|
|
|
|
if (ret == -1)
|
|
|
|
{
|
|
|
|
if (errno == ENOSYS)
|
|
|
|
FAIL_UNSUPPORTED ("modify_ldt not supported");
|
|
|
|
FAIL_EXIT1 ("modify_ldt failed (errno=%d)", errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2017-11-17 18:04:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
futex (int *uaddr, int futex_op, int val, void *timeout, int *uaddr2,
|
|
|
|
int val3)
|
|
|
|
{
|
|
|
|
return syscall (SYS_futex, uaddr, futex_op, val, timeout, uaddr2, val3);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
xsethandler (int sig, void (*handler)(int, siginfo_t *, void *), int flags)
|
|
|
|
{
|
|
|
|
struct sigaction sa = { 0 };
|
|
|
|
sa.sa_sigaction = handler;
|
|
|
|
sa.sa_flags = SA_SIGINFO | flags;
|
|
|
|
TEST_VERIFY_EXIT (sigemptyset (&sa.sa_mask) == 0);
|
|
|
|
TEST_VERIFY_EXIT (sigaction (sig, &sa, 0) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static jmp_buf jmpbuf;
|
|
|
|
|
|
|
|
static void
|
|
|
|
sigsegv_handler (int sig, siginfo_t *info, void *ctx_void)
|
|
|
|
{
|
|
|
|
siglongjmp (jmpbuf, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Points to an array of 1024 ints, each holding its own index. */
|
|
|
|
static const unsigned int *counter_page;
|
|
|
|
static struct user_desc *low_user_desc;
|
|
|
|
static struct user_desc *low_user_desc_clear; /* Used to delete GDT entry. */
|
|
|
|
static int gdt_entry_num;
|
|
|
|
|
|
|
|
static void
|
|
|
|
setup_counter_page (void)
|
|
|
|
{
|
|
|
|
long page_size = sysconf (_SC_PAGE_SIZE);
|
|
|
|
TEST_VERIFY_EXIT (page_size > 0);
|
|
|
|
unsigned int *page = xmmap (NULL, page_size, PROT_READ | PROT_WRITE,
|
|
|
|
MAP_ANONYMOUS | MAP_PRIVATE | MAP_32BIT, -1);
|
|
|
|
for (int i = 0; i < (page_size / sizeof (unsigned int)); i++)
|
|
|
|
page[i] = i;
|
|
|
|
counter_page = page;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
setup_low_user_desc (void)
|
|
|
|
{
|
|
|
|
low_user_desc = xmmap (NULL, 2 * sizeof (struct user_desc),
|
|
|
|
PROT_READ | PROT_WRITE,
|
|
|
|
MAP_ANONYMOUS | MAP_PRIVATE | MAP_32BIT, -1);
|
|
|
|
|
|
|
|
low_user_desc->entry_number = -1;
|
|
|
|
low_user_desc->base_addr = (unsigned long) &counter_page[1];
|
|
|
|
low_user_desc->limit = 0xffff;
|
|
|
|
low_user_desc->seg_32bit = 1;
|
|
|
|
low_user_desc->contents = 0;
|
|
|
|
low_user_desc->read_exec_only = 0;
|
|
|
|
low_user_desc->limit_in_pages = 1;
|
|
|
|
low_user_desc->seg_not_present = 0;
|
|
|
|
low_user_desc->useable = 0;
|
|
|
|
|
|
|
|
xset_thread_area (low_user_desc);
|
|
|
|
|
|
|
|
low_user_desc_clear = low_user_desc + 1;
|
|
|
|
low_user_desc_clear->entry_number = gdt_entry_num;
|
|
|
|
low_user_desc_clear->read_exec_only = 1;
|
|
|
|
low_user_desc_clear->seg_not_present = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Possible values of futex:
|
|
|
|
0: thread is idle.
|
|
|
|
1: thread armed.
|
|
|
|
2: thread should clear LDT entry 0.
|
|
|
|
3: thread should exit. */
|
|
|
|
static atomic_uint ftx;
|
|
|
|
|
|
|
|
static void *
|
|
|
|
threadproc (void *ctx)
|
|
|
|
{
|
|
|
|
while (1)
|
|
|
|
{
|
i386: Use pthread_barrier for synchronization on tst-bz21269
So I was able to reproduce the hangs in the original source, and debug
it, and fix it. In doing so, I realized that we can't use anything
complex to trigger the thread because that "anything" might also cause
the expected segfault and force everything out of sync again.
Here's what I ended up with, and it doesn't seem to hang where the
original one hung quite often (in a tight while..end loop). The key
changes are:
1. Calls to futex are error checked, with retries, to ensure that the
futexes are actually doing what they're supposed to be doing. In the
original code, nearly every futex call returned an error.
2. The main loop has checks for whether the thread ran or not, and
"unlocks" the thread if it didn't (this is how the original source
hangs).
Note: the usleep() is not for timing purposes, but just to give the
kernel an excuse to run the other thread at that time. The test will
not hang without it, but is more likely to test the right bugfix
if the usleep() is present.
2023-05-16 02:50:35 +00:00
|
|
|
/* Continue to wait here until we've successfully waited, unless
|
|
|
|
we're supposed to be clearing the LDT already. */
|
|
|
|
while (futex ((int *) &ftx, FUTEX_WAIT, 1, NULL, NULL, 0) < 0)
|
|
|
|
if (atomic_load (&ftx) >= 2)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Normally there's time to hit this busy loop and wait for ftx
|
|
|
|
to be set to 2. */
|
2017-11-17 18:04:29 +00:00
|
|
|
while (atomic_load (&ftx) != 2)
|
|
|
|
{
|
|
|
|
if (atomic_load (&ftx) >= 3)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* clear LDT entry 0. */
|
|
|
|
const struct user_desc desc = { 0 };
|
|
|
|
xmodify_ldt (1, &desc, sizeof (desc));
|
|
|
|
|
|
|
|
/* If ftx == 2, set it to zero, If ftx == 100, quit. */
|
|
|
|
if (atomic_fetch_add (&ftx, -2) != 2)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* As described in testcase, for historical reasons x86_32 Linux (and compat
|
|
|
|
on x86_64) interprets SA_RESTORER clear with nonzero sa_restorer as a
|
|
|
|
request for stack switching if the SS segment is 'funny' (this is default
|
|
|
|
scenario for vDSO system). This means that anything that tries to mix
|
|
|
|
signal handling with segmentation should explicit clear the sa_restorer.
|
|
|
|
|
|
|
|
This testcase check if sigaction in fact does it by changing the local
|
|
|
|
descriptor table (LDT) through the modify_ldt syscall and triggering
|
|
|
|
a synchronous segfault on iret fault by trying to install an invalid
|
|
|
|
segment. With a correct zeroed sa_restorer it should not trigger an
|
|
|
|
'real' SEGSEGV and allows the siglongjmp in signal handler. */
|
|
|
|
|
|
|
|
static int
|
|
|
|
do_test (void)
|
|
|
|
{
|
|
|
|
setup_counter_page ();
|
|
|
|
setup_low_user_desc ();
|
|
|
|
|
|
|
|
pthread_t thread;
|
|
|
|
unsigned short orig_ss;
|
|
|
|
|
|
|
|
xsethandler (SIGSEGV, sigsegv_handler, 0);
|
|
|
|
/* 32-bit kernels send SIGILL instead of SIGSEGV on IRET faults. */
|
|
|
|
xsethandler (SIGILL, sigsegv_handler, 0);
|
2018-03-29 09:42:24 +00:00
|
|
|
/* Some kernels send SIGBUS instead. */
|
|
|
|
xsethandler (SIGBUS, sigsegv_handler, 0);
|
2017-11-17 18:04:29 +00:00
|
|
|
|
|
|
|
thread = xpthread_create (0, threadproc, 0);
|
|
|
|
|
|
|
|
asm volatile ("mov %%ss, %0" : "=rm" (orig_ss));
|
|
|
|
|
|
|
|
for (int i = 0; i < 5; i++)
|
|
|
|
{
|
|
|
|
if (sigsetjmp (jmpbuf, 1) != 0)
|
|
|
|
continue;
|
|
|
|
|
i386: Use pthread_barrier for synchronization on tst-bz21269
So I was able to reproduce the hangs in the original source, and debug
it, and fix it. In doing so, I realized that we can't use anything
complex to trigger the thread because that "anything" might also cause
the expected segfault and force everything out of sync again.
Here's what I ended up with, and it doesn't seem to hang where the
original one hung quite often (in a tight while..end loop). The key
changes are:
1. Calls to futex are error checked, with retries, to ensure that the
futexes are actually doing what they're supposed to be doing. In the
original code, nearly every futex call returned an error.
2. The main loop has checks for whether the thread ran or not, and
"unlocks" the thread if it didn't (this is how the original source
hangs).
Note: the usleep() is not for timing purposes, but just to give the
kernel an excuse to run the other thread at that time. The test will
not hang without it, but is more likely to test the right bugfix
if the usleep() is present.
2023-05-16 02:50:35 +00:00
|
|
|
/* We may have longjmp'd before triggering the thread. If so,
|
|
|
|
trigger the thread now and wait for it. */
|
|
|
|
if (atomic_load (&ftx) == 1)
|
|
|
|
atomic_store (&ftx, 2);
|
|
|
|
|
|
|
|
/* Make sure the thread is ready after the last test. FTX is
|
|
|
|
initially zero for the first loop, and set to zero each time
|
|
|
|
the thread clears the LDT. */
|
2017-11-17 18:04:29 +00:00
|
|
|
while (atomic_load (&ftx) != 0)
|
|
|
|
;
|
|
|
|
|
|
|
|
struct user_desc desc = {
|
|
|
|
.entry_number = 0,
|
|
|
|
.base_addr = 0,
|
|
|
|
.limit = 0xffff,
|
|
|
|
.seg_32bit = 1,
|
|
|
|
.contents = 0,
|
|
|
|
.read_exec_only = 0,
|
|
|
|
.limit_in_pages = 1,
|
|
|
|
.seg_not_present = 0,
|
|
|
|
.useable = 0
|
|
|
|
};
|
|
|
|
|
|
|
|
xmodify_ldt (0x11, &desc, sizeof (desc));
|
|
|
|
|
i386: Use pthread_barrier for synchronization on tst-bz21269
So I was able to reproduce the hangs in the original source, and debug
it, and fix it. In doing so, I realized that we can't use anything
complex to trigger the thread because that "anything" might also cause
the expected segfault and force everything out of sync again.
Here's what I ended up with, and it doesn't seem to hang where the
original one hung quite often (in a tight while..end loop). The key
changes are:
1. Calls to futex are error checked, with retries, to ensure that the
futexes are actually doing what they're supposed to be doing. In the
original code, nearly every futex call returned an error.
2. The main loop has checks for whether the thread ran or not, and
"unlocks" the thread if it didn't (this is how the original source
hangs).
Note: the usleep() is not for timing purposes, but just to give the
kernel an excuse to run the other thread at that time. The test will
not hang without it, but is more likely to test the right bugfix
if the usleep() is present.
2023-05-16 02:50:35 +00:00
|
|
|
/* Arm the thread. We loop here until we've woken up one thread. */
|
|
|
|
atomic_store (&ftx, 1);
|
|
|
|
while (futex ((int*) &ftx, FUTEX_WAKE, 1, NULL, NULL, 0) < 1)
|
|
|
|
;
|
|
|
|
|
|
|
|
/* Give the thread a chance to get into it's busy loop. */
|
|
|
|
usleep (5);
|
2017-11-17 18:04:29 +00:00
|
|
|
|
i386: Use pthread_barrier for synchronization on tst-bz21269
So I was able to reproduce the hangs in the original source, and debug
it, and fix it. In doing so, I realized that we can't use anything
complex to trigger the thread because that "anything" might also cause
the expected segfault and force everything out of sync again.
Here's what I ended up with, and it doesn't seem to hang where the
original one hung quite often (in a tight while..end loop). The key
changes are:
1. Calls to futex are error checked, with retries, to ensure that the
futexes are actually doing what they're supposed to be doing. In the
original code, nearly every futex call returned an error.
2. The main loop has checks for whether the thread ran or not, and
"unlocks" the thread if it didn't (this is how the original source
hangs).
Note: the usleep() is not for timing purposes, but just to give the
kernel an excuse to run the other thread at that time. The test will
not hang without it, but is more likely to test the right bugfix
if the usleep() is present.
2023-05-16 02:50:35 +00:00
|
|
|
/* At *ANY* point after this instruction, we may segfault and
|
|
|
|
longjump back to the top of the loop. The intention is to
|
|
|
|
have this happen when the thread clears the LDT, but it could
|
|
|
|
happen elsewhen. */
|
2017-11-17 18:04:29 +00:00
|
|
|
asm volatile ("mov %0, %%ss" : : "r" (0x7));
|
|
|
|
|
|
|
|
/* Fire up thread modify_ldt call. */
|
|
|
|
atomic_store (&ftx, 2);
|
|
|
|
|
i386: Use pthread_barrier for synchronization on tst-bz21269
So I was able to reproduce the hangs in the original source, and debug
it, and fix it. In doing so, I realized that we can't use anything
complex to trigger the thread because that "anything" might also cause
the expected segfault and force everything out of sync again.
Here's what I ended up with, and it doesn't seem to hang where the
original one hung quite often (in a tight while..end loop). The key
changes are:
1. Calls to futex are error checked, with retries, to ensure that the
futexes are actually doing what they're supposed to be doing. In the
original code, nearly every futex call returned an error.
2. The main loop has checks for whether the thread ran or not, and
"unlocks" the thread if it didn't (this is how the original source
hangs).
Note: the usleep() is not for timing purposes, but just to give the
kernel an excuse to run the other thread at that time. The test will
not hang without it, but is more likely to test the right bugfix
if the usleep() is present.
2023-05-16 02:50:35 +00:00
|
|
|
/* And wait for it. */
|
2017-11-17 18:04:29 +00:00
|
|
|
while (atomic_load (&ftx) != 0)
|
|
|
|
;
|
|
|
|
|
|
|
|
/* On success, modify_ldt will segfault us synchronously and we will
|
|
|
|
escape via siglongjmp. */
|
|
|
|
support_record_failure ();
|
|
|
|
}
|
|
|
|
|
|
|
|
atomic_store (&ftx, 100);
|
|
|
|
futex ((int*) &ftx, FUTEX_WAKE, 0, NULL, NULL, 0);
|
|
|
|
|
|
|
|
xpthread_join (thread);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#include <support/test-driver.c>
|