Skip to content

Commit

Permalink
[libc++] Fix std::copy and std::move for ranges with potentially over…
Browse files Browse the repository at this point in the history
…lapping 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
  • Loading branch information
philnik777 committed Jun 30, 2023
1 parent e8e0f32 commit c4e9872
Show file tree
Hide file tree
Showing 22 changed files with 448 additions and 383 deletions.
1 change: 1 addition & 0 deletions libcxx/include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,7 @@ set(files
__type_traits/conjunction.h
__type_traits/copy_cv.h
__type_traits/copy_cvref.h
__type_traits/datasizeof.h
__type_traits/decay.h
__type_traits/dependent_type.h
__type_traits/disjunction.h
Expand Down
33 changes: 4 additions & 29 deletions libcxx/include/__algorithm/copy_move_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <__config>
#include <__iterator/iterator_traits.h>
#include <__memory/pointer_traits.h>
#include <__string/constexpr_c_functions.h>
#include <__type_traits/enable_if.h>
#include <__type_traits/is_always_bitcastable.h>
#include <__type_traits/is_constant_evaluated.h>
Expand Down Expand Up @@ -61,7 +62,8 @@ template <class _In, class _Out>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 pair<_In*, _Out*>
__copy_trivial_impl(_In* __first, _In* __last, _Out* __result) {
const size_t __n = static_cast<size_t>(__last - __first);
::__builtin_memmove(__result, __first, __n * sizeof(_Out));

std::__constexpr_memmove(__result, __first, __element_count(__n));

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

::__builtin_memmove(__result, __first, __n * sizeof(_Out));
std::__constexpr_memmove(__result, __first, __element_count(__n));

return std::make_pair(__last, __result);
}
Expand Down Expand Up @@ -119,16 +121,6 @@ __unwrap_and_dispatch(_InIter __first, _Sent __last, _OutIter __out_first) {
return _Algorithm()(std::move(__first), std::move(__last), std::move(__out_first));
}

template <class _IterOps, class _InValue, class _OutIter, class = void>
struct __can_copy_without_conversion : false_type {};

template <class _IterOps, class _InValue, class _OutIter>
struct __can_copy_without_conversion<
_IterOps,
_InValue,
_OutIter,
__enable_if_t<is_same<_InValue, typename _IterOps::template __value_type<_OutIter> >::value> > : true_type {};

template <class _AlgPolicy,
class _NaiveAlgorithm,
class _OptimizedAlgorithm,
Expand All @@ -137,23 +129,6 @@ template <class _AlgPolicy,
class _OutIter>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 pair<_InIter, _OutIter>
__dispatch_copy_or_move(_InIter __first, _Sent __last, _OutIter __out_first) {
#ifdef _LIBCPP_COMPILER_GCC
// GCC doesn't support `__builtin_memmove` during constant evaluation.
if (__libcpp_is_constant_evaluated()) {
return std::__unwrap_and_dispatch<_NaiveAlgorithm>(std::move(__first), std::move(__last), std::move(__out_first));
}
#else
// In Clang, `__builtin_memmove` only supports fully trivially copyable types (just having trivial copy assignment is
// insufficient). Also, conversions are not supported.
if (__libcpp_is_constant_evaluated()) {
using _InValue = typename _IterOps<_AlgPolicy>::template __value_type<_InIter>;
if (!is_trivially_copyable<_InValue>::value ||
!__can_copy_without_conversion<_IterOps<_AlgPolicy>, _InValue, _OutIter>::value) {
return std::__unwrap_and_dispatch<_NaiveAlgorithm>(std::move(__first), std::move(__last), std::move(__out_first));
}
}
#endif // _LIBCPP_COMPILER_GCC

using _Algorithm = __overload<_NaiveAlgorithm, _OptimizedAlgorithm>;
return std::__unwrap_and_dispatch<_Algorithm>(std::move(__first), std::move(__last), std::move(__out_first));
}
Expand Down
29 changes: 5 additions & 24 deletions libcxx/include/__string/char_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,25 +170,6 @@ struct _LIBCPP_DEPRECATED_("char_traits<T> for T not equal to char, wchar_t, cha
{return int_type(EOF);}
};

template <class _CharT>
_LIBCPP_HIDE_FROM_ABI static inline _LIBCPP_CONSTEXPR_SINCE_CXX20
_CharT* __char_traits_move(_CharT* __dest, const _CharT* __source, size_t __n) _NOEXCEPT
{
#ifdef _LIBCPP_COMPILER_GCC
if (__libcpp_is_constant_evaluated()) {
if (__n == 0)
return __dest;
_CharT* __allocation = new _CharT[__n];
std::copy_n(__source, __n, __allocation);
std::copy_n(static_cast<const _CharT*>(__allocation), __n, __dest);
delete[] __allocation;
return __dest;
}
#endif
::__builtin_memmove(__dest, __source, __n * sizeof(_CharT));
return __dest;
}

// char_traits<char>

template <>
Expand Down Expand Up @@ -249,7 +230,7 @@ struct _LIBCPP_TEMPLATE_VIS char_traits<char>

static inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
char_type* move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT {
return std::__char_traits_move(__s1, __s2, __n);
return std::__constexpr_memmove(__s1, __s2, __element_count(__n));
}

static inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Expand Down Expand Up @@ -320,7 +301,7 @@ struct _LIBCPP_TEMPLATE_VIS char_traits<wchar_t>

static inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
char_type* move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT {
return std::__char_traits_move(__s1, __s2, __n);
return std::__constexpr_memmove(__s1, __s2, __element_count(__n));
}

static inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Expand Down Expand Up @@ -384,7 +365,7 @@ struct _LIBCPP_TEMPLATE_VIS char_traits<char8_t>

static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
char_type* move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT {
return std::__char_traits_move(__s1, __s2, __n);
return std::__constexpr_memmove(__s1, __s2, __element_count(__n));
}

static _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20
Expand Down Expand Up @@ -468,7 +449,7 @@ struct _LIBCPP_TEMPLATE_VIS char_traits<char16_t>

_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
static char_type* move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT {
return std::__char_traits_move(__s1, __s2, __n);
return std::__constexpr_memmove(__s1, __s2, __element_count(__n));
}

_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
Expand Down Expand Up @@ -562,7 +543,7 @@ struct _LIBCPP_TEMPLATE_VIS char_traits<char32_t>

_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
static char_type* move(char_type* __s1, const char_type* __s2, size_t __n) _NOEXCEPT {
return std::__char_traits_move(__s1, __s2, __n);
return std::__constexpr_memmove(__s1, __s2, __element_count(__n));
}

_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
Expand Down
28 changes: 28 additions & 0 deletions libcxx/include/__string/constexpr_c_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@
#define _LIBCPP___STRING_CONSTEXPR_C_FUNCTIONS_H

#include <__config>
#include <__type_traits/datasizeof.h>
#include <__type_traits/is_always_bitcastable.h>
#include <__type_traits/is_constant_evaluated.h>
#include <__type_traits/is_equality_comparable.h>
#include <__type_traits/is_same.h>
#include <__type_traits/is_trivially_copyable.h>
#include <__type_traits/is_trivially_lexicographically_comparable.h>
#include <__type_traits/remove_cv.h>
#include <__utility/is_pointer_in_range.h>
#include <cstddef>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Expand Down Expand Up @@ -129,6 +133,30 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp* __constexpr_memchr(_Tp*
}
}

