ce426f
#
ce426f
# Based on this upstream commit:
ce426f
#
ce426f
# commit d8dd00805b8f3a011735d7a407097fb1c408d867
ce426f
# Author: H.J. Lu <hjl.tools@gmail.com>
ce426f
# Date:   Fri Nov 28 07:54:07 2014 -0800
ce426f
# 
ce426f
#     Resize DTV if the current DTV isn't big enough
ce426f
#     
ce426f
#     This patch changes _dl_allocate_tls_init to resize DTV if the current DTV
ce426f
#     isn't big enough.  Tested on X86-64, x32 and ia32.
ce426f
#     
ce426f
#         [BZ #13862]
ce426f
#         * elf/dl-tls.c: Include <atomic.h>.
ce426f
#         (oom): Remove #ifdef SHARED/#endif.
ce426f
#         (_dl_static_dtv, _dl_initial_dtv): Moved before ...
ce426f
#         (_dl_resize_dtv): This.  Extracted from _dl_update_slotinfo.
ce426f
#         (_dl_allocate_tls_init): Resize DTV if the current DTV isn't
ce426f
#         big enough.
ce426f
#         (_dl_update_slotinfo): Call _dl_resize_dtv to resize DTV.
ce426f
#         * nptl/Makefile (tests): Add tst-stack4.
ce426f
#         (modules-names): Add tst-stack4mod.
ce426f
#         ($(objpfx)tst-stack4): New.
ce426f
#         (tst-stack4mod.sos): Likewise.
ce426f
#         ($(objpfx)tst-stack4.out): Likewise.
ce426f
#         ($(tst-stack4mod.sos)): Likewise.
ce426f
#         (clean): Likewise.
ce426f
#         * nptl/tst-stack4.c: New file.
ce426f
#         * nptl/tst-stack4mod.c: Likewise.
ce426f
# 
ce426f
diff -urN glibc-2.17-c758a686/elf/dl-tls.c glibc-2.17-c758a686/elf/dl-tls.c
ce426f
--- glibc-2.17-c758a686/elf/dl-tls.c	2015-02-18 14:15:28.078461873 -0500
ce426f
+++ glibc-2.17-c758a686/elf/dl-tls.c	2015-02-18 14:38:37.630374771 -0500
ce426f
@@ -24,6 +24,7 @@
ce426f
 #include <stdlib.h>
ce426f
 #include <unistd.h>
ce426f
 #include <sys/param.h>
ce426f
+#include <atomic.h>
ce426f
 
ce426f
 #include <tls.h>
ce426f
 #include <dl-tls.h>
ce426f
@@ -35,14 +36,12 @@
ce426f
 
ce426f
 
ce426f
 /* Out-of-memory handler.  */
ce426f
-#ifdef SHARED
ce426f
 static void
ce426f
 __attribute__ ((__noreturn__))
ce426f
 oom (void)
ce426f
 {
ce426f
   _dl_fatal_printf ("cannot allocate memory for thread-local data: ABORT\n");
ce426f
 }
ce426f
-#endif
ce426f
 
ce426f
 
ce426f
 size_t
ce426f
@@ -392,6 +391,52 @@
ce426f
   return result;
ce426f
 }
ce426f
 
