548bcb
commit 9d0e30329c23b5ad736fda3f174208c25970dbce
548bcb
Author: Szabolcs Nagy <szabolcs.nagy@arm.com>
548bcb
Date:   Tue Dec 13 12:28:41 2016 +0000
548bcb
548bcb
    elf: Add test case for [BZ #19329]
548bcb
    
548bcb
    Test concurrent dlopen and pthread_create when the loaded modules have
548bcb
    TLS.  This triggers dl-tls assertion failures more reliably than the
548bcb
    nptl/tst-stack4 test.
548bcb
    
548bcb
    The dlopened module has 100 DT_NEEDED dependencies with TLS, they were
548bcb
    reused from an existing TLS test. The number of created threads during
548bcb
    dlopen depends on filesystem speed and hardware, but at most 3 threads
548bcb
    are alive at a time to limit resource usage.
548bcb
    
548bcb
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
548bcb
548bcb
Conflicts:
548bcb
	elf/Makefile
548bcb
	  (usual testing differences)
548bcb
548bcb
diff --git a/elf/Makefile b/elf/Makefile
548bcb
index 0995d810b57d0dda..be40e3761cf91c4a 100644
548bcb
--- a/elf/Makefile
548bcb
+++ b/elf/Makefile
548bcb
@@ -210,7 +210,7 @@ tests += restest1 preloadtest loadfail multiload origtest resolvfail \
548bcb
 	 tst-tls-ie tst-tls-ie-dlmopen \
548bcb
 	 argv0test \
548bcb
 	 tst-glibc-hwcaps tst-glibc-hwcaps-prepend tst-glibc-hwcaps-mask \
548bcb
-	 tst-tls20
548bcb
+	 tst-tls20 tst-tls21
548bcb
 #	 reldep9
548bcb
 tests-internal += loadtest unload unload2 circleload1 \
548bcb
 	 neededtest neededtest2 neededtest3 neededtest4 \
548bcb
@@ -333,7 +333,7 @@ modules-names = testobj1 testobj2 testobj3 testobj4 testobj5 testobj6 \
548bcb
 		libmarkermod2-1 libmarkermod2-2 \
548bcb
 		libmarkermod3-1 libmarkermod3-2 libmarkermod3-3 \
548bcb
 		libmarkermod4-1 libmarkermod4-2 libmarkermod4-3 libmarkermod4-4 \
548bcb
-		tst-tls20mod-bad
548bcb
+		tst-tls20mod-bad tst-tls21mod \
548bcb
 
548bcb
 # Most modules build with _ISOMAC defined, but those filtered out
548bcb
 # depend on internal headers.
548bcb
@@ -1836,3 +1836,8 @@ tst-tls20mod-bad.so-no-z-defs = yes
548bcb
 $(objpfx)tst-tls20: $(libdl) $(shared-thread-library)
548bcb
 $(objpfx)tst-tls20.out: $(objpfx)tst-tls20mod-bad.so \
548bcb
 			$(tst-tls-many-dynamic-modules:%=$(objpfx)%.so)
548bcb
+
548bcb
+# Reuses tst-tls-many-dynamic-modules
548bcb
+$(objpfx)tst-tls21: $(libdl) $(shared-thread-library)
548bcb
+$(objpfx)tst-tls21.out: $(objpfx)tst-tls21mod.so
548bcb
+$(objpfx)tst-tls21mod.so: $(tst-tls-many-dynamic-modules:%=$(objpfx)%.so)
548bcb
diff --git a/elf/tst-tls21.c b/elf/tst-tls21.c
548bcb
new file mode 100644
548bcb
index 0000000000000000..560bf5813a746417
548bcb
--- /dev/null
548bcb
+++ b/elf/tst-tls21.c
548bcb
@@ -0,0 +1,68 @@
548bcb
+/* Test concurrent dlopen and pthread_create: BZ 19329.
548bcb
+   Copyright (C) 2021 Free Software Foundation, Inc.
548bcb
+   This file is part of the GNU C Library.
548bcb
+
548bcb
+   The GNU C Library is free software; you can redistribute it and/or
548bcb
+   modify it under the terms of the GNU Lesser General Public
548bcb
+   License as published by the Free Software Foundation; either
548bcb
+   version 2.1 of the License, or (at your option) any later version.
548bcb
+
548bcb
+   The GNU C Library is distributed in the hope that it will be useful,
548bcb
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
548bcb
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
548bcb
+   Lesser General Public License for more details.
548bcb
+
548bcb
+   You should have received a copy of the GNU Lesser General Public
548bcb
+   License along with the GNU C Library; if not, see
548bcb
+   <http://www.gnu.org/licenses/>.  */
548bcb
+
548bcb
+#include <dlfcn.h>
548bcb
+#include <pthread.h>
548bcb
+#include <stdio.h>
548bcb
+#include <stdatomic.h>
548bcb
+#include <support/xdlfcn.h>
548bcb
+#include <support/xthread.h>
548bcb
+
548bcb
+#define THREADS 10000
548bcb
+
548bcb
+static atomic_int done;
548bcb
+
548bcb
+static void *
548bcb
+start (void *a)
548bcb
+{
548bcb
+  /* Load a module with many dependencies that each have TLS.  */
548bcb
+  xdlopen ("tst-tls21mod.so", RTLD_LAZY);
548bcb
+  atomic_store_explicit (&done, 1, memory_order_release);
548bcb
+  return 0;
548bcb
+}
548bcb
+
548bcb
+static void *
548bcb
+nop (void *a)
548bcb
+{
548bcb
+  return 0;
548bcb
+}
548bcb
+
548bcb
+static int
548bcb
+do_test (void)
548bcb
+{
548bcb
+  pthread_t t1, t2;
548bcb
+  int i;
548bcb
+
548bcb
+  /* Load a module with lots of dependencies and TLS.  */
548bcb
+  t1 = xpthread_create (0, start, 0);
548bcb
+
548bcb
+  /* Concurrently create lots of threads until dlopen is observably done.  */
548bcb
+  for (i = 0; i < THREADS; i++)
548bcb
+    {
548bcb
+      if (atomic_load_explicit (&done, memory_order_acquire) != 0)
548bcb
+	break;
548bcb
+      t2 = xpthread_create (0, nop, 0);
548bcb
+      xpthread_join (t2);
548bcb
+    }
548bcb
+
548bcb
+  xpthread_join (t1);
548bcb
+  printf ("threads created during dlopen: %d\n", i);
548bcb
+  return 0;
548bcb
+}
548bcb
+
548bcb
+#include <support/test-driver.c>
548bcb
diff --git a/elf/tst-tls21mod.c b/elf/tst-tls21mod.c
548bcb
new file mode 100644
548bcb
index 0000000000000000..206ece4fb34622a9
548bcb
--- /dev/null
548bcb
+++ b/elf/tst-tls21mod.c
548bcb
@@ -0,0 +1 @@
548bcb
+int __thread x;