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.
Confirmed (note this is hard to happen but it still can happen).
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.
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.
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
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
Mine
Created attachment 6401 [details] proposed patch Something to think about. This should only be applied to 3.4.x and 3.5.x branches.
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?
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.
Benjamin, ping.
Postponed until GCC 3.4.3.
Postponed until GCC 3.4.4.
Any news on this?
Moving to 4.0.2 pre Mark.
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.
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
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
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
Fixed on mainline and 4.0 branch. Will not fix on 3.4.