From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by sourceware.org (Postfix) with ESMTPS id CE3813858C2B; Mon, 6 Nov 2023 12:51:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CE3813858C2B Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org CE3813858C2B Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2001:67c:2178:6::1d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699275120; cv=none; b=P0sIN2BH5GI3iQy9Yor1997qo7WGwstv4RjKMgXunez9rYu+A5LP+Hk7JL6W+IGBi7w9heeSqLNDTWI1f5l7v8hTuXMuivQdol4hxziQE0jICTc4Rb5tz9gZO+WjP9nNTewKI1o0Wbf4exFACPKPZXzmrzbwnllxKF0psaz14eg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699275120; c=relaxed/simple; bh=5NS2M6i2JxbmCrT8xiFpW2avS9gFO9hgcKIx0xZ/GAI=; h=DKIM-Signature:DKIM-Signature:Date:From:To:Subject:Message-ID: MIME-Version; b=aZdZ11wB4civ9FMieEWTurCzJXWOqiMvDrCUgDwLs/VHWadr1xFpvepDGuDyxn8TPD0n002x1NFzUx4Ajw5zqEl2kl4cNMd++L1iZz7yuEY507oUv/5qrY/SLFj/yEuGuoKCIEfR2/yLRsHt2dqTZ1j1ktOO6RqcqzT355vCepM= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 9FC251FE12; Mon, 6 Nov 2023 12:51:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1699275117; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=h8NEJw3EDlsrmZluLSQDGjmj43zrYNduBRpzxlknKSs=; b=AdRjH89PAMw7z5YT+LdjGGHivU2ypiJkkDIRoGmD0rlbvtq4tcTNV09p+FDFlHcqrofVmr Qu/LDMYsKN+tLGMOKFLwDt4dgH5VgKC7NWN8SQAIuP3Ll22uDHtT4jei86E2NyPzTFU5Bb D8bbVrdfwJkt4ZKPAl6+02BynGyDbcg= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1699275117; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=h8NEJw3EDlsrmZluLSQDGjmj43zrYNduBRpzxlknKSs=; b=iBUJNMGUikVFaDV5YsBlIq3KHzn9Omv6+czcPZgvU7LyJkBf2E/rneC+56WffHVod1VIFm qxfTBIwBtx+z9UBQ== Received: from wotan.suse.de (wotan.suse.de [10.160.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 6450F2C1E6; Mon, 6 Nov 2023 12:51:57 +0000 (UTC) Date: Mon, 6 Nov 2023 12:51:57 +0000 (UTC) From: Richard Biener To: Jonathan Wakely cc: gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org Subject: Re: [PATCH] libstdc++/112351 - deal with __gthread_once failure during locale init In-Reply-To: Message-ID: References: <11125.123110606520900751@us-mta-255.us.mimecast.lan> User-Agent: Alpine 2.22 (LSU 394 2020-01-19) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-11.1 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Mon, 6 Nov 2023, Jonathan Wakely wrote: > On Mon, 6 Nov 2023 at 11:52, Richard Biener wrote: > > > > The following makes the C++98 locale init path follow the way the > > C++11 performs initialization. This way we deal with pthread_once > > failing, falling back to non-threadsafe initialization which, given we > > initialize from the library, should be serialized by the dynamic > > loader already. > > > > Bootstrapped and tested on x86_64-unknown-linux-gnu, OK for trunk? > > And GCC 13 branch? > > > > Thanks, > > Richard. > > > > PR libstdc++/112351 > > libstdc++-v3/ > > * src/c++98/locale.cc (locale::facet::_S_get_c_locale): > > Always perform non-threadsafe init when threadsafe init > > failed. > > --- > > libstdc++-v3/src/c++98/locale.cc | 7 ++----- > > 1 file changed, 2 insertions(+), 5 deletions(-) > > > > diff --git a/libstdc++-v3/src/c++98/locale.cc b/libstdc++-v3/src/c++98/locale.cc > > index d308140bab7..e9bec1db3b6 100644 > > --- a/libstdc++-v3/src/c++98/locale.cc > > +++ b/libstdc++-v3/src/c++98/locale.cc > > @@ -216,12 +216,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION > > #ifdef __GTHREADS > > if (__gthread_active_p()) > > __gthread_once(&_S_once, _S_initialize_once); > > - else > > #endif > > - { > > - if (!_S_c_locale) > > - _S_initialize_once(); > > - } > > + if (__builtin_expect (!_S_c_locale, 0)) > > + _S_initialize_once(); > > return _S_c_locale; > > } > > > I think this has a problem, which is handled correctly in > src/c++11/locale_init.cc by checking _S_classic inside the > _S_initialize_once function. We check _S_c_locale here (it's just a pointer) instead of in _S_initialize_once (), so I think the code is equivalent, no? > If the first call to __gthread_once does fail then _S_once will not be > changed. We will fall through to calling _S_initialize_once directly > (which is not thread-safe) and set _S_c_locale. > > The next time we call _S_initialize, __gthread_once will try to run > again, and because _S_once was not changed, it might call > _S_initialize_once() again, which writes to _S_c_locale again > (possibly causing a data race). Ah, yeah, so in the C++11 path the check for !_S_classic in locale::_S_initialize is redundant. But good spot. > I don't think the slightly different code in src/c++11/locale_init.cc > is different in order to handle __gthread_once failing, I think it's > different because the effects of locale::facet::_S_initialize_once() > and locale::_S_initialize_once() are different. One is safe to call > more than once, and the other isn't. > > I don't think we need to care about __gthread_once failing at all, do > we? There are no error conditions for pthread_once, it always returns > 0 (previous POSIX revisions said it could return EINVAL for an > uninitialized pthread_once_t but that can't happen here as it's > correctly initialized in src/c++11/locale.cc). Is the concern that it > can fail for non-posix thread models? (I didn't check if any of them > can actually fail) The concern is that there are actual products out that break with the new I/O initialization in libstdc++ for GCC13+ because they have bugs. It's easy enough to work around those by the proposed patch (plus correction for the above issue). I suppose the comment in locale::_S_initialize_once holds as well for the C++98 path. The failure mode of the product is that it overrides pthread_once but does nothing (not even indicate failure) when its pthread_* override mechanism isn't initialized yet. With libstdc++ from GCC13 we now use pthread_once "too early" and fail to initialize the locale object. Adjusted patch below. OK after another round of testing? Thanks, Richard. >From 4e3fa2f4426a5a10d189587b63e4d7298c347b01 Mon Sep 17 00:00:00 2001 From: Richard Biener Date: Mon, 6 Nov 2023 11:31:40 +0100 Subject: [PATCH] libstdc++/112351 - deal with __gthread_once failure during locale init To: gcc-patches@gcc.gnu.org The following makes the C++98 locale init path follow the way the C++11 performs initialization. This way we deal with pthread_once failing, falling back to non-threadsafe initialization which, given we initialize from the library, should be serialized by the dynamic loader already. PR libstdc++/112351 libstdc++-v3/ * src/c++98/locale.cc (locale::facet::_S_initialize_once): Check whether _S_c_locale is already initialized. (locale::facet::_S_get_c_locale): Always perform non-threadsafe init when threadsafe init failed. --- libstdc++-v3/src/c++98/locale.cc | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/libstdc++-v3/src/c++98/locale.cc b/libstdc++-v3/src/c++98/locale.cc index d308140bab7..1ef0c394cd7 100644 --- a/libstdc++-v3/src/c++98/locale.cc +++ b/libstdc++-v3/src/c++98/locale.cc @@ -206,6 +206,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION void locale::facet::_S_initialize_once() { + // Need to check this because we could get called once from + // _S_get_c_locale() when the program is single-threaded, and then again + // (via __gthread_once) when it's multi-threaded. + if (_S_c_locale) + return; + // Initialize the underlying locale model. _S_create_c_locale(_S_c_locale, _S_c_name); } @@ -216,12 +222,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION #ifdef __GTHREADS if (__gthread_active_p()) __gthread_once(&_S_once, _S_initialize_once); - else #endif - { - if (!_S_c_locale) - _S_initialize_once(); - } + if (__builtin_expect (!_S_c_locale, 0)) + _S_initialize_once(); return _S_c_locale; } -- 2.35.3