template <class _Tp, class _Up, __enable_if_t<__is_always_bitcastable<_Up, _Tp>::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _Tp*
__constexpr_memmove(_Tp* __dest, _Up* __src, __element_count __n) {
size_t __count = static_cast<size_t>(__n);
if (__libcpp_is_constant_evaluated()) {
#ifdef _LIBCPP_COMPILER_CLANG_BASED
if (is_same<__remove_cv_t<_Tp>, __remove_cv_t<_Up> >::value) {
::__builtin_memmove(__dest, __src, __count * sizeof(_Tp));
return __dest;
}
#endif
if (std::__is_pointer_in_range(__src, __src + __count, __dest)) {
for (; __count > 0; --__count)
__dest[__count - 1] = __src[__count - 1];
} else {
for (size_t __i = 0; __i != __count; ++__i)
__dest[__i] = __src[__i];
}
} else if (__count > 0) {
::__builtin_memmove(__dest, __src, (__count - 1) * sizeof(_Tp) + __libcpp_datasizeof<_Tp>::value);
}
return __dest;
}

_LIBCPP_END_NAMESPACE_STD

#endif // _LIBCPP___STRING_CONSTEXPR_C_FUNCTIONS_H
55 changes: 55 additions & 0 deletions libcxx/include/__type_traits/datasizeof.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef _LIBCPP___TYPE_TRAITS_DATASIZEOF_H
#define _LIBCPP___TYPE_TRAITS_DATASIZEOF_H

