Skip to content

Commit c4e9872

Browse files
committed
[libc++] Fix std::copy and std::move for ranges with potentially overlapping tail padding
This fixes thr bug reported in https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108846. Reviewed By: ldionne, #libc Spies: mstorsjo, libcxx-commits Differential Revision: https://reviews.llvm.org/D151953
1 parent e8e0f32 commit c4e9872

File tree

22 files changed

+448
-383
lines changed

22 files changed

+448
-383
lines changed

libcxx/include/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,7 @@ set(files
707707
__type_traits/conjunction.h
708708
__type_traits/copy_cv.h
709709
__type_traits/copy_cvref.h
710+
__type_traits/datasizeof.h
710711
__type_traits/decay.h
711712
__type_traits/dependent_type.h
712713
__type_traits/disjunction.h

libcxx/include/__algorithm/copy_move_common.h

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <__config>
1616
#include <__iterator/iterator_traits.h>
1717
#include <__memory/pointer_traits.h>
18+
#include <__string/constexpr_c_functions.h>
1819
#include <__type_traits/enable_if.h>
1920
#include <__type_traits/is_always_bitcastable.h>
2021
#include <__type_traits/is_constant_evaluated.h>
@@ -61,7 +62,8 @@ template <class _In, class _Out>
6162
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_In*, _Out*>
6263
__copy_trivial_impl(_In* __first, _In* __last, _Out* __result) {
6364
const size_t __n = static_cast<size_t>(__last - __first);
64-
::__builtin_memmove(__result, __first, __n * sizeof(_Out));
65+
66+
std::__constexpr_memmove(__result, __first, __element_count(__n));
6567

6668
return std::make_pair(__last, __result + __n);
6769
}
@@ -72,7 +74,7 @@ __copy_backward_trivial_impl(_In* __first, _In* __last, _Out* __result) {
7274
const size_t __n = static_cast<size_t>(__last - __first);
7375
__result -= __n;
7476

75-
::__builtin_memmove(__result, __first, __n * sizeof(_Out));
77+
std::__constexpr_memmove(__result, __first, __element_count(__n));
7678

7779
return std::make_pair(__last, __result);
7880
}
@@ -119,16 +121,6 @@ __unwrap_and_dispatch(_InIter __first, _Sent __last, _OutIter __out_first) {
119121
return _Algorithm()(std::move(__first), std::move(__last), std::move(__out_first));
120122
}
121123

122-
template <class _IterOps, class _InValue, class _OutIter, class = void>
123-
struct __can_copy_without_conversion : false_type {};
124-
125-
template <class _IterOps, class _InValue, class _OutIter>
126-
struct __can_copy_without_conversion<
127-
_IterOps,
128-
_InValue,
129-
_OutIter,
130-
__enable_if_t<is_same<_InValue, typename _IterOps::template __value_type<_OutIter> >::value> > : true_type {};
131-
132124
template <class _AlgPolicy,
133125
class _NaiveAlgorithm,
134126
class _OptimizedAlgorithm,
@@ -137,23 +129,6 @@ template <class _AlgPolicy,
137129
class _OutIter>
138130
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 pair<_InIter, _OutIter>
139131
__dispatch_copy_or_move(_InIter __first, _Sent __last, _OutIter __out_first) {
140-
#ifdef _LIBCPP_COMPILER_GCC
141-
// GCC doesn't support `__builtin_memmove` during constant evaluation.
142-
if (__libcpp_is_constant_evaluated()) {
143-
return std::__unwrap_and_dispatch<_NaiveAlgorithm>(std::move(__first), std::move(__last), std::move(__out_first));
144-
}
145-
#else
146-
// In Clang, `__builtin_memmove` only supports fully trivially copyable types (just having trivial copy assignment is
147-
// insufficient). Also, conversions are not supported.
148-
if (__libcpp_is_constant_evaluated()) {
149-
using _InValue = typename _IterOps<_AlgPolicy>::template __value_type<_InIter>;
150-
if (!is_trivially_copyable<_InValue>::value ||
151-
!__can_copy_without_conversion<_IterOps<_AlgPolicy>, _InValue, _OutIter>::value) {
152-
return std::__unwrap_and_dispatch<_NaiveAlgorithm>(std::move(__first), std::move(__last), std::move(__out_first));
153-
}
154-
}
155-
#endif // _LIBCPP_COMPILER_GCC
156-
157132
using _Algorithm = __overload<_NaiveAlgorithm, _OptimizedAlgorithm>;
158133
return std::__unwrap_and_dispatch<_Algorithm>(std::move(__first), std::move(__last), std::move(__out_first));
159134
}

libcxx/include/__string/char_traits.h

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -170,25 +170,6 @@ struct _LIBCPP_DEPRECATED_("char_traits<T> for T not equal to char, wchar_t, cha
170170
{return int_type(EOF);}
171171
};
172172

173-
template <class _CharT>
174-
_LIBCPP_HIDE_FROM_ABI static inline _LIBCPP_CONSTEXPR_SINCE_CXX20
175-
_CharT* __char_traits_move(_CharT* __dest, const _CharT* __source, size_t __n) _NOEXCEPT
176-
{
177-
#ifdef _LIBCPP_COMPILER_GCC
178-
if (__libcpp_is_constant_evaluated()) {
179-
if (__n == 0)
180-
return __dest;
181-
_CharT* __allocation = new _CharT[__n];
182-
std::copy_n(__source, __n, __allocation);
183-
std::copy_n(static_cast<const _CharT*>(__allocation), __n, __dest);
184-
delete[] __allocation;
185-
return __dest;
186-
}
187-
#endif
188-
::__builtin_memmove(__dest, __source, __n * sizeof(_CharT));
189-
return __dest;
190-
}
191-
192173
// char_traits<char>
193174

194175
template <>
@@ -249,7 +230,7 @@ struct _LIBCPP_TEMPLATE_VIS char_traits<char>
249230

250231
static inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
251232
char_type* move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT {
252-
return std::__char_traits_move(__s1, __s2, __n);
233+
return std::__constexpr_memmove(__s1, __s2, __element_count(__n));
253234
}
254235

255236
static inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
@@ -320,7 +301,7 @@ struct _LIBCPP_TEMPLATE_VIS char_traits<wchar_t>
320301

321302
static inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
322303
char_type* move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT {
323-
return std::__char_traits_move(__s1, __s2, __n);
304+
return std::__constexpr_memmove(__s1, __s2, __element_count(__n));
324305
}
325306

326307
static inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
@@ -384,7 +365,7 @@ struct _LIBCPP_TEMPLATE_VIS char_traits<char8_t>
384365

385366
static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
386367
char_type* move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT {
387-
return std::__char_traits_move(__s1, __s2, __n);
368+
return std::__constexpr_memmove(__s1, __s2, __element_count(__n));
388369
}
389370

390371
static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
@@ -468,7 +449,7 @@ struct _LIBCPP_TEMPLATE_VIS char_traits<char16_t>
468449

469450
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
470451
static char_type* move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT {
471-
return std::__char_traits_move(__s1, __s2, __n);
452+
return std::__constexpr_memmove(__s1, __s2, __element_count(__n));
472453
}
473454

474455
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
@@ -562,7 +543,7 @@ struct _LIBCPP_TEMPLATE_VIS char_traits<char32_t>
562543

563544
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
564545
static char_type* move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT {
565-
return std::__char_traits_move(__s1, __s2, __n);
546+
return std::__constexpr_memmove(__s1, __s2, __element_count(__n));
566547
}
567548

568549
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20

libcxx/include/__string/constexpr_c_functions.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@
1010
#define _LIBCPP___STRING_CONSTEXPR_C_FUNCTIONS_H
1111

1212
#include <__config>
13+
#include <__type_traits/datasizeof.h>
14+
#include <__type_traits/is_always_bitcastable.h>
1315
#include <__type_traits/is_constant_evaluated.h>
1416
#include <__type_traits/is_equality_comparable.h>
1517
#include <__type_traits/is_same.h>
18+
#include <__type_traits/is_trivially_copyable.h>
1619
#include <__type_traits/is_trivially_lexicographically_comparable.h>
1720
#include <__type_traits/remove_cv.h>
21+
#include <__utility/is_pointer_in_range.h>
1822
#include <cstddef>
1923

2024
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
@@ -129,6 +133,30 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp* __constexpr_memchr(_Tp*
129133
}
130134
}
131135