ce426f
+static dtv_t *
ce426f
+_dl_resize_dtv (dtv_t *dtv)
ce426f
+{
ce426f
+  /* Resize the dtv.  */
ce426f
+  dtv_t *newp;
ce426f
+  /* Load GL(dl_tls_max_dtv_idx) atomically since it may be written to by
ce426f
+     other threads concurrently. -- We don't have the required atomic
ce426f
+     infrastructure to load dl_tls_max_dtv_idx atomically, but on all the
ce426f
+     architectures we care about it should load atomically. If this had
ce426f
+     an atomic_load_acquire we would still be missing the releases for
ce426f
+     the writes.  */
ce426f
+  size_t newsize = GL(dl_tls_max_dtv_idx) + DTV_SURPLUS;
ce426f
+  size_t oldsize = dtv[-1].counter;
ce426f
+
ce426f
+#if SHARED
ce426f
+  if (dtv == GL(dl_initial_dtv))
ce426f
+    {
ce426f
+      /* This is the initial dtv that was either statically allocated in
ce426f
+	 __libc_setup_tls or allocated during rtld startup using the
ce426f
+	 dl-minimal.c malloc instead of the real malloc.  We can't free
ce426f
+	 it, we have to abandon the old storage.  */
ce426f
+
ce426f
+      newp = malloc ((2 + newsize) * sizeof (dtv_t));
ce426f
+      if (newp == NULL)
ce426f
+	oom ();
ce426f
+      memcpy (newp, &dtv[-1], (2 + oldsize) * sizeof (dtv_t));
ce426f
+    }
ce426f
+  else
ce426f
+#endif
ce426f
+    {
ce426f
+      newp = realloc (&dtv[-1],
ce426f
+		      (2 + newsize) * sizeof (dtv_t));
ce426f
+      if (newp == NULL)
ce426f
+	oom ();
ce426f
+    }
ce426f
+
ce426f
+  newp[0].counter = newsize;
ce426f
+
ce426f
+  /* Clear the newly allocated part.  */
ce426f
+  memset (newp + 2 + oldsize, '\0',
ce426f
+	  (newsize - oldsize) * sizeof (dtv_t));
ce426f
+
ce426f
+  /* Return the generation counter.  */
ce426f
+  return &newp[1];
ce426f
+}
ce426f
+
ce426f
 
ce426f
 void *
ce426f
 internal_function
ce426f
@@ -406,6 +451,16 @@
ce426f
   size_t total = 0;
ce426f
   size_t maxgen = 0;
ce426f
 
ce426f
+  /* Check if the current dtv is big enough.   */
ce426f
+  if (dtv[-1].counter < GL(dl_tls_max_dtv_idx))
ce426f
+    {
ce426f
+      /* Resize the dtv.  */
ce426f
+      dtv = _dl_resize_dtv (dtv);
ce426f
+
ce426f
+      /* Install this new dtv in the thread data structures.  */
ce426f
+      INSTALL_DTV (result, &dtv[-1]);
ce426f
+    }
ce426f
+
ce426f
   /* We have to prepare the dtv for all currently loaded modules using
ce426f
      TLS.  For those which are dynamically loaded we add the values
ce426f
      indicating deferred allocation.  */
ce426f
@@ -637,41 +692,10 @@
ce426f
 	      assert (total + cnt == modid);
ce426f
 	      if (dtv[-1].counter < modid)
