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