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

Packit 6c4009
/* Test allocation function behavior on allocation failure.
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 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 case attempts to trigger various unusual conditions
Packit 6c4009
   related to allocation failures, notably switching to a different
Packit 6c4009
   arena, and falling back to mmap (via sysmalloc).  */
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 <stdint.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <sys/resource.h>
Packit 6c4009
#include <sys/wait.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
/* Wrapper for calloc with an optimization barrier.  */
Packit 6c4009
static void *
Packit 6c4009
__attribute__ ((noinline, noclone))
Packit 6c4009
allocate_zeroed (size_t a, size_t b)
Packit 6c4009
{
Packit 6c4009
  return calloc (a, b);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* System page size, as determined by sysconf (_SC_PAGE_SIZE).  */
Packit 6c4009
static unsigned long page_size;
Packit 6c4009
Packit 6c4009
/* Test parameters. */
Packit 6c4009
static size_t allocation_size;
Packit 6c4009
static size_t alignment;
Packit 6c4009
static enum {
Packit 6c4009
  with_malloc,
Packit 6c4009
  with_realloc,
Packit 6c4009
  with_aligned_alloc,
Packit 6c4009
  with_memalign,
Packit 6c4009
  with_posix_memalign,
Packit 6c4009
  with_valloc,
Packit 6c4009
  with_pvalloc,
Packit 6c4009
  with_calloc,
Packit 6c4009
  last_allocation_function = with_calloc
Packit 6c4009
} allocation_function;
Packit 6c4009
Packit 6c4009
/* True if an allocation function uses the alignment test
Packit 6c4009
   parameter.  */
Packit 6c4009
const static bool alignment_sensitive[last_allocation_function + 1] =
Packit 6c4009
  {
Packit 6c4009
    [with_aligned_alloc] = true,
Packit 6c4009
    [with_memalign] = true,
Packit 6c4009
    [with_posix_memalign] = true,
Packit 6c4009
  };
Packit 6c4009
Packit 6c4009
/* Combined pointer/expected alignment result of an allocation
Packit 6c4009
   function.  */
Packit 6c4009
struct allocate_result {
Packit 6c4009
  void *pointer;
Packit 6c4009
  size_t alignment;
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
/* Call the allocation function specified by allocation_function, with
Packit 6c4009
   allocation_size and alignment (if applicable) as arguments.  No
Packit 6c4009
   alignment check.  */
Packit 6c4009
static struct allocate_result
Packit 6c4009
allocate_1 (void)
Packit 6c4009
{
Packit 6c4009
  switch (allocation_function)
Packit 6c4009
    {
Packit 6c4009
    case with_malloc:
Packit 6c4009
      return (struct allocate_result)
Packit 6c4009
        {malloc (allocation_size), _Alignof (max_align_t)};
Packit 6c4009
    case with_realloc:
Packit 6c4009
      {
Packit 6c4009
        void *p = realloc (NULL, 16);
Packit 6c4009
        void *q;
Packit 6c4009
        if (p == NULL)
Packit 6c4009
          q = NULL;
Packit 6c4009
        else
Packit 6c4009
          {
Packit 6c4009
            q = realloc (p, allocation_size);
Packit 6c4009
            if (q == NULL)
Packit 6c4009
              free (p);
Packit 6c4009
          }
Packit 6c4009
        return (struct allocate_result) {q, _Alignof (max_align_t)};
Packit 6c4009
      }
Packit 6c4009
    case with_aligned_alloc:
Packit 6c4009
      {
Packit 6c4009
        void *p = aligned_alloc (alignment, allocation_size);
Packit 6c4009
        return (struct allocate_result) {p, alignment};
Packit 6c4009
      }
Packit 6c4009
    case with_memalign:
Packit 6c4009
      {
Packit 6c4009
        void *p = memalign (alignment, allocation_size);
Packit 6c4009
        return (struct allocate_result) {p, alignment};
Packit 6c4009
      }
Packit 6c4009
    case with_posix_memalign:
Packit 6c4009
      {
Packit 6c4009
        void *p;
Packit 6c4009
        if (posix_memalign (&p, alignment, allocation_size))
Packit 6c4009
          {
Packit 6c4009
            if (errno == ENOMEM)
Packit 6c4009
              p = NULL;
Packit 6c4009
            else
Packit 6c4009
              {
Packit 6c4009
                printf ("error: posix_memalign (p, %zu, %zu): %m\n",
Packit 6c4009
                        alignment, allocation_size);
Packit 6c4009
                abort ();
Packit 6c4009
              }
Packit 6c4009
          }
Packit 6c4009
        return (struct allocate_result) {p, alignment};
Packit 6c4009
      }
Packit 6c4009
    case with_valloc:
Packit 6c4009
      {
Packit 6c4009
        void *p = valloc (allocation_size);
Packit 6c4009
        return (struct allocate_result) {p, page_size};
Packit 6c4009
      }
Packit 6c4009
    case with_pvalloc:
Packit 6c4009
      {
Packit 6c4009
        void *p = pvalloc (allocation_size);
Packit 6c4009
        return (struct allocate_result) {p, page_size};
Packit 6c4009
      }
Packit 6c4009
    case with_calloc:
Packit 6c4009
      {
Packit 6c4009
        char *p = allocate_zeroed (1, allocation_size);
Packit 6c4009
        /* Check for non-zero bytes.  */
Packit 6c4009
        if (p != NULL)
Packit 6c4009
          for (size_t i = 0; i < allocation_size; ++i)
Packit 6c4009
            if (p[i] != 0)
Packit 6c4009
              {
Packit 6c4009
                printf ("error: non-zero byte at offset %zu\n", i);
Packit 6c4009
                abort ();
Packit 6c4009
              }
Packit 6c4009
        return (struct allocate_result) {p, _Alignof (max_align_t)};
Packit 6c4009
      }
Packit 6c4009
    }
Packit 6c4009
  abort ();
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Call allocate_1 and perform the alignment check on the result.  */
Packit 6c4009
static void *
Packit 6c4009
allocate (void)
Packit 6c4009
{
Packit 6c4009
  struct allocate_result r = allocate_1 ();
Packit 6c4009
  if ((((uintptr_t) r.pointer) & (r.alignment - 1)) != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("error: allocation function %d, size %zu not aligned to %zu\n",
Packit 6c4009
              (int) allocation_function, allocation_size, r.alignment);
Packit 6c4009
      abort ();
Packit 6c4009
    }
Packit 6c4009
  return r.pointer;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Barriers to synchronize thread creation and termination.  */
Packit 6c4009
static pthread_barrier_t start_barrier;
Packit 6c4009
static pthread_barrier_t end_barrier;
Packit 6c4009
Packit 6c4009
/* Thread function which performs the allocation test.  Called by
Packit 6c4009
   pthread_create and from the main thread.  */
Packit 6c4009
static void *
Packit 6c4009
allocate_thread (void *closure)
Packit 6c4009
{
Packit 6c4009
  /* Wait for the creation of all threads.  */
Packit 6c4009
  {
Packit 6c4009
    int ret = pthread_barrier_wait (&start_barrier);
Packit 6c4009
    if (ret != 0 && ret != PTHREAD_BARRIER_SERIAL_THREAD)
Packit 6c4009
      {
Packit 6c4009
        errno = ret;
Packit 6c4009
        printf ("error: pthread_barrier_wait: %m\n");
Packit 6c4009
        abort ();
Packit 6c4009
      }
Packit 6c4009
  }
Packit 6c4009
Packit 6c4009
  /* Allocate until we run out of memory, creating a single-linked
Packit 6c4009
     list.  */
Packit 6c4009
  struct list {
Packit 6c4009
    struct list *next;
Packit 6c4009
  };
Packit 6c4009
  struct list *head = NULL;
Packit 6c4009
  while (true)
Packit 6c4009
    {
Packit 6c4009
      struct list *e = allocate ();
Packit 6c4009
      if (e == NULL)
Packit 6c4009
        break;
Packit 6c4009
Packit 6c4009
      e->next = head;
Packit 6c4009
      head = e;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Wait for the allocation of all available memory.  */
Packit 6c4009
  {
Packit 6c4009
    int ret = pthread_barrier_wait (&end_barrier);
Packit 6c4009
    if (ret != 0 && ret != PTHREAD_BARRIER_SERIAL_THREAD)
Packit 6c4009
      {
Packit 6c4009
        errno = ret;
Packit 6c4009
        printf ("error: pthread_barrier_wait: %m\n");
Packit 6c4009
        abort ();
Packit 6c4009
      }
Packit 6c4009
  }
Packit 6c4009
Packit 6c4009
  /* Free the allocated memory.  */
Packit 6c4009
  while (head != NULL)
Packit 6c4009
    {
Packit 6c4009
      struct list *next = head->next;
Packit 6c4009
      free (head);
Packit 6c4009
      head = next;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Number of threads (plus the main thread.  */
Packit 6c4009
enum { thread_count = 8 };
Packit 6c4009
Packit 6c4009
/* Thread attribute to request creation of threads with a non-default
Packit 6c4009
   stack size which is rather small.  This avoids interfering with the
Packit 6c4009
   configured address space limit.  */
Packit 6c4009
static pthread_attr_t small_stack;
Packit 6c4009
Packit 6c4009
/* Runs one test in multiple threads, all in a subprocess so that
Packit 6c4009
   subsequent tests do not interfere with each other.  */
Packit 6c4009
static void
Packit 6c4009
run_one (void)
Packit 6c4009
{
Packit 6c4009
  /* Isolate the tests in a subprocess, so that we can start over
Packit 6c4009
     from scratch.  */
Packit 6c4009
  pid_t pid = fork ();
Packit 6c4009
  if (pid == 0)
Packit 6c4009
    {
Packit 6c4009
      /* In the child process.  Create the allocation threads.  */
Packit 6c4009
      pthread_t threads[thread_count];
Packit 6c4009
Packit 6c4009
      for (unsigned i = 0; i < thread_count; ++i)
Packit 6c4009
        {
Packit 6c4009
          int ret = pthread_create (threads + i, &small_stack, allocate_thread, NULL);
Packit 6c4009
          if (ret != 0)
Packit 6c4009
            {
Packit 6c4009
              errno = ret;
Packit 6c4009
              printf ("error: pthread_create: %m\n");
Packit 6c4009
              abort ();
Packit 6c4009
            }
Packit 6c4009
        }
Packit 6c4009
Packit 6c4009
      /* Also run the test on the main thread.  */
Packit 6c4009
      allocate_thread (NULL);
Packit 6c4009
Packit 6c4009
      for (unsigned i = 0; i < thread_count; ++i)
Packit 6c4009
        {
Packit 6c4009
          int ret = pthread_join (threads[i], NULL);
Packit 6c4009
          if (ret != 0)
Packit 6c4009
            {
Packit 6c4009
              errno = ret;
Packit 6c4009
              printf ("error: pthread_join: %m\n");
Packit 6c4009
              abort ();
Packit 6c4009
            }
Packit 6c4009
        }
Packit 6c4009
      _exit (0);
Packit 6c4009
    }
Packit 6c4009
  else if (pid < 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("error: fork: %m\n");
Packit 6c4009
      abort ();
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* In the parent process.  Wait for the child process to exit.  */
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 (status != 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("error: exit status %d from child process\n", status);
Packit 6c4009
      exit (1);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Run all applicable allocation functions for the current test
Packit 6c4009
   parameters.  */
Packit 6c4009
static void
Packit 6c4009
run_allocation_functions (void)
Packit 6c4009
{
Packit 6c4009
  for (int af = 0; af <= last_allocation_function; ++af)
Packit 6c4009
    {
Packit 6c4009
      /* Run alignment-sensitive functions for non-default
Packit 6c4009
         alignments.  */
Packit 6c4009
      if (alignment_sensitive[af] != (alignment != 0))
Packit 6c4009
        continue;
Packit 6c4009
      allocation_function = af;
Packit 6c4009
      run_one ();
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  /* Limit the number of malloc arenas.  We use a very low number so
Packit 6c4009
     that despute the address space limit configured below, all
Packit 6c4009
     requested arenas a can be created.  */
Packit 6c4009
  if (mallopt (M_ARENA_MAX, 2) == 0)
Packit 6c4009
    {
Packit 6c4009
      printf ("error: mallopt (M_ARENA_MAX) failed\n");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Determine the page size.  */
Packit 6c4009
  {
Packit 6c4009
    long ret = sysconf (_SC_PAGE_SIZE);
Packit 6c4009
    if (ret < 0)
Packit 6c4009
      {
Packit 6c4009
        printf ("error: sysconf (_SC_PAGE_SIZE): %m\n");
Packit 6c4009
        return 1;
Packit 6c4009
      }
Packit 6c4009
    page_size = ret;
Packit 6c4009
  }
Packit 6c4009
Packit 6c4009
  /* Limit the size of the process, so that memory allocation in
Packit 6c4009
     allocate_thread will eventually fail, without impacting the
Packit 6c4009
     entire system.  */
Packit 6c4009
  {
Packit 6c4009
    struct rlimit limit;
Packit 6c4009
    if (getrlimit (RLIMIT_AS, &limit) != 0)
Packit 6c4009
      {
Packit 6c4009
        printf ("getrlimit (RLIMIT_AS) failed: %m\n");
Packit 6c4009
        return 1;
Packit 6c4009
      }
Packit 6c4009
    long target = 200 * 1024 * 1024;
Packit 6c4009
    if (limit.rlim_cur == RLIM_INFINITY || limit.rlim_cur > target)
Packit 6c4009
      {
Packit 6c4009
        limit.rlim_cur = target;
Packit 6c4009
        if (setrlimit (RLIMIT_AS, &limit) != 0)
Packit 6c4009
          {
Packit 6c4009
            printf ("setrlimit (RLIMIT_AS) failed: %m\n");
Packit 6c4009
            return 1;
Packit 6c4009
          }
Packit 6c4009
      }
Packit 6c4009
  }
Packit 6c4009
Packit 6c4009
  /* Initialize thread attribute with a reduced stack size.  */
Packit 6c4009
  {
Packit 6c4009
    int ret = pthread_attr_init (&small_stack);
Packit 6c4009
    if (ret != 0)
Packit 6c4009
      {
Packit 6c4009
        errno = ret;
Packit 6c4009
        printf ("error: pthread_attr_init: %m\n");
Packit 6c4009
        abort ();
Packit 6c4009
      }
Packit 6c4009
    unsigned long stack_size = ((256 * 1024) / page_size) * page_size;
Packit 6c4009
    if (stack_size < 4 * page_size)
Packit 6c4009
      stack_size = 8 * page_size;
Packit 6c4009
    ret = pthread_attr_setstacksize (&small_stack, stack_size);
Packit 6c4009
    if (ret != 0)
Packit 6c4009
      {
Packit 6c4009
        errno = ret;
Packit 6c4009
        printf ("error: pthread_attr_setstacksize: %m\n");
Packit 6c4009
        abort ();
Packit 6c4009
      }
Packit 6c4009
  }
Packit 6c4009
Packit 6c4009
  /* Initialize the barriers.  We run thread_count threads, plus 1 for
Packit 6c4009
     the main thread.  */
Packit 6c4009
  {
Packit 6c4009
    int ret = pthread_barrier_init (&start_barrier, NULL, thread_count + 1);
Packit 6c4009
    if (ret != 0)
Packit 6c4009
      {
Packit 6c4009
        errno = ret;
Packit 6c4009
        printf ("error: pthread_barrier_init: %m\n");
Packit 6c4009
        abort ();
Packit 6c4009
      }
Packit 6c4009
Packit 6c4009
    ret = pthread_barrier_init (&end_barrier, NULL, thread_count + 1);
Packit 6c4009
    if (ret != 0)
Packit 6c4009
      {
Packit 6c4009
        errno = ret;
Packit 6c4009
        printf ("error: pthread_barrier_init: %m\n");
Packit 6c4009
        abort ();
Packit 6c4009
      }
Packit 6c4009
  }
Packit 6c4009
Packit 6c4009
  allocation_size = 144;
Packit 6c4009
  run_allocation_functions ();
Packit 6c4009
  allocation_size = page_size;
Packit 6c4009
  run_allocation_functions ();
Packit 6c4009
Packit 6c4009
  alignment = 128;
Packit 6c4009
  allocation_size = 512;
Packit 6c4009
  run_allocation_functions ();
Packit 6c4009
Packit 6c4009
  allocation_size = page_size;
Packit 6c4009
  run_allocation_functions ();
Packit 6c4009
Packit 6c4009
  allocation_size = 17 * page_size;
Packit 6c4009
  run_allocation_functions ();
Packit 6c4009
Packit 6c4009
  /* Deallocation the barriers and the thread attribute.  */
Packit 6c4009
  {
Packit 6c4009
    int ret = pthread_barrier_destroy (&end_barrier);
Packit 6c4009
    if (ret != 0)
Packit 6c4009
      {
Packit 6c4009
        errno = ret;
Packit 6c4009
        printf ("error: pthread_barrier_destroy: %m\n");
Packit 6c4009
        return 1;
Packit 6c4009
      }
Packit 6c4009
    ret = pthread_barrier_destroy (&start_barrier);
Packit 6c4009
    if (ret != 0)
Packit 6c4009
      {
Packit 6c4009
        errno = ret;
Packit 6c4009
        printf ("error: pthread_barrier_destroy: %m\n");
Packit 6c4009
        return 1;
Packit 6c4009
      }
Packit 6c4009
    ret = pthread_attr_destroy (&small_stack);
Packit 6c4009
    if (ret != 0)
Packit 6c4009
      {
Packit 6c4009
        errno = ret;
Packit 6c4009
        printf ("error: pthread_attr_destroy: %m\n");
Packit 6c4009
        return 1;
Packit 6c4009
      }
Packit 6c4009
  }
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* The repeated allocations take some time on slow machines.  */
Packit 6c4009
#define TIMEOUT 100
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"