1*4d495c6eSApple OSS Distributions // 2*4d495c6eSApple OSS Distributions // Copyright (c) 2019 Apple, Inc. All rights reserved. 3*4d495c6eSApple OSS Distributions // 4*4d495c6eSApple OSS Distributions // @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5*4d495c6eSApple OSS Distributions // 6*4d495c6eSApple OSS Distributions // This file contains Original Code and/or Modifications of Original Code 7*4d495c6eSApple OSS Distributions // as defined in and that are subject to the Apple Public Source License 8*4d495c6eSApple OSS Distributions // Version 2.0 (the 'License'). You may not use this file except in 9*4d495c6eSApple OSS Distributions // compliance with the License. The rights granted to you under the License 10*4d495c6eSApple OSS Distributions // may not be used to create, or enable the creation or redistribution of, 11*4d495c6eSApple OSS Distributions // unlawful or unlicensed copies of an Apple operating system, or to 12*4d495c6eSApple OSS Distributions // circumvent, violate, or enable the circumvention or violation of, any 13*4d495c6eSApple OSS Distributions // terms of an Apple operating system software license agreement. 14*4d495c6eSApple OSS Distributions // 15*4d495c6eSApple OSS Distributions // Please obtain a copy of the License at 16*4d495c6eSApple OSS Distributions // http://www.opensource.apple.com/apsl/ and read it before using this file. 17*4d495c6eSApple OSS Distributions // 18*4d495c6eSApple OSS Distributions // The Original Code and all software distributed under the License are 19*4d495c6eSApple OSS Distributions // distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20*4d495c6eSApple OSS Distributions // EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21*4d495c6eSApple OSS Distributions // INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22*4d495c6eSApple OSS Distributions // FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23*4d495c6eSApple OSS Distributions // Please see the License for the specific language governing rights and 24*4d495c6eSApple OSS Distributions // limitations under the License. 25*4d495c6eSApple OSS Distributions // 26*4d495c6eSApple OSS Distributions // @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27*4d495c6eSApple OSS Distributions // 28*4d495c6eSApple OSS Distributions 29*4d495c6eSApple OSS Distributions #ifndef XNU_LIBKERN_LIBKERN_CXX_BOUNDED_ARRAY_REF_H 30*4d495c6eSApple OSS Distributions #define XNU_LIBKERN_LIBKERN_CXX_BOUNDED_ARRAY_REF_H 31*4d495c6eSApple OSS Distributions 32*4d495c6eSApple OSS Distributions #if !TAPI 33*4d495c6eSApple OSS Distributions 34*4d495c6eSApple OSS Distributions #if DRIVERKIT_FRAMEWORK_INCLUDE 35*4d495c6eSApple OSS Distributions #include <DriverKit/bounded_array.h> 36*4d495c6eSApple OSS Distributions #include <DriverKit/bounded_ptr.h> 37*4d495c6eSApple OSS Distributions #else 38*4d495c6eSApple OSS Distributions #include <libkern/c++/bounded_array.h> 39*4d495c6eSApple OSS Distributions #include <libkern/c++/bounded_ptr.h> 40*4d495c6eSApple OSS Distributions #endif /* DRIVERKIT_FRAMEWORK_INCLUDE */ 41*4d495c6eSApple OSS Distributions 42*4d495c6eSApple OSS Distributions #include <stddef.h> 43*4d495c6eSApple OSS Distributions #include <os/base.h> 44*4d495c6eSApple OSS Distributions 45*4d495c6eSApple OSS Distributions namespace libkern { 46*4d495c6eSApple OSS Distributions namespace bar_detail { 47*4d495c6eSApple OSS Distributions using nullptr_t = decltype(nullptr); 48*4d495c6eSApple OSS Distributions } 49*4d495c6eSApple OSS Distributions 50*4d495c6eSApple OSS Distributions // Represents a reference to a sequence of 0 or more elements consecutively in 51*4d495c6eSApple OSS Distributions // memory, i.e. a start pointer and a length. 52*4d495c6eSApple OSS Distributions // 53*4d495c6eSApple OSS Distributions // When elements of the sequence are accessed, `bounded_array_ref` ensures 54*4d495c6eSApple OSS Distributions // that those elements are in the bounds of the sequence (which are provided 55*4d495c6eSApple OSS Distributions // when the `bounded_array_ref` is constructed). 56*4d495c6eSApple OSS Distributions // 57*4d495c6eSApple OSS Distributions // This class does not own the underlying data. It is expected to be used in 58*4d495c6eSApple OSS Distributions // situations where the data resides in some other buffer, whose lifetime 59*4d495c6eSApple OSS Distributions // extends past that of the `bounded_array_ref`. For this reason, storing a 60*4d495c6eSApple OSS Distributions // `bounded_array_ref` adds the risk of a dangling pointer if the lifetime of 61*4d495c6eSApple OSS Distributions // the `bounded_array_ref` extends past that of the underlying data. 62*4d495c6eSApple OSS Distributions // 63*4d495c6eSApple OSS Distributions // `bounded_array_ref` is trivially copyable and it should be passed by value. 64*4d495c6eSApple OSS Distributions template <typename T, typename TrappingPolicy> 65*4d495c6eSApple OSS Distributions struct bounded_array_ref { 66*4d495c6eSApple OSS Distributions // Creates an empty `bounded_array_ref`. 67*4d495c6eSApple OSS Distributions // 68*4d495c6eSApple OSS Distributions // An empty `bounded_array_ref` does not reference anything, so its 69*4d495c6eSApple OSS Distributions // `data()` is null and its `size()` is 0. bounded_array_refbounded_array_ref70*4d495c6eSApple OSS Distributions explicit constexpr bounded_array_ref() noexcept : data_(nullptr), size_(0) 71*4d495c6eSApple OSS Distributions { 72*4d495c6eSApple OSS Distributions } 73*4d495c6eSApple OSS Distributions 74*4d495c6eSApple OSS Distributions // Creates a `bounded_array_ref` from a bounded pointer and a size. 75*4d495c6eSApple OSS Distributions // 76*4d495c6eSApple OSS Distributions // The resulting `bounded_array_ref` starts at the location where the 77*4d495c6eSApple OSS Distributions // pointer points, and has the given number of elements. All the elements 78*4d495c6eSApple OSS Distributions // must be in the bounds of the `bounded_ptr`, otherwise this constructor 79*4d495c6eSApple OSS Distributions // will trap. bounded_array_refbounded_array_ref80*4d495c6eSApple OSS Distributions explicit constexpr bounded_array_ref(bounded_ptr<T, TrappingPolicy> data, size_t n) 81*4d495c6eSApple OSS Distributions : data_(data.unsafe_discard_bounds()), size_(static_cast<uint32_t>(n)) 82*4d495c6eSApple OSS Distributions { 83*4d495c6eSApple OSS Distributions if (n != 0) { 84*4d495c6eSApple OSS Distributions data[n - 1]; // make sure the bounds are valid 85*4d495c6eSApple OSS Distributions // TODO: find a better way to do that 86*4d495c6eSApple OSS Distributions } 87*4d495c6eSApple OSS Distributions if (__improbable(n > UINT32_MAX)) { 88*4d495c6eSApple OSS Distributions TrappingPolicy::trap("bounded_array_ref: Can't construct from a size greater than UINT32_MAX"); 89*4d495c6eSApple OSS Distributions } 90*4d495c6eSApple OSS Distributions } 91*4d495c6eSApple OSS Distributions 92*4d495c6eSApple OSS Distributions // Creates a `bounded_array_ref` from a raw pointer and a size. 93*4d495c6eSApple OSS Distributions // 94*4d495c6eSApple OSS Distributions // The resulting `bounded_array_ref` starts at the location where the 95*4d495c6eSApple OSS Distributions // pointer points, and has the given number of elements. This constructor 96*4d495c6eSApple OSS Distributions // trusts that `n` elements are reachable from the given pointer. bounded_array_refbounded_array_ref97*4d495c6eSApple OSS Distributions explicit constexpr bounded_array_ref(T* data, size_t n) : data_(data), size_(static_cast<uint32_t>(n)) 98*4d495c6eSApple OSS Distributions { 99*4d495c6eSApple OSS Distributions if (__improbable(n > UINT32_MAX)) { 100*4d495c6eSApple OSS Distributions TrappingPolicy::trap("bounded_array_ref: Can't construct from a size greater than UINT32_MAX"); 101*4d495c6eSApple OSS Distributions } 102*4d495c6eSApple OSS Distributions } 103*4d495c6eSApple OSS Distributions 104*4d495c6eSApple OSS Distributions // Creates a `bounded_array_ref` from a `[first, last)` half-open range. 105*4d495c6eSApple OSS Distributions // 106*4d495c6eSApple OSS Distributions // The resulting `bounded_array_ref` starts at the location pointed-to by 107*4d495c6eSApple OSS Distributions // `first`, and contains `last - first` elements. The `[first, last)` 108*4d495c6eSApple OSS Distributions // half-open range must be a valid range, i.e. it must be the case that 109*4d495c6eSApple OSS Distributions // `first <= last`, otherwise the constructor traps. bounded_array_refbounded_array_ref110*4d495c6eSApple OSS Distributions explicit constexpr bounded_array_ref(T* first, T* last) : data_(first), size_(static_cast<uint32_t>(last - first)) 111*4d495c6eSApple OSS Distributions { 112*4d495c6eSApple OSS Distributions if (__improbable(first > last)) { 113*4d495c6eSApple OSS Distributions TrappingPolicy::trap("bounded_array_ref: The [first, last) constructor requires a valid range."); 114*4d495c6eSApple OSS Distributions } 115*4d495c6eSApple OSS Distributions if (__improbable(last - first > UINT32_MAX)) { 116*4d495c6eSApple OSS Distributions TrappingPolicy::trap("bounded_array_ref: Can't construct from a size greater than UINT32_MAX"); 117*4d495c6eSApple OSS Distributions } 118*4d495c6eSApple OSS Distributions } 119*4d495c6eSApple OSS Distributions 120*4d495c6eSApple OSS Distributions // Creates a `bounded_array_ref` from a `bounded_array`. 121*4d495c6eSApple OSS Distributions // 122*4d495c6eSApple OSS Distributions // The resulting `bounded_array_ref` starts at the first element of the 123*4d495c6eSApple OSS Distributions // `bounded_array`, and has the number of elements in the `bounded_array`. 124*4d495c6eSApple OSS Distributions template <size_t N> bounded_array_refbounded_array_ref125*4d495c6eSApple OSS Distributions constexpr bounded_array_ref(bounded_array<T, N, TrappingPolicy>& data) : data_(data.data()), size_(static_cast<uint32_t>(data.size())) 126*4d495c6eSApple OSS Distributions { 127*4d495c6eSApple OSS Distributions if (__improbable(data.size() > UINT32_MAX)) { 128*4d495c6eSApple OSS Distributions TrappingPolicy::trap("bounded_array_ref: Can't construct from a size greater than UINT32_MAX"); 129*4d495c6eSApple OSS Distributions } 130*4d495c6eSApple OSS Distributions } 131*4d495c6eSApple OSS Distributions 132*4d495c6eSApple OSS Distributions // Creates a `bounded_array_ref` from a C-style array. 133*4d495c6eSApple OSS Distributions // 134*4d495c6eSApple OSS Distributions // The resulting `bounded_array_ref` starts at the first element of the 135*4d495c6eSApple OSS Distributions // C-style array, and has the number of elements in that array. 136*4d495c6eSApple OSS Distributions template <size_t N> bounded_array_refbounded_array_ref137*4d495c6eSApple OSS Distributions constexpr bounded_array_ref(T (&array)[N]) : data_(array), size_(static_cast<uint32_t>(N)) 138*4d495c6eSApple OSS Distributions { 139*4d495c6eSApple OSS Distributions if (__improbable(N > UINT32_MAX)) { 140*4d495c6eSApple OSS Distributions TrappingPolicy::trap("bounded_array_ref: Can't construct from a size greater than UINT32_MAX"); 141*4d495c6eSApple OSS Distributions } 142*4d495c6eSApple OSS Distributions } 143*4d495c6eSApple OSS Distributions 144*4d495c6eSApple OSS Distributions constexpr 145*4d495c6eSApple OSS Distributions bounded_array_ref(bounded_array_ref const&) = default; 146*4d495c6eSApple OSS Distributions constexpr 147*4d495c6eSApple OSS Distributions bounded_array_ref(bounded_array_ref&& other) noexcept = default; 148*4d495c6eSApple OSS Distributions 149*4d495c6eSApple OSS Distributions constexpr bounded_array_ref& operator=(bounded_array_ref const&) = default; 150*4d495c6eSApple OSS Distributions constexpr bounded_array_ref& operator=(bounded_array_ref&& other) = default; 151*4d495c6eSApple OSS Distributions ~bounded_array_ref() = default; 152*4d495c6eSApple OSS Distributions 153*4d495c6eSApple OSS Distributions // Returns whether the `bounded_array_ref` points to a sequence or not. 154*4d495c6eSApple OSS Distributions // 155*4d495c6eSApple OSS Distributions // Note that pointing to a sequence at all is different from pointing to 156*4d495c6eSApple OSS Distributions // a valid sequence, or having a size of 0. If a `bounded_array_ref` 157*4d495c6eSApple OSS Distributions // points to a sequence (regardless of whether it is valid or whether 158*4d495c6eSApple OSS Distributions // the size of that sequence is 0), this operator will return true. 159*4d495c6eSApple OSS Distributions explicit 160*4d495c6eSApple OSS Distributions operator bool() const noexcept 161*4d495c6eSApple OSS Distributions { 162*4d495c6eSApple OSS Distributions return data_ != nullptr; 163*4d495c6eSApple OSS Distributions } 164*4d495c6eSApple OSS Distributions 165*4d495c6eSApple OSS Distributions using iterator = bounded_ptr<T, TrappingPolicy>; 166*4d495c6eSApple OSS Distributions 167*4d495c6eSApple OSS Distributions // The following methods allow obtaining iterators (i.e. cursors) to 168*4d495c6eSApple OSS Distributions // objects inside a `bounded_array_ref`. 169*4d495c6eSApple OSS Distributions // 170*4d495c6eSApple OSS Distributions // The iterators of a `bounded_array_ref` are `bounded_ptr`s, which know 171*4d495c6eSApple OSS Distributions // the bounds of the sequence and will trap when dereferenced outside 172*4d495c6eSApple OSS Distributions // of those bounds. 173*4d495c6eSApple OSS Distributions // 174*4d495c6eSApple OSS Distributions // `begin()` returns an iterator to the first element in the range, and 175*4d495c6eSApple OSS Distributions // `end()` returns an iterator to one-past-the-last element in the range. 176*4d495c6eSApple OSS Distributions // The `end()` iterator can't be dereferenced, since it is out of bounds. 177*4d495c6eSApple OSS Distributions // 178*4d495c6eSApple OSS Distributions // If the `bounded_array_ref` is empty, these methods will return null 179*4d495c6eSApple OSS Distributions // `bounded_ptr`s, which can be checked for equality but can't be 180*4d495c6eSApple OSS Distributions // dereferenced. 181*4d495c6eSApple OSS Distributions OS_ALWAYS_INLINE iterator beginbounded_array_ref182*4d495c6eSApple OSS Distributions begin() const noexcept 183*4d495c6eSApple OSS Distributions { 184*4d495c6eSApple OSS Distributions return iterator(data_, data_, data_ + size_); 185*4d495c6eSApple OSS Distributions } 186*4d495c6eSApple OSS Distributions iterator endbounded_array_ref187*4d495c6eSApple OSS Distributions end() const noexcept 188*4d495c6eSApple OSS Distributions { 189*4d495c6eSApple OSS Distributions return iterator(data_ + size_, data_, data_ + size_); 190*4d495c6eSApple OSS Distributions } 191*4d495c6eSApple OSS Distributions 192*4d495c6eSApple OSS Distributions // Returns the number of elements in the range referenced by the 193*4d495c6eSApple OSS Distributions // `bounded_array_ref`. 194*4d495c6eSApple OSS Distributions // 195*4d495c6eSApple OSS Distributions // This method returns `0` if the `bounded_array_ref` is null, since 196*4d495c6eSApple OSS Distributions // such an array ref behaves the same as an empty range. 197*4d495c6eSApple OSS Distributions constexpr size_t sizebounded_array_ref198*4d495c6eSApple OSS Distributions size() const noexcept 199*4d495c6eSApple OSS Distributions { 200*4d495c6eSApple OSS Distributions return size_; 201*4d495c6eSApple OSS Distributions } 202*4d495c6eSApple OSS Distributions 203*4d495c6eSApple OSS Distributions // This has the same behavior as size(), but is intended to avoid confusion 204*4d495c6eSApple OSS Distributions // about whether it is returning an array count or size in bytes. 205*4d495c6eSApple OSS Distributions constexpr size_t lengthbounded_array_ref206*4d495c6eSApple OSS Distributions length() const noexcept 207*4d495c6eSApple OSS Distributions { 208*4d495c6eSApple OSS Distributions return size_; 209*4d495c6eSApple OSS Distributions } 210*4d495c6eSApple OSS Distributions 211*4d495c6eSApple OSS Distributions // Returns a non-owning pointer to the underlying memory referenced by a 212*4d495c6eSApple OSS Distributions // `bounded_array_ref`. 213*4d495c6eSApple OSS Distributions // 214*4d495c6eSApple OSS Distributions // This method can be called even if the `bounded_array_ref` is null, in 215*4d495c6eSApple OSS Distributions // which case the returned pointer will be null. 216*4d495c6eSApple OSS Distributions constexpr T* databounded_array_ref217*4d495c6eSApple OSS Distributions data() const noexcept 218*4d495c6eSApple OSS Distributions { 219*4d495c6eSApple OSS Distributions return data_; 220*4d495c6eSApple OSS Distributions } 221*4d495c6eSApple OSS Distributions 222*4d495c6eSApple OSS Distributions // Access the n-th element of a `bounded_array_ref`. 223*4d495c6eSApple OSS Distributions // 224*4d495c6eSApple OSS Distributions // If `n` is out of the bounds of the sequence, this operation will 225*4d495c6eSApple OSS Distributions // trap. If the array ref is null, this operation will trap too. 226*4d495c6eSApple OSS Distributions // 227*4d495c6eSApple OSS Distributions // Design note: 228*4d495c6eSApple OSS Distributions // We voluntarily use a signed type to represent the index even though a 229*4d495c6eSApple OSS Distributions // negative index will always cause a trap. If we used an unsigned type, 230*4d495c6eSApple OSS Distributions // we could get an implicit conversion from signed to unsigned, which 231*4d495c6eSApple OSS Distributions // could silently wrap around. We think trapping early is more likely 232*4d495c6eSApple OSS Distributions // to be helpful in this situation. 233*4d495c6eSApple OSS Distributions OS_ALWAYS_INLINE T& 234*4d495c6eSApple OSS Distributions operator[](ptrdiff_t n) const 235*4d495c6eSApple OSS Distributions { 236*4d495c6eSApple OSS Distributions return begin()[n]; 237*4d495c6eSApple OSS Distributions } 238*4d495c6eSApple OSS Distributions 239*4d495c6eSApple OSS Distributions // Chop off the first `n` elements of the array, and keep `m` elements 240*4d495c6eSApple OSS Distributions // in the array. 241*4d495c6eSApple OSS Distributions // 242*4d495c6eSApple OSS Distributions // The resulting range can be described by `[beg + n, beg + n + m)`, where 243*4d495c6eSApple OSS Distributions // `beg` is the `begin()` of the range being sliced. This operation traps 244*4d495c6eSApple OSS Distributions // if `n + m` is larger than the number of elements in the array. 245*4d495c6eSApple OSS Distributions // 246*4d495c6eSApple OSS Distributions // Since `bounded_array_ref` checks (or assumes) that the range it is 247*4d495c6eSApple OSS Distributions // given on construction is within bounds and `slice()` checks that the 248*4d495c6eSApple OSS Distributions // produced slice is within the original range, it is impossible to create 249*4d495c6eSApple OSS Distributions // a `bounded_array_ref` that isn't a subset of a valid range using this 250*4d495c6eSApple OSS Distributions // function. 251*4d495c6eSApple OSS Distributions bounded_array_ref<T, TrappingPolicy> slicebounded_array_ref252*4d495c6eSApple OSS Distributions slice(size_t n, size_t m) const 253*4d495c6eSApple OSS Distributions { 254*4d495c6eSApple OSS Distributions uint32_t total; 255*4d495c6eSApple OSS Distributions if (__improbable(os_add_overflow(n, m, &total))) { 256*4d495c6eSApple OSS Distributions TrappingPolicy::trap("bounded_array_ref: n + m is larger than the size of any bounded_array_ref"); 257*4d495c6eSApple OSS Distributions } 258*4d495c6eSApple OSS Distributions if (__improbable(total > size())) { 259*4d495c6eSApple OSS Distributions TrappingPolicy::trap("bounded_array_ref: invalid slice provided, the indices are of bounds for the bounded_array_ref"); 260*4d495c6eSApple OSS Distributions } 261*4d495c6eSApple OSS Distributions return bounded_array_ref(data_ + n, m); 262*4d495c6eSApple OSS Distributions } 263*4d495c6eSApple OSS Distributions 264*4d495c6eSApple OSS Distributions private: 265*4d495c6eSApple OSS Distributions T* data_; 266*4d495c6eSApple OSS Distributions uint32_t size_; 267*4d495c6eSApple OSS Distributions }; 268*4d495c6eSApple OSS Distributions 269*4d495c6eSApple OSS Distributions // The comparison functions against `nullptr` all return whether the 270*4d495c6eSApple OSS Distributions // `bounded_array_ref` references a sequence or not. 271*4d495c6eSApple OSS Distributions template <typename T, typename P> 272*4d495c6eSApple OSS Distributions bool 273*4d495c6eSApple OSS Distributions operator==(bounded_array_ref<T, P> const& x, bar_detail::nullptr_t) 274*4d495c6eSApple OSS Distributions { 275*4d495c6eSApple OSS Distributions return !static_cast<bool>(x); 276*4d495c6eSApple OSS Distributions } 277*4d495c6eSApple OSS Distributions 278*4d495c6eSApple OSS Distributions template <typename T, typename P> 279*4d495c6eSApple OSS Distributions bool 280*4d495c6eSApple OSS Distributions operator!=(bounded_array_ref<T, P> const& x, bar_detail::nullptr_t) 281*4d495c6eSApple OSS Distributions { 282*4d495c6eSApple OSS Distributions return !(x == nullptr); 283*4d495c6eSApple OSS Distributions } 284*4d495c6eSApple OSS Distributions 285*4d495c6eSApple OSS Distributions template <typename T, typename P> 286*4d495c6eSApple OSS Distributions bool 287*4d495c6eSApple OSS Distributions operator==(bar_detail::nullptr_t, bounded_array_ref<T, P> const& x) 288*4d495c6eSApple OSS Distributions { 289*4d495c6eSApple OSS Distributions return x == nullptr; 290*4d495c6eSApple OSS Distributions } 291*4d495c6eSApple OSS Distributions 292*4d495c6eSApple OSS Distributions template <typename T, typename P> 293*4d495c6eSApple OSS Distributions bool 294*4d495c6eSApple OSS Distributions operator!=(bar_detail::nullptr_t, bounded_array_ref<T, P> const& x) 295*4d495c6eSApple OSS Distributions { 296*4d495c6eSApple OSS Distributions return x != nullptr; 297*4d495c6eSApple OSS Distributions } 298*4d495c6eSApple OSS Distributions } // end namespace libkern 299*4d495c6eSApple OSS Distributions 300*4d495c6eSApple OSS Distributions #endif /* !TAPI */ 301*4d495c6eSApple OSS Distributions 302*4d495c6eSApple OSS Distributions #endif // !XNU_LIBKERN_LIBKERN_CXX_BOUNDED_ARRAY_REF_H 303