|
 |
7729eb |
commit 024a7640ab9ecea80e527f4e4d7f7a1868e952c5
|
|
 |
7729eb |
Author: Szabolcs Nagy <szabolcs.nagy@arm.com>
|
|
 |
7729eb |
Date: Wed Sep 15 15:16:19 2021 +0100
|
|
 |
7729eb |
|
|
 |
7729eb |
elf: Avoid deadlock between pthread_create and ctors [BZ #28357]
|
|
 |
7729eb |
|
|
 |
7729eb |
The fix for bug 19329 caused a regression such that pthread_create can
|
|
 |
7729eb |
deadlock when concurrent ctors from dlopen are waiting for it to finish.
|
|
 |
7729eb |
Use a new GL(dl_load_tls_lock) in pthread_create that is not taken
|
|
 |
7729eb |
around ctors in dlopen.
|
|
 |
7729eb |
|
|
 |
7729eb |
The new lock is also used in __tls_get_addr instead of GL(dl_load_lock).
|
|
 |
7729eb |
|
|
 |
7729eb |
The new lock is held in _dl_open_worker and _dl_close_worker around
|
|
 |
7729eb |
most of the logic before/after the init/fini routines. When init/fini
|
|
 |
7729eb |
routines are running then TLS is in a consistent, usable state.
|
|
 |
7729eb |
In _dl_open_worker the new lock requires catching and reraising dlopen
|
|
 |
7729eb |
failures that happen in the critical section.
|
|
 |
7729eb |
|
|
 |
7729eb |
The new lock is reinitialized in a fork child, to keep the existing
|
|
 |
7729eb |
behaviour and it is kept recursive in case malloc interposition or TLS
|
|
 |
7729eb |
access from signal handlers can retake it. It is not obvious if this
|
|
 |
7729eb |
is necessary or helps, but avoids changing the preexisting behaviour.
|
|
 |
7729eb |
|
|
 |
7729eb |
The new lock may be more appropriate for dl_iterate_phdr too than
|
|
 |
7729eb |
GL(dl_load_write_lock), since TLS state of an incompletely loaded
|
|
 |
7729eb |
module may be accessed. If the new lock can replace the old one,
|
|
 |
7729eb |
that can be a separate change.
|
|
 |
7729eb |
|
|
 |
7729eb |
Fixes bug 28357.
|
|
 |
7729eb |
|
|
 |
7729eb |
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
|
|
 |
7729eb |
(cherry picked from commit 83b5323261bb72313bffcf37476c1b8f0847c736)
|
|
 |
7729eb |
|
|
 |
7729eb |
diff --git a/elf/dl-close.c b/elf/dl-close.c
|
|
 |
7729eb |
index f39001cab981b723..cd7b9c9fe83a1a44 100644
|
|
 |
7729eb |
--- a/elf/dl-close.c
|
|
 |
7729eb |
+++ b/elf/dl-close.c
|
|
 |
7729eb |
@@ -549,6 +549,9 @@ _dl_close_worker (struct link_map *map, bool force)
|
|
 |
7729eb |
size_t tls_free_end;
|
|
 |
7729eb |
tls_free_start = tls_free_end = NO_TLS_OFFSET;
|
|
 |
7729eb |
|
|
 |
7729eb |
+ /* Protects global and module specitic TLS state. */
|
|
 |
7729eb |
+ __rtld_lock_lock_recursive (GL(dl_load_tls_lock));
|
|
 |
7729eb |
+
|
|
 |
7729eb |
/* We modify the list of loaded objects. */
|
|
 |
7729eb |
__rtld_lock_lock_recursive (GL(dl_load_write_lock));
|
|
 |
7729eb |
|
|
 |
7729eb |
@@ -784,6 +787,9 @@ _dl_close_worker (struct link_map *map, bool force)
|
|
 |
7729eb |
GL(dl_tls_static_used) = tls_free_start;
|
|
 |
7729eb |
}
|
|
 |
7729eb |
|
|
 |
7729eb |
+ /* TLS is cleaned up for the unloaded modules. */
|
|
 |
7729eb |
+ __rtld_lock_unlock_recursive (GL(dl_load_tls_lock));
|
|
 |
7729eb |
+
|
|
 |
7729eb |
#ifdef SHARED
|
|
 |
7729eb |
/* Auditing checkpoint: we have deleted all objects. */
|
|
 |
