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