1*8d741a5dSApple OSS Distributions // 2*8d741a5dSApple OSS Distributions // Copyright (c) 2019 Apple, Inc. All rights reserved. 3*8d741a5dSApple OSS Distributions // 4*8d741a5dSApple OSS Distributions // @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5*8d741a5dSApple OSS Distributions // 6*8d741a5dSApple OSS Distributions // This file contains Original Code and/or Modifications of Original Code 7*8d741a5dSApple OSS Distributions // as defined in and that are subject to the Apple Public Source License 8*8d741a5dSApple OSS Distributions // Version 2.0 (the 'License'). You may not use this file except in 9*8d741a5dSApple OSS Distributions // compliance with the License. The rights granted to you under the License 10*8d741a5dSApple OSS Distributions // may not be used to create, or enable the creation or redistribution of, 11*8d741a5dSApple OSS Distributions // unlawful or unlicensed copies of an Apple operating system, or to 12*8d741a5dSApple OSS Distributions // circumvent, violate, or enable the circumvention or violation of, any 13*8d741a5dSApple OSS Distributions // terms of an Apple operating system software license agreement. 14*8d741a5dSApple OSS Distributions // 15*8d741a5dSApple OSS Distributions // Please obtain a copy of the License at 16*8d741a5dSApple OSS Distributions // http://www.opensource.apple.com/apsl/ and read it before using this file. 17*8d741a5dSApple OSS Distributions // 18*8d741a5dSApple OSS Distributions // The Original Code and all software distributed under the License are 19*8d741a5dSApple OSS Distributions // distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20*8d741a5dSApple OSS Distributions // EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21*8d741a5dSApple OSS Distributions // INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22*8d741a5dSApple OSS Distributions // FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23*8d741a5dSApple OSS Distributions // Please see the License for the specific language governing rights and 24*8d741a5dSApple OSS Distributions // limitations under the License. 25*8d741a5dSApple OSS Distributions // 26*8d741a5dSApple OSS Distributions // @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27*8d741a5dSApple OSS Distributions // 28*8d741a5dSApple OSS Distributions 29*8d741a5dSApple OSS Distributions #ifndef XNU_LIBKERN_LIBKERN_CXX_BOUNDED_PTR_H 30*8d741a5dSApple OSS Distributions #define XNU_LIBKERN_LIBKERN_CXX_BOUNDED_PTR_H 31*8d741a5dSApple OSS Distributions 32*8d741a5dSApple OSS Distributions #if !TAPI 33*8d741a5dSApple OSS Distributions 34*8d741a5dSApple OSS Distributions #include <stddef.h> 35*8d741a5dSApple OSS Distributions #include <stdint.h> 36*8d741a5dSApple OSS Distributions #include <os/overflow.h> 37*8d741a5dSApple OSS Distributions #include <os/base.h> 38*8d741a5dSApple OSS Distributions 39*8d741a5dSApple OSS Distributions #if !defined(__improbable) 40*8d741a5dSApple OSS Distributions # define __improbable(...) __builtin_expect((__VA_ARGS__), 0) 41*8d741a5dSApple OSS Distributions #endif 42*8d741a5dSApple OSS Distributions 43*8d741a5dSApple OSS Distributions namespace libkern { 44*8d741a5dSApple OSS Distributions namespace detail { 45*8d741a5dSApple OSS Distributions // Reimplementation of things in <type_traits> because we don't seem 46*8d741a5dSApple OSS Distributions // to have the right to rely on the C++ Standard Library (based on 47*8d741a5dSApple OSS Distributions // attempts to compile IOHIDFamily). 48*8d741a5dSApple OSS Distributions // TODO: Do we really need to re-implement this here? 49*8d741a5dSApple OSS Distributions template <typename ...> using void_t = void; 50*8d741a5dSApple OSS Distributions template <typename T> T && declval() noexcept; 51*8d741a5dSApple OSS Distributions using nullptr_t = decltype(nullptr); 52*8d741a5dSApple OSS Distributions template <bool Cond, typename T = void> struct enable_if; 53*8d741a5dSApple OSS Distributions template <typename T> struct enable_if<true, T> { using type = T; }; 54*8d741a5dSApple OSS Distributions template <bool Cond, typename T = void> using enable_if_t = typename enable_if<Cond, T>::type; 55*8d741a5dSApple OSS Distributions template <typename T1, typename T2> 56*8d741a5dSApple OSS Distributions constexpr bool is_convertible_v = __is_convertible_to(T1, T2); 57*8d741a5dSApple OSS Distributions 58*8d741a5dSApple OSS Distributions template <typename T> inline constexpr bool is_void_v = false; 59*8d741a5dSApple OSS Distributions template <> inline constexpr bool is_void_v<void> = true; 60*8d741a5dSApple OSS Distributions template <> inline constexpr bool is_void_v<void const> = true; 61*8d741a5dSApple OSS Distributions 62*8d741a5dSApple OSS Distributions template <typename T, typename U> struct copy_const { using type = U; }; 63*8d741a5dSApple OSS Distributions template <typename T, typename U> struct copy_const<T const, U> { using type = U const; }; 64*8d741a5dSApple OSS Distributions template <typename T, typename U> using copy_const_t = typename copy_const<T, U>::type; 65*8d741a5dSApple OSS Distributions 66*8d741a5dSApple OSS Distributions template <typename T, typename U> struct copy_cv { using type = U; }; 67*8d741a5dSApple OSS Distributions template <typename T, typename U> struct copy_cv<T const, U> { using type = U const; }; 68*8d741a5dSApple OSS Distributions template <typename T, typename U> struct copy_cv<T volatile, U> { using type = U volatile; }; 69*8d741a5dSApple OSS Distributions template <typename T, typename U> struct copy_cv<T const volatile, U> { using type = U const volatile; }; 70*8d741a5dSApple OSS Distributions template <typename T, typename U> using copy_cv_t = typename copy_cv<T, U>::type; 71*8d741a5dSApple OSS Distributions 72*8d741a5dSApple OSS Distributions template <typename T, typename U> 73*8d741a5dSApple OSS Distributions using WhenComparable = void_t< 74*8d741a5dSApple OSS Distributions decltype(declval<T>() == declval<U>()), 75*8d741a5dSApple OSS Distributions decltype(declval<T>() != declval<U>()) 76*8d741a5dSApple OSS Distributions >; 77*8d741a5dSApple OSS Distributions 78*8d741a5dSApple OSS Distributions template <typename T, typename U> 79*8d741a5dSApple OSS Distributions using WhenOrderable = void_t < 80*8d741a5dSApple OSS Distributions decltype(declval<T>() < declval<U>()), 81*8d741a5dSApple OSS Distributions decltype(declval<T>() > declval<U>()), 82*8d741a5dSApple OSS Distributions decltype(declval<T>() >= declval<U>()), 83*8d741a5dSApple OSS Distributions decltype(declval<T>() <= declval<U>()) 84*8d741a5dSApple OSS Distributions >; 85*8d741a5dSApple OSS Distributions 86*8d741a5dSApple OSS Distributions // Pretend that sizeof(void) is 1, otherwise the in-bounds check doesn't 87*8d741a5dSApple OSS Distributions // make sense for `bounded_ptr<void>`. 88*8d741a5dSApple OSS Distributions template <typename T> constexpr size_t sizeof_v = sizeof(T); 89*8d741a5dSApple OSS Distributions template <> inline constexpr size_t sizeof_v<void> = 1; 90*8d741a5dSApple OSS Distributions template <> inline constexpr size_t sizeof_v<void const> = 1; 91*8d741a5dSApple OSS Distributions template <> inline constexpr size_t sizeof_v<void volatile> = 1; 92*8d741a5dSApple OSS Distributions template <> inline constexpr size_t sizeof_v<void const volatile> = 1; 93*8d741a5dSApple OSS Distributions } // end namespace detail 94*8d741a5dSApple OSS Distributions 95*8d741a5dSApple OSS Distributions // Non-owning pointer to an object (or a range of objects) of type `T` 96*8d741a5dSApple OSS Distributions // that validates that the address is within some specified bounds on 97*8d741a5dSApple OSS Distributions // dereference-like operations. 98*8d741a5dSApple OSS Distributions // 99*8d741a5dSApple OSS Distributions // Conceptually, a `bounded_ptr` points within a range of memory `[begin, end)`. 100*8d741a5dSApple OSS Distributions // If accessing any part of the result of dereferencing the pointer would 101*8d741a5dSApple OSS Distributions // lead to an access outside of the `[begin, end)` range, the pointer is 102*8d741a5dSApple OSS Distributions // said to be out-of-bounds. Due to representational constraints, the range 103*8d741a5dSApple OSS Distributions // of in-bounds memory must be no larger than 4GB. 104*8d741a5dSApple OSS Distributions // 105*8d741a5dSApple OSS Distributions // Dereference-like operations (dereference, subscript, pointer member access) 106*8d741a5dSApple OSS Distributions // validate that the pointer is not out-of-bounds. If an out-of-bounds pointer 107*8d741a5dSApple OSS Distributions // is dereferenced, the `TrappingPolicy` is called as 108*8d741a5dSApple OSS Distributions // `TrappingPolicy::trap(some-message)`, and the operation is said to "trap". 109*8d741a5dSApple OSS Distributions // This terminology is used below to describe the behavior of the `TrappingPolicy`. 110*8d741a5dSApple OSS Distributions // 111*8d741a5dSApple OSS Distributions // Pointer arithmetic is allowed (and the bounds are not validated), so it is 112*8d741a5dSApple OSS Distributions // entirely possible to make a `bounded_ptr` point outside of its range. 113*8d741a5dSApple OSS Distributions // However, overflow checking is performed on arithmetic operations, and 114*8d741a5dSApple OSS Distributions // any operation resulting in an overflow will also "trap". 115*8d741a5dSApple OSS Distributions // 116*8d741a5dSApple OSS Distributions // The behavior of the `TrappingPolicy` can be customized as desired, however 117*8d741a5dSApple OSS Distributions // a trap should never return, causing the current `bounded_ptr` operation to 118*8d741a5dSApple OSS Distributions // be aborted. This is important since the trap could signify an integer 119*8d741a5dSApple OSS Distributions // overflow, a null-pointer dereference or something else that would lead to 120*8d741a5dSApple OSS Distributions // undefined behavior (UB) if `TrappingPolicy::trap` were to return. 121*8d741a5dSApple OSS Distributions // 122*8d741a5dSApple OSS Distributions // Creation of `bounded_ptr`s 123*8d741a5dSApple OSS Distributions // ========================== 124*8d741a5dSApple OSS Distributions // `bounded_ptr` provides a single constructor allowing the bounds of the 125*8d741a5dSApple OSS Distributions // pointer to be specified. When integrating `bounded_ptr` into an existing 126*8d741a5dSApple OSS Distributions // code base, it is recommended to use `bounded_ptr` as an iterator obtained 127*8d741a5dSApple OSS Distributions // from other container-like abstractions, instead of manually using the 128*8d741a5dSApple OSS Distributions // constructor that allows specifying a range. Specifying the range manually 129*8d741a5dSApple OSS Distributions // on construction is error-prone, and `bounded_ptr` can't help reduce 130*8d741a5dSApple OSS Distributions // out-of-bounds accesses if the bounds are specified incorrectly. 131*8d741a5dSApple OSS Distributions // 132*8d741a5dSApple OSS Distributions // Furthermore, it is a design choice to not provide a constructor that uses 133*8d741a5dSApple OSS Distributions // relative offsets from the pointer itself to determine the range, because 134*8d741a5dSApple OSS Distributions // such a constructor is deemed more confusing than helpful. For example, is 135*8d741a5dSApple OSS Distributions // the offset a number of bytes or a number of objects? Is the offset inclusive 136*8d741a5dSApple OSS Distributions // or exclusive? Instead, factory functions should be used to create `bounded_ptr`s. 137*8d741a5dSApple OSS Distributions // 138*8d741a5dSApple OSS Distributions // Remark on const-ness 139*8d741a5dSApple OSS Distributions // ==================== 140*8d741a5dSApple OSS Distributions // Like for raw pointers, the const-ness of a `bounded_ptr` has no bearing on 141*8d741a5dSApple OSS Distributions // whether the pointee is const. Hence, it is possible to obtain a non-const 142*8d741a5dSApple OSS Distributions // reference to an object from a const `bounded_ptr`. To encode a 143*8d741a5dSApple OSS Distributions // pointer-to-const, simply create a `bounded_ptr<T const>`. 144*8d741a5dSApple OSS Distributions template <typename T, typename TrappingPolicy> 145*8d741a5dSApple OSS Distributions struct __attribute__((trivial_abi)) bounded_ptr { 146*8d741a5dSApple OSS Distributions private: 147*8d741a5dSApple OSS Distributions using CharType = detail::copy_cv_t<T, char>; 148*8d741a5dSApple OSS Distributions 149*8d741a5dSApple OSS Distributions public: 150*8d741a5dSApple OSS Distributions // Creates a null `bounded_ptr`. 151*8d741a5dSApple OSS Distributions // 152*8d741a5dSApple OSS Distributions // A null `bounded_ptr` does not point to any object and is conceptually 153*8d741a5dSApple OSS Distributions // out of bounds, so dereferencing it will trap. "Observing" operations 154*8d741a5dSApple OSS Distributions // like comparison and check-for-null, along with assignment, are valid 155*8d741a5dSApple OSS Distributions // operations on a null `bounded_ptr`. 156*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE constexpr 157*8d741a5dSApple OSS Distributions bounded_ptr(detail::nullptr_t) 158*8d741a5dSApple OSS Distributions : base_(nullptr), count_(0), offset_(0) 159*8d741a5dSApple OSS Distributions { 160*8d741a5dSApple OSS Distributions } 161*8d741a5dSApple OSS Distributions 162*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE constexpr 163*8d741a5dSApple OSS Distributions explicit 164*8d741a5dSApple OSS Distributions bounded_ptr() 165*8d741a5dSApple OSS Distributions : bounded_ptr(nullptr) 166*8d741a5dSApple OSS Distributions { 167*8d741a5dSApple OSS Distributions } 168*8d741a5dSApple OSS Distributions 169*8d741a5dSApple OSS Distributions // Creates a `bounded_ptr` pointing to the given object, and whose bounds 170*8d741a5dSApple OSS Distributions // are described by the provided `[begin, end)` range. 171*8d741a5dSApple OSS Distributions // 172*8d741a5dSApple OSS Distributions // This constructor does not check whether the constructed pointer is 173*8d741a5dSApple OSS Distributions // within its bounds. However, it does check that the provided `[begin, end)` 174*8d741a5dSApple OSS Distributions // range is a valid range (that is, `begin <= end`). 175*8d741a5dSApple OSS Distributions // 176*8d741a5dSApple OSS Distributions // Furthermore, the number of bytes in the range of in-bounds memory must be 177*8d741a5dSApple OSS Distributions // representable by a uint32_t, which means that there can be no more than 178*8d741a5dSApple OSS Distributions // 2^32 bytes (i.e. 4GB) in that range. Otherwise, the constructor will trap. 179*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE explicit 180*8d741a5dSApple OSS Distributions bounded_ptr(T* pointer, T const* begin, T const* end) 181*8d741a5dSApple OSS Distributions { 182*8d741a5dSApple OSS Distributions base_ = reinterpret_cast<CharType*>(const_cast<T*>(begin)); 183*8d741a5dSApple OSS Distributions 184*8d741a5dSApple OSS Distributions // Store (end - begin) into count_, making sure we don't overflow 185*8d741a5dSApple OSS Distributions if (__improbable(os_sub_overflow(reinterpret_cast<uintptr_t>(end), 186*8d741a5dSApple OSS Distributions reinterpret_cast<uintptr_t>(begin), 187*8d741a5dSApple OSS Distributions &count_))) { 188*8d741a5dSApple OSS Distributions TrappingPolicy::trap("The range of valid memory is too large to be represented " 189*8d741a5dSApple OSS Distributions "by this type, or [begin, end) is not a well-formed range"); 190*8d741a5dSApple OSS Distributions } 191*8d741a5dSApple OSS Distributions 192*8d741a5dSApple OSS Distributions // Store (pointer - begin) into offset_, making sure we don't overflow. 193*8d741a5dSApple OSS Distributions // Note that offset_ can be negative if `pointer` is outside of the 194*8d741a5dSApple OSS Distributions // range delimited by [begin, end), which can be valid if it represents 195*8d741a5dSApple OSS Distributions // e.g. a subrange of an array. 196*8d741a5dSApple OSS Distributions if (__improbable(os_sub_overflow(reinterpret_cast<uintptr_t>(pointer), 197*8d741a5dSApple OSS Distributions reinterpret_cast<uintptr_t>(begin), 198*8d741a5dSApple OSS Distributions &offset_))) { 199*8d741a5dSApple OSS Distributions TrappingPolicy::trap("The offset of the pointer inside its valid memory " 200*8d741a5dSApple OSS Distributions "range can't be represented using int32_t"); 201*8d741a5dSApple OSS Distributions } 202*8d741a5dSApple OSS Distributions } 203*8d741a5dSApple OSS Distributions 204*8d741a5dSApple OSS Distributions // Creates a `bounded_ptr` to a type `T` from a `bounded_ptr` to a type `U`. 205*8d741a5dSApple OSS Distributions // 206*8d741a5dSApple OSS Distributions // This converting constructor is enabled whenever `U*` is implicitly 207*8d741a5dSApple OSS Distributions // convertible to `T*`. This allows the usual implicit conversions 208*8d741a5dSApple OSS Distributions // between base-and-derived types, and also from any type `U*` to a 209*8d741a5dSApple OSS Distributions // `void*`. If other casts (like between unrelated pointer types) are 210*8d741a5dSApple OSS Distributions // desired, `libkern::reinterpret_pointer_cast` can be used instead. 211*8d741a5dSApple OSS Distributions // 212*8d741a5dSApple OSS Distributions // The bounds on the resulting `bounded_ptr` are inherited from the 213*8d741a5dSApple OSS Distributions // original `bounded_ptr`. 214*8d741a5dSApple OSS Distributions template <typename U, typename Policy, typename = detail::enable_if_t<detail::is_convertible_v<U*, T*> > > 215*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE 216*8d741a5dSApple OSS Distributions bounded_ptr(bounded_ptr<U, Policy> const & other) 217*8d741a5dSApple OSS Distributions : base_(other.base_) 218*8d741a5dSApple OSS Distributions , count_(other.count_) 219*8d741a5dSApple OSS Distributions , offset_(static_cast<int32_t>(reinterpret_cast<CharType*>(static_cast<T*>(other.get_ptr_())) - other.base_)) 220*8d741a5dSApple OSS Distributions { 221*8d741a5dSApple OSS Distributions } 222*8d741a5dSApple OSS Distributions 223*8d741a5dSApple OSS Distributions // Assigns a `bounded_ptr` to a type `U` to a `bounded_ptr` to a type `T`, 224*8d741a5dSApple OSS Distributions // as long as `U*` is convertible to `T*`. 225*8d741a5dSApple OSS Distributions // 226*8d741a5dSApple OSS Distributions // This is a rebinding operation, like assignment between raw pointers, 227*8d741a5dSApple OSS Distributions // and the destination `bounded_ptr` will inherit the bounds of the 228*8d741a5dSApple OSS Distributions // source `bounded_ptr`. 229*8d741a5dSApple OSS Distributions template <typename U, typename Policy, typename = detail::enable_if_t<detail::is_convertible_v<U*, T*> > > 230*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE bounded_ptr& 231*8d741a5dSApple OSS Distributions operator=(bounded_ptr<U, Policy> const& other) 232*8d741a5dSApple OSS Distributions { 233*8d741a5dSApple OSS Distributions base_ = other.base_; 234*8d741a5dSApple OSS Distributions count_ = other.count_; 235*8d741a5dSApple OSS Distributions offset_ = static_cast<int32_t>(reinterpret_cast<CharType*>(static_cast<T*>(other.get_ptr_())) - other.base_); 236*8d741a5dSApple OSS Distributions return *this; 237*8d741a5dSApple OSS Distributions } 238*8d741a5dSApple OSS Distributions 239*8d741a5dSApple OSS Distributions // Sets a `bounded_ptr` to null. 240*8d741a5dSApple OSS Distributions // 241*8d741a5dSApple OSS Distributions // This is effectively equivalent to assigning a default-constructed 242*8d741a5dSApple OSS Distributions // `bounded_ptr` to the target. As a result, the original bounds of 243*8d741a5dSApple OSS Distributions // the `bounded_ptr` are discarded, and the resulting `bounded_ptr` 244*8d741a5dSApple OSS Distributions // is both out-of-bounds and also has no bounds assigned to it (like 245*8d741a5dSApple OSS Distributions // a default-constructed `bounded_ptr`). 246*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE bounded_ptr& 247*8d741a5dSApple OSS Distributions operator=(detail::nullptr_t) 248*8d741a5dSApple OSS Distributions { 249*8d741a5dSApple OSS Distributions *this = bounded_ptr(); 250*8d741a5dSApple OSS Distributions return *this; 251*8d741a5dSApple OSS Distributions } 252*8d741a5dSApple OSS Distributions 253*8d741a5dSApple OSS Distributions // Returns a reference to the object pointed-to by the `bounded_ptr`. 254*8d741a5dSApple OSS Distributions // 255*8d741a5dSApple OSS Distributions // Traps if the pointer is pointing outside of its bounds. 256*8d741a5dSApple OSS Distributions // 257*8d741a5dSApple OSS Distributions // Also note that this function will trap when dereferencing a null 258*8d741a5dSApple OSS Distributions // `bounded_ptr`, unless the bounds of the pointer have been set and 259*8d741a5dSApple OSS Distributions // include address 0, in which case there's effectively nothing to 260*8d741a5dSApple OSS Distributions // diagnose. 261*8d741a5dSApple OSS Distributions template <typename T_ = T> // delay instantiation to avoid forming invalid ref for bounded_ptr<void> 262*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE T_& 263*8d741a5dSApple OSS Distributions operator*() const 264*8d741a5dSApple OSS Distributions { 265*8d741a5dSApple OSS Distributions if (__improbable(!in_bounds_())) { 266*8d741a5dSApple OSS Distributions TrappingPolicy::trap("bounded_ptr<T>::operator*: Dereferencing this pointer " 267*8d741a5dSApple OSS Distributions "would access memory outside of the bounds set originally"); 268*8d741a5dSApple OSS Distributions } 269*8d741a5dSApple OSS Distributions return *get_ptr_(); 270*8d741a5dSApple OSS Distributions } 271*8d741a5dSApple OSS Distributions 272*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE T* 273*8d741a5dSApple OSS Distributions operator->() const 274*8d741a5dSApple OSS Distributions { 275*8d741a5dSApple OSS Distributions if (__improbable(!in_bounds_())) { 276*8d741a5dSApple OSS Distributions TrappingPolicy::trap("bounded_ptr<T>::operator->: Accessing a member through this pointer " 277*8d741a5dSApple OSS Distributions "would access memory outside of the bounds set originally"); 278*8d741a5dSApple OSS Distributions } 279*8d741a5dSApple OSS Distributions return get_ptr_(); 280*8d741a5dSApple OSS Distributions } 281*8d741a5dSApple OSS Distributions 282*8d741a5dSApple OSS Distributions // Provides access to the n-th element past the given pointer. 283*8d741a5dSApple OSS Distributions // 284*8d741a5dSApple OSS Distributions // The `bounded_ptr` validates whether the provided index is within the 285*8d741a5dSApple OSS Distributions // bounds of the `bounded_ptr`. Like for raw pointers, a negative index 286*8d741a5dSApple OSS Distributions // may be passed, in which case the pointer is accessed at a negative 287*8d741a5dSApple OSS Distributions // offset (which must still be in bounds). 288*8d741a5dSApple OSS Distributions template <typename T_ = T> // delay instantiation to avoid forming invalid ref for bounded_ptr<void> 289*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE T_& 290*8d741a5dSApple OSS Distributions operator[](ptrdiff_t n) const 291*8d741a5dSApple OSS Distributions { 292*8d741a5dSApple OSS Distributions return *(*this + n); 293*8d741a5dSApple OSS Distributions } 294*8d741a5dSApple OSS Distributions 295*8d741a5dSApple OSS Distributions // Converts a `bounded_ptr` to a raw pointer, after checking it is within 296*8d741a5dSApple OSS Distributions // its bounds. 297*8d741a5dSApple OSS Distributions // 298*8d741a5dSApple OSS Distributions // The primary intended usage of this function is to aid bridging between 299*8d741a5dSApple OSS Distributions // code that uses `bounded_ptr`s and code that does not. 300*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE T* 301*8d741a5dSApple OSS Distributions discard_bounds() const 302*8d741a5dSApple OSS Distributions { 303*8d741a5dSApple OSS Distributions if (__improbable(!in_bounds_())) { 304*8d741a5dSApple OSS Distributions TrappingPolicy::trap("bounded_ptr<T>::discard_bounds: Discarding the bounds on " 305*8d741a5dSApple OSS Distributions "this pointer would lose the fact that it is outside of the " 306*8d741a5dSApple OSS Distributions "bounds set originally"); 307*8d741a5dSApple OSS Distributions } 308*8d741a5dSApple OSS Distributions return get_ptr_(); 309*8d741a5dSApple OSS Distributions } 310*8d741a5dSApple OSS Distributions 311*8d741a5dSApple OSS Distributions // Converts a `bounded_ptr` to a raw pointer, without checking whether the 312*8d741a5dSApple OSS Distributions // pointer is within its bounds. 313*8d741a5dSApple OSS Distributions // 314*8d741a5dSApple OSS Distributions // Like `discard_bounds()`, the primary intended usage of this function 315*8d741a5dSApple OSS Distributions // is to aid bridging between code that uses `bounded_ptr`s and code that 316*8d741a5dSApple OSS Distributions // does not. However, unlike `discard_bounds()`, this function does not 317*8d741a5dSApple OSS Distributions // validate that the returned pointer is in bounds. This functionality is 318*8d741a5dSApple OSS Distributions // necessary when the pointer represents something that can't be 319*8d741a5dSApple OSS Distributions // dereferenced (hence it's OK for it to be out-of-bounds), but that 320*8d741a5dSApple OSS Distributions // is still useful for other purposes like comparing against other 321*8d741a5dSApple OSS Distributions // pointers. An example of that is the `end` pointer in a half-open 322*8d741a5dSApple OSS Distributions // interval `[begin, end)`, where the `end` pointer is out-of-bounds and 323*8d741a5dSApple OSS Distributions // can't be dereferenced, yet it's still useful to delimit the range. 324*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE T* 325*8d741a5dSApple OSS Distributions unsafe_discard_bounds() const 326*8d741a5dSApple OSS Distributions { 327*8d741a5dSApple OSS Distributions return get_ptr_(); 328*8d741a5dSApple OSS Distributions } 329*8d741a5dSApple OSS Distributions 330*8d741a5dSApple OSS Distributions // Implicit conversion to bool, returning whether the pointer is null. 331*8d741a5dSApple OSS Distributions // 332*8d741a5dSApple OSS Distributions // This operation does not perform any validation of the bounds. 333*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE explicit 334*8d741a5dSApple OSS Distributions operator bool() const 335*8d741a5dSApple OSS Distributions { 336*8d741a5dSApple OSS Distributions return get_ptr_() != nullptr; 337*8d741a5dSApple OSS Distributions } 338*8d741a5dSApple OSS Distributions 339*8d741a5dSApple OSS Distributions // Increment/decrement a `bounded_ptr`. 340*8d741a5dSApple OSS Distributions // 341*8d741a5dSApple OSS Distributions // Like for other arithmetic operations, this does not check whether the 342*8d741a5dSApple OSS Distributions // increment or decrement operation results in an out-of-bounds pointer. 343*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE bounded_ptr& 344*8d741a5dSApple OSS Distributions operator++() 345*8d741a5dSApple OSS Distributions { 346*8d741a5dSApple OSS Distributions *this += 1; 347*8d741a5dSApple OSS Distributions return *this; 348*8d741a5dSApple OSS Distributions } 349*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE bounded_ptr 350*8d741a5dSApple OSS Distributions operator++(int) 351*8d741a5dSApple OSS Distributions { 352*8d741a5dSApple OSS Distributions bounded_ptr old = *this; 353*8d741a5dSApple OSS Distributions ++*this; 354*8d741a5dSApple OSS Distributions return old; 355*8d741a5dSApple OSS Distributions } 356*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE bounded_ptr& 357*8d741a5dSApple OSS Distributions operator--() 358*8d741a5dSApple OSS Distributions { 359*8d741a5dSApple OSS Distributions *this -= 1; 360*8d741a5dSApple OSS Distributions return *this; 361*8d741a5dSApple OSS Distributions } 362*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE bounded_ptr 363*8d741a5dSApple OSS Distributions operator--(int) 364*8d741a5dSApple OSS Distributions { 365*8d741a5dSApple OSS Distributions bounded_ptr old = *this; 366*8d741a5dSApple OSS Distributions --*this; 367*8d741a5dSApple OSS Distributions return old; 368*8d741a5dSApple OSS Distributions } 369*8d741a5dSApple OSS Distributions 370*8d741a5dSApple OSS Distributions // Increment or decrement a `bounded_ptr` by a given offset. 371*8d741a5dSApple OSS Distributions // 372*8d741a5dSApple OSS Distributions // This is equivalent to adding the given offset to the underlying raw 373*8d741a5dSApple OSS Distributions // pointer. In particular, the bounds of the `bounded_ptr` are left 374*8d741a5dSApple OSS Distributions // untouched by this operation. Furthermore, like for raw pointers, it 375*8d741a5dSApple OSS Distributions // is possible to provide a negative offset, which will have the effect 376*8d741a5dSApple OSS Distributions // of decrementing the `bounded_ptr` instead of incrementing it. 377*8d741a5dSApple OSS Distributions // 378*8d741a5dSApple OSS Distributions // Also note that the offset is NOT a number of bytes -- just like for 379*8d741a5dSApple OSS Distributions // raw pointers, it is a number of "positions" to move the pointer from, 380*8d741a5dSApple OSS Distributions // which essentially means `n * sizeof(T)` bytes. Again, this works exactly 381*8d741a5dSApple OSS Distributions // the same as a raw pointer to an object of type `T`. 382*8d741a5dSApple OSS Distributions // 383*8d741a5dSApple OSS Distributions // Like other arithmetic operations, this does not check whether the 384*8d741a5dSApple OSS Distributions // increment or decrement operation results in an out-of-bounds pointer. 385*8d741a5dSApple OSS Distributions // However, this does check whether the arithmetic operation would result 386*8d741a5dSApple OSS Distributions // in an overflow, in which case the operation will trap. 387*8d741a5dSApple OSS Distributions template <typename T_ = T> 388*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE bounded_ptr& 389*8d741a5dSApple OSS Distributions operator+=(ptrdiff_t n) 390*8d741a5dSApple OSS Distributions { 391*8d741a5dSApple OSS Distributions static_assert(!detail::is_void_v<T_>, "Arithmetic on bounded_ptr<void> is not allowed."); 392*8d741a5dSApple OSS Distributions 393*8d741a5dSApple OSS Distributions ptrdiff_t bytes; 394*8d741a5dSApple OSS Distributions if (__improbable(os_mul_overflow(n, sizeof(T), &bytes))) { 395*8d741a5dSApple OSS Distributions TrappingPolicy::trap( 396*8d741a5dSApple OSS Distributions "bounded_ptr<T>::operator+=(n): Calculating the number of bytes to " 397*8d741a5dSApple OSS Distributions "add to the offset (n * sizeof(T)) would trigger an overflow"); 398*8d741a5dSApple OSS Distributions } 399*8d741a5dSApple OSS Distributions if (__improbable(os_add_overflow(offset_, bytes, &offset_))) { 400*8d741a5dSApple OSS Distributions TrappingPolicy::trap( 401*8d741a5dSApple OSS Distributions "bounded_ptr<T>::operator+=(n): Adding the specified number of bytes " 402*8d741a5dSApple OSS Distributions "to the offset representing the current position would overflow."); 403*8d741a5dSApple OSS Distributions } 404*8d741a5dSApple OSS Distributions return *this; 405*8d741a5dSApple OSS Distributions } 406*8d741a5dSApple OSS Distributions 407*8d741a5dSApple OSS Distributions template <typename T_ = T> 408*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE bounded_ptr& 409*8d741a5dSApple OSS Distributions operator-=(ptrdiff_t n) 410*8d741a5dSApple OSS Distributions { 411*8d741a5dSApple OSS Distributions static_assert(!detail::is_void_v<T_>, "Arithmetic on bounded_ptr<void> is not allowed."); 412*8d741a5dSApple OSS Distributions 413*8d741a5dSApple OSS Distributions ptrdiff_t bytes; 414*8d741a5dSApple OSS Distributions if (__improbable(os_mul_overflow(n, sizeof(T), &bytes))) { 415*8d741a5dSApple OSS Distributions TrappingPolicy::trap( 416*8d741a5dSApple OSS Distributions "bounded_ptr<T>::operator-=(n): Calculating the number of bytes to " 417*8d741a5dSApple OSS Distributions "subtract from the offset (n * sizeof(T)) would trigger an overflow"); 418*8d741a5dSApple OSS Distributions } 419*8d741a5dSApple OSS Distributions if (__improbable(os_sub_overflow(offset_, bytes, &offset_))) { 420*8d741a5dSApple OSS Distributions TrappingPolicy::trap( 421*8d741a5dSApple OSS Distributions "bounded_ptr<T>::operator-=(n): Subtracting the specified number of bytes " 422*8d741a5dSApple OSS Distributions "from the offset representing the current position would overflow."); 423*8d741a5dSApple OSS Distributions } 424*8d741a5dSApple OSS Distributions return *this; 425*8d741a5dSApple OSS Distributions } 426*8d741a5dSApple OSS Distributions 427*8d741a5dSApple OSS Distributions friend OS_ALWAYS_INLINE bounded_ptr 428*8d741a5dSApple OSS Distributions operator+(bounded_ptr p, ptrdiff_t n) 429*8d741a5dSApple OSS Distributions { 430*8d741a5dSApple OSS Distributions p += n; 431*8d741a5dSApple OSS Distributions return p; 432*8d741a5dSApple OSS Distributions } 433*8d741a5dSApple OSS Distributions friend OS_ALWAYS_INLINE bounded_ptr 434*8d741a5dSApple OSS Distributions operator+(ptrdiff_t n, bounded_ptr p) 435*8d741a5dSApple OSS Distributions { 436*8d741a5dSApple OSS Distributions p += n; 437*8d741a5dSApple OSS Distributions return p; 438*8d741a5dSApple OSS Distributions } 439*8d741a5dSApple OSS Distributions friend OS_ALWAYS_INLINE bounded_ptr 440*8d741a5dSApple OSS Distributions operator-(bounded_ptr p, ptrdiff_t n) 441*8d741a5dSApple OSS Distributions { 442*8d741a5dSApple OSS Distributions p -= n; 443*8d741a5dSApple OSS Distributions return p; 444*8d741a5dSApple OSS Distributions } 445*8d741a5dSApple OSS Distributions 446*8d741a5dSApple OSS Distributions // Returns the difference between two `bounded_ptr`s. 447*8d741a5dSApple OSS Distributions // 448*8d741a5dSApple OSS Distributions // This is semantically equivalent to subtracting the two underlying 449*8d741a5dSApple OSS Distributions // pointers. The bounds of the pointers are not validated by this 450*8d741a5dSApple OSS Distributions // operation. 451*8d741a5dSApple OSS Distributions friend OS_ALWAYS_INLINE ptrdiff_t 452*8d741a5dSApple OSS Distributions operator-(bounded_ptr const& a, bounded_ptr const& b) 453*8d741a5dSApple OSS Distributions { 454*8d741a5dSApple OSS Distributions return a.get_ptr_() - b.get_ptr_(); 455*8d741a5dSApple OSS Distributions } 456*8d741a5dSApple OSS Distributions 457*8d741a5dSApple OSS Distributions friend OS_ALWAYS_INLINE ptrdiff_t 458*8d741a5dSApple OSS Distributions operator-(bounded_ptr const& a, T const* b) 459*8d741a5dSApple OSS Distributions { 460*8d741a5dSApple OSS Distributions return a.get_ptr_() - b; 461*8d741a5dSApple OSS Distributions } 462*8d741a5dSApple OSS Distributions 463*8d741a5dSApple OSS Distributions friend OS_ALWAYS_INLINE ptrdiff_t 464*8d741a5dSApple OSS Distributions operator-(T const* a, bounded_ptr const& b) 465*8d741a5dSApple OSS Distributions { 466*8d741a5dSApple OSS Distributions return a - b.get_ptr_(); 467*8d741a5dSApple OSS Distributions } 468*8d741a5dSApple OSS Distributions 469*8d741a5dSApple OSS Distributions private: 470*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE bool 471*8d741a5dSApple OSS Distributions in_bounds_() const 472*8d741a5dSApple OSS Distributions { 473*8d741a5dSApple OSS Distributions static_assert(detail::sizeof_v<T> <= UINT32_MAX - INT32_MAX, 474*8d741a5dSApple OSS Distributions "The type pointed-to by bounded_ptr is too large, which would defeat " 475*8d741a5dSApple OSS Distributions "our optimization to check for inboundedness using arithmetic on unsigned"); 476*8d741a5dSApple OSS Distributions return offset_ >= 0 && static_cast<uint32_t>(offset_) + static_cast<uint32_t>(detail::sizeof_v<T>) <= count_; 477*8d741a5dSApple OSS Distributions } 478*8d741a5dSApple OSS Distributions 479*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE T* 480*8d741a5dSApple OSS Distributions get_ptr_() const 481*8d741a5dSApple OSS Distributions { 482*8d741a5dSApple OSS Distributions // Compute `base_ + offset_`, catching overflows. 483*8d741a5dSApple OSS Distributions uintptr_t ptr; 484*8d741a5dSApple OSS Distributions if (__improbable(os_add_overflow(reinterpret_cast<uintptr_t>(base_), offset_, &ptr))) { 485*8d741a5dSApple OSS Distributions TrappingPolicy::trap("This bounded_ptr is pointing to memory outside of what can " 486*8d741a5dSApple OSS Distributions "be represented by a native pointer."); 487*8d741a5dSApple OSS Distributions } 488*8d741a5dSApple OSS Distributions return reinterpret_cast<T*>(ptr); 489*8d741a5dSApple OSS Distributions } 490*8d741a5dSApple OSS Distributions 491*8d741a5dSApple OSS Distributions template <typename T_, typename U, typename Policy> 492*8d741a5dSApple OSS Distributions friend bounded_ptr<T_, Policy> reinterpret_pointer_cast(bounded_ptr<U, Policy> const&) noexcept; 493*8d741a5dSApple OSS Distributions 494*8d741a5dSApple OSS Distributions template <typename U, typename P> friend struct bounded_ptr; // for cross-type operations and conversions 495*8d741a5dSApple OSS Distributions 496*8d741a5dSApple OSS Distributions CharType* base_; // pointer to the beginning of the valid address range 497*8d741a5dSApple OSS Distributions uint32_t count_; // number of bytes considered in-bounds (non-negative) 498*8d741a5dSApple OSS Distributions int32_t offset_; // current offset into the range, in bytes 499*8d741a5dSApple OSS Distributions }; 500*8d741a5dSApple OSS Distributions 501*8d741a5dSApple OSS Distributions // Returns whether two `bounded_ptr`s point to the same object. 502*8d741a5dSApple OSS Distributions // 503*8d741a5dSApple OSS Distributions // This comparison is semantically equivalent to comparing the underlying 504*8d741a5dSApple OSS Distributions // raw pointers. In particular, it doesn't validate the bounds of either 505*8d741a5dSApple OSS Distributions // `bounded_ptr`, nor does it compare whether the two `bounded_ptr`s have 506*8d741a5dSApple OSS Distributions // the same bounds. 507*8d741a5dSApple OSS Distributions // 508*8d741a5dSApple OSS Distributions // This comparison is enabled between `bounded_ptr`s whenever the two 509*8d741a5dSApple OSS Distributions // corresponding raw pointer types are comparable. Comparison between a 510*8d741a5dSApple OSS Distributions // raw pointer and a `bounded_ptr` is also allowed, so long as the 511*8d741a5dSApple OSS Distributions // two corresponding raw pointer types are comparable. 512*8d741a5dSApple OSS Distributions template <typename T, typename P1, typename U, typename P2, typename = detail::WhenComparable<T*, U*> > 513*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE bool 514*8d741a5dSApple OSS Distributions operator==(bounded_ptr<T, P1> const& a, bounded_ptr<U, P2> const& b) 515*8d741a5dSApple OSS Distributions { 516*8d741a5dSApple OSS Distributions return a.unsafe_discard_bounds() == b.unsafe_discard_bounds(); 517*8d741a5dSApple OSS Distributions } 518*8d741a5dSApple OSS Distributions 519*8d741a5dSApple OSS Distributions template <typename T, typename P1, typename U, typename P2, typename = detail::WhenComparable<T*, U*> > 520*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE bool 521*8d741a5dSApple OSS Distributions operator!=(bounded_ptr<T, P1> const& a, bounded_ptr<U, P2> const& b) 522*8d741a5dSApple OSS Distributions { 523*8d741a5dSApple OSS Distributions return !(a == b); 524*8d741a5dSApple OSS Distributions } 525*8d741a5dSApple OSS Distributions 526*8d741a5dSApple OSS Distributions template <typename T, typename P, typename U, typename = detail::WhenComparable<T*, U*> > 527*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE bool 528*8d741a5dSApple OSS Distributions operator==(bounded_ptr<T, P> const& a, U* b) 529*8d741a5dSApple OSS Distributions { 530*8d741a5dSApple OSS Distributions return a.unsafe_discard_bounds() == b; 531*8d741a5dSApple OSS Distributions } 532*8d741a5dSApple OSS Distributions 533*8d741a5dSApple OSS Distributions template <typename T, typename P, typename U, typename = detail::WhenComparable<T*, U*> > 534*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE bool 535*8d741a5dSApple OSS Distributions operator==(U* a, bounded_ptr<T, P> const& b) 536*8d741a5dSApple OSS Distributions { 537*8d741a5dSApple OSS Distributions return a == b.unsafe_discard_bounds(); 538*8d741a5dSApple OSS Distributions } 539*8d741a5dSApple OSS Distributions 540*8d741a5dSApple OSS Distributions template <typename T, typename P, typename U, typename = detail::WhenComparable<T*, U*> > 541*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE bool 542*8d741a5dSApple OSS Distributions operator!=(bounded_ptr<T, P> const& a, U* b) 543*8d741a5dSApple OSS Distributions { 544*8d741a5dSApple OSS Distributions return !(a == b); 545*8d741a5dSApple OSS Distributions } 546*8d741a5dSApple OSS Distributions 547*8d741a5dSApple OSS Distributions template <typename T, typename P, typename U, typename = detail::WhenComparable<T*, U*> > 548*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE bool 549*8d741a5dSApple OSS Distributions operator!=(U* a, bounded_ptr<T, P> const& b) 550*8d741a5dSApple OSS Distributions { 551*8d741a5dSApple OSS Distributions return !(a == b); 552*8d741a5dSApple OSS Distributions } 553*8d741a5dSApple OSS Distributions 554*8d741a5dSApple OSS Distributions template <typename T, typename Policy> 555*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE bool 556*8d741a5dSApple OSS Distributions operator==(detail::nullptr_t, bounded_ptr<T, Policy> const& p) 557*8d741a5dSApple OSS Distributions { 558*8d741a5dSApple OSS Distributions return p.unsafe_discard_bounds() == nullptr; 559*8d741a5dSApple OSS Distributions } 560*8d741a5dSApple OSS Distributions 561*8d741a5dSApple OSS Distributions template <typename T, typename Policy> 562*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE bool 563*8d741a5dSApple OSS Distributions operator!=(detail::nullptr_t, bounded_ptr<T, Policy> const& p) 564*8d741a5dSApple OSS Distributions { 565*8d741a5dSApple OSS Distributions return p.unsafe_discard_bounds() != nullptr; 566*8d741a5dSApple OSS Distributions } 567*8d741a5dSApple OSS Distributions 568*8d741a5dSApple OSS Distributions template <typename T, typename Policy> 569*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE bool 570*8d741a5dSApple OSS Distributions operator==(bounded_ptr<T, Policy> const& p, detail::nullptr_t) 571*8d741a5dSApple OSS Distributions { 572*8d741a5dSApple OSS Distributions return p.unsafe_discard_bounds() == nullptr; 573*8d741a5dSApple OSS Distributions } 574*8d741a5dSApple OSS Distributions 575*8d741a5dSApple OSS Distributions template <typename T, typename Policy> 576*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE bool 577*8d741a5dSApple OSS Distributions operator!=(bounded_ptr<T, Policy> const& p, detail::nullptr_t) 578*8d741a5dSApple OSS Distributions { 579*8d741a5dSApple OSS Distributions return p.unsafe_discard_bounds() != nullptr; 580*8d741a5dSApple OSS Distributions } 581*8d741a5dSApple OSS Distributions 582*8d741a5dSApple OSS Distributions // Returns whether a `bounded_ptr` points to an address that is {less-than, 583*8d741a5dSApple OSS Distributions // less-than-or-equal-to, greater-than, greater-than-or-equal-to} the address 584*8d741a5dSApple OSS Distributions // held in another `bounded_ptr`. 585*8d741a5dSApple OSS Distributions // 586*8d741a5dSApple OSS Distributions // This doesn't validate the bounds of either `bounded_ptr`, nor does it 587*8d741a5dSApple OSS Distributions // compare those bounds to determine the ordering result. This ordering is 588*8d741a5dSApple OSS Distributions // semantically equivalent to ordering the result of calling `get()` on both 589*8d741a5dSApple OSS Distributions // `bounded_ptr`s. 590*8d741a5dSApple OSS Distributions // 591*8d741a5dSApple OSS Distributions // This ordering is enabled between `bounded_ptr`s whenever the two 592*8d741a5dSApple OSS Distributions // corresponding raw pointer types are orderable. Ordering between a 593*8d741a5dSApple OSS Distributions // raw pointer and a `bounded_ptr` is also allowed, so long as the 594*8d741a5dSApple OSS Distributions // two corresponding raw pointer types are orderable. 595*8d741a5dSApple OSS Distributions // 596*8d741a5dSApple OSS Distributions 597*8d741a5dSApple OSS Distributions template <typename T, typename U, typename P1, typename P2, typename = detail::WhenOrderable<T*, U*> > 598*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE bool 599*8d741a5dSApple OSS Distributions operator<(bounded_ptr<T, P1> const& a, bounded_ptr<U, P2> const& b) 600*8d741a5dSApple OSS Distributions { 601*8d741a5dSApple OSS Distributions return a.unsafe_discard_bounds() < b.unsafe_discard_bounds(); 602*8d741a5dSApple OSS Distributions } 603*8d741a5dSApple OSS Distributions 604*8d741a5dSApple OSS Distributions template <typename T, typename U, typename P1, typename P2, typename = detail::WhenOrderable<T*, U*> > 605*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE bool 606*8d741a5dSApple OSS Distributions operator<=(bounded_ptr<T, P1> const& a, bounded_ptr<U, P2> const& b) 607*8d741a5dSApple OSS Distributions { 608*8d741a5dSApple OSS Distributions return a.unsafe_discard_bounds() <= b.unsafe_discard_bounds(); 609*8d741a5dSApple OSS Distributions } 610*8d741a5dSApple OSS Distributions 611*8d741a5dSApple OSS Distributions template <typename T, typename U, typename P1, typename P2, typename = detail::WhenOrderable<T*, U*> > 612*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE bool 613*8d741a5dSApple OSS Distributions operator>(bounded_ptr<T, P1> const& a, bounded_ptr<U, P2> const& b) 614*8d741a5dSApple OSS Distributions { 615*8d741a5dSApple OSS Distributions return a.unsafe_discard_bounds() > b.unsafe_discard_bounds(); 616*8d741a5dSApple OSS Distributions } 617*8d741a5dSApple OSS Distributions 618*8d741a5dSApple OSS Distributions template <typename T, typename U, typename P1, typename P2, typename = detail::WhenOrderable<T*, U*> > 619*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE bool 620*8d741a5dSApple OSS Distributions operator>=(bounded_ptr<T, P1> const& a, bounded_ptr<U, P2> const& b) 621*8d741a5dSApple OSS Distributions { 622*8d741a5dSApple OSS Distributions return a.unsafe_discard_bounds() >= b.unsafe_discard_bounds(); 623*8d741a5dSApple OSS Distributions } 624*8d741a5dSApple OSS Distributions 625*8d741a5dSApple OSS Distributions template <typename T, typename U, typename P, typename = detail::WhenOrderable<T*, U*> > 626*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE bool 627*8d741a5dSApple OSS Distributions operator<(T* a, bounded_ptr<U, P> const& b) 628*8d741a5dSApple OSS Distributions { 629*8d741a5dSApple OSS Distributions return a < b.unsafe_discard_bounds(); 630*8d741a5dSApple OSS Distributions } 631*8d741a5dSApple OSS Distributions 632*8d741a5dSApple OSS Distributions template <typename T, typename U, typename P, typename = detail::WhenOrderable<T*, U*> > 633*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE bool 634*8d741a5dSApple OSS Distributions operator<(bounded_ptr<T, P> const& a, U* b) 635*8d741a5dSApple OSS Distributions { 636*8d741a5dSApple OSS Distributions return a.unsafe_discard_bounds() < b; 637*8d741a5dSApple OSS Distributions } 638*8d741a5dSApple OSS Distributions 639*8d741a5dSApple OSS Distributions template <typename T, typename U, typename P, typename = detail::WhenOrderable<T*, U*> > 640*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE bool 641*8d741a5dSApple OSS Distributions operator<=(T* a, bounded_ptr<U, P> const& b) 642*8d741a5dSApple OSS Distributions { 643*8d741a5dSApple OSS Distributions return a <= b.unsafe_discard_bounds(); 644*8d741a5dSApple OSS Distributions } 645*8d741a5dSApple OSS Distributions 646*8d741a5dSApple OSS Distributions template <typename T, typename U, typename P, typename = detail::WhenOrderable<T*, U*> > 647*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE bool 648*8d741a5dSApple OSS Distributions operator<=(bounded_ptr<T, P> const& a, U* b) 649*8d741a5dSApple OSS Distributions { 650*8d741a5dSApple OSS Distributions return a.unsafe_discard_bounds() <= b; 651*8d741a5dSApple OSS Distributions } 652*8d741a5dSApple OSS Distributions 653*8d741a5dSApple OSS Distributions template <typename T, typename U, typename P, typename = detail::WhenOrderable<T*, U*> > 654*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE bool 655*8d741a5dSApple OSS Distributions operator>(T* a, bounded_ptr<U, P> const& b) 656*8d741a5dSApple OSS Distributions { 657*8d741a5dSApple OSS Distributions return a > b.unsafe_discard_bounds(); 658*8d741a5dSApple OSS Distributions } 659*8d741a5dSApple OSS Distributions 660*8d741a5dSApple OSS Distributions template <typename T, typename U, typename P, typename = detail::WhenOrderable<T*, U*> > 661*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE bool 662*8d741a5dSApple OSS Distributions operator>(bounded_ptr<T, P> const& a, U* b) 663*8d741a5dSApple OSS Distributions { 664*8d741a5dSApple OSS Distributions return a.unsafe_discard_bounds() > b; 665*8d741a5dSApple OSS Distributions } 666*8d741a5dSApple OSS Distributions 667*8d741a5dSApple OSS Distributions template <typename T, typename U, typename P, typename = detail::WhenOrderable<T*, U*> > 668*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE bool 669*8d741a5dSApple OSS Distributions operator>=(T* a, bounded_ptr<U, P> const& b) 670*8d741a5dSApple OSS Distributions { 671*8d741a5dSApple OSS Distributions return a >= b.unsafe_discard_bounds(); 672*8d741a5dSApple OSS Distributions } 673*8d741a5dSApple OSS Distributions 674*8d741a5dSApple OSS Distributions template <typename T, typename U, typename P, typename = detail::WhenOrderable<T*, U*> > 675*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE bool 676*8d741a5dSApple OSS Distributions operator>=(bounded_ptr<T, P> const& a, U* b) 677*8d741a5dSApple OSS Distributions { 678*8d741a5dSApple OSS Distributions return a.unsafe_discard_bounds() >= b; 679*8d741a5dSApple OSS Distributions } 680*8d741a5dSApple OSS Distributions 681*8d741a5dSApple OSS Distributions template <typename T, typename U> 682*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE T* 683*8d741a5dSApple OSS Distributions reinterpret_pointer_cast(U* p) noexcept 684*8d741a5dSApple OSS Distributions { 685*8d741a5dSApple OSS Distributions return reinterpret_cast<T*>(p); 686*8d741a5dSApple OSS Distributions } 687*8d741a5dSApple OSS Distributions 688*8d741a5dSApple OSS Distributions // Reinterprets a `bounded_ptr` to a type `T` to a `bounded_ptr` to a type `U`. 689*8d741a5dSApple OSS Distributions // 690*8d741a5dSApple OSS Distributions // This is equivalent to `reinterpret_cast`ing the underlying pointer as well 691*8d741a5dSApple OSS Distributions // as the bounds of the original pointer. Like for a raw `reinterpret_cast`, 692*8d741a5dSApple OSS Distributions // no offset adjustment is performed (even if needed, e.g. for derived-to-base 693*8d741a5dSApple OSS Distributions // casts with multiple inheritance). Because this is extremely unsafe, it should 694*8d741a5dSApple OSS Distributions // be used extremely sparingly. 695*8d741a5dSApple OSS Distributions template <typename T, typename U, typename Policy> 696*8d741a5dSApple OSS Distributions OS_ALWAYS_INLINE bounded_ptr<T, Policy> 697*8d741a5dSApple OSS Distributions reinterpret_pointer_cast(bounded_ptr<U, Policy> const& p) noexcept 698*8d741a5dSApple OSS Distributions { 699*8d741a5dSApple OSS Distributions using CharType = detail::copy_cv_t<T, char>; 700*8d741a5dSApple OSS Distributions CharType* new_begin = reinterpret_cast<CharType*>(p.base_); 701*8d741a5dSApple OSS Distributions CharType* new_end = new_begin + p.count_; 702*8d741a5dSApple OSS Distributions return bounded_ptr<T, Policy>(reinterpret_cast<T*>(p.get_ptr_()), 703*8d741a5dSApple OSS Distributions reinterpret_cast<T const*>(new_begin), 704*8d741a5dSApple OSS Distributions reinterpret_cast<T const*>(new_end)); 705*8d741a5dSApple OSS Distributions } 706*8d741a5dSApple OSS Distributions } // end namespace libkern 707*8d741a5dSApple OSS Distributions 708*8d741a5dSApple OSS Distributions #endif /* !TAPI */ 709*8d741a5dSApple OSS Distributions 710*8d741a5dSApple OSS Distributions #endif // !XNU_LIBKERN_LIBKERN_CXX_BOUNDED_PTR_H 711