[Bug 280318] fork() can deadlock on rtld_phdr_lock
[email protected] Tue, 16 Jul 2024 20:43:51 +0000
| Newsgroups | gmane.os.freebsd.devel.threading |
|---|---|
| Message-ID | <[email protected]/bugzilla/> |
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=3D280318
Bug ID: 280318
Summary: fork() can deadlock on rtld_phdr_lock
Product: Base System
Version: 14.1-RELEASE
Hardware: Any
OS: Any
Status: New
Severity: Affects Only Me
Priority: ---
Component: threads
Assignee: [email protected]
Reporter: [email protected]
I ran into a deadlock when calling fork() in a multi-threaded app with ASAN
enabled. The backtrace looks like this:
(gdb) bt
#0 _umtx_op_err () at /usr/src/lib/libthr/arch/amd64/amd64/_umtx_op_err.S:=
38
#1 0x000000080054af50 in __thr_rwlock_wrlock (rwlock=3Drwlock@entry=3D0x80=
054f640,
tsp=3Dtsp@entry=3D0x0) at /usr/src/lib/libthr/thread/thr_umtx.c:324
#2 0x0000000800545571 in _thr_rwlock_wrlock (rwlock=3D<optimized out>,
tsp=3D<optimized out>) at /usr/src/lib/libthr/thread/thr_umtx.h:239
#3 _thr_rtld_wlock_acquire (lock=3D0x80054f640) at
/usr/src/lib/libthr/thread/thr_rtld.c:139
#4 0x000000080045ee49 in wlock_acquire (lock=3D0x80046bae0 <rtld_locks+32>,
lockstate=3D<optimized out>) at /usr/src/libexec/rtld-elf/rtld_lock.c:275
#5 _rtld_atfork_pre (locks=3Dlocks@entry=3D0x7fffffffb290) at
/usr/src/libexec/rtld-elf/rtld_lock.c:475
#6 0x000000080053e716 in thr_fork_impl (a=3D0x7fffffffb2f8) at
/usr/src/lib/libthr/thread/thr_fork.c:194
#7 0x000000080053e658 in __thr_fork () at
/usr/src/lib/libthr/thread/thr_fork.c:315
...
(gdb) thread 6
[Switching to thread 6 (LWP 154312 of process 84643)]
(gdb) bt
#0 __syscall () at __syscall.S:4
#1 0x0000000000311995 in __sanitizer::StaticSpinMutex::LockSlow (this=3D0x=
4af5b0
<__asan::instance+680>)
at
/usr/src/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_mu=
tex.cpp:24
#2 0x00000000002829b2 in __sanitizer::StaticSpinMutex::Lock (this=3D0x4af5=
b0
<__asan::instance+680>) at
/usr/src/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_mu=
tex.h:32
#3=20
__sanitizer::GenericScopedLock<__sanitizer::StaticSpinMutex>::GenericScoped=
Lock
(mu=3D0x4af5b0 <__asan::instance+680>, this=3D<optimized out>)
at
/usr/src/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_mu=
tex.h:383
#4 __sanitizer::LargeMmapAllocator<__asan::AsanMapUnmapCallback,
__sanitizer::LargeMmapAllocatorPtrArrayDynamic,
__sanitizer::LocalAddressSpaceView>::GetBlockBegin (
this=3D0x4af348 <__asan::instance+64>, ptr=3D0x802e4d120) at
/usr/src/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_al=
locator_secondary.h:184
#5 0x0000000000281666 in
__sanitizer::CombinedAllocator<__sanitizer::SizeClassAllocator64<__asan::AP=
64<__sanitizer::LocalAddressSpaceView>
>, __sanitizer::LargeMmapAllocatorPtrArrayDynamic>::GetBlockBegin
(p=3D0x802e4d120, this=3D<optimized out>) at
/usr/src/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_al=
locator_combined.h:132
#6 __asan::Allocator::GetAsanChunkByAddr (this=3D<optimized out>, p=3D3440=
8288544)
at /usr/src/contrib/llvm-project/compiler-rt/lib/asan/asan_allocator.cpp:824
#7 0x000000000027fbe3 in AllocationBegin (p=3D0x802e4d120) at
/usr/src/contrib/llvm-project/compiler-rt/lib/asan/asan_allocator.cpp:1215
#8 __sanitizer_get_allocated_begin (p=3D0x802e4d120) at
/usr/src/contrib/llvm-project/compiler-rt/lib/asan/asan_allocator.cpp:1256
#9 0x0000000000323f3f in __sanitizer::DTLS_on_tls_get_addr
(arg_void=3Darg_void@entry=3D0x7fffdf7f89d0, res=3Dres@entry=3D0x802e4d120,
static_tls_begin=3D0, static_tls_end=3D0)
at
/usr/src/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_tl=
s_get_addr.cpp:138
#10 0x00000000002beb19 in ___interceptor___tls_get_addr (arg=3D0x7fffdf7f89=
d0) at
/usr/src/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_co=
mmon_interceptors.inc:5426
#11 0x000000000031146c in __sanitizer::CollectStaticTlsBlocks
(info=3D0x7fffdf7f8da8, size=3D<optimized out>, data=3D0x6)
at
/usr/src/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_li=
nux_libcdep.cpp:399
#12 0x0000000800458f4d in dl_iterate_phdr (callback=3D0x311430
<__sanitizer::CollectStaticTlsBlocks(dl_phdr_info*, unsigned long, void*)>,
param=3D0x7fffdf7f8e38)
at /usr/src/libexec/rtld-elf/rtld.c:4246
#13 0x00000000003107b2 in __sanitizer::GetStaticTlsBoundary (addr=3D<optimi=
zed
out>, size=3D<optimized out>, align=3D<optimized out>)
at
/usr/src/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_li=
nux_libcdep.cpp:415
#14 __sanitizer::GetTls (addr=3Daddr@entry=3D0x802e8a030,
size=3Dsize@entry=3D0x7fffdf7f8ef0) at
/usr/src/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_li=
nux_libcdep.cpp:526
#15 0x0000000000310b8b in __sanitizer::GetThreadStackAndTls (main=3Dfalse,
stk_addr=3Dstk_addr@entry=3D0x802e8a010, stk_size=3D0x6,
stk_size@entry=3D0x7fffdf7f8ef8,=20
tls_addr=3D0x800701f2a <__syscall+10>, tls_addr@entry=3D0x802e8a030,
tls_size=3D0x0, tls_size@entry=3D0x7fffdf7f8ef0)
at
/usr/src/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_li=
nux_libcdep.cpp:610
#16 0x0000000000301ebe in __asan::AsanThread::SetThreadStackAndTls
(this=3Dthis@entry=3D0x802e8a000, options=3D<optimized out>)
at /usr/src/contrib/llvm-project/compiler-rt/lib/asan/asan_thread.cpp:3=
06
#17 0x0000000000301b5f in __asan::AsanThread::Init (this=3D0x802e8a000,
options=3Doptions@entry=3D0x0) at
/usr/src/contrib/llvm-project/compiler-rt/lib/asan/asan_thread.cpp:253
#18 0x0000000000301fb7 in __asan::AsanThread::ThreadStart (this=3D0x14b,
os_id=3D154312) at
/usr/src/contrib/llvm-project/compiler-rt/lib/asan/asan_thread.cpp:283
#19 0x00000000002f38b7 in asan_thread_start (arg=3D0x802e8a000) at
/usr/src/contrib/llvm-project/compiler-rt/lib/asan/asan_interceptors.cpp:230
#20 0x000000080053db05 in thread_start (curthread=3D0x51b000001c80) at
/usr/src/lib/libthr/thread/thr_create.c:289
#21 0x0000000000000000 in ?? ()
THread 1 is calling fork() which calls _rtld_atfork_pre() which acquires
rtld_phdr_lock. Simultaneously, thread 6 is in the middle of dl_iterate_ph=
dr()
with rtld_phdr_lock held outside the loop. But the callback is apparently
waiting for thread 1 to do something, which will never happen.
It can be reproduced without ASAN too. Here's a somewhat artificial
reproducer:
$ cat foo.c
#include <link.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
pthread_barrier_t in_callback, done_fork;
int callback(struct dl_phdr_info *info, size_t size, void *data) {
static int waited =3D 0;
if (!waited) {
pthread_barrier_wait(&in_callback);
pthread_barrier_wait(&done_fork);
waited =3D 1;
}
return 0;
}
void *start_routine(void *arg) {
dl_iterate_phdr(callback, NULL);
return NULL;
}
int main(void) {
pthread_barrier_init(&in_callback, NULL, 2);
pthread_barrier_init(&done_fork, NULL, 2);
/* Create a thread to call dl_iterate_phdr() */
pthread_t thread;
pthread_create(&thread, NULL, start_routine, NULL);
/* Wait for the dl_iterate_phdr() callback to start */
pthread_barrier_wait(&in_callback);
/* fork() will hang in _rtld_atfork_pre() */
pid_t pid =3D fork();
if (pid =3D=3D 0) {
return 0;
}
pthread_barrier_wait(&done_fork);
pthread_join(thread, NULL);
return 0;
}
$ cc -pthread foo.c -o foo
$ ./foo
That will hang until you kill the process.
--=20
You are receiving this mail because:
You are the assignee for the bug.=