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