#include <__config>
#include <__type_traits/is_class.h>
#include <__type_traits/is_final.h>
#include <cstddef>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
#endif

// This trait provides the size of a type excluding any tail padding.
//
// It is useful in contexts where performing an operation using the full size of the class (including padding) may
// have unintended side effects, such as overwriting a derived class' member when writing the tail padding of a class
// through a pointer-to-base.

_LIBCPP_BEGIN_NAMESPACE_STD

template <class _Tp>
struct __libcpp_datasizeof {
#if __has_cpp_attribute(__no_unique_address__)
template <class = char>
struct _FirstPaddingByte {
[[__no_unique_address__]] _Tp __v_;
char __first_padding_byte_;
};
#else
template <bool = __libcpp_is_final<_Tp>::value || !is_class<_Tp>::value>
struct _FirstPaddingByte : _Tp {
char __first_padding_byte_;
};

template <>
struct _FirstPaddingByte<true> {
_Tp __v_;
char __first_padding_byte_;
};
#endif

static const size_t value = offsetof(_FirstPaddingByte<>, __first_padding_byte_);
};

_LIBCPP_END_NAMESPACE_STD

#endif // _LIBCPP___TYPE_TRAITS_DATASIZEOF_H
24 changes: 17 additions & 7 deletions libcxx/include/__utility/is_pointer_in_range.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
#include <__type_traits/enable_if.h>
#include <__type_traits/integral_constant.h>
#include <__type_traits/is_constant_evaluated.h>
#include <__type_traits/is_function.h>
#include <__type_traits/is_member_pointer.h>
#include <__type_traits/void_t.h>
#include <__utility/declval.h>

Expand All @@ -26,14 +24,16 @@

_LIBCPP_BEGIN_NAMESPACE_STD

template <class _Tp, class _Up, class = void>
struct __is_less_than_comparable : false_type {};

template <class _Tp, class _Up>
struct __is_less_than_comparable<_Tp, _Up, __void_t<decltype(std::declval<_Tp>() < std::declval<_Up>())> > : true_type {
};