ce426f
 		{
ce426f
-		  /* Reallocate the dtv.  */
ce426f
-		  dtv_t *newp;
ce426f
-		  size_t newsize = GL(dl_tls_max_dtv_idx) + DTV_SURPLUS;
ce426f
-		  size_t oldsize = dtv[-1].counter;
ce426f
-
ce426f
-		  assert (map->l_tls_modid <= newsize);
ce426f
-
ce426f
-		  if (dtv == GL(dl_initial_dtv))
ce426f
-		    {
ce426f
-		      /* This is the initial dtv that was allocated
ce426f
-			 during rtld startup using the dl-minimal.c
ce426f
-			 malloc instead of the real malloc.  We can't
ce426f
-			 free it, we have to abandon the old storage.  */
ce426f
-
ce426f
-		      newp = malloc ((2 + newsize) * sizeof (dtv_t));
ce426f
-		      if (newp == NULL)
ce426f
-			oom ();
ce426f
-		      memcpy (newp, &dtv[-1], (2 + oldsize) * sizeof (dtv_t));
ce426f
-		    }
ce426f
-		  else
ce426f
-		    {
ce426f
-		      newp = realloc (&dtv[-1],
ce426f
-				      (2 + newsize) * sizeof (dtv_t));
ce426f
-		      if (newp == NULL)
ce426f
-			oom ();
ce426f
-		    }
ce426f
-
ce426f
-		  newp[0].counter = newsize;
ce426f
-
ce426f
-		  /* Clear the newly allocated part.  */
ce426f
-		  memset (newp + 2 + oldsize, '\0',
ce426f
-			  (newsize - oldsize) * sizeof (dtv_t));
ce426f
+		  /* Resize the dtv.  */
ce426f
+		  dtv = _dl_resize_dtv (dtv);
ce426f
 
ce426f
-		  /* Point dtv to the generation counter.  */
ce426f
-		  dtv = &newp[1];
ce426f
+		  assert (modid <= dtv[-1].counter);
ce426f
 
ce426f
 		  /* Install this new dtv in the thread data
ce426f
 		     structures.  */
ce426f
diff -urN glibc-2.17-c758a686/nptl/Makefile glibc-2.17-c758a686/nptl/Makefile
ce426f
--- glibc-2.17-c758a686/nptl/Makefile	2015-02-18 14:15:28.073462028 -0500
ce426f
+++ glibc-2.17-c758a686/nptl/Makefile	2015-02-18 14:15:49.817786667 -0500
ce426f
@@ -251,7 +251,7 @@
ce426f
 	tst-exec1 tst-exec2 tst-exec3 tst-exec4 \
ce426f
 	tst-exit1 tst-exit2 tst-exit3 \
ce426f
 	tst-stdio1 tst-stdio2 \
ce426f
-	tst-stack1 tst-stack2 tst-stack3 tst-pthread-getattr \
ce426f
+	tst-stack1 tst-stack2 tst-stack3 tst-stack4 tst-pthread-getattr \
ce426f
 	tst-unload \
ce426f
 	tst-dlsym1 \
ce426f
 	tst-sysconf \
ce426f
@@ -297,7 +297,7 @@
ce426f
 
ce426f
 modules-names = tst-atfork2mod tst-tls3mod tst-tls4moda tst-tls4modb \
ce426f
 		tst-tls5mod tst-tls5moda tst-tls5modb tst-tls5modc \
ce426f
-		tst-tls5modd tst-tls5mode tst-tls5modf \
ce426f
+		tst-tls5modd tst-tls5mode tst-tls5modf tst-stack4mod \
ce426f
 		tst-_res1mod1 tst-_res1mod2 tst-execstack-mod tst-fini1mod
ce426f
 extra-test-objs += $(addsuffix .os,$(strip $(modules-names))) tst-cleanup4aux.o
ce426f
 test-extras += $(modules-names) tst-cleanup4aux
ce426f
@@ -459,6 +459,19 @@
ce426f
 	$(common-objpfx)malloc/mtrace $(objpfx)tst-stack3.mtrace > $@
ce426f
 generated += tst-stack3-mem tst-stack3.mtrace
ce426f
 
ce426f
+$(objpfx)tst-stack4: $(libdl) $(shared-thread-library)
ce426f
+tst-stack4mod.sos=$(shell for i in 0 1 2 3 4 5 6 7 8 9 10 \
ce426f
+				   11 12 13 14 15 16 17 18 19; do \
ce426f
+			    for j in 0 1 2 3 4 5 6 7 8 9 10 \
ce426f
+				     11 12 13 14 15 16 17 18 19; do \
ce426f
+			      echo $(objpfx)tst-stack4mod-$$i-$$j.so; \
ce426f
+			    done; done)
ce426f
+$(objpfx)tst-stack4.out: $(tst-stack4mod.sos)
ce426f
+$(tst-stack4mod.sos): $(objpfx)tst-stack4mod.so
ce426f
+	cp -f $< $@
ce426f
+clean:
ce426f
+	rm -f $(tst-stack4mod.sos)
ce426f
+
ce426f
 $(objpfx)tst-cleanup4: $(objpfx)tst-cleanup4aux.o $(shared-thread-library)
ce426f
 $(objpfx)tst-cleanupx4: $(objpfx)tst-cleanup4aux.o $(shared-thread-library)
ce426f
 
ce426f
diff -urN glibc-2.17-c758a686/nptl/tst-stack4.c glibc-2.17-c758a686/nptl/tst-stack4.c
ce426f
--- glibc-2.17-c758a686/nptl/tst-stack4.c	1969-12-31 19:00:00.000000000 -0500
ce426f
+++ glibc-2.17-c758a686/nptl/tst-stack4.c	2015-02-18 14:15:49.817786667 -0500
ce426f
@@ -0,0 +1,159 @@
ce426f
+/* Test DTV size oveflow when pthread_create reuses old DTV and TLS is
ce426f
+   used by dlopened shared object.
ce426f
+   Copyright (C) 2014 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
ce426f
+   License as published by the Free Software Foundation; either
ce426f
+   version 2.1 of the 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; if not, see
ce426f
+   <http://www.gnu.org/licenses/>.  */
ce426f
+
ce426f
+#include <stdio.h>
ce426f
+#include <stdint.h>
ce426f
+#include <dlfcn.h>
ce426f
+#include <assert.h>
ce426f
+#include <pthread.h>
ce426f
+
ce426f
+/* The choices of thread count, and file counts are arbitary.
ce426f
+   The point is simply to run enough threads that an exiting
ce426f
+   thread has it's stack reused by another thread at the same
ce426f
+   time as new libraries have been loaded.  */
ce426f
+#define DSO_SHARED_FILES 20
ce426f
+#define DSO_OPEN_THREADS 20
ce426f
+#define DSO_EXEC_THREADS 2
ce426f
+
ce426f
+/* Used to make sure that only one thread is calling dlopen and dlclose
ce426f
+   at a time.  */
ce426f
+pthread_mutex_t g_lock;
ce426f
+
ce426f
+typedef void (*function) (void);
ce426f
+
ce426f
+void *
ce426f
+dso_invoke(void *dso_fun)
ce426f
+{
ce426f
+  function *fun_vec = (function *) dso_fun;
ce426f
+  int dso;
ce426f
+
ce426f
+  for (dso = 0; dso < DSO_SHARED_FILES; dso++)
ce426f
+    (*fun_vec[dso]) ();
ce426f
+
ce426f
+  pthread_exit (NULL);
ce426f
+}
ce426f
+
ce426f
+void *
ce426f
+dso_process (void * p)
ce426f
+{
ce426f
+  void *handle[DSO_SHARED_FILES];
ce426f
+  function fun_vec[DSO_SHARED_FILES];
ce426f
+  char dso_path[DSO_SHARED_FILES][100];
ce426f
+  int dso;
ce426f
+  uintptr_t t = (uintptr_t) p;
ce426f
+
ce426f
+  /* Open DSOs and get a function.  */
ce426f
+  for (dso = 0; dso < DSO_SHARED_FILES; dso++)
ce426f
+    {
ce426f
+      sprintf (dso_path[dso], "tst-stack4mod-%i-%i.so", t, dso);
ce426f
+
ce426f
+      pthread_mutex_lock (&g_lock);
ce426f
+
ce426f
+      handle[dso] = dlopen (dso_path[dso], RTLD_NOW);
ce426f
+      assert (handle[dso]);
ce426f
+
ce426f
+      fun_vec[dso] = (function) dlsym (handle[dso], "function");
ce426f
+      assert (fun_vec[dso]);
ce426f
+
ce426f
+      pthread_mutex_unlock (&g_lock);
ce426f
+    }
ce426f
+
ce426f
+  /* Spawn workers.  */
ce426f
+  pthread_t thread[DSO_EXEC_THREADS];
ce426f
+  int i, ret;
ce426f
+  uintptr_t result = 0;
ce426f
+  for (i = 0; i < DSO_EXEC_THREADS; i++)
ce426f
+    {
ce426f
+      pthread_mutex_lock (&g_lock);
ce426f
+      ret = pthread_create (&thread[i], NULL, dso_invoke, (void *) fun_vec);
ce426f
+      if (ret != 0)
ce426f
+	{
ce426f
+	  printf ("pthread_create failed: %d\n", ret);
ce426f
+	  result = 1;
ce426f
+	}
ce426f
+      pthread_mutex_unlock (&g_lock);
ce426f
+    }
ce426f
+
ce426f
+  if (!result)
ce426f
+    for (i = 0; i < DSO_EXEC_THREADS; i++)
ce426f
+      {
ce426f
+	ret = pthread_join (thread[i], NULL);
ce426f
+	if (ret != 0)
ce426f
+	  {
ce426f
+	    printf ("pthread_join failed: %d\n", ret);
ce426f
+	    result = 1;
ce426f
+	  }
ce426f
+      }
ce426f
+
ce426f
+  /* Close all DSOs.  */
ce426f
+  for (dso = 0; dso < DSO_SHARED_FILES; dso++)
ce426f
+    {
ce426f
+      pthread_mutex_lock (&g_lock);
ce426f
+      dlclose (handle[dso]);
ce426f
+      pthread_mutex_unlock (&g_lock);
ce426f
+    }
ce426f
+
ce426f
+  /* Exit.  */
ce426f
+  pthread_exit ((void *) result);
ce426f
+}
ce426f
+
ce426f
+static int
ce426f
+do_test (void)
ce426f
+{
ce426f
+  pthread_t thread[DSO_OPEN_THREADS];
ce426f
+  int i,j;
ce426f
+  int ret;
ce426f
+  int result = 0;
ce426f
+
ce426f
+  pthread_mutex_init (&g_lock, NULL);
ce426f
+
ce426f
+  /* 100 is arbitrary here and is known to trigger PR 13862.  */
ce426f
+  for (j = 0; j < 100; j++)
ce426f
+    {
ce426f
+      for (i = 0; i < DSO_OPEN_THREADS; i++)
ce426f
+	{
ce426f
+	  ret = pthread_create (&thread[i], NULL, dso_process,
ce426f
+				(void *) (uintptr_t) i);
ce426f
+	  if (ret != 0)
ce426f
+	    {
ce426f
+	      printf ("pthread_create failed: %d\n", ret);
ce426f
+	      result = 1;
ce426f
+	    }
ce426f
+	}
ce426f
+
ce426f
+      if (result)
ce426f
+	break;
ce426f
+
ce426f
+      for (i = 0; i < DSO_OPEN_THREADS; i++)
ce426f
+	{
ce426f
+	  ret = pthread_join (thread[i], NULL);
ce426f
+	  if (ret != 0)
ce426f
+	    {
ce426f
+	      printf ("pthread_join failed: %d\n", ret);
ce426f
+	      result = 1;
ce426f
+	    }
ce426f
+	}
ce426f
+    }
ce426f
+
ce426f
+  return result;
ce426f
+}
ce426f
+
ce426f
+#define TEST_FUNCTION do_test ()
ce426f
+#define TIMEOUT 100
ce426f
+#include "../test-skeleton.c"
ce426f
diff -urN glibc-2.17-c758a686/nptl/tst-stack4mod.c glibc-2.17-c758a686/nptl/tst-stack4mod.c
ce426f
--- glibc-2.17-c758a686/nptl/tst-stack4mod.c	1969-12-31 19:00:00.000000000 -0500
ce426f
+++ glibc-2.17-c758a686/nptl/tst-stack4mod.c	2015-02-18 14:15:49.817786667 -0500
ce426f
@@ -0,0 +1,28 @@
ce426f
+/* This tests DTV usage with TLS in dlopened shared object.
ce426f
+   Copyright (C) 2014 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
ce426f
+   License as published by the Free Software Foundation; either
ce426f
+   version 2.1 of the 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; if not, see
ce426f
+   <http://www.gnu.org/licenses/>.  */
ce426f
+
ce426f
+/* 256 is arbitrary here and is known to trigger PR 13862.  */
ce426f
+__thread int var[256] attribute_hidden = {0};
ce426f
+
ce426f
+void
ce426f
+function (void)
ce426f
+{
ce426f
+  int i;
ce426f
+  for (i = 0; i < sizeof (var) / sizeof (int); i++)
ce426f
+    var[i] = i;
ce426f
+}