7729eb |
if (__glibc_unlikely (do_audit))
|
|
 |
7729eb |
diff --git a/elf/dl-open.c b/elf/dl-open.c
|
|
 |
7729eb |
index 41c7250bf630f978..bc68e2c376debd71 100644
|
|
 |
7729eb |
--- a/elf/dl-open.c
|
|
 |
7729eb |
+++ b/elf/dl-open.c
|
|
 |
7729eb |
@@ -66,6 +66,9 @@ struct dl_open_args
|
|
 |
7729eb |
libc_map value in the namespace in case of a dlopen failure. */
|
|
 |
7729eb |
bool libc_already_loaded;
|
|
 |
7729eb |
|
|
 |
7729eb |
+ /* Set to true if the end of dl_open_worker_begin was reached. */
|
|
 |
7729eb |
+ bool worker_continue;
|
|
 |
7729eb |
+
|
|
 |
7729eb |
/* Original parameters to the program and the current environment. */
|
|
 |
7729eb |
int argc;
|
|
 |
7729eb |
char **argv;
|
|
 |
7729eb |
@@ -482,7 +485,7 @@ call_dl_init (void *closure)
|
|
 |
7729eb |
}
|
|
 |
7729eb |
|
|
 |
7729eb |
static void
|
|
 |
7729eb |
-dl_open_worker (void *a)
|
|
 |
7729eb |
+dl_open_worker_begin (void *a)
|
|
 |
7729eb |
{
|
|
 |
7729eb |
struct dl_open_args *args = a;
|
|
 |
7729eb |
const char *file = args->file;
|
|
 |
7729eb |
@@ -774,6 +777,36 @@ dl_open_worker (void *a)
|
|
 |
7729eb |
_dl_call_libc_early_init (libc_map, false);
|
|
 |
7729eb |
}
|
|
 |
7729eb |
|
|
 |
7729eb |
+ args->worker_continue = true;
|
|
 |
7729eb |
+}
|
|
 |
7729eb |
+
|
|
 |
7729eb |
+static void
|
|
 |
7729eb |
+dl_open_worker (void *a)
|
|
 |
