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