136+
template <class _Tp, class _Up, __enable_if_t<__is_always_bitcastable<_Up, _Tp>::value, int> = 0>
137+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp*
138+
__constexpr_memmove(_Tp* __dest, _Up* __src, __element_count __n) {
139+
size_t __count = static_cast<size_t>(__n);
140+
if (__libcpp_is_constant_evaluated()) {
141+
#ifdef _LIBCPP_COMPILER_CLANG_BASED
142+
if (is_same<__remove_cv_t<_Tp>, __remove_cv_t<_Up> >::value) {
143+
::__builtin_memmove(__dest, __src, __count * sizeof(_Tp));
144+
return __dest;
145+
}
146+
#endif
147+
if (std::__is_pointer_in_range(__src, __src + __count, __dest)) {
148+
for (; __count > 0; --__count)
149+
__dest[__count - 1] = __src[__count - 1];
150+
} else {
151+
for (size_t __i = 0; __i != __count; ++__i)
152+
__dest[__i] = __src[__i];
153+
}
154+
} else if (__count > 0) {
155+
::__builtin_memmove(__dest, __src, (__count - 1) * sizeof(_Tp) + __libcpp_datasizeof<_Tp>::value);
156+
}
157+
return __dest;
158+
}
159+
132160
_LIBCPP_END_NAMESPACE_STD
133161

