2016-05-12 06:54:17 +00:00
|
|
|
/* Test case for async-signal-safe fork (with respect to malloc).
|
2020-01-01 00:14:33 +00:00
|
|
|
Copyright (C) 2016-2020 Free Software Foundation, Inc.
|
2016-05-12 06:54:17 +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; see the file COPYING.LIB. If
|
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
|
|
|
not, see <https://www.gnu.org/licenses/>. */
|
2016-05-12 06:54:17 +00:00
|
|
|
|
|
|
|
/* This test will fail if the process is multi-threaded because we
|
|
|
|
only have an async-signal-safe fork in the single-threaded case
|
|
|
|
(where we skip acquiring the malloc heap locks).
|
|
|
|
|
|
|
|
This test only checks async-signal-safety with regards to malloc;
|
|
|
|
other, more rarely-used glibc subsystems could have locks which
|
|
|
|
still make fork unsafe, even in single-threaded processes. */
|
|
|
|
|
|
|
|
#include <errno.h>
|
2016-05-13 14:55:01 +00:00
|
|
|
#include <sched.h>
|
2016-05-12 06:54:17 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2019-05-08 13:29:13 +00:00
|
|
|
#include <array_length.h>
|
|
|
|
#include <support/check.h>
|
|
|
|
#include <support/support.h>
|
|
|
|
#include <support/xthread.h>
|
|
|
|
#include <support/xunistd.h>
|
2016-05-12 06:54:17 +00:00
|
|
|
|
|
|
|
/* How many malloc objects to keep arond. */
|
|
|
|
enum { malloc_objects = 1009 };
|
|
|
|
|
|
|
|
/* The maximum size of an object. */
|
|
|
|
enum { malloc_maximum_size = 70000 };
|
|
|
|
|
2019-05-08 13:29:13 +00:00
|
|
|
/* How many iterations the test performs before exiting. */
|
|
|
|
enum { iterations = 10000 };
|
2016-05-12 06:54:17 +00:00
|
|
|
|
2019-05-08 13:29:13 +00:00
|
|
|
/* Barrier for synchronization with the processes sending SIGUSR1
|
|
|
|
signals, to make it more likely that the signals arrive during a
|
|
|
|
fork/free/malloc call. */
|
|
|
|
static struct { pthread_barrier_t barrier; } *shared;
|
2016-05-12 06:54:17 +00:00
|
|
|
|
|
|
|
/* Set to 1 if SIGUSR1 is received. Used to detect a signal during
|
2019-05-08 13:29:13 +00:00
|
|
|
fork/free/malloc. */
|
2016-05-12 06:54:17 +00:00
|
|
|
static volatile sig_atomic_t sigusr1_received;
|
|
|
|
|
|
|
|
/* Periodically set to 1, to indicate that the process is making
|
|
|
|
progress. Checked by liveness_signal_handler. */
|
|
|
|
static volatile sig_atomic_t progress_indicator = 1;
|
|
|
|
|
|
|
|
static void
|
|
|
|
sigusr1_handler (int signo)
|
|
|
|
{
|
|
|
|
sigusr1_received = 1;
|
|
|
|
|
|
|
|
/* Perform a fork with a trivial subprocess. */
|
|
|
|
pid_t pid = fork ();
|
|
|
|
if (pid == -1)
|
|
|
|
{
|
|
|
|
write_message ("error: fork\n");
|
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
if (pid == 0)
|
|
|
|
_exit (0);
|
|
|
|
int status;
|
|
|
|
int ret = TEMP_FAILURE_RETRY (waitpid (pid, &status, 0));
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
write_message ("error: waitpid\n");
|
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
if (status != 0)
|
|
|
|
{
|
|
|
|
write_message ("error: unexpected exit status from subprocess\n");
|
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
liveness_signal_handler (int signo)
|
|
|
|
{
|
|
|
|
if (progress_indicator)
|
|
|
|
progress_indicator = 0;
|
|
|
|
else
|
|
|
|
write_message ("warning: process seems to be stuck\n");
|
|
|
|
}
|
|
|
|
|
2019-05-08 13:29:13 +00:00
|
|
|
/* Send SIGNO to the parent process. If SLEEP, wait a second between
|
|
|
|
signals, otherwise use barriers to delay sending signals. */
|
2016-05-12 06:54:17 +00:00
|
|
|
static void
|
|
|
|
__attribute__ ((noreturn))
|
|
|
|
signal_sender (int signo, bool sleep)
|
|
|
|
{
|
|
|
|
pid_t target = getppid ();
|
|
|
|
while (true)
|
|
|
|
{
|
2019-05-08 13:29:13 +00:00
|
|
|
if (!sleep)
|
|
|
|
xpthread_barrier_wait (&shared->barrier);
|
2016-05-12 06:54:17 +00:00
|
|
|
if (kill (target, signo) != 0)
|
|
|
|
{
|
|
|
|
dprintf (STDOUT_FILENO, "error: kill: %m\n");
|
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
if (sleep)
|
|
|
|
usleep (1 * 1000 * 1000);
|
2016-05-13 14:55:01 +00:00
|
|
|
else
|
2019-05-08 13:29:13 +00:00
|
|
|
xpthread_barrier_wait (&shared->barrier);
|
2016-05-12 06:54:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
do_test (void)
|
|
|
|
{
|
2019-05-08 13:29:13 +00:00
|
|
|
/* shared->barrier is intialized along with sigusr1_sender_pids
|
|
|
|
below. */
|
|
|
|
shared = support_shared_allocate (sizeof (*shared));
|
|
|
|
|
2016-05-12 06:54:17 +00:00
|
|
|
struct sigaction action =
|
|
|
|
{
|
|
|
|
.sa_handler = sigusr1_handler,
|
|
|
|
};
|
|
|
|
sigemptyset (&action.sa_mask);
|
|
|
|
|
|
|
|
if (sigaction (SIGUSR1, &action, NULL) != 0)
|
|
|
|
{
|
|
|
|
printf ("error: sigaction: %m");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
action.sa_handler = liveness_signal_handler;
|
|
|
|
if (sigaction (SIGUSR2, &action, NULL) != 0)
|
|
|
|
{
|
|
|
|
printf ("error: sigaction: %m");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-05-08 13:29:13 +00:00
|
|
|
pid_t sigusr2_sender_pid = xfork ();
|
2016-05-12 06:54:17 +00:00
|
|
|
if (sigusr2_sender_pid == 0)
|
|
|
|
signal_sender (SIGUSR2, true);
|
2019-05-08 13:29:13 +00:00
|
|
|
|
|
|
|
/* Send SIGUSR1 signals from several processes. Hopefully, one
|
|
|
|
signal will hit one of the ciritical functions. Use a barrier to
|
|
|
|
avoid sending signals while not running fork/free/malloc. */
|
|
|
|
pid_t sigusr1_sender_pids[5];
|
|
|
|
{
|
|
|
|
pthread_barrierattr_t attr;
|
|
|
|
xpthread_barrierattr_init (&attr);
|
|
|
|
xpthread_barrierattr_setpshared (&attr, PTHREAD_PROCESS_SHARED);
|
|
|
|
xpthread_barrier_init (&shared->barrier, &attr,
|
|
|
|
array_length (sigusr1_sender_pids) + 1);
|
|
|
|
xpthread_barrierattr_destroy (&attr);
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < array_length (sigusr1_sender_pids); ++i)
|
|
|
|
{
|
|
|
|
sigusr1_sender_pids[i] = fork ();
|
|
|
|
if (sigusr1_sender_pids[i] == 0)
|
|
|
|
signal_sender (SIGUSR1, false);
|
|
|
|
}
|
2016-05-12 06:54:17 +00:00
|
|
|
|
|
|
|
void *objects[malloc_objects] = {};
|
2019-05-08 13:29:13 +00:00
|
|
|
unsigned int fork_signals = 0;
|
|
|
|
unsigned int free_signals = 0;
|
|
|
|
unsigned int malloc_signals = 0;
|
2016-05-12 06:54:17 +00:00
|
|
|
unsigned seed = 1;
|
2019-05-08 13:29:13 +00:00
|
|
|
for (int i = 0; i < iterations; ++i)
|
2016-05-12 06:54:17 +00:00
|
|
|
{
|
|
|
|
progress_indicator = 1;
|
|
|
|
int slot = rand_r (&seed) % malloc_objects;
|
|
|
|
size_t size = rand_r (&seed) % malloc_maximum_size;
|
2019-05-08 13:29:13 +00:00
|
|
|
|
|
|
|
/* Occasionally do a fork first, to catch deadlocks there as
|
|
|
|
well (see bug 24161). */
|
|
|
|
bool do_fork = (rand_r (&seed) % 7) == 0;
|
|
|
|
|
|
|
|
xpthread_barrier_wait (&shared->barrier);
|
|
|
|
if (do_fork)
|
2016-05-12 06:54:17 +00:00
|
|
|
{
|
2019-05-08 13:29:13 +00:00
|
|
|
sigusr1_received = 0;
|
|
|
|
pid_t pid = xfork ();
|
|
|
|
if (sigusr1_received)
|
|
|
|
++fork_signals;
|
|
|
|
if (pid == 0)
|
|
|
|
_exit (0);
|
|
|
|
int status;
|
|
|
|
int ret = TEMP_FAILURE_RETRY (waitpid (pid, &status, 0));
|
|
|
|
if (ret < 0)
|
|
|
|
FAIL_EXIT1 ("waitpid: %m");
|
|
|
|
TEST_COMPARE (status, 0);
|
2016-05-12 06:54:17 +00:00
|
|
|
}
|
2019-05-08 13:29:13 +00:00
|
|
|
sigusr1_received = 0;
|
2016-05-12 06:54:17 +00:00
|
|
|
free (objects[slot]);
|
2019-05-08 13:29:13 +00:00
|
|
|
if (sigusr1_received)
|
|
|
|
++free_signals;
|
|
|
|
sigusr1_received = 0;
|
2016-05-12 06:54:17 +00:00
|
|
|
objects[slot] = malloc (size);
|
|
|
|
if (sigusr1_received)
|
2019-05-08 13:29:13 +00:00
|
|
|
++malloc_signals;
|
|
|
|
xpthread_barrier_wait (&shared->barrier);
|
|
|
|
|
2016-05-12 06:54:17 +00:00
|
|
|
if (objects[slot] == NULL)
|
|
|
|
{
|
|
|
|
printf ("error: malloc: %m\n");
|
2019-05-08 13:29:13 +00:00
|
|
|
for (size_t i = 0; i < array_length (sigusr1_sender_pids); ++i)
|
|
|
|
kill (sigusr1_sender_pids[i], SIGKILL);
|
2016-05-12 06:54:17 +00:00
|
|
|
kill (sigusr2_sender_pid, SIGKILL);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clean up allocations. */
|
|
|
|
for (int slot = 0; slot < malloc_objects; ++slot)
|
|
|
|
free (objects[slot]);
|
|
|
|
|
2019-05-08 13:29:13 +00:00
|
|
|
for (size_t i = 0; i < array_length (sigusr1_sender_pids); ++i)
|
|
|
|
kill (sigusr1_sender_pids[i], SIGKILL);
|
2016-05-12 06:54:17 +00:00
|
|
|
kill (sigusr2_sender_pid, SIGKILL);
|
|
|
|
|
2019-05-08 13:29:13 +00:00
|
|
|
printf ("info: signals received during fork: %u\n", fork_signals);
|
|
|
|
printf ("info: signals received during free: %u\n", free_signals);
|
|
|
|
printf ("info: signals received during malloc: %u\n", malloc_signals);
|
|
|
|
|
|
|
|
/* Do not destroy the barrier because of the SIGKILL above, which
|
|
|
|
may have left the barrier in an inconsistent state. */
|
|
|
|
support_shared_free (shared);
|
|
|
|
|
2016-05-12 06:54:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2019-05-08 13:29:13 +00:00
|
|
|
|
|
|
|
#define TIMEOUT 100
|
|
|
|
#include <support/test-driver.c>
|