DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (c68fe15a81fc)

VCS Links

Line Code
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 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
/* -*- 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/. */

#ifndef ds_PageProtectingVector_h
#define ds_PageProtectingVector_h

#include "mozilla/Atomics.h"
#include "mozilla/IntegerPrintfMacros.h"
#include "mozilla/PodOperations.h"
#include "mozilla/Types.h"
#include "mozilla/Vector.h"

#include "ds/MemoryProtectionExceptionHandler.h"
#include "gc/Memory.h"

namespace js {

/*
 * PageProtectingVector is a vector that can only grow or be cleared, restricts
 * access to memory pages that haven't been used yet, and marks all of its fully
 * used memory pages as read-only. It can be used to detect heap corruption in
 * important buffers, since anything that tries to write into its protected
 * pages will crash. On Nightly and Aurora, these crashes will additionally be
 * annotated with a moz crash reason using MemoryProtectionExceptionHandler.
 *
 * PageProtectingVector's protection is limited to full pages. If the front
 * of its buffer is not aligned on a page boundary, elems preceding the first
 * page boundary will not be protected. Similarly, the end of the buffer will
 * not be fully protected unless it is aligned on a page boundary. Altogether,
 * up to two pages of memory may not be protected.
 */
template <typename T, size_t MinInlineCapacity = 0,
          class AllocPolicy = mozilla::MallocAllocPolicy,
          bool ProtectUsed = true, bool ProtectUnused = true,
          size_t InitialLowerBound = 0, bool PoisonUnused = true,
          uint8_t PoisonPattern = 0xe3>
class PageProtectingVector final {
  mozilla::Vector<T, MinInlineCapacity, AllocPolicy> vector;

  static constexpr size_t toShift(size_t v) {
    return v <= 1 ? 0 : 1 + toShift(v >> 1);
  }

  static_assert(
      (sizeof(T) & (sizeof(T) - 1)) == 0,
      "For performance reasons, "
      "PageProtectingVector only works with power-of-2 sized elements!");

  static const size_t elemShift = toShift(sizeof(T));
  static const size_t elemSize = 1 << elemShift;
  static const size_t elemMask = elemSize - 1;

  /* We hardcode the page size here to minimize administrative overhead. */
  static const size_t pageShift = 12;
  static const size_t pageSize = 1 << pageShift;
  static const size_t pageMask = pageSize - 1;

  /*
   * The number of elements that can be added before we need to either adjust
   * the active page or resize the buffer. If |elemsUntilTest < 0| we will
   * take the slow paths in the append calls.
   */
  intptr_t elemsUntilTest;

  /*
   * The offset of the currently 'active' page - that is, the page that is
   * currently being written to. If both used and unused bytes are protected,
   * this will be the only (fully owned) page with read and write access.
   */
  size_t currPage;

  /*
   * The first fully owned page. This is the first page that can
   * be protected, but it may not be the first *active* page.
   */
  size_t initPage;

  /*
   * The last fully owned page. This is the last page that can
   * be protected, but it may not be the last *active* page.
   */
  size_t lastPage;

  /*
   * The size in elems that a buffer needs to be before its pages will be
   * protected. This is intended to reduce churn for small vectors while
   * still offering protection when they grow large enough.
   */
  size_t lowerBound;

#ifdef DEBUG
  bool regionUnprotected;
#endif

  bool usable;
  bool enabled;
  bool protectUsedEnabled;
  bool protectUnusedEnabled;

  MOZ_ALWAYS_INLINE void resetTest() {
    MOZ_ASSERT(protectUsedEnabled || protectUnusedEnabled);
    size_t nextPage =
        (pageSize - (uintptr_t(begin() + length()) & pageMask)) >> elemShift;
    size_t nextResize = capacity() - length();
    if (MOZ_LIKELY(nextPage <= nextResize)) {
      elemsUntilTest = intptr_t(nextPage);
    } else {
      elemsUntilTest = intptr_t(nextResize);
    }
  }

  MOZ_ALWAYS_INLINE void setTestInitial() {
    if (MOZ_LIKELY(!protectUsedEnabled && !protectUnusedEnabled)) {
      elemsUntilTest = intptr_t(capacity() - length());
    } else {
      resetTest();
    }
  }

