Blame malloc/tst-mallocfork2.c

Packit 6c4009
/* Test case for async-signal-safe fork (with respect to malloc).
Packit 6c4009
   Copyright (C) 2016-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 License as
Packit 6c4009
   published by the Free Software Foundation; either version 2.1 of the
Packit 6c4009
   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; see the file COPYING.LIB.  If
Packit 6c4009
   not, see <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
/* This test will fail if the process is multi-threaded because we
Packit 6c4009
   only have an async-signal-safe fork in the single-threaded case
Packit 6c4009
   (where we skip acquiring the malloc heap locks).
Packit 6c4009
Packit 6c4009
   This test only checks async-signal-safety with regards to malloc;
Packit 6c4009
   other, more rarely-used glibc subsystems could have locks which
Packit 6c4009
   still make fork unsafe, even in single-threaded processes.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <sched.h>
Packit 6c4009
#include <signal.h>
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <sys/wait.h>
Packit 6c4009
#include <time.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
/* How many malloc objects to keep arond.  */
Packit 6c4009
enum { malloc_objects = 1009 };
Packit 6c4009
Packit 6c4009
/* The maximum size of an object.  */
Packit 6c4009
enum { malloc_maximum_size = 70000 };
Packit 6c4009
Packit 6c4009
/* How many signals need to be delivered before the test exits.  */
Packit 6c4009
enum { signal_count = 1000 };
Packit 6c4009
Packit 6c4009
static int do_test (void);
Packit 6c4009
#define TIMEOUT 100
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"
Packit 6c4009
Packit 6c4009
/* Process ID of the subprocess which sends SIGUSR1 signals.  */
Packit 6c4009
static pid_t sigusr1_sender_pid;
Packit 6c4009
Packit 6c4009
/* Set to 1 if SIGUSR1 is received.  Used to detect a signal during
Packit 6c4009
   malloc/free.  */
Packit 6c4009
static volatile sig_atomic_t sigusr1_received;
Packit 6c4009
Packit 6c4009
/* Periodically set to 1, to indicate that the process is making
Packit 6c4009
   progress.  Checked by liveness_signal_handler.  */
