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