tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

NativeStackLimits.h (1662B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
      2 * vim: set ts=8 sts=2 et sw=2 tw=80:
      3 * This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #ifndef js_public_NativeStackLimit_h
      8 #define js_public_NativeStackLimit_h
      9 
     10 #include "mozilla/Assertions.h"  // MOZ_ASSERT
     11 
     12 #include <stddef.h>  // size_t
     13 #include <stdint.h>  // uint32_t, uintptr_t, UINTPTR_MAX
     14 #include <utility>   // std::move
     15 
     16 namespace JS {
     17 
     18 using NativeStackSize = size_t;
     19 
     20 using NativeStackBase = uintptr_t;
     21 
     22 using NativeStackLimit = uintptr_t;
     23 
     24 #if JS_STACK_GROWTH_DIRECTION > 0
     25 constexpr NativeStackLimit NativeStackLimitMin = 0;
     26 constexpr NativeStackLimit NativeStackLimitMax = UINTPTR_MAX;
     27 #else
     28 constexpr NativeStackLimit NativeStackLimitMin = UINTPTR_MAX;
     29 constexpr NativeStackLimit NativeStackLimitMax = 0;
     30 #endif
     31 
     32 #ifdef __wasi__
     33 // We build with the "stack-first" wasm-ld option, so the stack grows downward
     34 // toward zero. Let's set a limit just a bit above this so that we catch an
     35 // overflow before a Wasm trap occurs.
     36 constexpr NativeStackLimit WASINativeStackLimit = 1024;
     37 #endif  // __wasi__
     38 
     39 inline NativeStackLimit GetNativeStackLimit(NativeStackBase base,
     40                                            NativeStackSize size) {
     41 #if JS_STACK_GROWTH_DIRECTION > 0
     42  MOZ_ASSERT(base <= size_t(-1) - size);
     43  return base + size - 1;
     44 #else   // stack grows up
     45  MOZ_ASSERT(base >= size);
     46  return base - (size - 1);
     47 #endif  // stack grows down
     48 }
     49 
     50 }  // namespace JS
     51 
     52 #endif /* js_public_NativeStackLimit_h */