134162
#endif // _LIBCPP___STRING_CONSTEXPR_C_FUNCTIONS_H
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef _LIBCPP___TYPE_TRAITS_DATASIZEOF_H
10+
#define _LIBCPP___TYPE_TRAITS_DATASIZEOF_H
11+
12+
#include <__config>
13+
#include <__type_traits/is_class.h>
14+
#include <__type_traits/is_final.h>
15+
#include <cstddef>
16+
17+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
18+
# pragma GCC system_header
19+
#endif
20+
21+
// This trait provides the size of a type excluding any tail padding.
22+
//
23+
// It is useful in contexts where performing an operation using the full size of the class (including padding) may
24+
// have unintended side effects, such as overwriting a derived class' member when writing the tail padding of a class
25+
// through a pointer-to-base.
26+
27+
_LIBCPP_BEGIN_NAMESPACE_STD
28+
29+
template <class _Tp>
30+
struct __libcpp_datasizeof {
31+
#if __has_cpp_attribute(__no_unique_address__)
32+
template <class = char>
33+
struct _FirstPaddingByte {
34+
[[__no_unique_address__]] _Tp __v_;
35+
char __first_padding_byte_;
36+
};
37+
#else
38+
template <bool = __libcpp_is_final<_Tp>::value || !is_class<_Tp>::value>
39+
struct _FirstPaddingByte : _Tp {
40+
char __first_padding_byte_;
41+
};
42+
43+
template <>
44+
struct _FirstPaddingByte<true> {
45+
_Tp __v_;
46+
char __first_padding_byte_;
47+
};
48+
#endif
49+
50+
static const size_t value = offsetof(_FirstPaddingByte<>, __first_padding_byte_);
51+
};
52+
53+
_LIBCPP_END_NAMESPACE_STD
54+
55+
#endif // _LIBCPP___TYPE_TRAITS_DATASIZEOF_H

libcxx/include/__utility/is_pointer_in_range.h

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
#include <__type_traits/enable_if.h>
1616
#include <__type_traits/integral_constant.h>
1717
#include <__type_traits/is_constant_evaluated.h>
18-
#include <__type_traits/is_function.h>
19-
#include <__type_traits/is_member_pointer.h>
2018
#include <__type_traits/void_t.h>
2119
#include <__utility/declval.h>
2220

@@ -26,14 +24,16 @@
2624

2725
_LIBCPP_BEGIN_NAMESPACE_STD
2826

