8ae002
Based on the following upstream commit:
8ae002
8ae002
commit ef4f97648dc95849e417dd3e6328165de4c22185
8ae002
Author: Florian Weimer <fweimer@redhat.com>
8ae002
Date:   Fri Aug 26 22:40:27 2016 +0200
8ae002
8ae002
    malloc: Simplify static malloc interposition [BZ #20432]
8ae002
    
8ae002
    Existing interposed mallocs do not define the glibc-internal
8ae002
    fork callbacks (and they should not), so statically interposed
8ae002
    mallocs lead to link failures because the strong reference from
8ae002
    fork pulls in glibc's malloc, resulting in multiple definitions
8ae002
    of malloc-related symbols.
8ae002
8ae002
Adjustments: __libc_memalign is defined.  <support/xthread.h> is
8ae002
included because libsupport provides definitions of xthread_*
8ae002
functions, but not our version of the test skeleton.
8ae002
8ae002
Index: b/include/libc-symbols.h
8ae002
===================================================================
8ae002
--- a/include/libc-symbols.h
8ae002
+++ b/include/libc-symbols.h
8ae002
@@ -119,6 +119,21 @@
8ae002
 # define weak_extern(symbol) _weak_extern (weak symbol)
8ae002
 # define _weak_extern(expr) _Pragma (#expr)
8ae002
 
8ae002
+/* In shared builds, the expression call_function_static_weak
8ae002
+   (FUNCTION-SYMBOL, ARGUMENTS) invokes FUNCTION-SYMBOL (an
8ae002
+   identifier) unconditionally, with the (potentially empty) argument
8ae002
+   list ARGUMENTS.  In static builds, if FUNCTION-SYMBOL has a
8ae002
+   definition, the function is invoked as before; if FUNCTION-SYMBOL
8ae002
+   is NULL, no call is performed.  */
8ae002
+# ifdef SHARED
8ae002
+#  define call_function_static_weak(func, ...) func (__VA_ARGS__)
8ae002
+# else	/* !SHARED */
8ae002
+#  define call_function_static_weak(func, ...)		\
8ae002
+  ({							\
8ae002
+    extern __typeof__ (func) func weak_function;	\
8ae002
+    (func != NULL ? func (__VA_ARGS__) : (void)0);	\
8ae002
+  })
8ae002
+# endif
8ae002
 
8ae002
 #else /* __ASSEMBLER__ */
8ae002
 
8ae002
Index: b/malloc/Makefile
8ae002
===================================================================
8ae002
--- a/malloc/Makefile
8ae002
+++ b/malloc/Makefile
8ae002
@@ -28,7 +28,16 @@ tests := mallocbug tst-malloc tst-valloc
8ae002
 	 tst-mallocstate tst-mcheck tst-mallocfork tst-trim1 \
8ae002
 	 tst-malloc-usable \
8ae002
 	 tst-malloc-backtrace tst-malloc-thread-exit \
8ae002
-	 tst-malloc-thread-fail tst-malloc-fork-deadlock
8ae002
+	 tst-malloc-thread-fail tst-malloc-fork-deadlock \
8ae002
+	 tst-interpose-nothread \
8ae002
+	 tst-interpose-thread \
8ae002
+	 tst-interpose-static-nothread \
8ae002
+	 tst-interpose-static-thread \
8ae002
+
8ae002
+tests-static := \
8ae002
+	 tst-interpose-static-nothread \
8ae002
+	 tst-interpose-static-thread \
8ae002
+
8ae002
 test-srcs = tst-mtrace
8ae002
 
8ae002
 routines = malloc morecore mcheck mtrace obstack
8ae002
@@ -40,6 +49,15 @@ non-lib.a := libmcheck.a
8ae002
 extra-libs = libmemusage
8ae002
 extra-libs-others = $(extra-libs)
8ae002
 
8ae002
+# Helper objects for some tests.
8ae002
+extra-tests-objs += \
8ae002
+  tst-interpose-aux-nothread.o \
8ae002
+  tst-interpose-aux-thread.o \
8ae002
+
8ae002
+test-extras = \
8ae002
+  tst-interpose-aux-nothread \
8ae002
+  tst-interpose-aux-thread \
8ae002
+
8ae002
 libmemusage-routines = memusage
8ae002
 libmemusage-inhibit-o = $(filter-out .os,$(object-suffixes))
8ae002
 
8ae002
@@ -157,6 +175,17 @@ $(objpfx)libmemusage.so: $(common-objpfx
8ae002
 # Extra dependencies
8ae002
 $(foreach o,$(all-object-suffixes),$(objpfx)malloc$(o)): arena.c hooks.c
8ae002
 
8ae002
+# max_align_t requires C11.
8ae002
+CFLAGS-tst-interpose-aux-nothread.c = -std=gnu11
8ae002
+CFLAGS-tst-interpose-aux-thread.c = -std=gnu11
8ae002
+
8ae002
+$(objpfx)tst-interpose-nothread: $(objpfx)tst-interpose-aux-nothread.o
8ae002
+$(objpfx)tst-interpose-thread: \
8ae002
+  $(objpfx)tst-interpose-aux-thread.o $(shared-thread-library)
8ae002
+$(objpfx)tst-interpose-static-nothread: $(objpfx)tst-interpose-aux-nothread.o
8ae002
+$(objpfx)tst-interpose-static-thread: \
8ae002
+  $(objpfx)tst-interpose-aux-thread.o $(static-thread-library)
8ae002
+
8ae002
 # Compile the tests with a flag which suppresses the mallopt call in
8ae002
 # the test skeleton.
8ae002
 $(tests:%=$(objpfx)%.o): CPPFLAGS += -DTEST_NO_MALLOPT
8ae002
Index: b/malloc/tst-interpose-aux-nothread.c
8ae002
===================================================================
8ae002
--- /dev/null
8ae002
+++ b/malloc/tst-interpose-aux-nothread.c
8ae002
@@ -0,0 +1,20 @@
8ae002
+/* Interposed malloc, version without threading support.
8ae002
+   Copyright (C) 2016 Free Software Foundation, Inc.
8ae002
+   This file is part of the GNU C Library.
8ae002
+
8ae002
+   The GNU C Library is free software; you can redistribute it and/or
8ae002
+   modify it under the terms of the GNU Lesser General Public License as
8ae002
+   published by the Free Software Foundation; either version 2.1 of the
8ae002
+   License, or (at your option) any later version.
8ae002
+
8ae002
+   The GNU C Library is distributed in the hope that it will be useful,
8ae002
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
8ae002
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8ae002
+   Lesser General Public License for more details.
8ae002
+
8ae002
+   You should have received a copy of the GNU Lesser General Public
8ae002
+   License along with the GNU C Library; see the file COPYING.LIB.  If
8ae002
+   not, see <http://www.gnu.org/licenses/>.  */
8ae002
+
8ae002
+#define INTERPOSE_THREADS 0
8ae002
+#include "tst-interpose-aux.c"
8ae002
Index: b/malloc/tst-interpose-aux-thread.c
8ae002
===================================================================
8ae002
--- /dev/null
8ae002
+++ b/malloc/tst-interpose-aux-thread.c
8ae002
@@ -0,0 +1,20 @@
8ae002
+/* Interposed malloc, version with threading support.
8ae002
+   Copyright (C) 2016 Free Software Foundation, Inc.
8ae002
+   This file is part of the GNU C Library.
8ae002
+
8ae002
+   The GNU C Library is free software; you can redistribute it and/or
8ae002
+   modify it under the terms of the GNU Lesser General Public License as
8ae002
+   published by the Free Software Foundation; either version 2.1 of the
8ae002
+   License, or (at your option) any later version.
8ae002
+
8ae002
+   The GNU C Library is distributed in the hope that it will be useful,
8ae002
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
8ae002
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8ae002
+   Lesser General Public License for more details.
8ae002
+
8ae002
+   You should have received a copy of the GNU Lesser General Public
8ae002
+   License along with the GNU C Library; see the file COPYING.LIB.  If
8ae002
+   not, see <http://www.gnu.org/licenses/>.  */
8ae002
+
8ae002
+#define INTERPOSE_THREADS 1
8ae002
+#include "tst-interpose-aux.c"
8ae002
Index: b/malloc/tst-interpose-aux.c
8ae002
===================================================================
8ae002
--- /dev/null
8ae002
+++ b/malloc/tst-interpose-aux.c
8ae002
@@ -0,0 +1,283 @@
8ae002
+/* Minimal malloc implementation for interposition tests.
8ae002
+   Copyright (C) 2016 Free Software Foundation, Inc.
8ae002
+   This file is part of the GNU C Library.
8ae002
+
8ae002
+   The GNU C Library is free software; you can redistribute it and/or
8ae002
+   modify it under the terms of the GNU Lesser General Public License as
8ae002
+   published by the Free Software Foundation; either version 2.1 of the
8ae002
+   License, or (at your option) any later version.
8ae002
+
8ae002
+   The GNU C Library is distributed in the hope that it will be useful,
8ae002
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
8ae002
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8ae002
+   Lesser General Public License for more details.
8ae002
+
8ae002
+   You should have received a copy of the GNU Lesser General Public
8ae002
+   License along with the GNU C Library; see the file COPYING.LIB.  If
8ae002
+   not, see <http://www.gnu.org/licenses/>.  */
8ae002
+
8ae002
+#include "tst-interpose-aux.h"
8ae002
+
8ae002
+#include <errno.h>
8ae002
+#include <stdarg.h>
8ae002
+#include <stddef.h>
8ae002
+#include <stdio.h>
8ae002
+#include <stdlib.h>
8ae002
+#include <string.h>
8ae002
+#include <sys/mman.h>
8ae002
+#include <sys/uio.h>
8ae002
+#include <unistd.h>
8ae002
+
8ae002
+#if INTERPOSE_THREADS
8ae002
+#include <pthread.h>
8ae002
+#endif
8ae002
+
8ae002
+/* Print the error message and terminate the process with status 1.  */
8ae002
+__attribute__ ((noreturn))
8ae002
+__attribute__ ((format (printf, 1, 2)))
8ae002
+static void *
8ae002
+fail (const char *format, ...)
8ae002
+{
8ae002
+  /* This assumes that vsnprintf will not call malloc.  It does not do
8ae002
+     so for the format strings we use.  */
8ae002
+  char message[4096];
8ae002
+  va_list ap;
8ae002
+  va_start (ap, format);
8ae002
+  vsnprintf (message, sizeof (message), format, ap);
8ae002
+  va_end (ap);
8ae002
+
8ae002
+  enum { count = 3 };
8ae002
+  struct iovec iov[count];
8ae002
+
8ae002
+  iov[0].iov_base = (char *) "error: ";
8ae002
+  iov[1].iov_base = (char *) message;
8ae002
+  iov[2].iov_base = (char *) "\n";
8ae002
+
8ae002
+  for (int i = 0; i < count; ++i)
8ae002
+    iov[i].iov_len = strlen (iov[i].iov_base);
8ae002
+
8ae002
+  int unused __attribute__ ((unused));
8ae002
+  unused = writev (STDOUT_FILENO, iov, count);
8ae002
+  _exit (1);
8ae002
+}
8ae002
+
8ae002
+#if INTERPOSE_THREADS
8ae002
+static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
8ae002
+#endif
8ae002
+
8ae002
+static void
8ae002
+lock (void)
8ae002
+{
8ae002
+#if INTERPOSE_THREADS
8ae002
+  int ret = pthread_mutex_lock (&mutex);
8ae002
+  if (ret != 0)
8ae002
+    {
8ae002
+      errno = ret;
8ae002
+      fail ("pthread_mutex_lock: %m");
8ae002
+    }
8ae002
+#endif
8ae002
+}
8ae002
+
8ae002
+static void
8ae002
+unlock (void)
8ae002
+{
8ae002
+#if INTERPOSE_THREADS
8ae002
+  int ret = pthread_mutex_unlock (&mutex);
8ae002
+  if (ret != 0)
8ae002
+    {
8ae002
+      errno = ret;
8ae002
+      fail ("pthread_mutex_unlock: %m");
8ae002
+    }
8ae002
+#endif
8ae002
+}
8ae002
+
8ae002
+struct __attribute__ ((aligned (__alignof__ (max_align_t)))) allocation_header
8ae002
+{
8ae002
+  size_t allocation_index;
8ae002
+  size_t allocation_size;
8ae002
+};
8ae002
+
8ae002
+/* Array of known allocations, to track invalid frees.  */
8ae002
+enum { max_allocations = 65536 };
8ae002
+static struct allocation_header *allocations[max_allocations];
8ae002
+static size_t allocation_index;
8ae002
+static size_t deallocation_count;
8ae002
+
8ae002
+/* Sanity check for successful malloc interposition.  */
8ae002
+__attribute__ ((destructor))
8ae002
+static void
8ae002
+check_for_allocations (void)
8ae002
+{
8ae002
+  if (allocation_index == 0)
8ae002
+    {
8ae002
+      /* Make sure that malloc is called at least once from libc.  */
8ae002
+      void *volatile ptr = strdup ("ptr");
8ae002
+      free (ptr);
8ae002
+      /* Compiler barrier.  The strdup function calls malloc, which
8ae002
+         updates allocation_index, but strdup is marked __THROW, so
8ae002
+         the compiler could optimize away the reload.  */
8ae002
+      __asm__ volatile ("" ::: "memory");
8ae002
+      /* If the allocation count is still zero, it means we did not
8ae002
+         interpose malloc successfully.  */
8ae002
+      if (allocation_index == 0)
8ae002
+        fail ("malloc does not seem to have been interposed");
8ae002
+    }
8ae002
+}
8ae002
+
8ae002
+static struct allocation_header *get_header (const char *op, void *ptr)
8ae002
+{
8ae002
+  struct allocation_header *header = ((struct allocation_header *) ptr) - 1;
8ae002
+  if (header->allocation_index >= allocation_index)
8ae002
+    fail ("%s: %p: invalid allocation index: %zu (not less than %zu)",
8ae002
+          op, ptr, header->allocation_index, allocation_index);
8ae002
+  if (allocations[header->allocation_index] != header)
8ae002
+    fail ("%s: %p: allocation pointer does not point to header, but %p",
8ae002
+          op, ptr, allocations[header->allocation_index]);
8ae002
+  return header;
8ae002
+}
8ae002
+
8ae002
+/* Internal helper functions.  Those must be called while the lock is
8ae002
+   acquired.  */
8ae002
+
8ae002
+static void *
8ae002
+malloc_internal (size_t size)
8ae002
+{
8ae002
+  if (allocation_index == max_allocations)
8ae002
+    {
8ae002
+      errno = ENOMEM;
8ae002
+      return NULL;
8ae002
+    }
8ae002
+  size_t allocation_size = size + sizeof (struct allocation_header);
8ae002
+  if (allocation_size < size)
8ae002
+    {
8ae002
+      errno = ENOMEM;
8ae002
+      return NULL;
8ae002
+    }
8ae002
+
8ae002
+  size_t index = allocation_index++;
8ae002
+  void *result = mmap (NULL, allocation_size, PROT_READ | PROT_WRITE,
8ae002
+                       MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
8ae002
+  if (result == MAP_FAILED)
8ae002
+    return NULL;
8ae002
+  allocations[index] = result;
8ae002
+  *allocations[index] = (struct allocation_header)
8ae002
+    {
8ae002
+      .allocation_index = index,
8ae002
+      .allocation_size = allocation_size
8ae002
+    };
8ae002
+  return allocations[index] + 1;
8ae002
+}
8ae002
+
8ae002
+static void
8ae002
+free_internal (const char *op, struct allocation_header *header)
8ae002
+{
8ae002
+  size_t index = header->allocation_index;
8ae002
+  int result = mprotect (header, header->allocation_size, PROT_NONE);
8ae002
+  if (result != 0)
8ae002
+    fail ("%s: mprotect (%p, %zu): %m", op, header, header->allocation_size);
8ae002
+  /* Catch double-free issues.  */
8ae002
+  allocations[index] = NULL;
8ae002
+  ++deallocation_count;
8ae002
+}
8ae002
+
8ae002
+static void *
8ae002
+realloc_internal (void *ptr, size_t new_size)
8ae002
+{
8ae002
+  struct allocation_header *header = get_header ("realloc", ptr);
8ae002
+  size_t old_size = header->allocation_size - sizeof (struct allocation_header);
8ae002
+  if (old_size >= new_size)
8ae002
+    return ptr;
8ae002
+
8ae002
+  void *newptr = malloc_internal (new_size);
8ae002
+  if (newptr == NULL)
8ae002
+    return NULL;
8ae002
+  memcpy (newptr, ptr, old_size);
8ae002
+  free_internal ("realloc", header);
8ae002
+  return newptr;
8ae002
+}
8ae002
+
8ae002
+/* Public interfaces.  These functions must perform locking.  */
8ae002
+
8ae002
+size_t
8ae002
+malloc_allocation_count (void)
8ae002
+{
8ae002
+  lock ();
8ae002
+  size_t count = allocation_index;
8ae002
+  unlock ();
8ae002
+  return count;
8ae002
+}
8ae002
+
8ae002
+size_t
8ae002
+malloc_deallocation_count (void)
8ae002
+{
8ae002
+  lock ();
8ae002
+  size_t count = deallocation_count;
8ae002
+  unlock ();
8ae002
+  return count;
8ae002
+}
8ae002
+
8ae002
+void *
8ae002
+malloc (size_t size)
8ae002
+{
8ae002
+  lock ();
8ae002
+  void *result = malloc_internal (size);
8ae002
+  unlock ();
8ae002
+  return result;
8ae002
+}
8ae002
+
8ae002
+void
8ae002
+free (void *ptr)
8ae002
+{
8ae002
+  if (ptr == NULL)
8ae002
+    return;
8ae002
+  lock ();
8ae002
+  struct allocation_header *header = get_header ("free", ptr);
8ae002
+  free_internal ("free", header);
8ae002
+  unlock ();
8ae002
+}
8ae002
+
8ae002
+void *
8ae002
+calloc (size_t a, size_t b)
8ae002
+{
8ae002
+  if (b > 0 && a > SIZE_MAX / b)
8ae002
+    {
8ae002
+      errno = ENOMEM;
8ae002
+      return NULL;
8ae002
+    }
8ae002
+  lock ();
8ae002
+  /* malloc_internal uses mmap, so the memory is zeroed.  */
8ae002
+  void *result = malloc_internal (a * b);
8ae002
+  unlock ();
8ae002
+  return result;
8ae002
+}
8ae002
+
8ae002
+void *
8ae002
+realloc (void *ptr, size_t n)
8ae002
+{
8ae002
+  if (n ==0)
8ae002
+    {
8ae002
+      free (ptr);
8ae002
+      return NULL;
8ae002
+    }
8ae002
+  else if (ptr == NULL)
8ae002
+    return malloc (n);
8ae002
+  else
8ae002
+    {
8ae002
+      lock ();
8ae002
+      void *result = realloc_internal (ptr, n);
8ae002
+      unlock ();
8ae002
+      return result;
8ae002
+    }
8ae002
+}
8ae002
+
8ae002
+/* The dyanmic linker still uses __libc_memalign because we have not
8ae002
+   backported the fix for swbz#17730.  It does not normally request
8ae002
+   large alignments, so we can call malloc directly.  */
8ae002
+void *
8ae002
+__libc_memalign (size_t alignment, size_t size)
8ae002
+{
8ae002
+  void *result = malloc (size);
8ae002
+  if (((uintptr_t) result % alignment) != 0)
8ae002
+    fail ("could not fulfill requested alignment %zu", alignment);
8ae002
+  return result;
8ae002
+}
8ae002
Index: b/malloc/tst-interpose-aux.h
8ae002
===================================================================
8ae002
--- /dev/null
8ae002
+++ b/malloc/tst-interpose-aux.h
8ae002
@@ -0,0 +1,30 @@
8ae002
+/* Statistics interface for the minimal malloc implementation.
8ae002
+   Copyright (C) 2016 Free Software Foundation, Inc.
8ae002
+   This file is part of the GNU C Library.
8ae002
+
8ae002
+   The GNU C Library is free software; you can redistribute it and/or
8ae002
+   modify it under the terms of the GNU Lesser General Public License as
8ae002
+   published by the Free Software Foundation; either version 2.1 of the
8ae002
+   License, or (at your option) any later version.
8ae002
+
8ae002
+   The GNU C Library is distributed in the hope that it will be useful,
8ae002
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
8ae002
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8ae002
+   Lesser General Public License for more details.
8ae002
+
8ae002
+   You should have received a copy of the GNU Lesser General Public
8ae002
+   License along with the GNU C Library; see the file COPYING.LIB.  If
8ae002
+   not, see <http://www.gnu.org/licenses/>.  */
8ae002
+
8ae002
+#ifndef TST_INTERPOSE_AUX_H
8ae002
+#define TST_INTERPOSE_AUX_H
8ae002
+
8ae002
+#include <stddef.h>
8ae002
+
8ae002
+/* Return the number of allocations performed.  */
8ae002
+size_t malloc_allocation_count (void);
8ae002
+
8ae002
+/* Return the number of deallocations performed.  */
8ae002
+size_t malloc_deallocation_count (void);
8ae002
+
8ae002
+#endif /* TST_INTERPOSE_AUX_H */
8ae002
Index: b/malloc/tst-interpose-nothread.c
8ae002
===================================================================
8ae002
--- /dev/null
8ae002
+++ b/malloc/tst-interpose-nothread.c
8ae002
@@ -0,0 +1,20 @@
8ae002
+/* Malloc interposition test, dynamically-linked version without threads.
8ae002
+   Copyright (C) 2016 Free Software Foundation, Inc.
8ae002
+   This file is part of the GNU C Library.
8ae002
+
8ae002
+   The GNU C Library is free software; you can redistribute it and/or
8ae002
+   modify it under the terms of the GNU Lesser General Public License as
8ae002
+   published by the Free Software Foundation; either version 2.1 of the
8ae002
+   License, or (at your option) any later version.
8ae002
+
8ae002
+   The GNU C Library is distributed in the hope that it will be useful,
8ae002
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
8ae002
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8ae002
+   Lesser General Public License for more details.
8ae002
+
8ae002
+   You should have received a copy of the GNU Lesser General Public
8ae002
+   License along with the GNU C Library; see the file COPYING.LIB.  If
8ae002
+   not, see <http://www.gnu.org/licenses/>.  */
8ae002
+
8ae002
+#define INTERPOSE_THREADS 0
8ae002
+#include "tst-interpose-skeleton.c"
8ae002
Index: b/malloc/tst-interpose-skeleton.c
8ae002
===================================================================
8ae002
--- /dev/null
8ae002
+++ b/malloc/tst-interpose-skeleton.c
8ae002
@@ -0,0 +1,204 @@
8ae002
+/* Test driver for malloc interposition tests.
8ae002
+   Copyright (C) 2016 Free Software Foundation, Inc.
8ae002
+   This file is part of the GNU C Library.
8ae002
+
8ae002
+   The GNU C Library is free software; you can redistribute it and/or
8ae002
+   modify it under the terms of the GNU Lesser General Public License as
8ae002
+   published by the Free Software Foundation; either version 2.1 of the
8ae002
+   License, or (at your option) any later version.
8ae002
+
8ae002
+   The GNU C Library is distributed in the hope that it will be useful,
8ae002
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
8ae002
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8ae002
+   Lesser General Public License for more details.
8ae002
+
8ae002
+   You should have received a copy of the GNU Lesser General Public
8ae002
+   License along with the GNU C Library; see the file COPYING.LIB.  If
8ae002
+   not, see <http://www.gnu.org/licenses/>.  */
8ae002
+
8ae002
+#include <stdio.h>
8ae002
+#include <stdlib.h>
8ae002
+#include <string.h>
8ae002
+#include <unistd.h>
8ae002
+
8ae002
+#if INTERPOSE_THREADS
8ae002
+#include <support/xthread.h>
8ae002
+#endif
8ae002
+
8ae002
+static int do_test (void);
8ae002
+#define TEST_FUNCTION do_test ()
8ae002
+#include "../test-skeleton.c"
8ae002
+
8ae002
+/* Fills BUFFER with a test string.  */
8ae002
+static void
8ae002
+line_string (int number, char *buffer, size_t length)
8ae002
+{
8ae002
+  for (size_t i = 0; i < length - 2; ++i)
8ae002
+    buffer[i] = 'A' + ((number + i) % 26);
8ae002
+  buffer[length - 2] = '\n';
8ae002
+  buffer[length - 1] = '\0';
8ae002
+}
8ae002
+
8ae002
+/* Perform the tests.  */
8ae002
+static void *
8ae002
+run_tests (void *closure)
8ae002
+{
8ae002
+  char *temp_file_path;
8ae002
+  int fd = create_temp_file ("tst-malloc-interpose", &temp_file_path);
8ae002
+  if (fd < 0)
8ae002
+    _exit (1);
8ae002
+
8ae002
+  /* Line lengths excluding the line terminator.  */
8ae002
+  static const int line_lengths[] = { 0, 45, 80, 2, 8201, 0, 17, -1 };
8ae002
+
8ae002
+  /* Fill the test file with data.  */
8ae002
+  {
8ae002
+    FILE *fp = fdopen (fd, "w");
8ae002
+    for (int lineno = 0; line_lengths[lineno] >= 0; ++lineno)
8ae002
+      {
8ae002
+        char buffer[line_lengths[lineno] + 2];
8ae002
+        line_string (lineno, buffer, sizeof (buffer));
8ae002
+        fprintf (fp, "%s", buffer);
8ae002
+      }
8ae002
+
8ae002
+    if (ferror (fp))
8ae002
+      {
8ae002
+        printf ("error: fprintf: %m\n");
8ae002
+        _exit (1);
8ae002
+      }
8ae002
+    if (fclose (fp) != 0)
8ae002
+      {
8ae002
+        printf ("error: fclose: %m\n");
8ae002
+        _exit (1);
8ae002
+      }
8ae002
+  }
8ae002
+
8ae002
+  /* Read the test file.  This tests libc-internal allocation with
8ae002
+     realloc.  */
8ae002
+  {
8ae002
+    FILE *fp = fopen (temp_file_path, "r");
8ae002
+
8ae002
+    char *actual = NULL;
8ae002
+    size_t actual_size = 0;
8ae002
+    for (int lineno = 0; ; ++lineno)
8ae002
+      {
8ae002
+        errno = 0;
8ae002
+        ssize_t result = getline (&actual, &actual_size, fp);
8ae002
+        if (result == 0)
8ae002
+          {
8ae002
+            printf ("error: invalid return value 0 from getline\n");
8ae002
+            _exit (1);
8ae002
+          }
8ae002
+        if (result < 0 && errno != 0)
8ae002
+          {
8ae002
+            printf ("error: getline: %m\n");
8ae002
+            _exit (1);
8ae002
+          }
8ae002
+        if (result < 0 && line_lengths[lineno] >= 0)
8ae002
+          {
8ae002
+            printf ("error: unexpected end of file after line %d\n", lineno);
8ae002
+            _exit (1);
8ae002
+          }
8ae002
+        if (result > 0 && line_lengths[lineno] < 0)
8ae002
+          {
8ae002
+            printf ("error: no end of file after line %d\n", lineno);
8ae002
+            _exit (1);
8ae002
+          }
8ae002
+        if (result == -1 && line_lengths[lineno] == -1)
8ae002
+          /* End of file reached as expected.  */
8ae002
+          break;
8ae002
+
8ae002
+        if (result != line_lengths[lineno] + 1)
8ae002
+          {
8ae002
+            printf ("error: line length mismatch: expected %d, got %zd\n",
8ae002
+                    line_lengths[lineno], result);
8ae002
+            _exit (1);
8ae002
+          }
8ae002
+
8ae002
+        char expected[line_lengths[lineno] + 2];
8ae002
+        line_string (lineno, expected, sizeof (expected));
8ae002
+        if (strcmp (actual, expected) != 0)
8ae002
+          {
8ae002
+            printf ("error: line mismatch\n");
8ae002
+            printf ("error:   expected: [[%s]]\n", expected);
8ae002
+            printf ("error:   actual:   [[%s]]\n", actual);
8ae002
+            _exit (1);
8ae002
+          }
8ae002
+      }
8ae002
+
8ae002
+    if (fclose (fp) != 0)
8ae002
+      {
8ae002
+        printf ("error: fclose (after reading): %m\n");
8ae002
+        _exit (1);
8ae002
+      }
8ae002
+  }
8ae002
+
8ae002
+  free (temp_file_path);
8ae002
+
8ae002
+  /* Make sure that fork is working.  */
8ae002
+  pid_t pid = fork ();
8ae002
+  if (pid == -1)
8ae002
+    {
8ae002
+      printf ("error: fork: %m\n");
8ae002
+      _exit (1);
8ae002
+    }
8ae002
+  enum { exit_code = 55 };
8ae002
+  if (pid == 0)
8ae002
+    _exit (exit_code);
8ae002
+  int status;
8ae002
+  int ret = waitpid (pid, &status, 0);
8ae002
+  if (ret < 0)
8ae002
+    {
8ae002
+      printf ("error: waitpid: %m\n");
8ae002
+      _exit (1);
8ae002
+    }
8ae002
+  if (!WIFEXITED (status) || WEXITSTATUS (status) != exit_code)
8ae002
+    {
8ae002
+      printf ("error: unexpected exit status from child process: %d\n",
8ae002
+              status);
8ae002
+      _exit (1);
8ae002
+    }
8ae002
+
8ae002
+  return NULL;
8ae002
+}
8ae002
+
8ae002
+/* This is used to detect if malloc has not been successfully
8ae002
+   interposed.  The interposed malloc does not use brk/sbrk.  */
8ae002
+static void *initial_brk;
8ae002
+__attribute__ ((constructor))
8ae002
+static void
8ae002
+set_initial_brk (void)
8ae002
+{
8ae002
+  initial_brk = sbrk (0);
8ae002
+}
8ae002
+
8ae002
+/* Terminate the process if the break value has been changed.  */
8ae002
+__attribute__ ((destructor))
8ae002
+static void
8ae002
+check_brk (void)
8ae002
+{
8ae002
+  void *current = sbrk (0);
8ae002
+  if (current != initial_brk)
8ae002
+    {
8ae002
+      printf ("error: brk changed from %p to %p; no interposition?\n",
8ae002
+              initial_brk, current);
8ae002
+      _exit (1);
8ae002
+    }
8ae002
+}
8ae002
+
8ae002
+static int
8ae002
+do_test (void)
8ae002
+{
8ae002
+  check_brk ();
8ae002
+
8ae002
+#if INTERPOSE_THREADS
8ae002
+  pthread_t thr = xpthread_create (NULL, run_tests, NULL);
8ae002
+  xpthread_join (thr);
8ae002
+#else
8ae002
+  run_tests (NULL);
8ae002
+#endif
8ae002
+
8ae002
+  check_brk ();
8ae002
+
8ae002
+  return 0;
8ae002
+}
8ae002
Index: b/malloc/tst-interpose-static-nothread.c
8ae002
===================================================================
8ae002
--- /dev/null
8ae002
+++ b/malloc/tst-interpose-static-nothread.c
8ae002
@@ -0,0 +1,19 @@
8ae002
+/* Malloc interposition test, static version without threads.
8ae002
+   Copyright (C) 2016 Free Software Foundation, Inc.
8ae002
+   This file is part of the GNU C Library.
8ae002
+
8ae002
+   The GNU C Library is free software; you can redistribute it and/or
8ae002
+   modify it under the terms of the GNU Lesser General Public License as
8ae002
+   published by the Free Software Foundation; either version 2.1 of the
8ae002
+   License, or (at your option) any later version.
8ae002
+
8ae002
+   The GNU C Library is distributed in the hope that it will be useful,
8ae002
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
8ae002
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8ae002
+   Lesser General Public License for more details.
8ae002
+
8ae002
+   You should have received a copy of the GNU Lesser General Public
8ae002
+   License along with the GNU C Library; see the file COPYING.LIB.  If
8ae002
+   not, see <http://www.gnu.org/licenses/>.  */
8ae002
+
8ae002
+#include "tst-interpose-nothread.c"
8ae002
Index: b/malloc/tst-interpose-static-thread.c
8ae002
===================================================================
8ae002
--- /dev/null
8ae002
+++ b/malloc/tst-interpose-static-thread.c
8ae002
@@ -0,0 +1,19 @@
8ae002
+/* Malloc interposition test, static version with threads.
8ae002
+   Copyright (C) 2016 Free Software Foundation, Inc.
8ae002
+   This file is part of the GNU C Library.
8ae002
+
8ae002
+   The GNU C Library is free software; you can redistribute it and/or
8ae002
+   modify it under the terms of the GNU Lesser General Public License as
8ae002
+   published by the Free Software Foundation; either version 2.1 of the
8ae002
+   License, or (at your option) any later version.
8ae002
+
8ae002
+   The GNU C Library is distributed in the hope that it will be useful,
8ae002
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
8ae002
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8ae002
+   Lesser General Public License for more details.
8ae002
+
8ae002
+   You should have received a copy of the GNU Lesser General Public
8ae002
+   License along with the GNU C Library; see the file COPYING.LIB.  If
8ae002
+   not, see <http://www.gnu.org/licenses/>.  */
8ae002
+
8ae002
+#include "tst-interpose-nothread.c"
8ae002
Index: b/malloc/tst-interpose-thread.c
8ae002
===================================================================
8ae002
--- /dev/null
8ae002
+++ b/malloc/tst-interpose-thread.c
8ae002
@@ -0,0 +1,20 @@
8ae002
+/* Malloc interposition test, dynamically-linked version with threads.
8ae002
+   Copyright (C) 2016 Free Software Foundation, Inc.
8ae002
+   This file is part of the GNU C Library.
8ae002
+
8ae002
+   The GNU C Library is free software; you can redistribute it and/or
8ae002
+   modify it under the terms of the GNU Lesser General Public License as
8ae002
+   published by the Free Software Foundation; either version 2.1 of the
8ae002
+   License, or (at your option) any later version.
8ae002
+
8ae002
+   The GNU C Library is distributed in the hope that it will be useful,
8ae002
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
8ae002
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
8ae002
+   Lesser General Public License for more details.
8ae002
+
8ae002
+   You should have received a copy of the GNU Lesser General Public
8ae002
+   License along with the GNU C Library; see the file COPYING.LIB.  If
8ae002
+   not, see <http://www.gnu.org/licenses/>.  */
8ae002
+
8ae002
+#define INTERPOSE_THREADS 1
8ae002
+#include "tst-interpose-skeleton.c"
8ae002
Index: b/nptl/sysdeps/unix/sysv/linux/fork.c
8ae002
===================================================================
8ae002
--- a/nptl/sysdeps/unix/sysv/linux/fork.c
8ae002
+++ b/nptl/sysdeps/unix/sysv/linux/fork.c
8ae002
@@ -119,7 +119,7 @@ __libc_fork (void)
8ae002
   /* Acquire malloc locks.  This needs to come last because fork
8ae002
      handlers may use malloc, and the libio list lock has an indirect
8ae002
      malloc dependency as well (via the getdelim function).  */
8ae002
-  __malloc_fork_lock_parent ();
8ae002
+  call_function_static_weak (__malloc_fork_lock_parent);
8ae002
 
8ae002
 #ifndef NDEBUG
8ae002
   pid_t ppid = THREAD_GETMEM (THREAD_SELF, tid);
8ae002
@@ -178,7 +178,7 @@ __libc_fork (void)
8ae002
 #endif
8ae002
 
8ae002
       /* Release malloc locks.  */
8ae002
-      __malloc_fork_unlock_child ();
8ae002
+      call_function_static_weak (__malloc_fork_unlock_child);
8ae002
 
8ae002
       /* Reset the file list.  These are recursive mutexes.  */
8ae002
       fresetlockfiles ();
8ae002
@@ -222,7 +222,7 @@ __libc_fork (void)
8ae002
       THREAD_SETMEM (THREAD_SELF, pid, parentpid);
8ae002
 
8ae002
       /* Release malloc locks, parent process variant.  */
8ae002
-      __malloc_fork_unlock_parent ();
8ae002
+      call_function_static_weak (__malloc_fork_unlock_parent);
8ae002
 
8ae002
       /* We execute this even if the 'fork' call failed.  */
8ae002
       _IO_list_unlock ();