Packit 6c4009
static volatile sig_atomic_t progress_indicator = 1;
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
sigusr1_handler (int signo)
Packit 6c4009
{
Packit 6c4009
  /* Let the main program make progress, by temporarily suspending
Packit 6c4009
     signals from the subprocess.  */
Packit 6c4009
  if (sigusr1_received)
Packit 6c4009
    return;
Packit 6c4009
  /* sigusr1_sender_pid might not be initialized in the parent when
Packit 6c4009
     the first SIGUSR1 signal arrives.  */
Packit 6c4009
  if (sigusr1_sender_pid > 0 && kill (sigusr1_sender_pid, SIGSTOP) != 0)
Packit 6c4009
    {
Packit 6c4009
      write_message ("error: kill (SIGSTOP)\n");
Packit 6c4009
      abort ();
Packit 6c4009
    }
Packit 6c4009
  sigusr1_received = 1;
Packit 6c4009
Packit 6c4009
  /* Perform a fork with a trivial subprocess.  */
Packit 6c4009
  pid_t pid = fork ();
Packit 6c4009
  if (pid == -1)
Packit 6c4009
    {
Packit 6c4009
      write_message ("error: fork\n");
Packit 6c4009
      abort ();
Packit 6c4009
    }
Packit 6c4009
  if (pid == 0)
Packit 6c4009
    _exit (0);
Packit 6c4009
  int status;
Packit 6c4009
  int ret = TEMP_FAILURE_RETRY (waitpid (pid, &status, 0));
Packit 6c4009
  if (ret < 0)
Packit 6c4009
    {
Packit 6c4009
      write_message ("error: waitpid\n");
Packit 6c4009
      abort ();
Packit 6c4009
    }
Packit 6c4009
  if (status != 0)
Packit 6c4009
    {
Packit 6c4009
      write_message ("error: unexpected exit status from subprocess\n");
Packit 6c4009
      abort ();
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
liveness_signal_handler (int signo)
Packit 6c4009
{
Packit 6c4009
  if (progress_indicator)
Packit 6c4009
    progress_indicator = 0;
Packit 6c4009
  else
Packit 6c4009
    write_message ("warning: process seems to be stuck\n");
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
__attribute__ ((noreturn))
Packit 6c4009
signal_sender (int signo, bool sleep)
Packit 6c4009
{
Packit 6c4009
  pid_t target = getppid ();
Packit 6c4009
  while (true)
Packit 6c4009
    {
Packit 6c4009
      if (kill (target, signo) != 0)
Packit 6c4009
        {
Packit 6c4009
          dprintf (STDOUT_FILENO, "error: kill: %m\n");
Packit 6c4009
          abort ();
Packit 6c4009
        }
Packit 6c4009
      if (sleep)
Packit 6c4009
        usleep (1 * 1000 * 1000);
Packit 6c4009
      else
Packit 6c4009
        /* Reduce the rate at which we send signals.  */
Packit 6c4009
        sched_yield ();
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  struct sigaction action =
Packit 6c4009
    {
Packit 6c4009
      .sa_handler = sigusr1_handler,
Packit 6c4009
    };
Packit 6c4009
  sigemptyset (&action.sa_mask);
Packit 6c4009
Packit 6c4009
  if (sigaction (SIGUSR1, &action, NULL) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("error: sigaction: %m");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  action.sa_handler = liveness_signal_handler;
Packit 6c4009
  if (sigaction (SIGUSR2, &action, NULL) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("error: sigaction: %m");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  pid_t sigusr2_sender_pid = fork ();
Packit 6c4009
  if (sigusr2_sender_pid == 0)
Packit 6c4009
    signal_sender (SIGUSR2, true);
Packit 6c4009
  sigusr1_sender_pid = fork ();
Packit 6c4009
  if (sigusr1_sender_pid == 0)
Packit 6c4009
    signal_sender (SIGUSR1, false);
Packit 6c4009
Packit 6c4009
  void *objects[malloc_objects] = {};
Packit 6c4009
  unsigned signals = 0;
Packit 6c4009
  unsigned seed = 1;
Packit 6c4009
  time_t last_report = 0;
Packit 6c4009
  while (signals < signal_count)
Packit 6c4009
    {
Packit 6c4009
      progress_indicator = 1;
Packit 6c4009
      int slot = rand_r (&seed) % malloc_objects;
Packit 6c4009
      size_t size = rand_r (&seed) % malloc_maximum_size;
Packit 6c4009
      if (kill (sigusr1_sender_pid, SIGCONT) != 0)
Packit 6c4009
        {
Packit 6c4009
          printf ("error: kill (SIGCONT): %m\n");
Packit 6c4009
          signal (SIGUSR1, SIG_IGN);
Packit 6c4009
          kill (sigusr1_sender_pid, SIGKILL);
Packit 6c4009
          kill (sigusr2_sender_pid, SIGKILL);
Packit 6c4009
          return 1;
Packit 6c4009
        }
Packit 6c4009
      sigusr1_received = false;
Packit 6c4009
      free (objects[slot]);
Packit 6c4009
      objects[slot] = malloc (size);
Packit 6c4009
      if (sigusr1_received)
Packit 6c4009
        {
Packit 6c4009
          ++signals;
Packit 6c4009
          time_t current = time (0);
Packit 6c4009
          if (current != last_report)
Packit 6c4009
            {
Packit 6c4009
              printf ("info: SIGUSR1 signal count: %u\n", signals);
Packit 6c4009
              last_report = current;
Packit 6c4009
            }
Packit 6c4009
        }
Packit 6c4009
      if (objects[slot] == NULL)
Packit 6c4009
        {
Packit 6c4009
          printf ("error: malloc: %m\n");
Packit 6c4009
          signal (SIGUSR1, SIG_IGN);
Packit 6c4009
          kill (sigusr1_sender_pid, SIGKILL);
Packit 6c4009
          kill (sigusr2_sender_pid, SIGKILL);
Packit 6c4009
          return 1;
Packit 6c4009
        }
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Clean up allocations.  */
Packit 6c4009
  for (int slot = 0; slot < malloc_objects; ++slot)
Packit 6c4009
    free (objects[slot]);
Packit 6c4009
Packit 6c4009
  /* Terminate the signal-sending subprocess.  The SIGUSR1 handler
Packit 6c4009
     should no longer run because it uses sigusr1_sender_pid.  */
Packit 6c4009
  signal (SIGUSR1, SIG_IGN);
Packit 6c4009
  kill (sigusr1_sender_pid, SIGKILL);
Packit 6c4009
  kill (sigusr2_sender_pid, SIGKILL);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}