From patchwork Thu Jun 21 23:28:51 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 932995 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-480247-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="aMFxENDf"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 41BdFH1Sqmz9s2R for ; Fri, 22 Jun 2018 09:29:05 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; q=dns; s= default; b=vQnA1CpXdUzvqzTVH3eMubA4EIBJux3mKS4yrzoGSdKClef5c1/GC 95XBsBPR+ydBxCquP+ye8fsP+Nq1iWw9Bbx82LLZ2+94Nc6jRyWcuCk4Wo38I8Vq D0GLYqrhla0fJIq1D7QVL24XKZ06qFVWurpqmTgzMFka2DzrSt2gHw= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; s= default; bh=H9myv6zQAN1humhD5SeIo6fhJfg=; b=aMFxENDf95LhecUXEWiN jBxFn6Hi4JfmSOwgjMf64qwq/pK+NA7/okRjNFI91vXt38tISsC+ykASs7BKwpDe BzraKWoQdBM/Vn7HTfn4qk+0BDSMHl1XHDgj9pip2f3qrACNFS74THrfHzdygq4y /FEvESUH5u5Drniap3zowts= Received: (qmail 102560 invoked by alias); 21 Jun 2018 23:28:57 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 102536 invoked by uid 89); 21 Jun 2018 23:28:56 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.0 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=compares X-Spam-User: qpsmtpd, 2 recipients X-HELO: mx1.redhat.com Received: from mx3-rdu2.redhat.com (HELO mx1.redhat.com) (66.187.233.73) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 21 Jun 2018 23:28:54 +0000 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C3292400ADF1; Thu, 21 Jun 2018 23:28:52 +0000 (UTC) Received: from localhost (unknown [10.33.36.14]) by smtp.corp.redhat.com (Postfix) with ESMTP id 82BD41C5BF; Thu, 21 Jun 2018 23:28:52 +0000 (UTC) Date: Fri, 22 Jun 2018 00:28:51 +0100 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] PR libstdc++/86138 prevent implicit instantiation of COW empty rep Message-ID: <20180621232851.GA6885@redhat.com> MIME-Version: 1.0 Content-Disposition: inline X-Clacks-Overhead: GNU Terry Pratchett User-Agent: Mutt/1.9.2 (2017-12-15) The explicit instantiation declarations for std::basic_string are disabled for C++17 (and later) so that basic_string symbols get implicitly instantiated in every translation unit that needs them. On targets that don't support STB_GNU_UNIQUE this leads to multiple copies of the empty rep symbol for COW strings. In order to detect whether a COW string needs to deallocate its storage it compares the address with the empty rep. When there are multiple copies of the empty rep object the address is not unique, and so string destructors try to delete the empty rep, which crashes. In order to guarantee uniqueness of the _S_empty_rep_storage symbol this patch adds an explicit instantiation declaration for just that symbol. This means the other symbols are still implicitly instantiated in C++17 code, but for the empty rep the definition in the library gets used. Separately, there is no need for C++17 code to implicitly instantiate the I/O functions for strings, so this also restores the explicit instantiation declarations for those functions. PR libstdc++/86138 * include/bits/basic_string.tcc: [__cplusplus > 201402 && !_GLIBCXX_USE_CXX11_ABI] (basic_string::_Rep::_S_empty_rep_storage) (basic_string::_Rep::_S_empty_rep_storage): Add explicit instantiation declarations. [__cplusplus > 201402] (operator>>, operator<<, getline): Re-enable explicit instantiation declarations. * testsuite/21_strings/basic_string/cons/char/86138.cc: New. * testsuite/21_strings/basic_string/cons/wchar_t/86138.cc: New. Tested x86_64-linux, committed to trunk. If this passes testing on Cygwin I'll also backport it to gcc-7 and gcc-8, as the explicit instantiation declarations are disabled for C++17 on those branches. commit 798a049013ecffc0f428c0b936d3f0770471e8c6 Author: Jonathan Wakely Date: Fri Jun 22 00:10:40 2018 +0100 PR libstdc++/86138 prevent implicit instantiation of COW empty rep The explicit instantiation declarations for std::basic_string are disabled for C++17 (and later) so that basic_string symbols get implicitly instantiated in every translation unit that needs them. On targets that don't support STB_GNU_UNIQUE this leads to multiple copies of the empty rep symbol for COW strings. In order to detect whether a COW string needs to deallocate its storage it compares the address with the empty rep. When there are multiple copies of the empty rep object the address is not unique, and so string destructors try to delete the empty rep, which crashes. In order to guarantee uniqueness of the _S_empty_rep_storage symbol this patch adds an explicit instantiation declaration for just that symbol. This means the other symbols are still implicitly instantiated in C++17 code, but for the empty rep the definition in the library gets used. Separately, there is no need for C++17 code to implicitly instantiate the I/O functions for strings, so this also restores the explicit instantiation declarations for those functions. PR libstdc++/86138 * include/bits/basic_string.tcc: [__cplusplus > 201402 && !_GLIBCXX_USE_CXX11_ABI] (basic_string::_Rep::_S_empty_rep_storage) (basic_string::_Rep::_S_empty_rep_storage): Add explicit instantiation declarations. [__cplusplus > 201402] (operator>>, operator<<, getline): Re-enable explicit instantiation declarations. * testsuite/21_strings/basic_string/cons/char/86138.cc: New. * testsuite/21_strings/basic_string/cons/wchar_t/86138.cc: New. diff --git a/libstdc++-v3/include/bits/basic_string.tcc b/libstdc++-v3/include/bits/basic_string.tcc index be8815c711b..9fbea84c4af 100644 --- a/libstdc++-v3/include/bits/basic_string.tcc +++ b/libstdc++-v3/include/bits/basic_string.tcc @@ -1597,8 +1597,21 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION // Inhibit implicit instantiations for required instantiations, // which are defined via explicit instantiations elsewhere. -#if _GLIBCXX_EXTERN_TEMPLATE > 0 && __cplusplus <= 201402L +#if _GLIBCXX_EXTERN_TEMPLATE > 0 + // The explicit instantiations definitions in src/c++11/string-inst.cc + // are compiled as C++14, so the new C++17 members aren't instantiated. + // Until those definitions are compiled as C++17 suppress the declaration, + // so C++17 code will implicitly instantiate std::string and std::wstring + // as needed. +# if __cplusplus <= 201402L extern template class basic_string; +# elif ! _GLIBCXX_USE_CXX11_ABI + // Still need to prevent implicit instantiation of the COW empty rep, + // to ensure the definition in libstdc++.so is unique (PR 86138). + extern template basic_string::size_type + basic_string::_Rep::_S_empty_rep_storage[]; +# endif + extern template basic_istream& operator>>(basic_istream&, string&); @@ -1613,7 +1626,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION getline(basic_istream&, string&); #ifdef _GLIBCXX_USE_WCHAR_T +# if __cplusplus <= 201402L extern template class basic_string; +# elif ! _GLIBCXX_USE_CXX11_ABI + extern template basic_string::size_type + basic_string::_Rep::_S_empty_rep_storage[]; +# endif + extern template basic_istream& operator>>(basic_istream&, wstring&); @@ -1626,8 +1645,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION extern template basic_istream& getline(basic_istream&, wstring&); -#endif -#endif +#endif // _GLIBCXX_USE_WCHAR_T +#endif // _GLIBCXX_EXTERN_TEMPLATE > 0 _GLIBCXX_END_NAMESPACE_VERSION } // namespace std diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/cons/char/86138.cc b/libstdc++-v3/testsuite/21_strings/basic_string/cons/char/86138.cc new file mode 100644 index 00000000000..224ea42a3c7 --- /dev/null +++ b/libstdc++-v3/testsuite/21_strings/basic_string/cons/char/86138.cc @@ -0,0 +1,30 @@ +// Copyright (C) 2018 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-options "-std=gnu++17" } +// { dg-do compile { target c++17 } } +// { dg-final { scan-assembler-not "_ZNSs4_Rep20_S_empty_rep_storageE:" } } + +#undef _GLIBCXX_USE_CXX11_ABI +#define _GLIBCXX_USE_CXX11_ABI 0 +#include + +void +test01(std::string* s) +{ + s->~basic_string(); +} diff --git a/libstdc++-v3/testsuite/21_strings/basic_string/cons/wchar_t/86138.cc b/libstdc++-v3/testsuite/21_strings/basic_string/cons/wchar_t/86138.cc new file mode 100644 index 00000000000..88f136bc7bc --- /dev/null +++ b/libstdc++-v3/testsuite/21_strings/basic_string/cons/wchar_t/86138.cc @@ -0,0 +1,30 @@ +// Copyright (C) 2018 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-options "-std=gnu++17" } +// { dg-do compile { target c++17 } } +// { dg-final { scan-assembler-not "_ZNSbIwSt11char_traitsIwESaIwEE4_Rep20_S_empty_rep_storageE:" } } + +#undef _GLIBCXX_USE_CXX11_ABI +#define _GLIBCXX_USE_CXX11_ABI 0 +#include + +void +test01(std::wstring* s) +{ + s->~basic_string(); +}