olga / rpms / glibc

Forked from rpms/glibc 5 years ago
Clone

Blame SOURCES/glibc-rh906468-2.patch

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