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