f57669
Short description: malloc: Test various special cases related to allocation failures
f57669
Author(s): Florian Weimer <fweimer@redhat.com>
f57669
Origin: git://sourceware.org/git/glibc.git
f57669
Bug-RHEL: #1296453 (rhel-7.2.z),  #1293976 (rhel-7.3), #1256285 (SRT)
f57669
Bug-Fedora: NA
f57669
Bug-Upstream: NA
f57669
Upstream status: committed
f57669
#
f57669
# commit 1bd5483e104c8bde6e61dc5e3f8a848bc861872d
f57669
# Author: Florian Weimer <fweimer@redhat.com>
f57669
# Date:   Tue Dec 29 20:32:35 2015 +0100
f57669
# 
f57669
#     malloc: Test various special cases related to allocation failures
f57669
#     
f57669
#     This test case exercises unusual code paths in allocation functions,
f57669
#     related to allocation failures.  Specifically, the test can reveal
f57669
#     the following bugs:
f57669
#     
f57669
#     (a) calloc returns non-zero memory on fallback to sysmalloc.
f57669
#     (b) calloc can self-deadlock because it fails to release
f57669
#         the arena lock on certain allocation failures.
f57669
#     (c) pvalloc can dereference a NULL arena pointer.
f57669
#     
f57669
#     (a) and (b) appear specific to a faulty downstream backport.
f57669
#     (c) was fixed as part of commit 10ad46bc6526edc5c7afcc57112da96917ff3629.
f57669
#     
f57669
#     The test for (a) was inspired by a reproducer supplied by Jeff Layton.
f57669
# 
f57669
Index: glibc-2.17-c758a686/malloc/Makefile
f57669
===================================================================
f57669
--- glibc-2.17-c758a686.orig/malloc/Makefile
f57669
+++ glibc-2.17-c758a686/malloc/Makefile
bca718
@@ -27,7 +27,8 @@ headers := $(dist-headers) obstack.h mch
f57669
 tests := mallocbug tst-malloc tst-valloc tst-calloc tst-obstack \
f57669
 	 tst-mallocstate tst-mcheck tst-mallocfork tst-trim1 \
bca718
 	 tst-malloc-usable \
bca718
-	 tst-malloc-backtrace tst-malloc-thread-exit
bca718
+	 tst-malloc-backtrace tst-malloc-thread-exit \
f57669
+	 tst-malloc-thread-fail
f57669
 test-srcs = tst-mtrace
f57669
 
f57669
 routines = malloc morecore mcheck mtrace obstack
