summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Fiselier <eric@efcs.ca>2019-01-16 01:54:34 +0000
committerEric Fiselier <eric@efcs.ca>2019-01-16 01:54:34 +0000
commit685a8292b343c5dcb5af1ca24d9fb61ca01369f6 (patch)
tree4ac01c06fb3b673eee34ae370d2208f7ec4600ca
parentc2b831cab86e884b89c1363e4dd3ec7b96484f0c (diff)
Fix PR40230 - std::pair may have padding on FreeBSD.
Summary: FreeBSD ships a very old and deprecated ABI for std::pair where the copy and move constructors are not allowed to be trivial. D25389 change how this was implemented by introducing a non-trivial base class. This patch, introduced in October 2016, introduced an ABI bug that caused nested `std::pair` instantiations to have padding. For example: ``` using PairT = std::pair< std::pair<char, char>, char >; static_assert(offsetof(PairT, first) == 0, "First member should exist at offset zero"); // Fails on FreeBSD! ``` The bug occurs because the base class for the first element (the nested pair) cannot be put at offset zero because the top-level pair already has the same base class laid out there. This patch fixes that ABI bug by templating the dummy base class on the same parameters as the pair. Technically this fix is an ABI break for users who depend on the "broken" ABI introduced in 2016. I'm putting this up for review so that the FreeBSD maintainers can sign off on fixing the ABI by breaking the ABI. Another option, since we have to "break" the ABI to fix it, would be to move FreeBSD off the deprecated non-trivial pair ABI instead. Also see: * https://llvm.org/PR40230 * https://reviews.llvm.org/D21329 Reviewers: rsmith, dim, emaste Reviewed By: rsmith Subscribers: mclow.lists, krytarowski, christof, ldionne, libcxx-commits Differential Revision: https://reviews.llvm.org/D56357
-rw-r--r--libcxx/include/utility3
-rw-r--r--libcxx/test/libcxx/utilities/utility/pairs/pairs.pair/non_trivial_copy_move_ABI.pass.cpp15
-rw-r--r--libcxx/test/libcxx/utilities/utility/pairs/pairs.pair/trivial_copy_move_ABI.pass.cpp15
3 files changed, 30 insertions, 3 deletions
diff --git a/libcxx/include/utility b/libcxx/include/utility
index 3fa0bc4c737..74bbc5cf34f 100644
--- a/libcxx/include/utility
+++ b/libcxx/include/utility
@@ -303,6 +303,7 @@ extern _LIBCPP_EXPORTED_FROM_ABI const piecewise_construct_t piecewise_construct
#endif
#if defined(_LIBCPP_DEPRECATED_ABI_DISABLE_PAIR_TRIVIAL_COPY_CTOR)
+template <class, class>
struct __non_trivially_copyable_base {
_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
__non_trivially_copyable_base() _NOEXCEPT {}
@@ -314,7 +315,7 @@ struct __non_trivially_copyable_base {
template <class _T1, class _T2>
struct _LIBCPP_TEMPLATE_VIS pair
#if defined(_LIBCPP_DEPRECATED_ABI_DISABLE_PAIR_TRIVIAL_COPY_CTOR)
-: private __non_trivially_copyable_base
+: private __non_trivially_copyable_base<_T1, _T2>
#endif
{
typedef _T1 first_type;
diff --git a/libcxx/test/libcxx/utilities/utility/pairs/pairs.pair/non_trivial_copy_move_ABI.pass.cpp b/libcxx/test/libcxx/utilities/utility/pairs/pairs.pair/non_trivial_copy_move_ABI.pass.cpp
index 8b5969d5198..58ea6ecde99 100644
--- a/libcxx/test/libcxx/utilities/utility/pairs/pairs.pair/non_trivial_copy_move_ABI.pass.cpp
+++ b/libcxx/test/libcxx/utilities/utility/pairs/pairs.pair/non_trivial_copy_move_ABI.pass.cpp
@@ -30,6 +30,7 @@
#include <utility>
#include <type_traits>
#include <cstdlib>
+#include <cstddef>
#include <cassert>
#include "test_macros.h"
@@ -86,7 +87,7 @@ static_assert(!HasNonTrivialABI<Trivial>::value, "");
#endif
-int main()
+void test_trivial()
{
{
typedef std::pair<int, short> P;
@@ -150,3 +151,15 @@ int main()
}
#endif
}
+
+void test_layout() {
+ typedef std::pair<std::pair<char, char>, char> PairT;
+ static_assert(sizeof(PairT) == 3, "");
+ static_assert(TEST_ALIGNOF(PairT) == TEST_ALIGNOF(char), "");
+ static_assert(offsetof(PairT, first) == 0, "");
+}
+
+int main() {
+ test_trivial();
+ test_layout();
+}
diff --git a/libcxx/test/libcxx/utilities/utility/pairs/pairs.pair/trivial_copy_move_ABI.pass.cpp b/libcxx/test/libcxx/utilities/utility/pairs/pairs.pair/trivial_copy_move_ABI.pass.cpp
index ec9cc7ec3e0..73462338877 100644
--- a/libcxx/test/libcxx/utilities/utility/pairs/pairs.pair/trivial_copy_move_ABI.pass.cpp
+++ b/libcxx/test/libcxx/utilities/utility/pairs/pairs.pair/trivial_copy_move_ABI.pass.cpp
@@ -25,6 +25,7 @@
#include <utility>
#include <type_traits>
#include <cstdlib>
+#include <cstddef>
#include <cassert>
#include "test_macros.h"
@@ -81,7 +82,7 @@ static_assert(HasTrivialABI<Trivial>::value, "");
#endif
-int main()
+void test_trivial()
{
{
typedef std::pair<int, short> P;
@@ -145,3 +146,15 @@ int main()
}
#endif
}
+
+void test_layout() {
+ typedef std::pair<std::pair<char, char>, char> PairT;
+ static_assert(sizeof(PairT) == 3, "");
+ static_assert(TEST_ALIGNOF(PairT) == TEST_ALIGNOF(char), "");
+ static_assert(offsetof(PairT, first) == 0, "");
+}
+
+int main() {
+ test_trivial();
+ test_layout();
+}