7729eb |
+{
|
|
 |
7729eb |
+ struct dl_open_args *args = a;
|
|
 |
7729eb |
+
|
|
 |
7729eb |
+ args->worker_continue = false;
|
|
 |
7729eb |
+
|
|
 |
7729eb |
+ {
|
|
 |
7729eb |
+ /* Protects global and module specific TLS state. */
|
|
 |
7729eb |
+ __rtld_lock_lock_recursive (GL(dl_load_tls_lock));
|
|
 |
7729eb |
+
|
|
 |
7729eb |
+ struct dl_exception ex;
|
|
 |
7729eb |
+ int err = _dl_catch_exception (&ex, dl_open_worker_begin, args);
|
|
 |
7729eb |
+
|
|
 |
7729eb |
+ __rtld_lock_unlock_recursive (GL(dl_load_tls_lock));
|
|
 |
7729eb |
+
|
|
 |
7729eb |
+ if (__glibc_unlikely (ex.errstring != NULL))
|
|
 |
7729eb |
+ /* Reraise the error. */
|
|
 |
7729eb |
+ _dl_signal_exception (err, &ex, NULL);
|
|
 |
7729eb |
+ }
|
|
 |
7729eb |
+
|
|
 |
7729eb |
+ if (!args->worker_continue)
|
|
 |
7729eb |
+ return;
|
|
 |
7729eb |
+
|
|
 |
7729eb |
+ int mode = args->mode;
|
|
 |
7729eb |
+ struct link_map *new = args->map;
|
|
 |
7729eb |
+
|
|
 |
7729eb |
/* Run the initializer functions of new objects. Temporarily
|
|
 |
7729eb |
disable the exception handler, so that lazy binding failures are
|
|
 |
7729eb |
fatal. */
|
|
 |
7729eb |
diff --git a/elf/dl-support.c b/elf/dl-support.c
|
|
 |
7729eb |
index 01557181753fb38d..d8c06ba7eb4c76ea 100644
|
|
 |
7729eb |
--- a/elf/dl-support.c
|
|
 |
7729eb |
+++ b/elf/dl-support.c
|
|
 |
7729eb |
@@ -229,6 +229,13 @@ __rtld_lock_define_initialized_recursive (, _dl_load_lock)
|
|
 |
7729eb |
list of loaded objects while an object is added to or removed from
|
|
 |
7729eb |
that list. */
|
|
 |
7729eb |
__rtld_lock_define_initialized_recursive (, _dl_load_write_lock)
|
|
 |
7729eb |
+ /* This lock protects global and module specific TLS related data.
|
|
 |
7729eb |
+ E.g. it is held in dlopen and dlclose when GL(dl_tls_generation),
|
|
 |
7729eb |
+ GL(dl_tls_max_dtv_idx) or GL(dl_tls_dtv_slotinfo_list) are
|
|
 |
7729eb |
+ accessed and when TLS related relocations are processed for a
|
|
 |
7729eb |
+ module. It was introduced to keep pthread_create accessing TLS
|
|
 |
7729eb |
+ state that is being set up. */
|
|
 |
7729eb |
+__rtld_lock_define_initialized_recursive (, _dl_load_tls_lock)
|
|
 |
7729eb |
|
|
 |
7729eb |
|
|
 |
7729eb |
#ifdef HAVE_AUX_VECTOR
|
|
 |
7729eb |
diff --git a/elf/dl-tls.c b/elf/dl-tls.c
|
|
 |
7729eb |
index 423e380f7ca654fe..40263cf586e74c64 100644
|
|
 |
7729eb |
--- a/elf/dl-tls.c
|
|
 |
7729eb |
+++ b/elf/dl-tls.c
|
|
 |
7729eb |
@@ -532,7 +532,7 @@ _dl_allocate_tls_init (void *result)
|
|
 |
7729eb |
size_t maxgen = 0;
|
|
 |
7729eb |
|
|
 |
7729eb |
/* Protects global dynamic TLS related state. */
|
|
 |
7729eb |
- __rtld_lock_lock_recursive (GL(dl_load_lock));
|
|
 |
7729eb |
+ __rtld_lock_lock_recursive (GL(dl_load_tls_lock));
|
|
 |
7729eb |
|
|
 |
7729eb |
/* Check if the current dtv is big enough. */
|
|
 |
7729eb |
if (dtv[-1].counter < GL(dl_tls_max_dtv_idx))
|
|
 |
7729eb |
@@ -606,7 +606,7 @@ _dl_allocate_tls_init (void *result)
|
|
 |
7729eb |
listp = listp->next;
|
|
 |
7729eb |
assert (listp != NULL);
|
|
 |
7729eb |
}
|
|
 |
7729eb |
- __rtld_lock_unlock_recursive (GL(dl_load_lock));
|
|
 |
7729eb |
+ __rtld_lock_unlock_recursive (GL(dl_load_tls_lock));
|
|
 |
7729eb |
|
|
 |
7729eb |
/* The DTV version is up-to-date now. */
|
|
 |
7729eb |
dtv[0].counter = maxgen;
|
|
 |
7729eb |
@@ -745,7 +745,7 @@ _dl_update_slotinfo (unsigned long int req_modid)
|
|
 |
7729eb |
|
|
 |
7729eb |
Here the dtv needs to be updated to new_gen generation count.
|
|
 |
7729eb |
|
|
 |
7729eb |
- This code may be called during TLS access when GL(dl_load_lock)
|
|
 |
7729eb |
+ This code may be called during TLS access when GL(dl_load_tls_lock)
|
|
 |
7729eb |
is not held. In that case the user code has to synchronize with
|
|
 |
7729eb |
dlopen and dlclose calls of relevant modules. A module m is
|
|
 |
7729eb |
relevant if the generation of m <= new_gen and dlclose of m is
|
|
 |
7729eb |
@@ -867,11 +867,11 @@ tls_get_addr_tail (GET_ADDR_ARGS, dtv_t *dtv, struct link_map *the_map)
|
|
 |
7729eb |
if (__glibc_unlikely (the_map->l_tls_offset
|
|
 |
7729eb |
!= FORCED_DYNAMIC_TLS_OFFSET))
|
|
 |