  MOZ_ALWAYS_INLINE void resetForNewBuffer() {
    initPage = (uintptr_t(begin() - 1) >> pageShift) + 1;
    currPage = (uintptr_t(begin() + length()) >> pageShift);
    lastPage = (uintptr_t(begin() + capacity()) >> pageShift) - 1;
    protectUsedEnabled =
        ProtectUsed && usable && enabled && initPage <= lastPage &&
        (uintptr_t(begin()) & elemMask) == 0 && capacity() >= lowerBound;
    protectUnusedEnabled =
        ProtectUnused && usable && enabled && initPage <= lastPage &&
        (uintptr_t(begin()) & elemMask) == 0 && capacity() >= lowerBound;
    setTestInitial();
  }

  MOZ_ALWAYS_INLINE void poisonNewBuffer() {
    if (!PoisonUnused) {
      return;
    }
    T* addr = begin() + length();
    size_t toPoison = (capacity() - length()) * sizeof(T);
    memset(addr, PoisonPattern, toPoison);
  }

  MOZ_ALWAYS_INLINE void addExceptionHandler() {
    if (MOZ_UNLIKELY(protectUsedEnabled || protectUnusedEnabled)) {
      MemoryProtectionExceptionHandler::addRegion(begin(), capacity()
                                                               << elemShift);
    }
  }

  MOZ_ALWAYS_INLINE void removeExceptionHandler() {
    if (MOZ_UNLIKELY(protectUsedEnabled || protectUnusedEnabled)) {
      MemoryProtectionExceptionHandler::removeRegion(begin());
    }
  }

  MOZ_ALWAYS_INLINE void protectUsed() {
    if (MOZ_LIKELY(!protectUsedEnabled)) {
      return;
    }
    if (MOZ_UNLIKELY(currPage <= initPage)) {
      return;
    }
    T* addr = reinterpret_cast<T*>(initPage << pageShift);
    size_t size = (currPage - initPage) << pageShift;
    gc::MakePagesReadOnly(addr, size);
  }

  MOZ_ALWAYS_INLINE void unprotectUsed() {
    if (MOZ_LIKELY(!protectUsedEnabled)) {
      return;
    }
    if (MOZ_UNLIKELY(currPage <= initPage)) {
      return;
    }
    T* addr = reinterpret_cast<T*>(initPage << pageShift);
    size_t size = (currPage - initPage) << pageShift;
    gc::UnprotectPages(addr, size);
  }

  MOZ_ALWAYS_INLINE void protectUnused() {
    if (MOZ_LIKELY(!protectUnusedEnabled)) {
      return;
    }
    if (MOZ_UNLIKELY(currPage >= lastPage)) {
      return;
    }
    T* addr = reinterpret_cast<T*>((currPage + 1) << pageShift);
    size_t size = (lastPage - currPage) << pageShift;
    gc::ProtectPages(addr, size);
  }

  MOZ_ALWAYS_INLINE void unprotectUnused() {
    if (MOZ_LIKELY(!protectUnusedEnabled)) {
      return;
    }
    if (MOZ_UNLIKELY(currPage >= lastPage)) {
      return;
    }
    T* addr = reinterpret_cast<T*>((currPage + 1) << pageShift);
    size_t size = (lastPage - currPage) << pageShift;
    gc::UnprotectPages(addr, size);
  }

  MOZ_ALWAYS_INLINE void protectNewBuffer() {
    resetForNewBuffer();
    addExceptionHandler();
    poisonNewBuffer();
    protectUsed();
    protectUnused();
  }

  MOZ_ALWAYS_INLINE void unprotectOldBuffer() {
    MOZ_ASSERT(!regionUnprotected);
    unprotectUnused();
    unprotectUsed();
    removeExceptionHandler();
  }

  MOZ_ALWAYS_INLINE void protectUnusedPartial(size_t curr, size_t next) {
    if (MOZ_LIKELY(!protectUnusedEnabled)) {
      return;
    }
    if (MOZ_UNLIKELY(next > lastPage)) {
      --next;
    }
    if (MOZ_UNLIKELY(next == curr)) {
      return;
    }
    void* addr = reinterpret_cast<T*>((curr + 1) << pageShift);
    size_t size = (next - curr) << pageShift;
    gc::ProtectPages(addr, size);
  }

  MOZ_ALWAYS_INLINE void unprotectUnusedPartial(size_t curr, size_t next) {
    if (MOZ_LIKELY(!protectUnusedEnabled)) {
      return;
    }
    if (MOZ_UNLIKELY(next > lastPage)) {
      --next;
    }
    if (MOZ_UNLIKELY(next == curr)) {
      return;
    }
    void* addr = reinterpret_cast<T*>((curr + 1) << pageShift);
    size_t size = (next - curr) << pageShift;
    gc::UnprotectPages(addr, size);
  }

