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