From nobody Fri Nov 22 04:55:25 2024 X-Original-To: dev-commits-src-all@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 4XvjTV0q0kz5dLq2; Fri, 22 Nov 2024 04:55:26 +0000 (UTC) (envelope-from git@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 "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4XvjTT6PNcz3xCk; Fri, 22 Nov 2024 04:55:25 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1732251325; 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=ipqirr36YpVFfsK2gnDrYDuCuHMjmM89NeNCpMIzRSc=; b=UzXJ7ov30mu9O8SkyVy+edF5vetFnl5Yt5V7rC1+EQMWtHEx6OxWoXEW+nnvjP2l1WBuev 5XtlmkA5jQJRmNWyjedkIbtcfglVw1SgwhbPaPgAg1mKIrLKDdYrktzN4WM8HUGKp9UG2p j2TS9mgLk84jYn4rXFQsIo+ytR6lhkgES0beqBBz089bstsG3AoBr0oRikAhwl9VXbmfL0 uEH09F1f4CyxyyoVTaU2oAm+XEeLGIQUjTgVLG1fpnjHzQPe97bY3IWPDGYUoDNjtXXe/U umoKfcUY+Qih2NVgsRWmjvk5GpBC2HSD4ckSl5AqmDfhoeTi04wgTMO5jld8bg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1732251325; 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=ipqirr36YpVFfsK2gnDrYDuCuHMjmM89NeNCpMIzRSc=; b=flc522COV/sN9fkepRly89FQz5MnVMRO79ltdFcOEoO1FHZPx6h652CShz+pKGGGeR0JmM 9z2iba8RGZPFhLHhWSzaPaS/w6/48OMPhqwBnf/+kIgtKFGw1kPIJtr02vv+N1QDt8ey4y o8/6zci/IL1dN8RqAqvxdiwFbi2r7VnE+O1Sa7doyBjI4glaxIksCTlhEvv+WiGd7CwB8B 4rBA1CFpJ8OBwys0Wl5xE+I+DKD/hQNU/3LJy+iVGiRtnTV2KYlJswkqsfov+juSczXZsU /gS02ZcV+qFB1a+yAxjAuShnNasZeQ5OsrcJjWD8M7UGwcUUqalYjAj3utw6Pw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1732251325; a=rsa-sha256; cv=none; b=g6puHfV7lvkOBRivD1bNHq+VmcfzWym5Ct0pUxlATEjd8lRhgVWaFm6gs/aWU2VOiAinGj dMTFh2GRfI8uEzk49MDlVL7w/GJ+emiiUusw6LixI3EEKQYvRDegIqRb/5JMWEbf1yMzLP MWnSJfXc1k6yo7bU/7fCljyCBegYbt0H92gBrBsYLJAw35Hp8aiEzXbrEd2FR5dxKlMXTz p+VeYfSr0T/DfS8yikPdVn8BFnQsTflDtxBxGh2IUP1NeceNnNNjwObCRCWRVF1AUOMQaG qN5jxi8xylkKsqg9VjbvLb+U5UO5AOUnbXIC4NpVx0a5dh65PbuDHuofVjAhDA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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 4XvjTT52J5z19Hd; Fri, 22 Nov 2024 04:55:25 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 4AM4tP64035817; Fri, 22 Nov 2024 04:55:25 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 4AM4tPqg035814; Fri, 22 Nov 2024 04:55:25 GMT (envelope-from git) Date: Fri, 22 Nov 2024 04:55:25 GMT Message-Id: <202411220455.4AM4tPqg035814@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Kyle Evans Subject: git: c5a7b5882ae2 - stable/14 - libthr: allow very early atfork registration List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kevans X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: c5a7b5882ae28932f79c29678fae36498467e8ea Auto-Submitted: auto-generated The branch stable/14 has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=c5a7b5882ae28932f79c29678fae36498467e8ea commit c5a7b5882ae28932f79c29678fae36498467e8ea Author: Kyle Evans AuthorDate: 2024-11-14 01:33:44 +0000 Commit: Kyle Evans CommitDate: 2024-11-22 04:54:55 +0000 libthr: allow very early atfork registration LSan wants to be able to register atfork handlers at __lsan_init time, which can happen either at the first intercepted function call or in a .preinit_array function. Both of these end up being very early in rtld and executed with the bind lock held, which ends up causing problems when we go to _libpthread_init(). Instead of requiring libpthread to be initialized, just insert the new atfork handler straight into the list if it's not ready yet. The critical section and locking should not be necessary if we're really executing this early, as there won't be any threads to contend with. Reviewed by: kib (earlier version), markj (cherry picked from commit 4b202f4faf40fd7af8b84491360186aed8ce5733) --- lib/libthr/tests/atfork_test.c | 53 ++++++++++++++++++++++++++++++++++++++++++ lib/libthr/thread/thr_fork.c | 20 +++++++++------- lib/libthr/thread/thr_init.c | 1 - 3 files changed, 65 insertions(+), 9 deletions(-) diff --git a/lib/libthr/tests/atfork_test.c b/lib/libthr/tests/atfork_test.c index 5133330b1247..cb0fcb7e62db 100644 --- a/lib/libthr/tests/atfork_test.c +++ b/lib/libthr/tests/atfork_test.c @@ -25,6 +25,58 @@ static int child; static int forked; static int parent; +/* + * We'll disable prefork unless we're specifically running the preinit test to + * be sure that we don't mess up any other tests' results. + */ +static bool prefork_enabled; + +static void +prefork(void) +{ + if (prefork_enabled) + forked++; +} + +static void +registrar(void) +{ + pthread_atfork(prefork, NULL, NULL); +} + +static __attribute__((section(".preinit_array"), used)) +void (*preinitfn)(void) = ®istrar; + +/* + * preinit_atfork() just enables the prepare handler that we registered in a + * .preinit_array entry and checks that forking actually invoked that callback. + * We don't bother testing all three callbacks here because the implementation + * doesn't really lend itself to the kind of error where we only have a partial + * set of callbacks registered. + */ +ATF_TC(preinit_atfork); +ATF_TC_HEAD(preinit_atfork, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Checks that atfork callbacks may be registered in .preinit_array functions"); +} +ATF_TC_BODY(preinit_atfork, tc) +{ + pid_t p; + + (void)signal(SIGCHLD, SIG_IGN); + prefork_enabled = true; + p = fork(); + + ATF_REQUIRE(p >= 0); + if (p == 0) + _exit(0); + + prefork_enabled = false; + + ATF_REQUIRE(forked != 0); +} + static void basic_prepare(void) { @@ -221,6 +273,7 @@ ATF_TC_BODY(multi_atfork, tc) ATF_TP_ADD_TCS(tp) { + ATF_TP_ADD_TC(tp, preinit_atfork); ATF_TP_ADD_TC(tp, basic_atfork); ATF_TP_ADD_TC(tp, multi_atfork); return (atf_no_error()); diff --git a/lib/libthr/thread/thr_fork.c b/lib/libthr/thread/thr_fork.c index 614841cc314f..aa54b0444cf4 100644 --- a/lib/libthr/thread/thr_fork.c +++ b/lib/libthr/thread/thr_fork.c @@ -84,20 +84,24 @@ _thr_atfork(void (*prepare)(void), void (*parent)(void), struct pthread *curthread; struct pthread_atfork *af; - _thr_check_init(); - if ((af = malloc(sizeof(struct pthread_atfork))) == NULL) return (ENOMEM); - curthread = _get_curthread(); af->prepare = prepare; af->parent = parent; af->child = child; - THR_CRITICAL_ENTER(curthread); - _thr_rwl_wrlock(&_thr_atfork_lock); - TAILQ_INSERT_TAIL(&_thr_atfork_list, af, qe); - _thr_rwl_unlock(&_thr_atfork_lock); - THR_CRITICAL_LEAVE(curthread); + + if (_thr_initial != NULL) { + curthread = _get_curthread(); + THR_CRITICAL_ENTER(curthread); + _thr_rwl_wrlock(&_thr_atfork_lock); + TAILQ_INSERT_TAIL(&_thr_atfork_list, af, qe); + _thr_rwl_unlock(&_thr_atfork_lock); + THR_CRITICAL_LEAVE(curthread); + } else { + TAILQ_INSERT_TAIL(&_thr_atfork_list, af, qe); + } + return (0); } diff --git a/lib/libthr/thread/thr_init.c b/lib/libthr/thread/thr_init.c index 007cf5be81c9..ff59288d919e 100644 --- a/lib/libthr/thread/thr_init.c +++ b/lib/libthr/thread/thr_init.c @@ -519,7 +519,6 @@ init_private(void) env = getenv("LIBPTHREAD_QUEUE_FIFO"); if (env) _thr_queuefifo = atoi(env); - TAILQ_INIT(&_thr_atfork_list); env = getenv("LIBPTHREAD_UMTX_MIN_TIMEOUT"); if (env) { char *endptr;