  MOZ_ALWAYS_INLINE void protectUsedPartial(size_t curr, size_t next) {
    if (MOZ_LIKELY(!protectUsedEnabled)) {
      return;
    }
    if (MOZ_UNLIKELY(curr < initPage)) {
      ++curr;
    }
    if (MOZ_UNLIKELY(next == curr)) {
      return;
    }
    void* addr = reinterpret_cast<T*>(curr << pageShift);
    size_t size = (next - curr) << pageShift;
    gc::MakePagesReadOnly(addr, size);
  }

  MOZ_ALWAYS_INLINE MOZ_MUST_USE bool reserveNewBuffer(size_t size) {
    unprotectOldBuffer();
    bool ret = vector.reserve(size);
    protectNewBuffer();
    return ret;
  }

  template <typename U>
  MOZ_ALWAYS_INLINE void infallibleAppendNewPage(const U* values, size_t size) {
    size_t nextPage = uintptr_t(begin() + length() + size) >> pageShift;
    MOZ_ASSERT(currPage < nextPage);
    unprotectUnusedPartial(currPage, nextPage);
    vector.infallibleAppend(values, size);
    protectUsedPartial(currPage, nextPage);
    currPage = nextPage;
    resetTest();
  }

  template <typename U>
  MOZ_ALWAYS_INLINE MOZ_MUST_USE bool appendNewPage(const U* values,
                                                    size_t size) {
    size_t nextPage = uintptr_t(begin() + length() + size) >> pageShift;
    MOZ_ASSERT(currPage < nextPage);
    unprotectUnusedPartial(currPage, nextPage);
    bool ret = vector.append(values, size);
    if (MOZ_LIKELY(ret)) {
      protectUsedPartial(currPage, nextPage);
      currPage = nextPage;
    } else {
      protectUnusedPartial(currPage, nextPage);
    }
    resetTest();
    return ret;
  }

  template <typename U>
  MOZ_ALWAYS_INLINE MOZ_MUST_USE bool appendNewBuffer(const U* values,
                                                      size_t size) {
    unprotectOldBuffer();
    bool ret = vector.append(values, size);
    protectNewBuffer();
    return ret;
  }

  MOZ_NEVER_INLINE void unprotectRegionSlow(uintptr_t l, uintptr_t r);
  MOZ_NEVER_INLINE void reprotectRegionSlow(uintptr_t l, uintptr_t r);

  MOZ_NEVER_INLINE MOZ_MUST_USE bool reserveSlow(size_t size);

  template <typename U>
  MOZ_NEVER_INLINE void infallibleAppendSlow(const U* values, size_t size);

  template <typename U>
  MOZ_NEVER_INLINE MOZ_MUST_USE bool appendSlow(const U* values, size_t size);

 public:
  explicit PageProtectingVector(AllocPolicy policy = AllocPolicy())
      : vector(std::move(policy)),
        elemsUntilTest(0),
        currPage(0),
        initPage(0),
        lastPage(0),
        lowerBound(InitialLowerBound),
#ifdef DEBUG
        regionUnprotected(false),
#endif
        usable(true),
        enabled(true),
        protectUsedEnabled(false),
        protectUnusedEnabled(false) {
    if (gc::SystemPageSize() != pageSize) {
      usable = false;
    }
    protectNewBuffer();
  }

  ~PageProtectingVector() { unprotectOldBuffer(); }

  void disableProtection() {
    MOZ_ASSERT(enabled);
    unprotectOldBuffer();
    enabled = false;
    resetForNewBuffer();
  }

  void enableProtection() {
    MOZ_ASSERT(!enabled);
    enabled = true;
    protectNewBuffer();
  }

  /*
   * Sets the lower bound on the size, in elems, that this vector's underlying
   * capacity has to be before its used pages will be protected.
   */
  void setLowerBoundForProtection(size_t elems) {
    if (lowerBound != elems) {
      unprotectOldBuffer();
      lowerBound = elems;
      protectNewBuffer();
    }
  }

  /* Disable protection on the smallest containing region. */
  MOZ_ALWAYS_INLINE void unprotectRegion(T* first, size_t size) {
#ifdef DEBUG
    regionUnprotected = true;
#endif
    if (MOZ_UNLIKELY(protectUsedEnabled)) {
      uintptr_t l = uintptr_t(first) >> pageShift;
      uintptr_t r = uintptr_t(first + size - 1) >> pageShift;
      if (r >= initPage && l < currPage) {
        unprotectRegionSlow(l, r);
      }
    }
  }

