From patchwork Wed Dec 3 08:57:35 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Takashi Yano X-Patchwork-Id: 125817 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5AB6C4D8D1FB for ; Wed, 3 Dec 2025 08:59:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5AB6C4D8D1FB Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=nifty.ne.jp header.i=@nifty.ne.jp header.a=rsa-sha256 header.s=default-1th84yt82rvi header.b=PrgziYGm X-Original-To: newlib@sourceware.org Delivered-To: newlib@sourceware.org Received: from mta-snd-e10.mail.nifty.com (mta-snd-e10.mail.nifty.com [106.153.226.42]) by sourceware.org (Postfix) with ESMTPS id 098AD4906CAC for ; Wed, 3 Dec 2025 08:58:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 098AD4906CAC Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=nifty.ne.jp Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=nifty.ne.jp ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 098AD4906CAC Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=106.153.226.42 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1764752301; cv=none; b=PlA7zgj8f6uc6cDz+yfkoRdfn+sKQIxh0NC0H1V+42+3uu8YC6JsnUmbSiAI33lZiPAVGD+CPntIWqkWzOZfiikjPuw+n/UhsmjHbAP6gbCCTCIgGcSiS77RkFo8Dx5iyfYaqnRXXgeuSkepYALqXuedurxIFRVpci0RWkM/nsU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1764752301; c=relaxed/simple; bh=zP5vYNCi/OyIEVrw2+MCBI2PFr1LPWgz/TT/I1wbo6A=; h=From:To:Subject:Date:Message-ID:MIME-Version:DKIM-Signature; b=fvcCjPuwW2mhkFqQy8ubj5rKNiHX3aL9IJeVHn/QBdKKhLcI8tCnhUnOwFnwZKWDrUd/DF5zO0IKhYIiudmr3MFRgx5YopsB82Aj+jbAn8LpflAuSEbXundlG85bJbu5oQY3EWW0AMUQ7klSF2HyYuMp4E6St5F2MH4D9tMt8Jw= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 098AD4906CAC Received: from HP-Z230 by mta-snd-e10.mail.nifty.com with ESMTP id <20251203085814129.FPXR.34837.HP-Z230@nifty.com>; Wed, 3 Dec 2025 17:58:14 +0900 From: Takashi Yano To: newlib@sourceware.org Cc: Takashi Yano , Sebastian Huber , Corinna Vinschen Subject: [PATCH] newlib: copy args for atexit()'ed function before unlock the mutex Date: Wed, 3 Dec 2025 17:57:35 +0900 Message-ID: <20251203085803.579-1-takashi.yano@nifty.ne.jp> X-Mailer: git-send-email 2.51.0 MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.ne.jp; s=default-1th84yt82rvi; t=1764752294; bh=ji5sijRFD7yRkguHpkCzl5//lScUAsEKny8h8EHO/cU=; h=From:To:Cc:Subject:Date; b=PrgziYGmrFb3tWxya2iuXx+XtIwmtvhJM3eSoqEFYv50eovpIUZzQImjUxv1AxSU9v75VSQl KXTeFhMHlAXyycBGdnQCEZV0CwHM36oxnQIO3RcJkfsprFPf10j1QeBxkams8jo8fc3e2CAQA5 4csXhCiVtuUwrMRhYhFIT6fjTbRUmeUYWiiqmpZEeHcaxKkqwspDQSOU0yW+fKUMmXz4P/HRGx qrSUhsLCNtqR0F2Hfs4SjhVhARoLMUzd62qVkIosubvVRTd3NcTdWfR7/dOgFTVI7ms25v+C2B 3DaAUR8/RSkJoMIdKEzAmCHMMXXZjVyDl7+YQ5QMsg8uwh+Q== X-Spam-Status: No, score=-10.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED, SPF_HELO_PASS, SPF_PASS, TXREP, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: newlib@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Newlib mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: newlib-bounces~patchwork=sourceware.org@sourceware.org The commit a2a8bc771f2f has a problem that __atexit which includes args for atexit()'ed function may be touched by another thread since the mutex is unlocked while calling the function. With this path, the args, etc. are copyed to local variable to prevent this problem. Fixes: a2a8bc771f2f ("newlib: Unlock the mutex while calling atexit()'ed functions") Suggested-by: Sebastian Huber Reviewed-by: Corinna Vinschen , Sebastian Huber Signed-off-by: Takashi Yano --- newlib/libc/stdlib/__call_atexit.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/newlib/libc/stdlib/__call_atexit.c b/newlib/libc/stdlib/__call_atexit.c index 44f1f6acc..15ecda343 100644 --- a/newlib/libc/stdlib/__call_atexit.c +++ b/newlib/libc/stdlib/__call_atexit.c @@ -93,6 +93,8 @@ __call_exitprocs (int code, void *d) for (n = p->_ind - 1; n >= 0; n--) { int ind; + __ULong fntypes, is_cxa; + void *fnarg; i = 1 << n; @@ -114,18 +116,22 @@ __call_exitprocs (int code, void *d) ind = p->_ind; + fntypes = args->_fntypes; + is_cxa = args->_is_cxa; + fnarg = args->_fnargs[n]; + #ifndef __SINGLE_THREAD__ /* Unlock __atexit_recursive_mutex; otherwise, the function fn() may deadlock if it waits for another thread which calls atexit(). */ __lock_release_recursive(__atexit_recursive_mutex); #endif /* Call the function. */ - if (!args || (args->_fntypes & i) == 0) + if (!args || (fntypes & i) == 0) fn (); - else if ((args->_is_cxa & i) == 0) - (*((void (*)(int, void *)) fn))(code, args->_fnargs[n]); + else if ((is_cxa & i) == 0) + (*((void (*)(int, void *)) fn))(code, fnarg); else - (*((void (*)(void *)) fn))(args->_fnargs[n]); + (*((void (*)(void *)) fn))(fnarg); #ifndef __SINGLE_THREAD__ __lock_acquire_recursive(__atexit_recursive_mutex); #endif