summaryrefslogtreecommitdiffstats
path: root/mfbt/RangedPtr.h
blob: 65db3b034ac2f7d589a8cbb37632ec228c4e8b22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
 * Implements a smart pointer asserted to remain within a range specified at
 * construction.
 */

#ifndef mozilla_RangedPtr_h
#define mozilla_RangedPtr_h

#include "mozilla/ArrayUtils.h"
#include "mozilla/Assertions.h"
#include "mozilla/Attributes.h"

#include <stdint.h>
#include <cstddef>

namespace mozilla {

/*
 * RangedPtr is a smart pointer restricted to an address range specified at
 * creation.  The pointer (and any smart pointers derived from it) must remain
 * within the range [start, end] (inclusive of end to facilitate use as
 * sentinels).  Dereferencing or indexing into the pointer (or pointers derived
 * from it) must remain within the range [start, end).  All the standard pointer
 * operators are defined on it; in debug builds these operations assert that the
 * range specified at construction is respected.
 *
 * In theory passing a smart pointer instance as an argument can be slightly
 * slower than passing a T* (due to ABI requirements for passing structs versus
 * passing pointers), if the method being called isn't inlined.  If you are in
 * extremely performance-critical code, you may want to be careful using this
 * smart pointer as an argument type.
 *
 * RangedPtr<T> intentionally does not implicitly convert to T*.  Use get() to
 * explicitly convert to T*.  Keep in mind that the raw pointer of course won't
 * implement bounds checking in debug builds.
 */
template <typename T>
class RangedPtr {
  template <typename U>
  friend class RangedPtr;

  T* mPtr;

#if defined(DEBUG) || defined(FUZZING)
  T* const mRangeStart;
  T* const mRangeEnd;
#endif

  void checkSanity() {
    MOZ_ASSERT_DEBUG_OR_FUZZING(mRangeStart <= mPtr);
    MOZ_ASSERT_DEBUG_OR_FUZZING(mPtr <= mRangeEnd);
  }

  /* Creates a new pointer for |aPtr|, restricted to this pointer's range. */
  RangedPtr<T> create(T* aPtr) const {
#if defined(DEBUG) || defined(FUZZING)
    return RangedPtr<T>(aPtr, mRangeStart, mRangeEnd);
#else
    return RangedPtr<T>(aPtr, nullptr, size_t(0));
#endif
  }

  uintptr_t asUintptr() const { return reinterpret_cast<uintptr_t>(mPtr); }

 public:
  RangedPtr(T* aPtr, T* aStart, T* aEnd)
      : mPtr(aPtr)
#if defined(DEBUG) || defined(FUZZING)
        ,
        mRangeStart(aStart),
        mRangeEnd(aEnd)
#endif
  {
    MOZ_ASSERT_DEBUG_OR_FUZZING(mRangeStart <= mRangeEnd);
    checkSanity();
  }
  RangedPtr(T* aPtr, T* aStart, size_t aLength)
      : mPtr(aPtr)
#if defined(DEBUG) || defined(FUZZING)
        ,
        mRangeStart(aStart),
        mRangeEnd(aStart + aLength)
#endif
  {
    MOZ_ASSERT_DEBUG_OR_FUZZING(aLength <= size_t(-1) / sizeof(T));
    MOZ_ASSERT_DEBUG_OR_FUZZING(reinterpret_cast<uintptr_t>(mRangeStart) +
                                    aLength * sizeof(T) >=
                                reinterpret_cast<uintptr_t>(mRangeStart));
    checkSanity();
  }

  /* Equivalent to RangedPtr(aPtr, aPtr, aLength). */
  RangedPtr(T* aPtr, size_t aLength)
      : mPtr(aPtr)
#if defined(DEBUG) || defined(FUZZING)
        ,
        mRangeStart(aPtr),
        mRangeEnd(aPtr + aLength)
#endif
  {
    MOZ_ASSERT_DEBUG_OR_FUZZING(aLength <= size_t(-1) / sizeof(T));
    MOZ_ASSERT_DEBUG_OR_FUZZING(reinterpret_cast<uintptr_t>(mRangeStart) +
                                    aLength * sizeof(T) >=
                                reinterpret_cast<uintptr_t>(mRangeStart));
    checkSanity();
  }

  /* Equivalent to RangedPtr(aArr, aArr, N). */
  template <size_t N>
  explicit RangedPtr(T (&aArr)[N])
      : mPtr(aArr)
#if defined(DEBUG) || defined(FUZZING)
        ,
        mRangeStart(aArr),
        mRangeEnd(aArr + N)
#endif
  {
    checkSanity();
  }

  RangedPtr(const RangedPtr& aOther)
      : mPtr(aOther.mPtr)
#if defined(DEBUG) || defined(FUZZING)
        ,
        mRangeStart(aOther.mRangeStart),
        mRangeEnd(aOther.mRangeEnd)
#endif
  {
    checkSanity();
  }

  template <typename U>
  MOZ_IMPLICIT RangedPtr(const RangedPtr<U>& aOther)
      : mPtr(aOther.mPtr)
#if defined(DEBUG) || defined(FUZZING)
        ,
        mRangeStart(aOther.mRangeStart),
        mRangeEnd(aOther.mRangeEnd)
#endif
  {
    checkSanity();
  }

  T* get() const { return mPtr; }

  explicit operator bool() const { return mPtr != nullptr; }