  /* Re-enable protection on the smallest containing region. */
  MOZ_ALWAYS_INLINE void reprotectRegion(T* first, size_t size) {
#ifdef DEBUG
    regionUnprotected = false;
#endif
    if (MOZ_UNLIKELY(protectUsedEnabled)) {
      uintptr_t l = uintptr_t(first) >> pageShift;
      uintptr_t r = uintptr_t(first + size - 1) >> pageShift;
      if (r >= initPage && l < currPage) {
        reprotectRegionSlow(l, r);
      }
    }
  }

  MOZ_ALWAYS_INLINE size_t capacity() const { return vector.capacity(); }
  MOZ_ALWAYS_INLINE size_t length() const { return vector.length(); }

  MOZ_ALWAYS_INLINE T* begin() { return vector.begin(); }
  MOZ_ALWAYS_INLINE const T* begin() const { return vector.begin(); }

  void clear() {
    unprotectOldBuffer();
    vector.clear();
    protectNewBuffer();
  }

  MOZ_ALWAYS_INLINE MOZ_MUST_USE bool reserve(size_t size) {
    if (MOZ_LIKELY(size <= capacity())) {
      return vector.reserve(size);
    }
    return reserveSlow(size);
  }

  template <typename U>
  MOZ_ALWAYS_INLINE void infallibleAppend(const U* values, size_t size) {
    elemsUntilTest -= size;
    if (MOZ_LIKELY(elemsUntilTest >= 0)) {
      return vector.infallibleAppend(values, size);
    }
    infallibleAppendSlow(values, size);
  }

  template <typename U>
  MOZ_ALWAYS_INLINE MOZ_MUST_USE bool append(const U* values, size_t size) {
    elemsUntilTest -= size;
    if (MOZ_LIKELY(elemsUntilTest >= 0)) {
      return vector.append(values, size);
    }
    return appendSlow(values, size);
  }
};

template <typename T, size_t A, class B, bool C, bool D, size_t E, bool F,
          uint8_t G>
MOZ_NEVER_INLINE void
PageProtectingVector<T, A, B, C, D, E, F, G>::unprotectRegionSlow(uintptr_t l,
                                                                  uintptr_t r) {
  if (l < initPage) {
    l = initPage;
  }
  if (r >= currPage) {
    r = currPage - 1;
  }
  T* addr = reinterpret_cast<T*>(l << pageShift);
  size_t size = (r - l + 1) << pageShift;
  gc::UnprotectPages(addr, size);
}

template <typename T, size_t A, class B, bool C, bool D, size_t E, bool F,
          uint8_t G>
MOZ_NEVER_INLINE void
PageProtectingVector<T, A, B, C, D, E, F, G>::reprotectRegionSlow(uintptr_t l,
                                                                  uintptr_t r) {
  if (l < initPage) {
    l = initPage;
  }
  if (r >= currPage) {
    r = currPage - 1;
  }
  T* addr = reinterpret_cast<T*>(l << pageShift);
  size_t size = (r - l + 1) << pageShift;
  gc::MakePagesReadOnly(addr, size);
}

template <typename T, size_t A, class B, bool C, bool D, size_t E, bool F,
          uint8_t G>
MOZ_NEVER_INLINE MOZ_MUST_USE bool
PageProtectingVector<T, A, B, C, D, E, F, G>::reserveSlow(size_t size) {
  return reserveNewBuffer(size);
}

template <typename T, size_t A, class B, bool C, bool D, size_t E, bool F,
          uint8_t G>
template <typename U>
MOZ_NEVER_INLINE void
PageProtectingVector<T, A, B, C, D, E, F, G>::infallibleAppendSlow(
    const U* values, size_t size) {
  // Ensure that we're here because we reached a page
  // boundary and not because of a buffer overflow.
  MOZ_RELEASE_ASSERT(
      MOZ_LIKELY(length() + size <= capacity()),
      "About to overflow our AssemblerBuffer using infallibleAppend!");
  infallibleAppendNewPage(values, size);
}

template <typename T, size_t A, class B, bool C, bool D, size_t E, bool F,
          uint8_t G>
template <typename U>
MOZ_NEVER_INLINE MOZ_MUST_USE bool
PageProtectingVector<T, A, B, C, D, E, F, G>::appendSlow(const U* values,
                                                         size_t size) {
  if (MOZ_LIKELY(length() + size <= capacity())) {
    return appendNewPage(values, size);
  }
  return appendNewBuffer(values, size);
}

} /* namespace js */

#endif /* ds_PageProtectingVector_h */