7729eb |
{
|
|
 |
7729eb |
- __rtld_lock_lock_recursive (GL(dl_load_lock));
|
|
 |
7729eb |
+ __rtld_lock_lock_recursive (GL(dl_load_tls_lock));
|
|
 |
7729eb |
if (__glibc_likely (the_map->l_tls_offset == NO_TLS_OFFSET))
|
|
 |
7729eb |
{
|
|
 |
7729eb |
the_map->l_tls_offset = FORCED_DYNAMIC_TLS_OFFSET;
|
|
 |
7729eb |
- __rtld_lock_unlock_recursive (GL(dl_load_lock));
|
|
 |
7729eb |
+ __rtld_lock_unlock_recursive (GL(dl_load_tls_lock));
|
|
 |
7729eb |
}
|
|
 |
7729eb |
else if (__glibc_likely (the_map->l_tls_offset
|
|
 |
7729eb |
!= FORCED_DYNAMIC_TLS_OFFSET))
|
|
 |
7729eb |
@@ -883,7 +883,7 @@ tls_get_addr_tail (GET_ADDR_ARGS, dtv_t *dtv, struct link_map *the_map)
|
|
 |
7729eb |
#else
|
|
 |
7729eb |
# error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
|
|
 |
7729eb |
#endif
|
|
 |
7729eb |
- __rtld_lock_unlock_recursive (GL(dl_load_lock));
|
|
 |
7729eb |
+ __rtld_lock_unlock_recursive (GL(dl_load_tls_lock));
|
|
 |
7729eb |
|
|
 |
7729eb |
dtv[GET_ADDR_MODULE].pointer.to_free = NULL;
|
|
 |
7729eb |
dtv[GET_ADDR_MODULE].pointer.val = p;
|
|
 |
7729eb |
@@ -891,7 +891,7 @@ tls_get_addr_tail (GET_ADDR_ARGS, dtv_t *dtv, struct link_map *the_map)
|
|
 |
7729eb |
return (char *) p + GET_ADDR_OFFSET;
|
|
 |
7729eb |
}
|
|
 |
7729eb |
else
|
|
 |
7729eb |
- __rtld_lock_unlock_recursive (GL(dl_load_lock));
|
|
 |
7729eb |
+ __rtld_lock_unlock_recursive (GL(dl_load_tls_lock));
|
|
 |
7729eb |
}
|
|
 |
7729eb |
struct dtv_pointer result = allocate_and_init (the_map);
|
|
 |
7729eb |
dtv[GET_ADDR_MODULE].pointer = result;
|
|
 |
7729eb |
@@ -962,7 +962,7 @@ _dl_tls_get_addr_soft (struct link_map *l)
|
|
 |
7729eb |
return NULL;
|
|
 |
7729eb |
|
|
 |
7729eb |
dtv_t *dtv = THREAD_DTV ();
|
|
 |
7729eb |
- /* This may be called without holding the GL(dl_load_lock). Reading
|
|
 |
7729eb |
+ /* This may be called without holding the GL(dl_load_tls_lock). Reading
|
|
 |
7729eb |
arbitrary gen value is fine since this is best effort code. */
|
|
 |
7729eb |
size_t gen = atomic_load_relaxed (&GL(dl_tls_generation));
|
|
 |
7729eb |
if (__glibc_unlikely (dtv[0].counter != gen))
|
|
 |
7729eb |
diff --git a/elf/rtld.c b/elf/rtld.c
|
|
 |
7729eb |
index d733359eaf808b8a..08cf50145a1c01ce 100644
|
|
 |
7729eb |
--- a/elf/rtld.c
|
|
 |
7729eb |
+++ b/elf/rtld.c
|
|
 |
7729eb |
@@ -322,6 +322,7 @@ struct rtld_global _rtld_global =
|
|
 |
7729eb |
#ifdef _LIBC_REENTRANT
|
|
 |
7729eb |
._dl_load_lock = _RTLD_LOCK_RECURSIVE_INITIALIZER,
|
|
 |
7729eb |
._dl_load_write_lock = _RTLD_LOCK_RECURSIVE_INITIALIZER,
|
|
 |
7729eb |
+ ._dl_load_tls_lock = _RTLD_LOCK_RECURSIVE_INITIALIZER,
|
|
 |
7729eb |
#endif
|
|
 |
7729eb |
._dl_nns = 1,
|
|
 |
7729eb |
._dl_ns =
|
|
 |
7729eb |
diff --git a/posix/fork.c b/posix/fork.c
|
|
 |
7729eb |
index c471f7b15fe4290d..021691b9b7441f15 100644
|
|
 |
7729eb |
--- a/posix/fork.c
|
|
 |
7729eb |
+++ b/posix/fork.c
|
|
 |
7729eb |
@@ -99,6 +99,9 @@ __libc_fork (void)
|
|
 |
7729eb |
/* Reset the lock the dynamic loader uses to protect its data. */
|
|
 |
