mirror of
git://sourceware.org/git/glibc.git
synced 2025-01-06 12:00:24 +08:00
83b5323261
The fix for bug 19329 caused a regression such that pthread_create can deadlock when concurrent ctors from dlopen are waiting for it to finish. Use a new GL(dl_load_tls_lock) in pthread_create that is not taken around ctors in dlopen. The new lock is also used in __tls_get_addr instead of GL(dl_load_lock). The new lock is held in _dl_open_worker and _dl_close_worker around most of the logic before/after the init/fini routines. When init/fini routines are running then TLS is in a consistent, usable state. In _dl_open_worker the new lock requires catching and reraising dlopen failures that happen in the critical section. The new lock is reinitialized in a fork child, to keep the existing behaviour and it is kept recursive in case malloc interposition or TLS access from signal handlers can retake it. It is not obvious if this is necessary or helps, but avoids changing the preexisting behaviour. The new lock may be more appropriate for dl_iterate_phdr too than GL(dl_load_write_lock), since TLS state of an incompletely loaded module may be accessed. If the new lock can replace the old one, that can be a separate change. Fixes bug 28357. Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
120 lines
3.3 KiB
C
120 lines
3.3 KiB
C
/* Verify that pthread_create does not deadlock when ctors take locks.
|
|
Copyright (C) 2021 Free Software Foundation, Inc.
|
|
This file is part of the GNU C Library.
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
|
modify it under the terms of the GNU Lesser General Public
|
|
License as published by the Free Software Foundation; either
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
License along with the GNU C Library; if not, see
|
|
<https://www.gnu.org/licenses/>. */
|
|
|
|
#include <stdio.h>
|
|
#include <support/xdlfcn.h>
|
|
#include <support/xthread.h>
|
|
|
|
/*
|
|
Check if ctor and pthread_create deadlocks in
|
|
|
|
thread 1: dlopen -> ctor -> lock(user_lock)
|
|
thread 2: lock(user_lock) -> pthread_create
|
|
|
|
or in
|
|
|
|
thread 1: dlclose -> dtor -> lock(user_lock)
|
|
thread 2: lock(user_lock) -> pthread_create
|
|
*/
|
|
|
|
static pthread_barrier_t bar_ctor;
|
|
static pthread_barrier_t bar_dtor;
|
|
static pthread_mutex_t user_lock = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
void
|
|
ctor (void)
|
|
{
|
|
xpthread_barrier_wait (&bar_ctor);
|
|
dprintf (1, "thread 1: in ctor: started.\n");
|
|
xpthread_mutex_lock (&user_lock);
|
|
dprintf (1, "thread 1: in ctor: locked user_lock.\n");
|
|
xpthread_mutex_unlock (&user_lock);
|
|
dprintf (1, "thread 1: in ctor: unlocked user_lock.\n");
|
|
dprintf (1, "thread 1: in ctor: done.\n");
|
|
}
|
|
|
|
void
|
|
dtor (void)
|
|
{
|
|
xpthread_barrier_wait (&bar_dtor);
|
|
dprintf (1, "thread 1: in dtor: started.\n");
|
|
xpthread_mutex_lock (&user_lock);
|
|
dprintf (1, "thread 1: in dtor: locked user_lock.\n");
|
|
xpthread_mutex_unlock (&user_lock);
|
|
dprintf (1, "thread 1: in dtor: unlocked user_lock.\n");
|
|
dprintf (1, "thread 1: in dtor: done.\n");
|
|
}
|
|
|
|
static void *
|
|
thread3 (void *a)
|
|
{
|
|
dprintf (1, "thread 3: started.\n");
|
|
dprintf (1, "thread 3: done.\n");
|
|
return 0;
|
|
}
|
|
|
|
static void *
|
|
thread2 (void *a)
|
|
{
|
|
pthread_t t3;
|
|
dprintf (1, "thread 2: started.\n");
|
|
|
|
xpthread_mutex_lock (&user_lock);
|
|
dprintf (1, "thread 2: locked user_lock.\n");
|
|
xpthread_barrier_wait (&bar_ctor);
|
|
t3 = xpthread_create (0, thread3, 0);
|
|
xpthread_mutex_unlock (&user_lock);
|
|
dprintf (1, "thread 2: unlocked user_lock.\n");
|
|
xpthread_join (t3);
|
|
|
|
xpthread_mutex_lock (&user_lock);
|
|
dprintf (1, "thread 2: locked user_lock.\n");
|
|
xpthread_barrier_wait (&bar_dtor);
|
|
t3 = xpthread_create (0, thread3, 0);
|
|
xpthread_mutex_unlock (&user_lock);
|
|
dprintf (1, "thread 2: unlocked user_lock.\n");
|
|
xpthread_join (t3);
|
|
|
|
dprintf (1, "thread 2: done.\n");
|
|
return 0;
|
|
}
|
|
|
|
static void
|
|
thread1 (void)
|
|
{
|
|
dprintf (1, "thread 1: started.\n");
|
|
xpthread_barrier_init (&bar_ctor, NULL, 2);
|
|
xpthread_barrier_init (&bar_dtor, NULL, 2);
|
|
pthread_t t2 = xpthread_create (0, thread2, 0);
|
|
void *p = xdlopen ("tst-create1mod.so", RTLD_NOW | RTLD_GLOBAL);
|
|
dprintf (1, "thread 1: dlopen done.\n");
|
|
xdlclose (p);
|
|
dprintf (1, "thread 1: dlclose done.\n");
|
|
xpthread_join (t2);
|
|
dprintf (1, "thread 1: done.\n");
|
|
}
|
|
|
|
static int
|
|
do_test (void)
|
|
{
|
|
thread1 ();
|
|
return 0;
|
|
}
|
|
|
|
#include <support/test-driver.c>
|