xref: /freebsd/contrib/llvm-project/libcxx/include/__memory/array_cookie.h (revision e64bea71c21eb42e97aa615188ba91f6cce0d36d)
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef _LIBCPP___MEMORY_ARRAY_COOKIE_H
11 #define _LIBCPP___MEMORY_ARRAY_COOKIE_H
12 
13 #include <__config>
14 #include <__configuration/abi.h>
15 #include <__cstddef/size_t.h>
16 #include <__memory/addressof.h>
17 #include <__type_traits/integral_constant.h>
18 #include <__type_traits/is_trivially_destructible.h>
19 #include <__type_traits/negation.h>
20 
21 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22 #  pragma GCC system_header
23 #endif
24 
25 _LIBCPP_BEGIN_NAMESPACE_STD
26 
27 // Trait representing whether a type requires an array cookie at the start of its allocation when
28 // allocated as `new T[n]` and deallocated as `delete[] array`.
29 //
30 // Under the Itanium C++ ABI [1] and the ARM ABI which derives from it, we know that an array cookie is available
31 // unless `T` is trivially destructible and the call to `operator delete[]` is not a sized operator delete. Under
32 // other ABIs, we assume there are no array cookies.
33 //
34 // [1]: https://itanium-cxx-abi.github.io/cxx-abi/abi.html#array-cookies
35 #if defined(_LIBCPP_ABI_ITANIUM) || defined(_LIBCPP_ABI_ITANIUM_WITH_ARM_DIFFERENCES)
36 // TODO: Use a builtin instead
37 // TODO: We should factor in the choice of the usual deallocation function in this determination:
38 //       a cookie may be available in more cases but we ignore those for now.
39 template <class _Tp>
40 struct __has_array_cookie : _Not<is_trivially_destructible<_Tp> > {};
41 #else
42 template <class _Tp>
43 struct __has_array_cookie : false_type {};
44 #endif
45 
46 struct __itanium_array_cookie {
47   size_t __element_count;
48 };
49 
50 template <class _Tp>
51 struct [[__gnu__::__aligned__(_LIBCPP_ALIGNOF(_Tp))]] __arm_array_cookie {
52   size_t __element_size;
53   size_t __element_count;
54 };
55 
56 // Return the element count in the array cookie located before the given pointer.
57 //
58 // In the Itanium ABI [1]
59 // ----------------------
60 // The element count is stored immediately before the first element of the array. If the preferred alignment
61 // of array elements (which is different from the ABI alignment) is more than that of size_t, additional
62 // padding bytes exist before the array cookie. Assuming array elements of size and alignment 16 bytes, that
63 // gives us the following layout:
64 //
65 // |ooooooooxxxxxxxxaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbccccccccccccccccdddddddddddddddd|
66 //  ^^^^^^^^        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
67 //     |    ^^^^^^^^                               |
68 //     |       |                              array elements
69 //  padding    |
70 //       element count
71 //
72 //
73 // In the Itanium ABI with ARM differences [2]
74 // -------------------------------------------
75 // The array cookie is stored at the very start of the allocation and it has the following form:
76 //
77 //    struct array_cookie {
78 //      std::size_t element_size; // element_size != 0
79 //      std::size_t element_count;
80 //    };
81 //
82 // Assuming elements of size and alignment 32 bytes, this gives us the following layout:
83 //
84 //  |xxxxxxxxXXXXXXXXooooooooooooooooaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb|
85 //   ^^^^^^^^        ^^^^^^^^^^^^^^^^
86 //      |    ^^^^^^^^        |       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
87 // element size  |        padding                                 |
88 //         element count                                     array elements
89 //
90 // We must be careful to take into account the alignment of the array cookie, which may result in padding
91 // bytes between the element count and the first element of the array. Note that for ARM, the compiler
92 // aligns the array cookie using the ABI alignment, not the preferred alignment of array elements.
93 //
94 // [1]: https://itanium-cxx-abi.github.io/cxx-abi/abi.html#array-cookies
95 // [2]: https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Handle-C++-differences
96 template <class _Tp>
97 // Avoid failures when -fsanitize-address-poison-custom-array-cookie is enabled
__get_array_cookie(_Tp const * __ptr)98 _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_SANITIZE("address") size_t __get_array_cookie([[__maybe_unused__]] _Tp const* __ptr) {
99   static_assert(
100       __has_array_cookie<_Tp>::value, "Trying to access the array cookie of a type that is not guaranteed to have one");
101 
102 #if defined(_LIBCPP_ABI_ITANIUM)
103   using _ArrayCookie = __itanium_array_cookie;
104 #elif defined(_LIBCPP_ABI_ITANIUM_WITH_ARM_DIFFERENCES)
105   using _ArrayCookie = __arm_array_cookie<_Tp>;
106 #else
107   static_assert(false, "The array cookie layout is unknown on this ABI");
108   struct _ArrayCookie { // dummy definition required to make the function parse
109     size_t element_count;
110   };
111 #endif
112 
113   char const* __array_cookie_start = reinterpret_cast<char const*>(__ptr) - sizeof(_ArrayCookie);
114   _ArrayCookie __cookie;
115   // This is necessary to avoid violating strict aliasing. It's valid because _ArrayCookie is an
116   // implicit lifetime type.
117   __builtin_memcpy(std::addressof(__cookie), __array_cookie_start, sizeof(_ArrayCookie));
118   return __cookie.__element_count;
119 }
120 
121 _LIBCPP_END_NAMESPACE_STD
122 
123 #endif // _LIBCPP___MEMORY_ARRAY_COOKIE_H
124