7729eb |
__rtld_lock_initialize (GL(dl_load_lock));
|
|
 |
7729eb |
|
|
 |
7729eb |
+ /* Reset the lock protecting dynamic TLS related data. */
|
|
 |
7729eb |
+ __rtld_lock_initialize (GL(dl_load_tls_lock));
|
|
 |
7729eb |
+
|
|
 |
7729eb |
reclaim_stacks ();
|
|
 |
7729eb |
|
|
 |
7729eb |
/* Run the handlers registered for the child. */
|
|
 |
7729eb |
diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
|
|
 |
7729eb |
index 9c15259236adab43..1ceb9c3212c148ba 100644
|
|
 |
7729eb |
--- a/sysdeps/generic/ldsodefs.h
|
|
 |
7729eb |
+++ b/sysdeps/generic/ldsodefs.h
|
|
 |
7729eb |
@@ -372,6 +372,13 @@ struct rtld_global
|
|
 |
7729eb |
list of loaded objects while an object is added to or removed
|
|
 |
7729eb |
from that list. */
|
|
 |
7729eb |
__rtld_lock_define_recursive (EXTERN, _dl_load_write_lock)
|
|
 |
7729eb |
+ /* This lock protects global and module specific TLS related data.
|
|
 |
7729eb |
+ E.g. it is held in dlopen and dlclose when GL(dl_tls_generation),
|
|
 |
7729eb |
+ GL(dl_tls_max_dtv_idx) or GL(dl_tls_dtv_slotinfo_list) are
|
|
 |
7729eb |
+ accessed and when TLS related relocations are processed for a
|
|
 |
7729eb |
+ module. It was introduced to keep pthread_create accessing TLS
|
|
 |
7729eb |
+ state that is being set up. */
|
|
 |
7729eb |
+ __rtld_lock_define_recursive (EXTERN, _dl_load_tls_lock)
|
|
 |
7729eb |
|
|
 |
7729eb |
/* Incremented whenever something may have been added to dl_loaded. */
|
|
 |
7729eb |
EXTERN unsigned long long _dl_load_adds;
|
|
 |
7729eb |
@@ -1261,7 +1268,7 @@ extern int _dl_scope_free (void *) attribute_hidden;
|
|
 |
7729eb |
|
|
 |
7729eb |
/* Add module to slot information data. If DO_ADD is false, only the
|
|
 |
7729eb |
required memory is allocated. Must be called with GL
|
|
 |
7729eb |
- (dl_load_lock) acquired. If the function has already been called
|
|
 |
7729eb |
+ (dl_load_tls_lock) acquired. If the function has already been called
|
|
 |
7729eb |
for the link map L with !do_add, then this function will not raise
|
|
 |
7729eb |
an exception, otherwise it is possible that it encounters a memory
|
|
 |
7729eb |
allocation failure. */
|
|
 |
7729eb |
diff --git a/sysdeps/pthread/Makefile b/sysdeps/pthread/Makefile
|
|
 |
7729eb |
index 0af9c59b425aefb1..df8943f4860a39d8 100644
|
|
 |
7729eb |
--- a/sysdeps/pthread/Makefile
|
|
 |
7729eb |
+++ b/sysdeps/pthread/Makefile
|
|
 |
7729eb |
@@ -152,15 +152,17 @@ tests += tst-cancelx2 tst-cancelx3 tst-cancelx6 tst-cancelx8 tst-cancelx9 \
|
|
 |
7729eb |
tst-cleanupx0 tst-cleanupx1 tst-cleanupx2 tst-cleanupx3
|
|
 |
7729eb |
|
|
 |
7729eb |
ifeq ($(build-shared),yes)
|
|
 |
7729eb |
-tests += tst-atfork2 tst-pt-tls4 tst-_res1 tst-fini1
|
|
 |
7729eb |
+tests += tst-atfork2 tst-pt-tls4 tst-_res1 tst-fini1 tst-create1
|
|
 |
7729eb |
tests-nolibpthread += tst-fini1
|
|
 |
7729eb |
endif
|
|
 |
7729eb |
|
|
 |
7729eb |
modules-names += tst-atfork2mod tst-tls4moda tst-tls4modb \
|
|
 |
7729eb |
- tst-_res1mod1 tst-_res1mod2 tst-fini1mod
|
|
 |
