Bug 13583 - [3.4/4.0/4.1 Regression] __use_cache not threadsafe
Summary: [3.4/4.0/4.1 Regression] __use_cache not threadsafe
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 3.3.2
: P2 minor
Target Milestone: 4.0.3
Assignee: Benjamin Kosnik
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-01-06 12:17 UTC by Pétur Runólfsson
Modified: 2005-10-11 06:28 UTC (History)
3 users (show)

See Also:
Host: i686-pc-linux-gnu
Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu
Known to work: 3.2
Known to fail: 3.4.1 4.0.0
Last reconfirmed: 2005-08-06 15:38:33


Attachments
proposed patch (2.46 KB, patch)
2004-05-27 16:47 UTC, Benjamin Kosnik
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Pétur Runólfsson 2004-01-06 12:17:16 UTC
This fragment in __use_cache (taken from the 3.3 branch) is not threadsafe:

      if (__builtin_expect(!__cache, false))
        {
          __cache = new __locale_cache<_Facet>(__loc);
          __loc._M_impl->_M_install_cache(__cache, __i);
        }

If this code is executed by two threads at the same time, two instances of
the cache are created, of which one will be leaked.

The version on mainline has the same problem.

Since __use_cache first appeard in 3.3.1, this is a regression.
Comment 1 Andrew Pinski 2004-01-06 18:48:11 UTC
Confirmed (note this is hard to happen but it still can happen).
Comment 2 Steven Bosscher 2004-01-10 11:14:02 UTC
How does this happen in 3.4?  __locale_cache was removed on mainline: 
 
2003-06-26  Benjamin Kosnik  <bkoz@redhat.com> 
            Jerry Quinn  <jlquinn@optonline.net> 
(...) 
	(__locale_cache_base): Remove. 
	(__locale_cache): Remove. 
Comment 3 Paolo Carlini 2004-01-10 11:33:08 UTC
Hi Steven!
__use_cache and the whole locale caching machinery is definitely present
in mainline (just grep libstdc++-v3/include/bits), otherwise we wouldn't
be so blazingly fast ;)
The code is somewhat different, tough, I'm not deadly sure that it's
affected by the very same problem.
Comment 4 Loren Rittle 2004-02-11 04:40:39 UTC
Without discounting this type of report (i.e. we want to fix this): On
most/all supported CPU arches, we believe that this is a contained
memory leak (bounded by number of threads competing to build
the cache which itself is bound by number of threads) rather than
failing to be threadsafe...

As discussed on the list, we will not fix this for 3.3.X or 3.4. - Loren
Comment 5 Benjamin Kosnik 2004-03-17 19:48:41 UTC
It is believed this can be fixed without messing with the libstdc++ ABI. 
Something like the __glibcxx_mutex_lock bits from locale::locale and
locale::global can be used, or we can do this right and do something equivalent
with RAII.

So, this is lower-priority than it looks at first glance.

-benjamin
Comment 6 Benjamin Kosnik 2004-05-27 16:45:51 UTC
Mine
Comment 7 Benjamin Kosnik 2004-05-27 16:47:01 UTC
Created attachment 6401 [details]
proposed patch


Something to think about. This should only be applied to 3.4.x and 3.5.x
branches.
Comment 8 Giovanni Bajo 2004-06-06 03:46:56 UTC
Since there is a proposed patch for 3.4, I'm tentatively moving back the target 
to 3.4.1. Benjamin, do you reckon the patch can be applied then?
Comment 9 Mark Mitchell 2004-06-07 14:22:27 UTC
This is OK for 3.4.1 if it goes in on mainline.  However, I'm not going to hold
the release for it -- so I've retargeted at 3.4.2.  If it goes in for 3.4.1,
please set the target back to 3.4.1.
Comment 10 Steven Bosscher 2004-08-12 07:53:06 UTC
Benjamin, ping. 
Comment 11 Mark Mitchell 2004-08-29 18:45:11 UTC
Postponed until GCC 3.4.3.
Comment 12 Mark Mitchell 2004-10-30 20:01:03 UTC
Postponed until GCC 3.4.4.
Comment 13 Andrew Pinski 2005-01-27 00:31:07 UTC
Any news on this?
Comment 14 Andrew Pinski 2005-07-22 21:13:28 UTC
Moving to 4.0.2 pre Mark.
Comment 15 Ian Lance Taylor 2005-09-17 13:26:38 UTC
Why should we not just apply Benjamin's patch for this PR?  As far as I can see
any patch will have to work along the lines of his existing one.
Comment 16 Benjamin Kosnik 2005-09-21 18:54:53 UTC
Hey Ian. I haven't looked at this in a bit.

