diff mbox series

PR libstdc++/87822 fix layout change for nested std::pair

Message ID 20181031123119.GA17297@redhat.com
State New
Headers show
Series PR libstdc++/87822 fix layout change for nested std::pair | expand

Commit Message

Jonathan Wakely Oct. 31, 2018, 12:31 p.m. UTC
The introduction of the empty __pair_base base class for PR 86751
changed the layout of std::pair<std::pair<...>, ...>. The outer pair and
its first member both have a base class of the same type, which cannot
exist at the same address. This causes the first member to be at a
non-zero offset.

The solution is to make the base class depend on the template
parameters, so that each pair type has a different base class type,
which allows the base classes of the outer pair and its first member to
have the same address.

	PR libstdc++/87822
	* include/bits/stl_pair.h (__pair_base): Change to class template.
	(pair): Make base class type depend on template parameters.
	* testsuite/20_util/pair/87822.cc: New test.

Tested powerpc64le-linux and x86_64-linux, committed to trunk.
Backports to gcc-7 and gcc-8 branches coming ASAP.

This layout change is unfortunately in gcc 6.5 and that branch is now
closed. I'm very sorry :-(
commit 581b5447f18f4758a55b1fda4f8bf597e9466d40
Author: redi <redi@138bc75d-0d04-0410-961f-82ee72b054a4>
Date:   Wed Oct 31 12:29:02 2018 +0000

    PR libstdc++/87822 fix layout change for nested std::pair
    
    The introduction of the empty __pair_base base class for PR 86751
    changed the layout of std::pair<std::pair<...>, ...>. The outer pair and
    its first member both have a base class of the same type, which cannot
    exist at the same address. This causes the first member to be at a
    non-zero offset.
    
    The solution is to make the base class depend on the template
    parameters, so that each pair type has a different base class type,
    which allows the base classes of the outer pair and its first member to
    have the same address.
    
            PR libstdc++/87822
            * include/bits/stl_pair.h (__pair_base): Change to class template.
            (pair): Make base class type depend on template parameters.
            * testsuite/20_util/pair/87822.cc: New test.
    
    git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@265678 138bc75d-0d04-0410-961f-82ee72b054a4

Comments

Jonathan Wakely Oct. 31, 2018, 12:59 p.m. UTC | #1
On 31/10/18 12:31 +0000, Jonathan Wakely wrote:
>The introduction of the empty __pair_base base class for PR 86751
>changed the layout of std::pair<std::pair<...>, ...>. The outer pair and
>its first member both have a base class of the same type, which cannot
>exist at the same address. This causes the first member to be at a
>non-zero offset.
>
>The solution is to make the base class depend on the template
>parameters, so that each pair type has a different base class type,
>which allows the base classes of the outer pair and its first member to
>have the same address.
>
>	PR libstdc++/87822
>	* include/bits/stl_pair.h (__pair_base): Change to class template.
>	(pair): Make base class type depend on template parameters.
>	* testsuite/20_util/pair/87822.cc: New test.

Jakub suggested some more testing, as attached.

Tested powerpc64le-linux and x86_64-linux, committed to trunk.
I'll include this in the backports too.
commit 75efd61131798bd9001b15b551bd6f1cbfc2bc33
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Wed Oct 31 12:57:07 2018 +0000

    More testing for std::pair layout change
    
            * testsuite/20_util/pair/87822.cc: Test deeper nesting.

diff --git a/libstdc++-v3/testsuite/20_util/pair/87822.cc b/libstdc++-v3/testsuite/20_util/pair/87822.cc
index cd099d6f9f9..523d583f08a 100644
--- a/libstdc++-v3/testsuite/20_util/pair/87822.cc
+++ b/libstdc++-v3/testsuite/20_util/pair/87822.cc
@@ -26,6 +26,7 @@ test01()
   static_assert(sizeof(p) == (3 * sizeof(int)), "PR libstdc++/87822");
 #endif
   VERIFY( (void*)&p == (void*)&p.first );
+  VERIFY( (void*)&p == (void*)&p.first.first );
 }
 
 struct empty { };
@@ -40,8 +41,24 @@ test02()
   VERIFY( (void*)&p == (void*)&p.first );
 }
 
+void
+test03()
+{
+  typedef std::pair<int, int> int_pair;
+  typedef std::pair<int_pair, int_pair> int_pair_pair;
+  std::pair<int_pair_pair, int_pair_pair> p;
+#if __cplusplus >= 201103L
+  static_assert(sizeof(int_pair_pair) == (2 * sizeof(int_pair)), "nested");
+  static_assert(sizeof(p) == (2 * sizeof(int_pair_pair)), "nested again");
+#endif
+  VERIFY( (void*)&p == (void*)&p.first );
+  VERIFY( (void*)&p == (void*)&p.first.first );
+  VERIFY( (void*)&p == (void*)&p.first.first.first );
+}
+
 int main()
 {
   test01();
   test02();
+  test03();
 }
diff mbox series

Patch

diff --git a/libstdc++-v3/include/bits/stl_pair.h b/libstdc++-v3/include/bits/stl_pair.h
index ea8bd981559..48af2b02ef9 100644
--- a/libstdc++-v3/include/bits/stl_pair.h
+++ b/libstdc++-v3/include/bits/stl_pair.h
@@ -187,7 +187,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
   };
 #endif // C++11
 
-  class __pair_base
+  template<typename _U1, typename _U2> class __pair_base
   {
 #if __cplusplus >= 201103L
     template<typename _T1, typename _T2> friend struct pair;
@@ -206,7 +206,7 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
    */
   template<typename _T1, typename _T2>
     struct pair
-    : private __pair_base
+    : private __pair_base<_T1, _T2>
     {
       typedef _T1 first_type;    /// @c first_type is the first bound type
       typedef _T2 second_type;   /// @c second_type is the second bound type
diff --git a/libstdc++-v3/testsuite/20_util/pair/87822.cc b/libstdc++-v3/testsuite/20_util/pair/87822.cc
new file mode 100644
index 00000000000..cd099d6f9f9
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/pair/87822.cc
@@ -0,0 +1,47 @@ 
+// 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
+// <http://www.gnu.org/licenses/>.
+
+#include <utility>
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+  std::pair<std::pair<int, int>, int> p;
+#if __cplusplus >= 201103L
+  static_assert(sizeof(p) == (3 * sizeof(int)), "PR libstdc++/87822");
+#endif
+  VERIFY( (void*)&p == (void*)&p.first );
+}
+
+struct empty { };
+
+void
+test02()
+{
+  std::pair<std::pair<empty, empty>, empty> p;
+#if __cplusplus >= 201103L
+  static_assert(sizeof(p) == (3 * sizeof(empty)), "PR libstdc++/87822");
+#endif
+  VERIFY( (void*)&p == (void*)&p.first );
+}
+
+int main()
+{
+  test01();
+  test02();
+}