7729eb |
+ tst-_res1mod1 tst-_res1mod2 tst-fini1mod \
|
|
 |
7729eb |
+ tst-create1mod
|
|
 |
7729eb |
test-modules = $(addprefix $(objpfx),$(addsuffix .so,$(modules-names)))
|
|
 |
7729eb |
|
|
 |
7729eb |
tst-atfork2mod.so-no-z-defs = yes
|
|
 |
7729eb |
+tst-create1mod.so-no-z-defs = yes
|
|
 |
7729eb |
|
|
 |
7729eb |
ifeq ($(build-shared),yes)
|
|
 |
7729eb |
# Build all the modules even when not actually running test programs.
|
|
 |
7729eb |
@@ -279,4 +281,8 @@ LDFLAGS-tst-join7mod.so = -Wl,-soname,tst-join7mod.so
|
|
 |
7729eb |
|
|
 |
7729eb |
CFLAGS-tst-unwind-thread.c += -funwind-tables
|
|
 |
7729eb |
|
|
 |
7729eb |
+LDFLAGS-tst-create1 = -Wl,-export-dynamic
|
|
 |
7729eb |
+$(objpfx)tst-create1: $(shared-thread-library)
|
|
 |
7729eb |
+$(objpfx)tst-create1.out: $(objpfx)tst-create1mod.so
|
|
 |
7729eb |
+
|
|
 |
7729eb |
endif
|
|
 |
7729eb |
diff --git a/sysdeps/pthread/tst-create1.c b/sysdeps/pthread/tst-create1.c
|
|
 |
7729eb |
new file mode 100644
|
|
 |
7729eb |
index 0000000000000000..932586c30990d1d4
|
|
 |
7729eb |
--- /dev/null
|
|
 |
7729eb |
+++ b/sysdeps/pthread/tst-create1.c
|
|
 |
7729eb |
@@ -0,0 +1,119 @@
|
|
 |
7729eb |
+/* Verify that pthread_create does not deadlock when ctors take locks.
|
|
 |
7729eb |
+ Copyright (C) 2021 Free Software Foundation, Inc.
|
|
 |
7729eb |
+ This file is part of the GNU C Library.
|
|
 |
7729eb |
+
|
|
 |
7729eb |
+ The GNU C Library is free software; you can redistribute it and/or
|
|
 |
7729eb |
+ modify it under the terms of the GNU Lesser General Public
|
|
 |
7729eb |
+ License as published by the Free Software Foundation; either
|
|
 |
7729eb |
+ version 2.1 of the License, or (at your option) any later version.
|
|
 |
7729eb |
+
|
|
 |
7729eb |
+ The GNU C Library is distributed in the hope that it will be useful,
|
|
 |
7729eb |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
 |
7729eb |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
 |
7729eb |
+ Lesser General Public License for more details.
|
|
 |
7729eb |
+
|
|
 |
7729eb |
+ You should have received a copy of the GNU Lesser General Public
|
|
 |
7729eb |
+ License along with the GNU C Library; if not, see
|
|
 |
7729eb |
+ <https://www.gnu.org/licenses/>. */
|
|
 |
7729eb |
+
|
|
 |
7729eb |
+#include <stdio.h>
|
|
 |
7729eb |
+#include <support/xdlfcn.h>
|
|
 |
7729eb |
+#include <support/xthread.h>
|
|
 |
7729eb |
+
|
|
 |
7729eb |
+/*
|
|
 |
7729eb |
+Check if ctor and pthread_create deadlocks in
|
|
 |
7729eb |
+
|
|
 |
7729eb |
+thread 1: dlopen -> ctor -> lock(user_lock)
|
|
 |
7729eb |
+thread 2: lock(user_lock) -> pthread_create
|
|
 |
7729eb |
+
|
|
 |
7729eb |
+or in
|
|
 |
7729eb |
+
|
|
 |
7729eb |
+thread 1: dlclose -> dtor -> lock(user_lock)
|
|
 |
7729eb |
+thread 2: lock(user_lock) -> pthread_create
|
|
 |
7729eb |
+*/
|
|
 |
7729eb |
+
|
|
 |
7729eb |
+static pthread_barrier_t bar_ctor;
|
|
 |
7729eb |
+static pthread_barrier_t bar_dtor;
|
|
 |
7729eb |
+static pthread_mutex_t user_lock = PTHREAD_MUTEX_INITIALIZER;
|
|
 |
7729eb |
+
|
|
 |