bca718
@@ -44,6 +45,8 @@ libmemusage-inhibit-o = $(filter-out .os
f57669
 
f57669
 $(objpfx)tst-malloc-backtrace: $(common-objpfx)nptl/libpthread.so \
f57669
 			       $(common-objpfx)nptl/libpthread_nonshared.a
f57669
+$(objpfx)tst-malloc-thread-fail: $(common-objpfx)nptl/libpthread.so \
f57669
+			       $(common-objpfx)nptl/libpthread_nonshared.a
bca718
 $(objpfx)tst-malloc-thread-exit: $(common-objpfx)nptl/libpthread.so \
bca718
 			       $(common-objpfx)nptl/libpthread_nonshared.a
f57669
 
f57669
Index: glibc-2.17-c758a686/malloc/tst-malloc-thread-fail.c
f57669
===================================================================
f57669
--- /dev/null
f57669
+++ glibc-2.17-c758a686/malloc/tst-malloc-thread-fail.c
f57669
@@ -0,0 +1,468 @@
f57669
+/* Test allocation function behavior on allocation failure.
f57669
+   Copyright (C) 2015 Free Software Foundation, Inc.
f57669
+   This file is part of the GNU C Library.
f57669
+
f57669
+   The GNU C Library is free software; you can redistribute it and/or
f57669
+   modify it under the terms of the GNU Lesser General Public License as
f57669
+   published by the Free Software Foundation; either version 2.1 of the
f57669
+   License, or (at your option) any later version.
f57669
+
f57669
+   The GNU C Library is distributed in the hope that it will be useful,
f57669
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
f57669
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
f57669
+   Lesser General Public License for more details.
f57669
+
f57669
+   You should have received a copy of the GNU Lesser General Public
f57669
+   License along with the GNU C Library; see the file COPYING.LIB.  If
f57669
+   not, see <http://www.gnu.org/licenses/>.  */
f57669
+
f57669
+/* This test case attempts to trigger various unusual conditions
f57669
+   related to allocation failures, notably switching to a different
f57669
+   arena, and falling back to mmap (via sysmalloc).  */
f57669
+
f57669
+#include <errno.h>
f57669
+#include <malloc.h>
f57669
+#include <pthread.h>
f57669
+#include <stdbool.h>
f57669
+#include <stdint.h>
f57669
+#include <stdio.h>
f57669
+#include <stdlib.h>
f57669
+#include <sys/resource.h>
f57669
+#include <sys/wait.h>
f57669
+#include <unistd.h>
f57669
+#include <stddef.h>
f57669
+
f57669
+/* This mirrors the C11 max_align_t type provided by GCC, but it is
f57669
+   also available in C99 mode.  The aligned attributes are required
f57669
+   because some ABIs have reduced alignment requirements for struct
f57669
+   and union members.  */
f57669
+typedef struct {
f57669
+  long long ll __attribute__ ((__aligned__ (__alignof__ (long long))));
f57669
+  long double ld __attribute__ ((__aligned__ (__alignof__ (long double))));
f57669
+} libc_max_align_t;
f57669
+
f57669
+/* Wrapper for calloc with an optimization barrier.  */
f57669
+static void *
f57669
+__attribute__ ((noinline, noclone))
f57669
+allocate_zeroed (size_t a, size_t b)
f57669
+{
f57669
+  return calloc (a, b);
f57669
+}
f57669
+
f57669
+/* System page size, as determined by sysconf (_SC_PAGE_SIZE).  */
f57669
+static unsigned long page_size;
f57669
+
f57669
+/* Test parameters. */
f57669
+static size_t allocation_size;
f57669
+static size_t alignment;
f57669
+static enum {
f57669
+  with_malloc,
f57669
+  with_realloc,
f57669
+  with_aligned_alloc,
f57669
+  with_memalign,
f57669
+  with_posix_memalign,
f57669
+  with_valloc,
f57669
+  with_pvalloc,
f57669
+  with_calloc,
f57669
+  last_allocation_function = with_calloc
f57669
+} allocation_function;
f57669
+
f57669
+/* True if an allocation function uses the alignment test
f57669
+   parameter.  */
f57669
+const static bool alignment_sensitive[last_allocation_function + 1] =
f57669
+  {
f57669
+    [with_aligned_alloc] = true,
f57669
+    [with_memalign] = true,
f57669
+    [with_posix_memalign] = true,
f57669
+  };
f57669
+
f57669
+/* Combined pointer/expected alignment result of an allocation
f57669
+   function.  */
f57669
+struct allocate_result {
f57669
+  void *pointer;
f57669
+  size_t alignment;
f57669
+};
f57669
+
f57669
+/* Call the allocation function specified by allocation_function, with
f57669
+   allocation_size and alignment (if applicable) as arguments.  No
f57669
+   alignment check.  */
f57669
+static struct allocate_result
f57669
+allocate_1 (void)
f57669
+{
f57669
+  switch (allocation_function)
f57669
+    {
f57669
+    case with_malloc:
f57669
+      return (struct allocate_result)
f57669
+        {malloc (allocation_size), __alignof__ (libc_max_align_t)};
f57669
+    case with_realloc:
f57669
+      {
f57669
+        void *p = realloc (NULL, 16);
f57669
+        void *q;
f57669
+        if (p == NULL)
f57669
+          q = NULL;
f57669
+        else
f57669
+          {
f57669
+            q = realloc (p, allocation_size);
f57669
+            if (q == NULL)
f57669
+              free (p);
f57669
+          }
f57669
+        return (struct allocate_result) {q, __alignof__ (libc_max_align_t)};
f57669
+      }
f57669
+    case with_aligned_alloc:
f57669
+      {
f57669
+        void *p = aligned_alloc (alignment, allocation_size);
f57669
+        return (struct allocate_result) {p, alignment};
f57669
+      }
f57669
+    case with_memalign:
f57669
+      {
f57669
+        void *p = memalign (alignment, allocation_size);
f57669
+        return (struct allocate_result) {p, alignment};
f57669
+      }
f57669
+    case with_posix_memalign:
f57669
+      {
f57669
+        void *p;
f57669
+        if (posix_memalign (&p, alignment, allocation_size))
f57669
+          {
f57669
+            if (errno == ENOMEM)
f57669
+              p = NULL;
f57669
+            else
f57669
+              {
f57669
+                printf ("error: posix_memalign (p, %zu, %zu): %m\n",
f57669
+                        alignment, allocation_size);
f57669
+                abort ();
f57669
+              }
f57669
+          }
f57669
+        return (struct allocate_result) {p, alignment};
f57669
+      }
f57669
+    case with_valloc:
f57669
+      {
f57669
+        void *p = valloc (allocation_size);
f57669
+        return (struct allocate_result) {p, page_size};
f57669
+      }
f57669
+    case with_pvalloc:
f57669
+      {
f57669
+        void *p = pvalloc (allocation_size);
f57669
+        return (struct allocate_result) {p, page_size};
f57669
+      }
f57669
+    case with_calloc:
f57669
+      {
f57669
+        char *p = allocate_zeroed (1, allocation_size);
f57669
+        /* Check for non-zero bytes.  */
f57669
+        if (p != NULL)
f57669
+          for (size_t i = 0; i < allocation_size; ++i)
f57669
+            if (p[i] != 0)
f57669
+              {
f57669
+                printf ("error: non-zero byte at offset %zu\n", i);
f57669
+                abort ();
f57669
+              }
f57669
+        return (struct allocate_result) {p, __alignof__ (libc_max_align_t)};
f57669
+      }
f57669
+    }
f57669
+  abort ();
f57669
+}
f57669
+
f57669
+/* Call allocate_1 and perform the alignment check on the result.  */
f57669
+static void *
f57669
+allocate (void)
f57669
+{
f57669
+  struct allocate_result r = allocate_1 ();
f57669
+#if __powerpc__ == 1 && __powerpc64__ == 0
f57669
+  /* Sourceware bug 6527 on 32-bit POWER.
f57669
+     Ignore 16-byte alignment requirement when using malloc, realloc, or
f57669
+     calloc, since these functions are known not to provide enough
f57669
+     alignment.  */
f57669
+  if ((((uintptr_t) r.pointer) & (r.alignment - 1)) != 0
f57669
+      && !(r.alignment == 16
f57669
+	   && (allocation_function == with_malloc
f57669
+	       || allocation_function == with_realloc
f57669
+	       || allocation_function == with_calloc)))
f57669
+#else
f57669
+  if ((((uintptr_t) r.pointer) & (r.alignment - 1)) != 0)
f57669
+#endif
f57669
+    {
f57669
+      printf ("error: allocation function %d, size %zu not aligned to %zu\n",
f57669
+              (int) allocation_function, allocation_size, r.alignment);
f57669
+      abort ();
f57669
+    }
f57669
+  return r.pointer;
f57669
+}
f57669
+
f57669
+/* Barriers to synchronize thread creation and termination.  */
f57669
+static pthread_barrier_t start_barrier;
f57669
+static pthread_barrier_t end_barrier;
f57669
+
f57669
+/* Thread function which performs the allocation test.  Called by
f57669
+   pthread_create and from the main thread.  */
f57669
+static void *
f57669
+allocate_thread (void *closure)
f57669
+{
f57669
+  /* Wait for the creation of all threads.  */
f57669
+  {
f57669
+    int ret = pthread_barrier_wait (&start_barrier);
f57669
+    if (ret != 0 && ret != PTHREAD_BARRIER_SERIAL_THREAD)
f57669
+      {
f57669
+        errno = ret;
f57669
+        printf ("error: pthread_barrier_wait: %m\n");
f57669
+        abort ();
f57669
+      }
f57669
+  }
f57669
+
f57669
+  /* Allocate until we run out of memory, creating a single-linked
f57669
+     list.  */
f57669
+  struct list {
f57669
+    struct list *next;
f57669
+  };
f57669
+  struct list *head = NULL;
f57669
+  while (true)
f57669
+    {
f57669
+      struct list *e = allocate ();
f57669
+      if (e == NULL)
f57669
+        break;
f57669
+
f57669
+      e->next = head;
f57669
+      head = e;
f57669
+    }
f57669
+
f57669
+  /* Wait for the allocation of all available memory.  */
f57669
+  {
f57669
+    int ret = pthread_barrier_wait (&end_barrier);
f57669
+    if (ret != 0 && ret != PTHREAD_BARRIER_SERIAL_THREAD)
f57669
+      {
f57669
+        errno = ret;
f57669
+        printf ("error: pthread_barrier_wait: %m\n");
f57669
+        abort ();
f57669
+      }
f57669
+  }
f57669
+
f57669
+  /* Free the allocated memory.  */
f57669
+  while (head != NULL)
f57669
+    {
f57669
+      struct list *next = head->next;
f57669
+      free (head);
f57669
+      head = next;
f57669
+    }
f57669
+
f57669
+  return NULL;
f57669
+}
f57669
+
f57669
+/* Number of threads (plus the main thread.  */
f57669
+enum { thread_count = 8 };
f57669
+
f57669
+/* Thread attribute to request creation of threads with a non-default
f57669
+   stack size which is rather small.  This avoids interfering with the
f57669
+   configured address space limit.  */
f57669
+static pthread_attr_t small_stack;
f57669
+
f57669
+/* Runs one test in multiple threads, all in a subprocess so that
f57669
+   subsequent tests do not interfere with each other.  */
f57669
+static void
f57669
+run_one (void)
f57669
+{
f57669
+  /* Isolate the tests in a subprocess, so that we can start over
f57669
+     from scratch.  */
f57669
+  pid_t pid = fork ();
f57669
+  if (pid == 0)
f57669
+    {
f57669
+      /* In the child process.  Create the allocation threads.  */
f57669
+      pthread_t threads[thread_count];
f57669
+
f57669
+      for (unsigned i = 0; i < thread_count; ++i)
f57669
+        {
f57669
+          int ret = pthread_create (threads + i, &small_stack, allocate_thread, NULL);
f57669
+          if (ret != 0)
f57669
+            {
f57669
+              errno = ret;
f57669
+              printf ("error: pthread_create: %m\n");
f57669
+              abort ();
f57669
+            }
f57669
+        }
f57669
+
f57669
+      /* Also run the test on the main thread.  */
f57669
+      allocate_thread (NULL);
f57669
+
f57669
+      for (unsigned i = 0; i < thread_count; ++i)
f57669
+        {
f57669
+          int ret = pthread_join (threads[i], NULL);
f57669
+          if (ret != 0)
f57669
+            {
f57669
+              errno = ret;
f57669
+              printf ("error: pthread_join: %m\n");
f57669
+              abort ();
f57669
+            }
f57669
+        }
f57669
+      _exit (0);
f57669
+    }
f57669
+  else if (pid < 0)
f57669
+    {
f57669
+      printf ("error: fork: %m\n");
f57669
+      abort ();
f57669
+    }
f57669
+
f57669
+  /* In the parent process.  Wait for the child process to exit.  */
f57669
+  int status;
f57669
+  if (waitpid (pid, &status, 0) < 0)
f57669
+    {
f57669
+      printf ("error: waitpid: %m\n");
f57669
+      abort ();
f57669
+    }
f57669
+  if (status != 0)
f57669
+    {
f57669
+      printf ("error: exit status %d from child process\n", status);
f57669
+      exit (1);
f57669
+    }
f57669
+}
f57669
+
f57669
+/* Run all applicable allocation functions for the current test
f57669
+   parameters.  */
f57669
+static void
f57669
+run_allocation_functions (void)
f57669
+{
f57669
+  for (int af = 0; af <= last_allocation_function; ++af)
f57669
+    {
f57669
+      /* Run alignment-sensitive functions for non-default
f57669
+         alignments.  */
f57669
+      if (alignment_sensitive[af] != (alignment != 0))
f57669
+        continue;
f57669
+      allocation_function = af;
f57669
+      run_one ();
f57669
+    }
f57669
+}
f57669
+
f57669
+int
f57669
+do_test (void)
f57669
+{
f57669
+  /* Limit the number of malloc arenas.  We use a very low number so
f57669
+     that despute the address space limit configured below, all
f57669
+     requested arenas a can be created.  */
f57669
+  if (mallopt (M_ARENA_MAX, 2) == 0)
f57669
+    {
f57669
+      printf ("error: mallopt (M_ARENA_MAX) failed\n");
f57669
+      return 1;
f57669
+    }
f57669
+
f57669
+  /* Determine the page size.  */
f57669
+  {
f57669
+    long ret = sysconf (_SC_PAGE_SIZE);
f57669
+    if (ret < 0)
f57669
+      {
f57669
+        printf ("error: sysconf (_SC_PAGE_SIZE): %m\n");
f57669
+        return 1;
f57669
+      }
f57669
+    page_size = ret;
f57669
+  }
f57669
+
f57669
+  /* Limit the size of the process, so that memory allocation in
f57669
+     allocate_thread will eventually fail, without impacting the
f57669
+     entire system.  */
f57669
+  {
f57669
+    struct rlimit limit;
f57669
+    if (getrlimit (RLIMIT_AS, &limit) != 0)
f57669
+      {
f57669
+        printf ("getrlimit (RLIMIT_AS) failed: %m\n");
f57669
+        return 1;
f57669
+      }
f57669
+    long target = 200 * 1024 * 1024;
f57669
+    if (limit.rlim_cur == RLIM_INFINITY || limit.rlim_cur > target)
f57669
+      {
f57669
+        limit.rlim_cur = target;
f57669
+        if (setrlimit (RLIMIT_AS, &limit) != 0)
f57669
+          {
f57669
+            printf ("setrlimit (RLIMIT_AS) failed: %m\n");
f57669
+            return 1;
f57669
+          }
f57669
+      }
f57669
+  }
f57669
+
f57669
+  /* Initialize thread attribute with a reduced stack size.  */
f57669
+  {
f57669
+    int ret = pthread_attr_init (&small_stack);
f57669
+    if (ret != 0)
f57669
+      {
f57669
+        errno = ret;
f57669
+        printf ("error: pthread_attr_init: %m\n");
f57669
+        abort ();
f57669
+      }
f57669
+    unsigned long stack_size = ((256 * 1024) / page_size) * page_size;
f57669
+    if (stack_size < 4 * page_size)
f57669
+      stack_size = 8 * page_size;
f57669
+    ret = pthread_attr_setstacksize (&small_stack, stack_size);
f57669
+    if (ret != 0)
f57669
+      {
f57669
+        errno = ret;
f57669
+        printf ("error: pthread_attr_setstacksize: %m\n");
f57669
+        abort ();
f57669
+      }
f57669
+  }
f57669
+
f57669
+  /* Initialize the barriers.  We run thread_count threads, plus 1 for
f57669
+     the main thread.  */
f57669
+  {
f57669
+    int ret = pthread_barrier_init (&start_barrier, NULL, thread_count + 1);
f57669
+    if (ret != 0)
f57669
+      {
f57669
+        errno = ret;
f57669
+        printf ("error: pthread_barrier_init: %m\n");
f57669
+        abort ();
f57669
+      }
f57669
+
f57669
+    ret = pthread_barrier_init (&end_barrier, NULL, thread_count + 1);
f57669
+    if (ret != 0)
f57669
+      {
f57669
+        errno = ret;
f57669
+        printf ("error: pthread_barrier_init: %m\n");
f57669
+        abort ();
f57669
+      }
f57669
+  }
f57669
+
f57669
+  allocation_size = 144;
f57669
+  run_allocation_functions ();
f57669
+  allocation_size = page_size;
f57669
+  run_allocation_functions ();
f57669
+
f57669
+  alignment = 128;
f57669
+  allocation_size = 512;
f57669
+  run_allocation_functions ();
f57669
+
f57669
+  allocation_size = page_size;
f57669
+  run_allocation_functions ();
f57669
+
f57669
+  allocation_size = 17 * page_size;
f57669
+  run_allocation_functions ();
f57669
+
f57669
+  /* Deallocation the barriers and the thread attribute.  */
f57669
+  {
f57669
+    int ret = pthread_barrier_destroy (&end_barrier);
f57669
+    if (ret != 0)
f57669
+      {
f57669
+        errno = ret;
f57669
+        printf ("error: pthread_barrier_destroy: %m\n");
f57669
+        return 1;
f57669
+      }
f57669
+    ret = pthread_barrier_destroy (&start_barrier);
f57669
+    if (ret != 0)
f57669
+      {
f57669
+        errno = ret;
f57669
+        printf ("error: pthread_barrier_destroy: %m\n");
f57669
+        return 1;
f57669
+      }
f57669
+    ret = pthread_attr_destroy (&small_stack);
f57669
+    if (ret != 0)
f57669
+      {
f57669
+        errno = ret;
f57669
+        printf ("error: pthread_attr_destroy: %m\n");
f57669
+        return 1;
f57669
+      }
f57669
+  }
f57669
+
f57669
+  return 0;
f57669
+}
f57669
+
f57669
+/* The repeated allocations take some time on slow machines.  */
f57669
+#define TIMEOUT 20
f57669
+
f57669
+/* No malloc perturbation should be used. We are testing that
f57669
+   default 0 perturbation values work.  */
f57669
+#define MALLOC_PERTURB 0
f57669
+
f57669
+#define TEST_FUNCTION do_test ()
f57669
+#include "../test-skeleton.c"
f57669
Index: glibc-2.17-c758a686/test-skeleton.c
f57669
===================================================================
f57669
--- glibc-2.17-c758a686.orig/test-skeleton.c
f57669
+++ glibc-2.17-c758a686/test-skeleton.c
f57669
@@ -233,7 +233,10 @@ main (int argc, char *argv[])
f57669
   pid_t termpid;
f57669
 
f57669
   /* Make uses of freed and uninitialized memory known.  */
f57669
-  mallopt (M_PERTURB, 42);
f57669
+#ifndef MALLOC_PERTURB
f57669
+# define MALLOC_PERTURB 42
f57669
+#endif
f57669
+  mallopt (M_PERTURB, MALLOC_PERTURB);
f57669
 
f57669
 #ifdef STDOUT_UNBUFFERED
f57669
   setbuf (stdout, NULL);