  void checkIdenticalRange(const RangedPtr<T>& aOther) const {
    MOZ_ASSERT_DEBUG_OR_FUZZING(mRangeStart == aOther.mRangeStart);
    MOZ_ASSERT_DEBUG_OR_FUZZING(mRangeEnd == aOther.mRangeEnd);
  }

  template <typename U>
  RangedPtr<U> ReinterpretCast() const {
#if defined(DEBUG) || defined(FUZZING)
    return {reinterpret_cast<U*>(mPtr), reinterpret_cast<U*>(mRangeStart),
            reinterpret_cast<U*>(mRangeEnd)};
#else
    return {reinterpret_cast<U*>(mPtr), nullptr, nullptr};
#endif
  }

  /*
   * You can only assign one RangedPtr into another if the two pointers have
   * the same valid range:
   *
   *   char arr1[] = "hi";
   *   char arr2[] = "bye";
   *   RangedPtr<char> p1(arr1, 2);
   *   p1 = RangedPtr<char>(arr1 + 1, arr1, arr1 + 2); // works
   *   p1 = RangedPtr<char>(arr2, 3);                  // asserts
   */
  RangedPtr<T>& operator=(const RangedPtr<T>& aOther) {
    checkIdenticalRange(aOther);
    mPtr = aOther.mPtr;
    checkSanity();
    return *this;
  }

  RangedPtr<T> operator+(size_t aInc) const {
    MOZ_ASSERT_DEBUG_OR_FUZZING(aInc <= size_t(-1) / sizeof(T));
    MOZ_ASSERT_DEBUG_OR_FUZZING(asUintptr() + aInc * sizeof(T) >= asUintptr());
    return create(mPtr + aInc);
  }

  RangedPtr<T> operator-(size_t aDec) const {
    MOZ_ASSERT_DEBUG_OR_FUZZING(aDec <= size_t(-1) / sizeof(T));
    MOZ_ASSERT_DEBUG_OR_FUZZING(asUintptr() - aDec * sizeof(T) <= asUintptr());
    return create(mPtr - aDec);
  }

  /*
   * You can assign a raw pointer into a RangedPtr if the raw pointer is
   * within the range specified at creation.
   */
  template <typename U>
  RangedPtr<T>& operator=(U* aPtr) {
    *this = create(aPtr);
    return *this;
  }

  template <typename U>
  RangedPtr<T>& operator=(const RangedPtr<U>& aPtr) {
    MOZ_ASSERT_DEBUG_OR_FUZZING(mRangeStart <= aPtr.mPtr);
    MOZ_ASSERT_DEBUG_OR_FUZZING(aPtr.mPtr <= mRangeEnd);
    mPtr = aPtr.mPtr;
    checkSanity();
    return *this;
  }

  RangedPtr<T>& operator++() { return (*this += 1); }

  RangedPtr<T> operator++(int) {
    RangedPtr<T> rcp = *this;
    ++*this;
    return rcp;
  }

  RangedPtr<T>& operator--() { return (*this -= 1); }

  RangedPtr<T> operator--(int) {
    RangedPtr<T> rcp = *this;
    --*this;
    return rcp;
  }

  RangedPtr<T>& operator+=(size_t aInc) {
    *this = *this + aInc;
    return *this;
  }

  RangedPtr<T>& operator-=(size_t aDec) {
    *this = *this - aDec;
    return *this;
  }

  T& operator[](ptrdiff_t aIndex) const {
    MOZ_ASSERT_DEBUG_OR_FUZZING(size_t(aIndex > 0 ? aIndex : -aIndex) <=
                                size_t(-1) / sizeof(T));
    return *create(mPtr + aIndex);
  }

  T& operator*() const {
    MOZ_ASSERT_DEBUG_OR_FUZZING(mPtr >= mRangeStart);
    MOZ_ASSERT_DEBUG_OR_FUZZING(mPtr < mRangeEnd);
    return *mPtr;
  }

  T* operator->() const {
    MOZ_ASSERT_DEBUG_OR_FUZZING(mPtr >= mRangeStart);
    MOZ_ASSERT_DEBUG_OR_FUZZING(mPtr < mRangeEnd);
    return mPtr;
  }

  template <typename U>
  bool operator==(const RangedPtr<U>& aOther) const {
    return mPtr == aOther.mPtr;
  }
  template <typename U>
  bool operator!=(const RangedPtr<U>& aOther) const {
    return !(*this == aOther);
  }

  template <typename U>
  bool operator==(const U* u) const {
    return mPtr == u;
  }
  template <typename U>
  bool operator!=(const U* u) const {
    return !(*this == u);
  }

  bool operator==(std::nullptr_t) const { return mPtr == nullptr; }
  bool operator!=(std::nullptr_t) const { return mPtr != nullptr; }

  template <typename U>
  bool operator<(const RangedPtr<U>& aOther) const {
    return mPtr < aOther.mPtr;
  }
  template <typename U>
  bool operator<=(const RangedPtr<U>& aOther) const {
    return mPtr <= aOther.mPtr;
  }

  template <typename U>
  bool operator>(const RangedPtr<U>& aOther) const {
    return mPtr > aOther.mPtr;
  }
  template <typename U>
  bool operator>=(const RangedPtr<U>& aOther) const {
    return mPtr >= aOther.mPtr;
  }

  size_t operator-(const RangedPtr<T>& aOther) const {
    MOZ_ASSERT_DEBUG_OR_FUZZING(mPtr >= aOther.mPtr);
    return PointerRangeSize(aOther.mPtr, mPtr);
  }

 private:
  RangedPtr() = delete;
};

} /* namespace mozilla */

#endif /* mozilla_RangedPtr_h */