template <class _Tp, class _Up, __enable_if_t<__is_less_than_comparable<const _Tp*, const _Up*>::value, int> = 0>
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_SANITIZE("address") bool __is_pointer_in_range(
const _Tp* __begin, const _Tp* __end, const _Up* __ptr) {
static_assert(!is_function<_Tp>::value && !is_function<_Up>::value,
"__is_pointer_in_range should not be called with function pointers");
static_assert(!is_member_pointer<_Tp>::value && !is_member_pointer<_Up>::value,
"__is_pointer_in_range should not be called with member pointers");

if (__libcpp_is_constant_evaluated()) {
_LIBCPP_ASSERT_UNCATEGORIZED(__builtin_constant_p(__begin <= __end), "__begin and __end do not form a range");

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

template <class _Tp, class _Up, __enable_if_t<!__is_less_than_comparable<const _Tp*, const _Up*>::value, int> = 0>
_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_SANITIZE("address") bool __is_pointer_in_range(
const _Tp* __begin, const _Tp* __end, const _Up* __ptr) {
if (__libcpp_is_constant_evaluated())
return false;

return reinterpret_cast<const char*>(__begin) <= reinterpret_cast<const char*>(__ptr) &&
reinterpret_cast<const char*>(__ptr) < reinterpret_cast<const char*>(__end);
}

_LIBCPP_END_NAMESPACE_STD

#endif // _LIBCPP___UTILITY_IS_POINTER_IN_RANGE_H
1 change: 1 addition & 0 deletions libcxx/include/module.modulemap.in
Original file line number Diff line number Diff line change
Expand Up @@ -1576,6 +1576,7 @@ module std [system] {
module conjunction { private header "__type_traits/conjunction.h" }
module copy_cv { private header "__type_traits/copy_cv.h" }
module copy_cvref { private header "__type_traits/copy_cvref.h" }
module datasizeof { private header "__type_traits/datasizeof.h" }
module decay { private header "__type_traits/decay.h" }
module dependent_type { private header "__type_traits/dependent_type.h" }
module disjunction { private header "__type_traits/disjunction.h" }
Expand Down
23 changes: 1 addition & 22 deletions libcxx/include/string
Original file line number Diff line number Diff line change
Expand Up @@ -663,13 +663,6 @@ struct __can_be_converted_to_string_view : public _BoolConstant<

struct __uninitialized_size_tag {};

template <class _Tp, class _Up, class = void>
struct __is_less_than_comparable : false_type {};

template <class _Tp, class _Up>
struct __is_less_than_comparable<_Tp, _Up, __void_t<decltype(std::declval<_Tp>() < std::declval<_Up>())> > : true_type {
};

template<class _CharT, class _Traits, class _Allocator>
class basic_string
{
Expand Down Expand Up @@ -1910,25 +1903,11 @@ private:
return *this;
}

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

template <
class _Tp,
__enable_if_t<!__is_less_than_comparable<const __remove_cvref_t<_Tp>*, const value_type*>::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __addr_in_range(const _Tp& __v) const {
if (__libcpp_is_constant_evaluated())
return false;

auto __t_ptr = reinterpret_cast<const char*>(std::addressof(__v));
return reinterpret_cast<const char*>(data()) <= __t_ptr &&
__t_ptr < reinterpret_cast<const char*>(data() + size() + 1);
}

_LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI
void __throw_length_error() const {
std::__throw_length_error("basic_string");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,26 +156,6 @@ void test_one(Func func) {
}
}));
}

// Empty input sequence.
{
const std::size_t N = 0;

From input[1] = {make<From>(1)};
To output[1] = {make<To>(2)};

auto in = InIter(input);
auto in_end = InIter(input + N);
auto sent = SentWrapper<decltype(in_end)>(in_end);
auto out = OutIter(output);

assert(!memmove_called);
func(in, sent, out, N);

assert(memmove_called);
memmove_called = false;
assert(output[0] == make<To>(2));
}
}

template <class InIter, template <class> class SentWrapper, class OutIter>
Expand Down
3 changes: 3 additions & 0 deletions libcxx/test/libcxx/transitive_includes/cxx03.csv
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ array algorithm
array compare
array concepts
array cstddef
array cstdint
array cstdlib
array initializer_list
array iterator
Expand Down Expand Up @@ -506,6 +507,7 @@ locale version
map compare
map concepts
map cstddef
map cstdint
map cstdlib
map functional
map initializer_list
Expand Down Expand Up @@ -731,6 +733,7 @@ semaphore version
set compare
set concepts
set cstddef
set cstdint
set cstdlib
set functional
set initializer_list
Expand Down

0 comments on commit c4e9872

Please sign in to comment.