27+
template <class _Tp, class _Up, class = void>
28+
struct __is_less_than_comparable : false_type {};
29+
2930
template <class _Tp, class _Up>
31+
struct __is_less_than_comparable<_Tp, _Up, __void_t<decltype(std::declval<_Tp>() < std::declval<_Up>())> > : true_type {
32+
};
33+
34+
template <class _Tp, class _Up, __enable_if_t<__is_less_than_comparable<const _Tp*, const _Up*>::value, int> = 0>
3035
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_SANITIZE("address") bool __is_pointer_in_range(
3136
const _Tp* __begin, const _Tp* __end, const _Up* __ptr) {
32-
static_assert(!is_function<_Tp>::value && !is_function<_Up>::value,
33-
"__is_pointer_in_range should not be called with function pointers");
34-
static_assert(!is_member_pointer<_Tp>::value && !is_member_pointer<_Up>::value,
35-
"__is_pointer_in_range should not be called with member pointers");
36-
3737
if (__libcpp_is_constant_evaluated()) {
3838
_LIBCPP_ASSERT_UNCATEGORIZED(__builtin_constant_p(__begin <= __end), "__begin and __end do not form a range");
3939

@@ -47,6 +47,16 @@ _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_SANITIZE("address
4747
return !__less<>()(__ptr, __begin) && __less<>()(__ptr, __end);
4848
}
4949

50+
template <class _Tp, class _Up, __enable_if_t<!__is_less_than_comparable<const _Tp*, const _Up*>::value, int> = 0>
51+
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_SANITIZE("address") bool __is_pointer_in_range(
52+
const _Tp* __begin, const _Tp* __end, const _Up* __ptr) {
53+
if (__libcpp_is_constant_evaluated())
54+
return false;
55+
56+
return reinterpret_cast<const char*>(__begin) <= reinterpret_cast<const char*>(__ptr) &&
57+
reinterpret_cast<const char*>(__ptr) < reinterpret_cast<const char*>(__end);
58+
}
59+
5060
_LIBCPP_END_NAMESPACE_STD
5161

5262
#endif // _LIBCPP___UTILITY_IS_POINTER_IN_RANGE_H

libcxx/include/module.modulemap.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,6 +1576,7 @@ module std [system] {
15761576
module conjunction { private header "__type_traits/conjunction.h" }
15771577
module copy_cv { private header "__type_traits/copy_cv.h" }
15781578
module copy_cvref { private header "__type_traits/copy_cvref.h" }
1579+
module datasizeof { private header "__type_traits/datasizeof.h" }
15791580
module decay { private header "__type_traits/decay.h" }
15801581
module dependent_type { private header "__type_traits/dependent_type.h" }
15811582
module disjunction { private header "__type_traits/disjunction.h" }

libcxx/include/string

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -663,13 +663,6 @@ struct __can_be_converted_to_string_view : public _BoolConstant<
663663

664664
struct __uninitialized_size_tag {};
665665

666-
template <class _Tp, class _Up, class = void>
667-
struct __is_less_than_comparable : false_type {};
668-
669-
template <class _Tp, class _Up>
670-
struct __is_less_than_comparable<_Tp, _Up, __void_t<decltype(std::declval<_Tp>() < std::declval<_Up>())> > : true_type {
671-
};
672-
673666
template<class _CharT, class _Traits, class _Allocator>
674667
class basic_string
675668
{
@@ -1910,25 +1903,11 @@ private:
19101903
return *this;
19111904
}
19121905

1913-
template <
1914-
class _Tp,
1915-
__enable_if_t<__is_less_than_comparable<const __remove_cvref_t<_Tp>*, const value_type*>::value, int> = 0>
1906+
template <class _Tp>
19161907
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __addr_in_range(const _Tp& __v) const {
19171908
return std::__is_pointer_in_range(data(), data() + size() + 1, std::addressof(__v));
19181909
}
19191910

1920-
template <
1921-
class _Tp,
1922-
__enable_if_t<!__is_less_than_comparable<const __remove_cvref_t<_Tp>*, const value_type*>::value, int> = 0>
1923-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __addr_in_range(const _Tp& __v) const {
1924-
if (__libcpp_is_constant_evaluated())
1925-
return false;
1926-
1927-
auto __t_ptr = reinterpret_cast<const char*>(std::addressof(__v));
1928-
return reinterpret_cast<const char*>(data()) <= __t_ptr &&
1929-
__t_ptr < reinterpret_cast<const char*>(data() + size() + 1);
1930-
}
1931-
19321911
_LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
19331912
void __throw_length_error() const {
19341913
std::__throw_length_error("basic_string");

libcxx/test/libcxx/algorithms/alg.modifying.operations/copy_move_trivial.pass.cpp

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -156,26 +156,6 @@ void test_one(Func func) {
156156
}
157157
}));
158158
}
159-
160-
// Empty input sequence.
161-
{
162-
const std::size_t N = 0;
163-
164-
From input[1] = {make<From>(1)};
165-
To output[1] = {make<To>(2)};
166-
167-
auto in = InIter(input);
168-
auto in_end = InIter(input + N);
169-
auto sent = SentWrapper<decltype(in_end)>(in_end);
170-
auto out = OutIter(output);
171-
172-
assert(!memmove_called);
173-
func(in, sent, out, N);
174-
175-
assert(memmove_called);
176-
memmove_called = false;
177-
assert(output[0] == make<To>(2));
178-
}
179159
}
180160

181161
template <class InIter, template <class> class SentWrapper, class OutIter>

libcxx/test/libcxx/transitive_includes/cxx03.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ array algorithm
4242
array compare
4343
array concepts
4444
array cstddef
45+
array cstdint
4546
array cstdlib
4647
array initializer_list
4748
array iterator
@@ -506,6 +507,7 @@ locale version
506507
map compare
507508
map concepts
508509
map cstddef
510+
map cstdint
509511
map cstdlib
510512
map functional
511513
map initializer_list
@@ -731,6 +733,7 @@ semaphore version
731733
set compare
732734
set concepts
733735
set cstddef
736+
set cstdint
734737
set cstdlib
735738
set functional
736739
set initializer_list

0 commit comments

Comments
 (0)