7729eb |
+void
|
|
 |
7729eb |
+ctor (void)
|
|
 |
7729eb |
+{
|
|
 |
7729eb |
+ xpthread_barrier_wait (&bar_ctor);
|
|
 |
7729eb |
+ dprintf (1, "thread 1: in ctor: started.\n");
|
|
 |
7729eb |
+ xpthread_mutex_lock (&user_lock);
|
|
 |
7729eb |
+ dprintf (1, "thread 1: in ctor: locked user_lock.\n");
|
|
 |
7729eb |
+ xpthread_mutex_unlock (&user_lock);
|
|
 |
7729eb |
+ dprintf (1, "thread 1: in ctor: unlocked user_lock.\n");
|
|
 |
7729eb |
+ dprintf (1, "thread 1: in ctor: done.\n");
|
|
 |
7729eb |
+}
|
|
 |
7729eb |
+
|
|
 |
7729eb |
+void
|
|
 |
7729eb |
+dtor (void)
|
|
 |
7729eb |
+{
|
|
 |
7729eb |
+ xpthread_barrier_wait (&bar_dtor);
|
|
 |
7729eb |
+ dprintf (1, "thread 1: in dtor: started.\n");
|
|
 |
7729eb |
+ xpthread_mutex_lock (&user_lock);
|
|
 |
7729eb |
+ dprintf (1, "thread 1: in dtor: locked user_lock.\n");
|
|
 |
7729eb |
+ xpthread_mutex_unlock (&user_lock);
|
|
 |
7729eb |
+ dprintf (1, "thread 1: in dtor: unlocked user_lock.\n");
|
|
 |
7729eb |
+ dprintf (1, "thread 1: in dtor: done.\n");
|
|
 |
7729eb |
+}
|
|
 |
7729eb |
+
|
|
 |
7729eb |
+static void *
|
|
 |
7729eb |
+thread3 (void *a)
|
|
 |
7729eb |
+{
|
|
 |
7729eb |
+ dprintf (1, "thread 3: started.\n");
|
|
 |
7729eb |
+ dprintf (1, "thread 3: done.\n");
|
|
 |
7729eb |
+ return 0;
|
|
 |
7729eb |
+}
|
|
 |
7729eb |
+
|
|
 |
7729eb |
+static void *
|
|
 |
7729eb |
+thread2 (void *a)
|
|
 |
7729eb |
+{
|
|
 |
7729eb |
+ pthread_t t3;
|
|
 |
7729eb |
+ dprintf (1, "thread 2: started.\n");
|
|
 |
7729eb |
+
|
|
 |
7729eb |
+ xpthread_mutex_lock (&user_lock);
|
|
 |
7729eb |
+ dprintf (1, "thread 2: locked user_lock.\n");
|
|
 |
7729eb |
+ xpthread_barrier_wait (&bar_ctor);
|
|
 |
7729eb |
+ t3 = xpthread_create (0, thread3, 0);
|
|
 |
7729eb |
+ xpthread_mutex_unlock (&user_lock);
|
|
 |
7729eb |
+ dprintf (1, "thread 2: unlocked user_lock.\n");
|
|
 |
7729eb |
+ xpthread_join (t3);
|
|
 |
7729eb |
+
|
|
 |
7729eb |
+ xpthread_mutex_lock (&user_lock);
|
|
 |
7729eb |
+ dprintf (1, "thread 2: locked user_lock.\n");
|
|
 |
7729eb |
+ xpthread_barrier_wait (&bar_dtor);
|
|
 |
7729eb |
+ t3 = xpthread_create (0, thread3, 0);
|
|
 |
7729eb |
+ xpthread_mutex_unlock (&user_lock);
|
|
 |
7729eb |
+ dprintf (1, "thread 2: unlocked user_lock.\n");
|
|
 |
7729eb |
+ xpthread_join (t3);
|
|
 |
7729eb |
+
|
|
 |
7729eb |
+ dprintf (1, "thread 2: done.\n");
|
|
 |
7729eb |
+ return 0;
|
|
 |
7729eb |
+}
|
|
 |
7729eb |
+
|
|
 |
7729eb |
+static void
|
|
 |
7729eb |
+thread1 (void)
|
|
 |
