Blame malloc/tst-malloc-thread-exit.c

Packit 6c4009
/* Test malloc with concurrent thread termination.
Packit 6c4009
   Copyright (C) 2015-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
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the 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; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
/* This thread spawns a number of outer threads, equal to the arena
Packit 6c4009
   limit.  The outer threads run a loop which start and join two
Packit 6c4009
   different kinds of threads: the first kind allocates (attaching an
Packit 6c4009
   arena to the thread; malloc_first_thread) and waits, the second
Packit 6c4009
   kind waits and allocates (wait_first_threads).  Both kinds of
Packit 6c4009
   threads exit immediately after waiting.  The hope is that this will
Packit 6c4009
   exhibit races in thread termination and arena management,
Packit 6c4009
   particularly related to the arena free list.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <malloc.h>
Packit 6c4009
#include <pthread.h>
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
#include <support/support.h>
Packit 6c4009
#include <support/xthread.h>
Packit 6c4009
#include <support/test-driver.h>
Packit 6c4009
Packit 6c4009
static bool termination_requested;
Packit 6c4009
static int inner_thread_count = 4;
Packit 6c4009
static size_t malloc_size = 32;
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
__attribute__ ((noinline, noclone))
Packit 6c4009
unoptimized_free (void *ptr)
Packit 6c4009
{
Packit 6c4009
  free (ptr);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void *
Packit 6c4009
malloc_first_thread (void * closure)
Packit 6c4009
{
Packit 6c4009
  pthread_barrier_t *barrier = closure;
Packit 6c4009
  void *ptr = xmalloc (malloc_size);
Packit 6c4009
  xpthread_barrier_wait (barrier);
Packit 6c4009
  unoptimized_free (ptr);
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void *
Packit 6c4009
wait_first_thread (void * closure)
Packit 6c4009
{
Packit 6c4009
  pthread_barrier_t *barrier = closure;
Packit 6c4009
  xpthread_barrier_wait (barrier);
Packit 6c4009
  void *ptr = xmalloc (malloc_size);
Packit 6c4009
  unoptimized_free (ptr);
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void *
Packit 6c4009
outer_thread (void *closure)
Packit 6c4009
{
Packit 6c4009
  pthread_t *threads = xcalloc (sizeof (*threads), inner_thread_count);
Packit 6c4009
  while (!__atomic_load_n (&termination_requested, __ATOMIC_RELAXED))
Packit 6c4009
    {
Packit 6c4009
      pthread_barrier_t barrier;
Packit 6c4009
      xpthread_barrier_init (&barrier, NULL, inner_thread_count + 1);
Packit 6c4009
      for (int i = 0; i < inner_thread_count; ++i)
Packit 6c4009
        {
Packit 6c4009
          void *(*func) (void *);
Packit 6c4009
          if ((i  % 2) == 0)
Packit 6c4009
            func = malloc_first_thread;
Packit 6c4009
          else
Packit 6c4009
            func = wait_first_thread;
Packit 6c4009
          threads[i] = xpthread_create (NULL, func, &barrier);
Packit 6c4009
        }
Packit 6c4009
      xpthread_barrier_wait (&barrier);
Packit 6c4009
      for (int i = 0; i < inner_thread_count; ++i)
Packit 6c4009
        xpthread_join (threads[i]);
Packit 6c4009
      xpthread_barrier_destroy (&barrier);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  free (threads);
Packit 6c4009
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  /* The number of threads should be smaller than the number of
Packit 6c4009
     arenas, so that there will be some free arenas to add to the
Packit 6c4009
     arena free list.  */
Packit 6c4009
  enum { outer_thread_count = 2 };
Packit 6c4009
  if (mallopt (M_ARENA_MAX, 8) == 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
  pthread_t *threads = xcalloc (sizeof (*threads), outer_thread_count);
Packit 6c4009
  for (long i = 0; i < outer_thread_count; ++i)
Packit 6c4009
    threads[i] = xpthread_create (NULL, outer_thread, NULL);
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
  for (long i = 0; i < outer_thread_count; ++i)
Packit 6c4009
    xpthread_join (threads[i]);
Packit 6c4009
  free (threads);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>