Path: blob/main/contrib/llvm-project/libcxx/include/__memory/array_cookie.h
213766 views
// -*- C++ -*-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-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP___MEMORY_ARRAY_COOKIE_H10#define _LIBCPP___MEMORY_ARRAY_COOKIE_H1112#include <__config>13#include <__configuration/abi.h>14#include <__cstddef/size_t.h>15#include <__memory/addressof.h>16#include <__type_traits/integral_constant.h>17#include <__type_traits/is_trivially_destructible.h>18#include <__type_traits/negation.h>1920#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)21# pragma GCC system_header22#endif2324_LIBCPP_BEGIN_NAMESPACE_STD2526// Trait representing whether a type requires an array cookie at the start of its allocation when27// allocated as `new T[n]` and deallocated as `delete[] array`.28//29// Under the Itanium C++ ABI [1] and the ARM ABI which derives from it, we know that an array cookie is available30// unless `T` is trivially destructible and the call to `operator delete[]` is not a sized operator delete. Under31// other ABIs, we assume there are no array cookies.32//33// [1]: https://itanium-cxx-abi.github.io/cxx-abi/abi.html#array-cookies34#if defined(_LIBCPP_ABI_ITANIUM) || defined(_LIBCPP_ABI_ITANIUM_WITH_ARM_DIFFERENCES)35// TODO: Use a builtin instead36// TODO: We should factor in the choice of the usual deallocation function in this determination:37// a cookie may be available in more cases but we ignore those for now.38template <class _Tp>39struct __has_array_cookie : _Not<is_trivially_destructible<_Tp> > {};40#else41template <class _Tp>42struct __has_array_cookie : false_type {};43#endif4445struct __itanium_array_cookie {46size_t __element_count;47};4849template <class _Tp>50struct [[__gnu__::__aligned__(_LIBCPP_ALIGNOF(_Tp))]] __arm_array_cookie {51size_t __element_size;52size_t __element_count;53};5455// Return the element count in the array cookie located before the given pointer.56//57// In the Itanium ABI [1]58// ----------------------59// The element count is stored immediately before the first element of the array. If the preferred alignment60// of array elements (which is different from the ABI alignment) is more than that of size_t, additional61// padding bytes exist before the array cookie. Assuming array elements of size and alignment 16 bytes, that62// gives us the following layout:63//64// |ooooooooxxxxxxxxaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbccccccccccccccccdddddddddddddddd|65// ^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^66// | ^^^^^^^^ |67// | | array elements68// padding |69// element count70//71//72// In the Itanium ABI with ARM differences [2]73// -------------------------------------------74// The array cookie is stored at the very start of the allocation and it has the following form:75//76// struct array_cookie {77// std::size_t element_size; // element_size != 078// std::size_t element_count;79// };80//81// Assuming elements of size and alignment 32 bytes, this gives us the following layout:82//83// |xxxxxxxxXXXXXXXXooooooooooooooooaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb|84// ^^^^^^^^ ^^^^^^^^^^^^^^^^85// | ^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^86// element size | padding |87// element count array elements88//89// We must be careful to take into account the alignment of the array cookie, which may result in padding90// bytes between the element count and the first element of the array. Note that for ARM, the compiler91// aligns the array cookie using the ABI alignment, not the preferred alignment of array elements.92//93// [1]: https://itanium-cxx-abi.github.io/cxx-abi/abi.html#array-cookies94// [2]: https://developer.apple.com/documentation/xcode/writing-arm64-code-for-apple-platforms#Handle-C++-differences95template <class _Tp>96// Avoid failures when -fsanitize-address-poison-custom-array-cookie is enabled97_LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_SANITIZE("address") size_t __get_array_cookie([[__maybe_unused__]] _Tp const* __ptr) {98static_assert(99__has_array_cookie<_Tp>::value, "Trying to access the array cookie of a type that is not guaranteed to have one");100101#if defined(_LIBCPP_ABI_ITANIUM)102using _ArrayCookie = __itanium_array_cookie;103#elif defined(_LIBCPP_ABI_ITANIUM_WITH_ARM_DIFFERENCES)104using _ArrayCookie = __arm_array_cookie<_Tp>;105#else106static_assert(false, "The array cookie layout is unknown on this ABI");107struct _ArrayCookie { // dummy definition required to make the function parse108size_t element_count;109};110#endif111112char const* __array_cookie_start = reinterpret_cast<char const*>(__ptr) - sizeof(_ArrayCookie);113_ArrayCookie __cookie;114// This is necessary to avoid violating strict aliasing. It's valid because _ArrayCookie is an115// implicit lifetime type.116__builtin_memcpy(std::addressof(__cookie), __array_cookie_start, sizeof(_ArrayCookie));117return __cookie.__element_count;118}119120_LIBCPP_END_NAMESPACE_STD121122#endif // _LIBCPP___MEMORY_ARRAY_COOKIE_H123124125