7729eb |
+{
|
|
 |
7729eb |
+ dprintf (1, "thread 1: started.\n");
|
|
 |
7729eb |
+ xpthread_barrier_init (&bar_ctor, NULL, 2);
|
|
 |
7729eb |
+ xpthread_barrier_init (&bar_dtor, NULL, 2);
|
|
 |
7729eb |
+ pthread_t t2 = xpthread_create (0, thread2, 0);
|
|
 |
7729eb |
+ void *p = xdlopen ("tst-create1mod.so", RTLD_NOW | RTLD_GLOBAL);
|
|
 |
7729eb |
+ dprintf (1, "thread 1: dlopen done.\n");
|
|
 |
7729eb |
+ xdlclose (p);
|
|
 |
7729eb |
+ dprintf (1, "thread 1: dlclose done.\n");
|
|
 |
7729eb |
+ xpthread_join (t2);
|
|
 |
7729eb |
+ dprintf (1, "thread 1: done.\n");
|
|
 |
7729eb |
+}
|
|
 |
7729eb |
+
|
|
 |
7729eb |
+static int
|
|
 |
7729eb |
+do_test (void)
|
|
 |
7729eb |
+{
|
|
 |
7729eb |
+ thread1 ();
|
|
 |
7729eb |
+ return 0;
|
|
 |
7729eb |
+}
|
|
 |
7729eb |
+
|
|
 |
7729eb |
+#include <support/test-driver.c>
|
|
 |
7729eb |
diff --git a/sysdeps/pthread/tst-create1mod.c b/sysdeps/pthread/tst-create1mod.c
|
|
 |
7729eb |
new file mode 100644
|
|
 |
7729eb |
index 0000000000000000..62c9006961683177
|
|
 |
7729eb |
--- /dev/null
|
|
 |
7729eb |
+++ b/sysdeps/pthread/tst-create1mod.c
|
|
 |
7729eb |
@@ -0,0 +1,41 @@
|
|
 |
7729eb |
+/* Verify that pthread_create does not deadlock when ctors take locks.
|
|
 |
7729eb |
+ Copyright (C) 2021 Free Software Foundation, Inc.
|
|
 |
7729eb |
+ This file is part of the GNU C Library.
|
|
 |
7729eb |
+
|
|
 |
7729eb |
+ The GNU C Library is free software; you can redistribute it and/or
|
|
 |
7729eb |
+ modify it under the terms of the GNU Lesser General Public
|
|
 |
7729eb |
+ License as published by the Free Software Foundation; either
|
|
 |
7729eb |
+ version 2.1 of the License, or (at your option) any later version.
|
|
 |
7729eb |
+
|
|
 |
7729eb |
+ The GNU C Library is distributed in the hope that it will be useful,
|
|
 |
7729eb |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
 |
7729eb |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
 |
7729eb |
+ Lesser General Public License for more details.
|
|
 |
7729eb |
+
|
|
 |
7729eb |
+ You should have received a copy of the GNU Lesser General Public
|
|
 |
7729eb |
+ License along with the GNU C Library; if not, see
|
|
 |
7729eb |
+ <https://www.gnu.org/licenses/>. */
|
|
 |
7729eb |
+
|
|
 |
7729eb |
+#include <stdio.h>
|
|
 |
7729eb |
+
|
|
 |
7729eb |
+/* Require TLS setup for the module. */
|
|
 |
7729eb |
+__thread int tlsvar;
|
|
 |
7729eb |
+
|
|
 |
7729eb |
+void ctor (void);
|
|
 |
7729eb |
+void dtor (void);
|
|
 |
7729eb |
+
|
|
 |
7729eb |
+static void __attribute__ ((constructor))
|
|
 |
7729eb |
+do_init (void)
|
|
 |
7729eb |
+{
|
|
 |
7729eb |
+ dprintf (1, "constructor started: %d.\n", tlsvar++);
|
|
 |
7729eb |
+ ctor ();
|
|
 |
7729eb |
+ dprintf (1, "constructor done: %d.\n", tlsvar++);
|
|
 |
7729eb |
+}
|
|
 |
7729eb |
+
|
|
 |
7729eb |
+static void __attribute__ ((destructor))
|
|
 |
7729eb |
+do_end (void)
|
|
 |
7729eb |
+{
|
|
 |
7729eb |
+ dprintf (1, "destructor started: %d.\n", tlsvar++);
|
|
 |
7729eb |
+ dtor ();
|
|
 |
7729eb |
+ dprintf (1, "destructor done: %d.\n", tlsvar++);
|
|
 |
7729eb |
+}
|