From nobody Tue Jul 16 20:43:51 2024 X-Original-To: threads@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4WNrdN1wQcz5QCss for ; Tue, 16 Jul 2024 20:43:52 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4WNrdM5fdFz4G6W for ; Tue, 16 Jul 2024 20:43:51 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1721162631; a=rsa-sha256; cv=none; b=KaEk9RTJy5Z87d2EhZUtbrRKkN0VsUZNJ8QlryNyUFY0xfuF6tyYwmje9ySUFLJaH/xuPB hHt0y/mOPdkr+gUP+RlmA6CNHDt4MaXihaaIrYoU5VkQzYGdniTeHYoKbOlhtH7rcoCoAe 0Mjz2on8gokh1mm/6G5CCkjbvNC5WiHYJWO6UGaiJLuMfCgPUKnp4N9cx1AK9hx+ykiMrS hNYZL7dx6jPZQPu/zIDd5vF/Rqecd7A+wEUZlyAS9afU2Cx/4t9VzdyeE9Nkuh3NBZaS4G Jyot1PVGRp5P09B/QhHI4i0fmyX00CcZ3Mm29e57PIl6HUWP8f1QHL1Rk1d7aw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1721162631; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=IPKy0IxES8S+mKAZBS2bk92NDmoxQKrl48RdaNBeImI=; b=tmTVLehLBzr+RxtfeBxytv4S7J7+9w1vmZAYWnfLhKkJwU2AScAlu6WCa6s3J8w6Sqn/eG Vq0Lt5ZL57ge0ROpdRgKwrJqhDsoZqRe6oMqgiYzdUxZYuuTslQ21fmSHhhdn3aHHWYR8S bz5dHBXD1XdyJtKhhNQjCjJVEI2Cb8F4kQpGzfOWhR+ytU//c2JYFBDf+rhq0H7x3HCIiY a/fVu1gwIhyUj4I28zKnPiTvVtCd9c1LbMrFkC0PAlo4Dqxv7pWpv/cDuoA+4At5BwLGrB AQqxWr6e/SkoPp0ylvX3dofzThnl/nEZgqFLnpq0NsC3k6Q733WXlQFz7yaenA== Received: from kenobi.freebsd.org (kenobi.freebsd.org [IPv6:2610:1c1:1:606c::50:1d]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4WNrdM5Gw7zqTT for ; Tue, 16 Jul 2024 20:43:51 +0000 (UTC) (envelope-from bugzilla-noreply@freebsd.org) Received: from kenobi.freebsd.org ([127.0.1.5]) by kenobi.freebsd.org (8.15.2/8.15.2) with ESMTP id 46GKhp7l045667 for ; Tue, 16 Jul 2024 20:43:51 GMT (envelope-from bugzilla-noreply@freebsd.org) Received: (from www@localhost) by kenobi.freebsd.org (8.15.2/8.15.2/Submit) id 46GKhpBQ045666 for threads@FreeBSD.org; Tue, 16 Jul 2024 20:43:51 GMT (envelope-from bugzilla-noreply@freebsd.org) X-Authentication-Warning: kenobi.freebsd.org: www set sender to bugzilla-noreply@freebsd.org using -f From: bugzilla-noreply@freebsd.org To: threads@FreeBSD.org Subject: [Bug 280318] fork() can deadlock on rtld_phdr_lock Date: Tue, 16 Jul 2024 20:43:51 +0000 X-Bugzilla-Reason: AssignedTo X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: Base System X-Bugzilla-Component: threads X-Bugzilla-Version: 14.1-RELEASE X-Bugzilla-Keywords: X-Bugzilla-Severity: Affects Only Me X-Bugzilla-Who: tavianator@tavianator.com X-Bugzilla-Status: New X-Bugzilla-Resolution: X-Bugzilla-Priority: --- X-Bugzilla-Assigned-To: threads@FreeBSD.org X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version rep_platform op_sys bug_status bug_severity priority component assigned_to reporter Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: https://bugs.freebsd.org/bugzilla/ Auto-Submitted: auto-generated List-Id: Threading List-Archive: https://lists.freebsd.org/archives/freebsd-threads List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: freebsd-threads@freebsd.org Sender: owner-freebsd-threads@FreeBSD.org MIME-Version: 1.0 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: threads@FreeBSD.org Reporter: tavianator@tavianator.com 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, tsp=3D) 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 , lockstate=3D) 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) 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) at /usr/src/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_al= locator_combined.h:132 #6 __asan::Allocator::GetAsanChunkByAddr (this=3D, 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, 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, size=3D, align=3D) 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) 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 #include #include #include 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.=