The problem with the posted patch is that we should not be exporting the mutex
objects. See the rest of the libstdc++ code for suggested usage.

-benjamin

Comment 17 GCC Commits 2005-10-11 06:19:20 UTC
Subject: Bug 13583

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	ian@gcc.gnu.org	2005-10-11 06:19:13

Modified files:
	libstdc++-v3   : ChangeLog configure configure.ac 
	libstdc++-v3/config: linker-map.gnu 
	libstdc++-v3/include/bits: locale_classes.h 
	libstdc++-v3/src: locale.cc 
	libstdc++-v3/testsuite: testsuite_abi.cc 

Log message:
	PR libstdc++/13583
	* include/bits/locale_classes.h (locale::_Impl::_M_install_cache):
	Move out of line.
	* src/locale.cc: Define here, add mutex.
	* configure.ac (libtool_VERSION): To 6:7:0.
	* configure: Regenerate.
	* testsuite/testsuite_abi.cc (check_version): Add GLIBCXX_3.4.7.
	* config/linker-map.gnu: Export locale::_Impl::_M_install_cache.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/ChangeLog.diff?cvsroot=gcc&r1=1.3127&r2=1.3128
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/configure.diff?cvsroot=gcc&r1=1.449&r2=1.450
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/configure.ac.diff?cvsroot=gcc&r1=1.39&r2=1.40
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/config/linker-map.gnu.diff?cvsroot=gcc&r1=1.86&r2=1.87
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/include/bits/locale_classes.h.diff?cvsroot=gcc&r1=1.25&r2=1.26
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/src/locale.cc.diff?cvsroot=gcc&r1=1.111&r2=1.112
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/testsuite/testsuite_abi.cc.diff?cvsroot=gcc&r1=1.13&r2=1.14

Comment 18 GCC Commits 2005-10-11 06:22:17 UTC
Subject: Bug 13583

CVSROOT:	/cvs/gcc
Module name:	gcc
Branch: 	gcc-4_0-branch
Changes by:	ian@gcc.gnu.org	2005-10-11 06:22:08

Modified files:
	libstdc++-v3   : ChangeLog configure configure.ac 
	libstdc++-v3/config: linker-map.gnu 
	libstdc++-v3/include/bits: locale_classes.h 
	libstdc++-v3/src: locale.cc 
	libstdc++-v3/testsuite: testsuite_abi.cc 

Log message:
	PR libstdc++/13583
	* include/bits/locale_classes.h (locale::_Impl::_M_install_cache):
	Move out of line.
	* src/locale.cc: Define here, add mutex.
	* configure.ac (libtool_VERSION): To 6:7:0.
	* configure: Regenerate.
	* testsuite/testsuite_abi.cc (check_version): Add GLIBCXX_3.4.7.
	* config/linker-map.gnu: Export locale::_Impl::_M_install_cache.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.2917.2.93&r2=1.2917.2.94
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/configure.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.423.2.6&r2=1.423.2.7
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/configure.ac.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.32.10.3&r2=1.32.10.4
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/config/linker-map.gnu.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.78.2.4&r2=1.78.2.5
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/include/bits/locale_classes.h.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.24&r2=1.24.12.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/src/locale.cc.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.110&r2=1.110.42.1
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/libstdc++-v3/testsuite/testsuite_abi.cc.diff?cvsroot=gcc&only_with_tag=gcc-4_0-branch&r1=1.8.12.4&r2=1.8.12.5

Comment 19 Ian Lance Taylor 2005-10-11 06:28:01 UTC
Fixed on mainline and 4.0 branch.  Will not fix on 3.4.