| 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___MEMORY_VALID_RANGE_H |
| 10 | #define _LIBCPP___MEMORY_VALID_RANGE_H |
| 11 | |
| 12 | #include <__algorithm/comp.h> |
| 13 | #include <__assert> |
| 14 | #include <__config> |
| 15 | #include <__iterator/iterator_traits.h> |
| 16 | #include <__memory/assume_aligned.h> |
| 17 | #include <__memory/pointer_traits.h> |
| 18 | #include <__type_traits/is_constant_evaluated.h> |
| 19 | #include <__type_traits/is_same.h> |
| 20 | #include <__type_traits/remove_cvref.h> |
| 21 | |
| 22 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 23 | # pragma GCC system_header |
| 24 | #endif |
| 25 | |
| 26 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 27 | |
| 28 | // A valid range as defined by the C++ Standard has the following constraints: |
| 29 | // - [__first, __last) is dereferenceable |
| 30 | // - __last is reachable from __first |
| 31 | // - if __first and __last are contiguous iterators, the pointers they "decay to" are correctly aligned according to the |
| 32 | // language rules for pointers |
| 33 | |
| 34 | // This function attempts to detect invalid ranges as defined above. Specifically, it checks bullet (2). This also means |
| 35 | // that it doesn't return whether a range is actually valid, but only whether a range is definitely not valid. |
| 36 | // The checks may be extended in the future. |
| 37 | template <class _Tp> |
| 38 | _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_SANITIZE("address" ) bool |
| 39 | __is_valid_range(const _Tp* __first, const _Tp* __last) { |
| 40 | if (__libcpp_is_constant_evaluated()) { |
| 41 | // If this is not a constant during constant evaluation, that is because __first and __last are not |
| 42 | // part of the same allocation. If they are part of the same allocation, we must still make sure they |
| 43 | // are ordered properly. |
| 44 | return __builtin_constant_p(__first <= __last) && __first <= __last; |
| 45 | } |
| 46 | |
| 47 | return !__less<>()(__last, __first); |
| 48 | } |
| 49 | |
| 50 | // This function allows the compiler to assume that [__first, __last) is a valid range as defined above. |
| 51 | // |
| 52 | // In practice, we only add explicit assumptions for bullets (1) and (3). These assumptions allow (currently only |
| 53 | // clang-based compilers) to auto-vectorize algorithms that contain early returns. |
| 54 | template <class _Iter, class _Sent> |
| 55 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void |
| 56 | __assume_valid_range([[__maybe_unused__]] _Iter&& __first, [[__maybe_unused__]] _Sent&& __last) { |
| 57 | #if defined(_LIBCPP_CLANG_VER) && _LIBCPP_CLANG_VER >= 2300 && !defined(_LIBCPP_CXX03_LANG) |
| 58 | if constexpr (__libcpp_is_contiguous_iterator<__remove_cvref_t<_Iter>>::value && |
| 59 | is_same<__remove_cvref_t<_Iter>, __remove_cvref_t<_Sent>>::value) { |
| 60 | _LIBCPP_ASSERT_INTERNAL(std::__is_valid_range(std::__to_address(__first), std::__to_address(__last)), |
| 61 | "Valid range assumption does not hold" ); |
| 62 | if (!__libcpp_is_constant_evaluated()) { |
| 63 | using __value_type = typename iterator_traits<__remove_cvref_t<_Iter>>::value_type; |
| 64 | __builtin_assume_dereferenceable(std::__to_address(__first), (__last - __first) * sizeof(__value_type)); |
| 65 | (void)std::__assume_aligned<_LIBCPP_ALIGNOF(__value_type)>(std::__to_address(__first)); |
| 66 | (void)std::__assume_aligned<_LIBCPP_ALIGNOF(__value_type)>(std::__to_address(__last)); |
| 67 | } |
| 68 | } |
| 69 | #endif |
| 70 | } |
| 71 | |
| 72 | _LIBCPP_END_NAMESPACE_STD |
| 73 | |
| 74 | #endif // _LIBCPP___MEMORY_VALID_RANGE_H |
| 75 | |