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