Blame malloc/tst-malloc-fork-deadlock.c

Packit 6c4009
/* Test concurrent fork, getline, and fflush (NULL).
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
#include <sys/wait.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <pthread.h>
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <malloc.h>
Packit 6c4009
#include <time.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <signal.h>
Packit 6c4009
Packit 6c4009
#include <support/xthread.h>
Packit 6c4009
#include <support/temp_file.h>
Packit 6c4009
#include <support/test-driver.h>
Packit 6c4009
Packit 6c4009
enum {
Packit 6c4009
  /* Number of threads which call fork.  */
Packit 6c4009
  fork_thread_count = 4,
Packit 6c4009
  /* Number of threads which call getline (and, indirectly,
Packit 6c4009
     malloc).  */
Packit 6c4009
  read_thread_count = 8,
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
static bool termination_requested;
Packit 6c4009
Packit 6c4009
static void *
Packit 6c4009
fork_thread_function (void *closure)
Packit 6c4009
{
Packit 6c4009
  while (!__atomic_load_n (&termination_requested, __ATOMIC_RELAXED))
Packit 6c4009
    {
Packit 6c4009
      pid_t pid = fork ();
Packit 6c4009
      if (pid < 0)
Packit 6c4009
        {
Packit 6c4009
          printf ("error: fork: %m\n");
Packit 6c4009
          abort ();
Packit 6c4009
        }
Packit 6c4009
      else if (pid == 0)
Packit 6c4009
        _exit (17);
Packit 6c4009
Packit 6c4009
      int status;
Packit 6c4009
      if (waitpid (pid, &status, 0) < 0)
Packit 6c4009
        {
Packit 6c4009
          printf ("error: waitpid: %m\n");
Packit 6c4009
          abort ();
Packit 6c4009
        }
Packit 6c4009
      if (!WIFEXITED (status) || WEXITSTATUS (status) != 17)
Packit 6c4009
        {
Packit 6c4009
          printf ("error: waitpid returned invalid status: %d\n", status);
Packit 6c4009
          abort ();
Packit 6c4009
        }
Packit 6c4009
    }
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static char *file_to_read;
Packit 6c4009
Packit 6c4009
static void *
Packit 6c4009
read_thread_function (void *closure)
Packit 6c4009
{
Packit 6c4009
  FILE *f = fopen (file_to_read, "r");
Packit 6c4009
  if (f == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("error: fopen (%s): %m\n", file_to_read);
Packit 6c4009
      abort ();
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  while (!__atomic_load_n (&termination_requested, __ATOMIC_RELAXED))
Packit 6c4009
    {
Packit 6c4009
      rewind (f);
Packit 6c4009
      char *line = NULL;
Packit 6c4009
      size_t line_allocated = 0;
Packit 6c4009
      ssize_t ret = getline (&line, &line_allocated, f);
Packit 6c4009
      if (ret < 0)
Packit 6c4009
        {
Packit 6c4009
          printf ("error: getline: %m\n");
Packit 6c4009
          abort ();
Packit 6c4009
        }
Packit 6c4009
      free (line);
Packit 6c4009
    }
Packit 6c4009
  fclose (f);
Packit 6c4009
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void *
Packit 6c4009
flushall_thread_function (void *closure)
Packit 6c4009
{
Packit 6c4009
  while (!__atomic_load_n (&termination_requested, __ATOMIC_RELAXED))
Packit 6c4009
    if (fflush (NULL) != 0)
Packit 6c4009
      {
Packit 6c4009
        printf ("error: fflush (NULL): %m\n");
Packit 6c4009
        abort ();
Packit 6c4009
      }
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
create_threads (pthread_t *threads, size_t count, void *(*func) (void *))
Packit 6c4009
{
Packit 6c4009
  for (size_t i = 0; i < count; ++i)
Packit 6c4009
    threads[i] = xpthread_create (NULL, func, NULL);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
join_threads (pthread_t *threads, size_t count)
Packit 6c4009
{
Packit 6c4009
  for (size_t i = 0; i < count; ++i)
Packit 6c4009
    xpthread_join (threads[i]);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Create a file which consists of a single long line, and assigns
Packit 6c4009
   file_to_read.  The hope is that this triggers an allocation in
Packit 6c4009
   getline which needs a lock.  */
Packit 6c4009
static void
Packit 6c4009
create_file_with_large_line (void)
Packit 6c4009
{
Packit 6c4009
  int fd = create_temp_file ("bug19431-large-line", &file_to_read);
Packit 6c4009
  if (fd < 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("error: create_temp_file: %m\n");
Packit 6c4009
      abort ();
Packit 6c4009
    }
Packit 6c4009
  FILE *f = fdopen (fd, "w+");
Packit 6c4009
  if (f == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("error: fdopen: %m\n");
Packit 6c4009
      abort ();
Packit 6c4009
    }
Packit 6c4009
  for (int i = 0; i < 50000; ++i)
Packit 6c4009
    fputc ('x', f);
Packit 6c4009
  fputc ('\n', f);
Packit 6c4009
  if (ferror (f))
Packit 6c4009
    {
Packit 6c4009
      printf ("error: fputc: %m\n");
Packit 6c4009
      abort ();
Packit 6c4009
    }
Packit 6c4009
  if (fclose (f) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("error: fclose: %m\n");
Packit 6c4009
      abort ();
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  /* Make sure that we do not exceed the arena limit with the number
Packit 6c4009
     of threads we configured.  */
Packit 6c4009
  if (mallopt (M_ARENA_MAX, 400) == 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("error: mallopt (M_ARENA_MAX) failed\n");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Leave some room for shutting down all threads gracefully.  */
Packit 6c4009
  int timeout = 3;
Packit 6c4009
  if (timeout > DEFAULT_TIMEOUT)
Packit 6c4009
    timeout = DEFAULT_TIMEOUT - 1;
Packit 6c4009
Packit 6c4009
  create_file_with_large_line ();
Packit 6c4009
Packit 6c4009
  pthread_t fork_threads[fork_thread_count];
Packit 6c4009
  create_threads (fork_threads, fork_thread_count, fork_thread_function);
Packit 6c4009
  pthread_t read_threads[read_thread_count];
Packit 6c4009
  create_threads (read_threads, read_thread_count, read_thread_function);
Packit 6c4009
  pthread_t flushall_threads[1];
Packit 6c4009
  create_threads (flushall_threads, 1, flushall_thread_function);
Packit 6c4009
Packit 6c4009
  struct timespec ts = {timeout, 0};
Packit 6c4009
  if (nanosleep (&ts, NULL))
Packit 6c4009
    {
Packit 6c4009
      printf ("error: error: nanosleep: %m\n");
Packit 6c4009
      abort ();
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  __atomic_store_n (&termination_requested, true, __ATOMIC_RELAXED);
Packit 6c4009
Packit 6c4009
  join_threads (flushall_threads, 1);
Packit 6c4009
  join_threads (read_threads, read_thread_count);
Packit 6c4009
  join_threads (fork_threads, fork_thread_count);
Packit 6c4009
Packit 6c4009
  free (file_to_read);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>