From nobody Wed Jul 02 18:28:28 2025 X-Original-To: dev-commits-src-branches@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 4bXT183hKcz60m5h; Wed, 02 Jul 2025 18:28:28 +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 4bXT182SNQz3JB2; Wed, 02 Jul 2025 18:28:28 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1751480908; 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=5Q76oUPL2p+DJzTWDJegEx3wkhZHjzQKoG+8DS2h6Jw=; b=HxWF7S9Or+9A5QP/Y3cG+7Zr4OJtLZGWx1+yFAIQSpsFoinev6oDjs6zbNqkBDk2qqesiL ANLQU3MkBfjm/HhUJuDcEPpxT16me0wQ3e/MfNvJya+GWaPf6//AfEwOCP7XHJiXdwx23m UnBFcB4vKDht0CzqCgAQQOMy4V8G9hXGiM3JwnHSzLGcb3kbQfhUW5WUfhdnMJ6XMyEpX8 FBWKb4GF7N/B+q9dZ+jRBKsSEivnBzZOHJdWKyYMz+3lXBzVZz06WW9sV5LNK3BPSghwqw 79Cg7Lx7KXUIJkvKp9p5Bc5tXE+7WX2uD8zQi70fDs2gG7tjzHfyW+pbXiqBmg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1751480908; 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=5Q76oUPL2p+DJzTWDJegEx3wkhZHjzQKoG+8DS2h6Jw=; b=AFcq6wgzHERuugbs+KchaUhi6eVI2E7CBiEcOLFeYZ/QFZPV5mcLSbvXc7clDLJGJX75dP linSdE0xCJBKuJkK3w51e81cSTG7wYgCGLogNM0eNIAyiv+sN3JMxuToJMZaGdsMnGLN24 NenObFV84oRbUj+m/uMxGEW8ehpmzkzlP3aHLWp8YJd5MzzHfWR6NJt9mcXnnawTqF1NnY GLNJ+vzPFLCED3Dt/GKNLLYhxhn+HDy+64be4HQpfZyWlet0Oud/+dLd8Qpfrk61mgHVoB clhhw0ERSGOzCqmIHGZyrgVohidiVs0xaZCs2SQveCSUXILPHawNGSwguAEKBg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1751480908; a=rsa-sha256; cv=none; b=WMG23X60KWVFr8J/AM7tYsTMg58vTSyhzdNC/U4kpHENo/3LJ7fU2FXmlEUxmbd8dvYwRe xGtsd9UAz1tKYw/lCFvxyChWbHHmunQy1GFq0a2l67kT5FPfu7ACNK+W+dW80aaOljfIfw BnsMcmphWE47SrjgqVaId0vF2me0lEwHZds8kqRacL9nscypq1NAP8CRR0VwSBTRxmifQE Yj5q2B3tVLcSCxs+4w518NAmGLC1c+Faaszaou3Apfp6GMNmR979Z+1w9Oy/G6XZOqF5CG 2NDsQD39AL96wMB/FhqWoUXEAq17GHJpxuanRFrUqRk2UXeu/PwLQzOOO0N6xg== 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 4bXT181g9fzVpN; Wed, 02 Jul 2025 18:28:28 +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 562ISSWt057716; Wed, 2 Jul 2025 18:28:28 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 562ISSgA057713; Wed, 2 Jul 2025 18:28:28 GMT (envelope-from git) Date: Wed, 2 Jul 2025 18:28:28 GMT Message-Id: <202507021828.562ISSgA057713@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Gordon Tetlow Subject: git: f936833911d7 - releng/13.5 - libc: allow __cxa_atexit handlers to be added during __cxa_finalize List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-branches@freebsd.org Sender: owner-dev-commits-src-branches@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: gordon X-Git-Repository: src X-Git-Refname: refs/heads/releng/13.5 X-Git-Reftype: branch X-Git-Commit: f936833911d765c1dc9640913b73acfa1d751742 Auto-Submitted: auto-generated The branch releng/13.5 has been updated by gordon: URL: https://cgit.FreeBSD.org/src/commit/?id=f936833911d765c1dc9640913b73acfa1d751742 commit f936833911d765c1dc9640913b73acfa1d751742 Author: Aurélien Croc de Suray AuthorDate: 2025-04-05 00:47:53 +0000 Commit: Gordon Tetlow CommitDate: 2025-07-02 05:42:46 +0000 libc: allow __cxa_atexit handlers to be added during __cxa_finalize science/dlib-cpp reveals an interesting scenario that works fine on other platforms but not on FreeBSD; notably, it ends up creating a new global object from some destructor which is called during __cxa_finalize. This breaks when libdlib is dlopen()ed and then subsequently dlclose()ed, as we never end up invoking the created object's dtor until program exit when the shlib is already unmapped. Fix it by noting when we're in the middle of __cxa_finalize for a dso, and then restarting the search if __cxa_atexit() was called in the middle somewhere. We wait until we've processed the initial set before starting over and processing the newly added handlers as if it were a complete set of handlers added during runtime. The alternative is calling them as they're added to maintain a LIFO in terms of total ordering, but in theory a constructor could add another global object that also needs to be destroyed, and that object needs to be destroyed after the one that constructed it to avoid creating unexpected lifetime issues. This manifests in the pdlib PHP extension for dlib crashing, see [0]. [0] https://github.com/goodspb/pdlib/issues/39 PR: 285870 Reviewed by: kevans (also supplied commit message) Approved by: so Security: FreeBSD-EN-25:09.libc (cherry picked from commit 23427c8e1fedb9fc68ad0bd27a59c7ffd2b3008c) (cherry picked from commit 04f7496f89e28057079f3f0b1a02d7d9d874487f) --- lib/libc/stdlib/atexit.c | 61 ++++++++++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 25 deletions(-) diff --git a/lib/libc/stdlib/atexit.c b/lib/libc/stdlib/atexit.c index 6b63d9e728c4..5b39552821ed 100644 --- a/lib/libc/stdlib/atexit.c +++ b/lib/libc/stdlib/atexit.c @@ -39,6 +39,7 @@ static char sccsid[] = "@(#)atexit.c 8.2 (Berkeley) 7/3/94"; #include "namespace.h" #include #include +#include #include #include #include @@ -60,6 +61,8 @@ _Block_copy(void*); #define ATEXIT_FN_CXA 2 static pthread_mutex_t atexit_mutex = PTHREAD_MUTEX_INITIALIZER; +static void *current_finalize_dso = NULL; +static bool call_finalize_again = false; #define _MUTEX_LOCK(x) if (__isthreaded) _pthread_mutex_lock(x) #define _MUTEX_UNLOCK(x) if (__isthreaded) _pthread_mutex_unlock(x) @@ -119,6 +122,9 @@ atexit_register(struct atexit_fn *fptr) __atexit = p; } p->fns[p->ind++] = *fptr; + if (current_finalize_dso != NULL && + current_finalize_dso == fptr->fn_dso) + call_finalize_again = true; _MUTEX_UNLOCK(&atexit_mutex); return 0; } @@ -212,33 +218,38 @@ __cxa_finalize(void *dso) } _MUTEX_LOCK(&atexit_mutex); - for (p = __atexit; p; p = p->next) { - for (n = p->ind; --n >= 0;) { - if (p->fns[n].fn_type == ATEXIT_FN_EMPTY) - continue; /* already been called */ - fn = p->fns[n]; - if (dso != NULL && dso != fn.fn_dso) { - /* wrong DSO ? */ - if (!has_phdr || global_exit || - !__elf_phdr_match_addr(&phdr_info, - fn.fn_ptr.cxa_func)) - continue; + current_finalize_dso = dso; + do { + call_finalize_again = false; + for (p = __atexit; p; p = p->next) { + for (n = p->ind; --n >= 0;) { + if (p->fns[n].fn_type == ATEXIT_FN_EMPTY) + continue; /* already been called */ + fn = p->fns[n]; + if (dso != NULL && dso != fn.fn_dso) { + /* wrong DSO ? */ + if (!has_phdr || global_exit || + !__elf_phdr_match_addr(&phdr_info, + fn.fn_ptr.cxa_func)) + continue; + } + /* + Mark entry to indicate that this particular + handler has already been called. + */ + p->fns[n].fn_type = ATEXIT_FN_EMPTY; + _MUTEX_UNLOCK(&atexit_mutex); + + /* Call the function of correct type. */ + if (fn.fn_type == ATEXIT_FN_CXA) + fn.fn_ptr.cxa_func(fn.fn_arg); + else if (fn.fn_type == ATEXIT_FN_STD) + fn.fn_ptr.std_func(); + _MUTEX_LOCK(&atexit_mutex); } - /* - Mark entry to indicate that this particular handler - has already been called. - */ - p->fns[n].fn_type = ATEXIT_FN_EMPTY; - _MUTEX_UNLOCK(&atexit_mutex); - - /* Call the function of correct type. */ - if (fn.fn_type == ATEXIT_FN_CXA) - fn.fn_ptr.cxa_func(fn.fn_arg); - else if (fn.fn_type == ATEXIT_FN_STD) - fn.fn_ptr.std_func(); - _MUTEX_LOCK(&atexit_mutex); } - } + } while (call_finalize_again); + current_finalize_dso = NULL; _MUTEX_UNLOCK(&atexit_mutex); if (dso == NULL) _MUTEX_DESTROY(&atexit_mutex);