| 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_friend_StackLimits_h |
| 8 |
#define js_friend_StackLimits_h |
| 9 |
|
| 10 |
#include "mozilla/Attributes.h" // MOZ_ALWAYS_INLINE, MOZ_COLD |
| 11 |
#include "mozilla/Likely.h" // MOZ_LIKELY |
| 12 |
#include "mozilla/Variant.h" // mozilla::Variant, mozilla::AsVariant |
| 13 |
|
| 14 |
#include <stddef.h> // size_t |
| 15 |
|
| 16 |
#include "jstypes.h" // JS_PUBLIC_API |
| 17 |
|
| 18 |
#include "js/HeapAPI.h" // JS::StackKind, JS::StackForTrustedScript, JS::StackForUntrustedScript |
| 19 |
#include "js/RootingAPI.h" // JS::RootingContext |
| 20 |
#include "js/Stack.h" // JS::NativeStackLimit |
| 21 |
#include "js/Utility.h" // JS_STACK_OOM_POSSIBLY_FAIL |
| 22 |
|
| 23 |
struct JS_PUBLIC_API JSContext; |
| 24 |
|
| 25 |
#ifndef JS_STACK_GROWTH_DIRECTION |
| 26 |
# ifdef __hppa |
| 27 |
# define JS_STACK_GROWTH_DIRECTION (1) |
| 28 |
# else |
| 29 |
# define JS_STACK_GROWTH_DIRECTION (-1) |
| 30 |
# endif |
| 31 |
#endif |
| 32 |
|
| 33 |
namespace js { |
| 34 |
|
| 35 |
class FrontendContext; |
| 36 |
|
| 37 |
#ifdef __wasi__ |
| 38 |
extern MOZ_COLD JS_PUBLIC_API void IncWasiRecursionDepth(JSContext* cx); |
| 39 |
extern MOZ_COLD JS_PUBLIC_API void DecWasiRecursionDepth(JSContext* cx); |
| 40 |
extern MOZ_COLD JS_PUBLIC_API bool CheckWasiRecursionLimit(JSContext* cx); |
| 41 |
|
| 42 |
extern MOZ_COLD JS_PUBLIC_API void IncWasiRecursionDepth(FrontendContext* fc); |
| 43 |
extern MOZ_COLD JS_PUBLIC_API void DecWasiRecursionDepth(FrontendContext* fc); |
| 44 |
extern MOZ_COLD JS_PUBLIC_API bool CheckWasiRecursionLimit(FrontendContext* fc); |
| 45 |
#endif // __wasi__ |
| 46 |
|
| 47 |
// The minimum margin for stack limit to ensure that the periodic |
| 48 |
// AutoCheckRecursionLimit operation is sufficient. |
| 49 |
// |
| 50 |
// See FrontendContext::checkAndUpdateFrontendContextRecursionLimit and |
| 51 |
// JS::ThreadStackQuotaForSize. |
| 52 |
static constexpr size_t MinimumStackLimitMargin = 32 * 1024; |
| 53 |
|
| 54 |
// AutoCheckRecursionLimit can be used to check whether we're close to using up |
| 55 |
// the C++ stack. |
| 56 |
// |
| 57 |
// Typical usage is like this: |
| 58 |
// |
| 59 |
// AutoCheckRecursionLimit recursion(cx); |
| 60 |
// if (!recursion.check(cx)) { |
| 61 |
// return false; |
| 62 |
// } |
| 63 |
// |
| 64 |
// The check* functions return |false| if we are close to the stack limit. |
| 65 |
// They also report an overrecursion error, except for the DontReport variants. |
| 66 |
// |
| 67 |
// The checkSystem variant gives us a little extra space so we can ensure that |
| 68 |
// crucial code is able to run. |
| 69 |
// |
| 70 |
// checkConservative allows less space than any other check, including a safety |
| 71 |
// buffer (as in, it uses the untrusted limit and subtracts a little more from |
| 72 |
// it). |
| 73 |
class MOZ_RAII AutoCheckRecursionLimit { |
| 74 |
[[nodiscard]] MOZ_ALWAYS_INLINE bool checkLimitImpl( |
| 75 |
JS::NativeStackLimit limit, void* sp) const; |
| 76 |
|
| 77 |
MOZ_ALWAYS_INLINE JS::NativeStackLimit getStackLimitSlow(JSContext* cx) const; |
| 78 |
MOZ_ALWAYS_INLINE JS::NativeStackLimit getStackLimitHelper( |
| 79 |
JSContext* cx, JS::StackKind kind, int extraAllowance) const; |
| 80 |
|
| 81 |
JS::NativeStackLimit getStackLimit(FrontendContext* fc) const; |
| 82 |
|
| 83 |
JS_PUBLIC_API JS::StackKind stackKindForCurrentPrincipal(JSContext* cx) const; |
| 84 |
|
| 85 |
#ifdef __wasi__ |
| 86 |
// The JSContext outlives AutoCheckRecursionLimit so it is safe to use raw |
| 87 |
// pointer here. |
| 88 |
mozilla::Variant<JSContext*, FrontendContext*> context_; |
| 89 |
#endif // __wasi__ |
| 90 |
|
| 91 |
public: |
| 92 |
explicit MOZ_ALWAYS_INLINE AutoCheckRecursionLimit(JSContext* cx) |
| 93 |
#ifdef __wasi__ |
| 94 |
: context_(mozilla::AsVariant(cx)) |
| 95 |
#endif // __wasi__ |
| 96 |
{ |
| 97 |
#ifdef __wasi__ |
| 98 |
incWasiRecursionDepth(); |
| 99 |
#endif // __wasi__ |
| 100 |
} |
| 101 |
|
| 102 |
explicit MOZ_ALWAYS_INLINE AutoCheckRecursionLimit(FrontendContext* fc) |
| 103 |
#ifdef __wasi__ |
| 104 |
: context_(mozilla::AsVariant(fc)) |
| 105 |
#endif // __wasi__ |
| 106 |
{ |
| 107 |
#ifdef __wasi__ |
| 108 |
incWasiRecursionDepth(); |
| 109 |
#endif // __wasi__ |
| 110 |
} |
| 111 |
|
| 112 |
MOZ_ALWAYS_INLINE ~AutoCheckRecursionLimit() { |
| 113 |
#ifdef __wasi__ |
| 114 |
decWasiRecursionDepth(); |
| 115 |
#endif // __wasi__ |
| 116 |
} |
| 117 |
|
| 118 |
#ifdef __wasi__ |
| 119 |
MOZ_ALWAYS_INLINE void incWasiRecursionDepth() { |
| 120 |
if (context_.is<JSContext*>()) { |
| 121 |
JSContext* cx = context_.as<JSContext*>(); |
| 122 |
IncWasiRecursionDepth(cx); |
| 123 |
} else { |
| 124 |
FrontendContext* fc = context_.as<FrontendContext*>(); |
| 125 |
IncWasiRecursionDepth(fc); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
MOZ_ALWAYS_INLINE void decWasiRecursionDepth() { |
| 130 |
if (context_.is<JSContext*>()) { |
| 131 |
JSContext* cx = context_.as<JSContext*>(); |
| 132 |
DecWasiRecursionDepth(cx); |
| 133 |
} else { |
| 134 |
FrontendContext* fc = context_.as<FrontendContext*>(); |
| 135 |
DecWasiRecursionDepth(fc); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
MOZ_ALWAYS_INLINE bool checkWasiRecursionLimit() const { |
| 140 |
if (context_.is<JSContext*>()) { |
| 141 |
JSContext* cx = context_.as<JSContext*>(); |
| 142 |
if (!CheckWasiRecursionLimit(cx)) { |
| 143 |
return false; |
| 144 |
} |
| 145 |
} else { |
| 146 |
FrontendContext* fc = context_.as<FrontendContext*>(); |
| 147 |
if (!CheckWasiRecursionLimit(fc)) { |
| 148 |
return false; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
return true; |
| 153 |
} |
| 154 |
#endif // __wasi__ |
| 155 |
|
| 156 |
AutoCheckRecursionLimit(const AutoCheckRecursionLimit&) = delete; |
| 157 |
void operator=(const AutoCheckRecursionLimit&) = delete; |
| 158 |
|
| 159 |
[[nodiscard]] MOZ_ALWAYS_INLINE bool check(JSContext* cx) const; |
| 160 |
[[nodiscard]] MOZ_ALWAYS_INLINE bool check(FrontendContext* fc) const; |
| 161 |
[[nodiscard]] MOZ_ALWAYS_INLINE bool checkDontReport(JSContext* cx) const; |
| 162 |
[[nodiscard]] MOZ_ALWAYS_INLINE bool checkDontReport( |
| 163 |
FrontendContext* fc) const; |
| 164 |
[[nodiscard]] MOZ_ALWAYS_INLINE bool checkWithExtra(JSContext* cx, |
| 165 |
size_t extra) const; |
| 166 |
[[nodiscard]] MOZ_ALWAYS_INLINE bool checkWithStackPointerDontReport( |
| 167 |
JSContext* cx, void* sp) const; |
| 168 |
[[nodiscard]] MOZ_ALWAYS_INLINE bool checkWithStackPointerDontReport( |
| 169 |
FrontendContext* fc, void* sp) const; |
| 170 |
|
| 171 |
[[nodiscard]] MOZ_ALWAYS_INLINE bool checkConservative(JSContext* cx) const; |
| 172 |
[[nodiscard]] MOZ_ALWAYS_INLINE bool checkConservativeDontReport( |
| 173 |
JSContext* cx) const; |
| 174 |
[[nodiscard]] MOZ_ALWAYS_INLINE bool checkConservativeDontReport( |
| 175 |
JS::NativeStackLimit limit) const; |
| 176 |
|
| 177 |
[[nodiscard]] MOZ_ALWAYS_INLINE bool checkSystem(JSContext* cx) const; |
| 178 |
[[nodiscard]] MOZ_ALWAYS_INLINE bool checkSystemDontReport( |
| 179 |
JSContext* cx) const; |
| 180 |
}; |
| 181 |
|
| 182 |
extern MOZ_COLD JS_PUBLIC_API void ReportOverRecursed(JSContext* maybecx); |
| 183 |
extern MOZ_COLD JS_PUBLIC_API void ReportOverRecursed(FrontendContext* fc); |
| 184 |
|
| 185 |
MOZ_ALWAYS_INLINE bool AutoCheckRecursionLimit::checkLimitImpl( |
| 186 |
JS::NativeStackLimit limit, void* sp) const { |
| 187 |
JS_STACK_OOM_POSSIBLY_FAIL(); |
| 188 |
|
| 189 |
#ifdef __wasi__ |
| 190 |
if (!checkWasiRecursionLimit()) { |
| 191 |
return false; |
| 192 |
} |
| 193 |
#endif // __wasi__ |
| 194 |
|
| 195 |
#if JS_STACK_GROWTH_DIRECTION > 0 |
| 196 |
return MOZ_LIKELY(JS::NativeStackLimit(sp) < limit); |
| 197 |
#else |
| 198 |
return MOZ_LIKELY(JS::NativeStackLimit(sp) > limit); |
| 199 |
#endif |
| 200 |
} |
| 201 |
|
| 202 |
#ifdef ENABLE_WASM_JSPI |
| 203 |
bool IsSuspendableStackActive(JSContext* cx); |
| 204 |
JS::NativeStackLimit GetSuspendableStackLimit(JSContext* cx); |
| 205 |
#endif |
| 206 |
|
| 207 |
MOZ_ALWAYS_INLINE JS::NativeStackLimit |
| 208 |
AutoCheckRecursionLimit::getStackLimitSlow(JSContext* cx) const { |
| 209 |
JS::StackKind kind = stackKindForCurrentPrincipal(cx); |
| 210 |
#ifdef ENABLE_WASM_JSPI |
| 211 |
if (IsSuspendableStackActive(cx)) { |
| 212 |
MOZ_RELEASE_ASSERT(kind == JS::StackForUntrustedScript); |
| 213 |
return GetSuspendableStackLimit(cx); |
| 214 |
} |
| 215 |
#endif |
| 216 |
return getStackLimitHelper(cx, kind, 0); |
| 217 |
} |
| 218 |
|
| 219 |
MOZ_ALWAYS_INLINE JS::NativeStackLimit |
| 220 |
AutoCheckRecursionLimit::getStackLimitHelper(JSContext* cx, JS::StackKind kind, |
| 221 |
int extraAllowance) const { |
| 222 |
JS::NativeStackLimit limit = |
| 223 |
JS::RootingContext::get(cx)->nativeStackLimit[kind]; |
| 224 |
#if JS_STACK_GROWTH_DIRECTION > 0 |
| 225 |
limit += extraAllowance; |
| 226 |
#else |
| 227 |
limit -= extraAllowance; |
| 228 |
#endif |
| 229 |
return limit; |
| 230 |
} |
| 231 |
|
| 232 |
MOZ_ALWAYS_INLINE bool AutoCheckRecursionLimit::check(JSContext* cx) const { |
| 233 |
if (MOZ_UNLIKELY(!checkDontReport(cx))) { |
| 234 |
ReportOverRecursed(cx); |
| 235 |
return false; |
| 236 |
} |
| 237 |
return true; |
| 238 |
} |
| 239 |
|
| 240 |
MOZ_ALWAYS_INLINE bool AutoCheckRecursionLimit::check( |
| 241 |
FrontendContext* fc) const { |
| 242 |
if (MOZ_UNLIKELY(!checkDontReport(fc))) { |
| 243 |
ReportOverRecursed(fc); |
| 244 |
return false; |
| 245 |
} |
| 246 |
return true; |
| 247 |
} |
| 248 |
|
| 249 |
MOZ_ALWAYS_INLINE bool AutoCheckRecursionLimit::checkDontReport( |
| 250 |
JSContext* cx) const { |
| 251 |
return checkWithStackPointerDontReport(cx, __builtin_frame_address(0)); |
| 252 |
} |
| 253 |
|
| 254 |
MOZ_ALWAYS_INLINE bool AutoCheckRecursionLimit::checkDontReport( |
| 255 |
FrontendContext* fc) const { |
| 256 |
return checkWithStackPointerDontReport(fc, __builtin_frame_address(0)); |
| 257 |
} |
| 258 |
|
| 259 |
MOZ_ALWAYS_INLINE bool AutoCheckRecursionLimit::checkWithStackPointerDontReport( |
| 260 |
JSContext* cx, void* sp) const { |
| 261 |
// getStackLimitSlow(cx) is pretty slow because it has to do an uninlined |
| 262 |
// call to stackKindForCurrentPrincipal to determine which stack limit to |
| 263 |
// use. To work around this, check the untrusted limit first to avoid the |
| 264 |
// overhead in most cases. |
| 265 |
JS::NativeStackLimit untrustedLimit = |
| 266 |
getStackLimitHelper(cx, JS::StackForUntrustedScript, 0); |
| 267 |
if (MOZ_LIKELY(checkLimitImpl(untrustedLimit, sp))) { |
| 268 |
return true; |
| 269 |
} |
| 270 |
return checkLimitImpl(getStackLimitSlow(cx), sp); |
| 271 |
} |
| 272 |
|
| 273 |
#ifdef DEBUG |
| 274 |
extern void CheckAndUpdateFrontendContextRecursionLimit(FrontendContext* fc, |
| 275 |
void* sp); |
| 276 |
#endif |
| 277 |
|
| 278 |
MOZ_ALWAYS_INLINE bool AutoCheckRecursionLimit::checkWithStackPointerDontReport( |
| 279 |
FrontendContext* fc, void* sp) const { |
| 280 |
#ifdef DEBUG |
| 281 |
CheckAndUpdateFrontendContextRecursionLimit(fc, sp); |
| 282 |
#endif |
| 283 |
return checkLimitImpl(getStackLimit(fc), sp); |
| 284 |
} |
| 285 |
|
| 286 |
MOZ_ALWAYS_INLINE bool AutoCheckRecursionLimit::checkWithExtra( |
| 287 |
JSContext* cx, size_t extra) const { |
| 288 |
char* sp = reinterpret_cast<char*>(__builtin_frame_address(0)); |
| 289 |
#if JS_STACK_GROWTH_DIRECTION > 0 |
| 290 |
sp += extra; |
| 291 |
#else |
| 292 |
sp -= extra; |
| 293 |
#endif |
| 294 |
if (MOZ_UNLIKELY(!checkWithStackPointerDontReport(cx, sp))) { |
| 295 |
ReportOverRecursed(cx); |
| 296 |
return false; |
| 297 |
} |
| 298 |
return true; |
| 299 |
} |
| 300 |
|
| 301 |
MOZ_ALWAYS_INLINE bool AutoCheckRecursionLimit::checkSystem( |
| 302 |
JSContext* cx) const { |
| 303 |
if (MOZ_UNLIKELY(!checkSystemDontReport(cx))) { |
| 304 |
ReportOverRecursed(cx); |
| 305 |
return false; |
| 306 |
} |
| 307 |
return true; |
| 308 |
} |
| 309 |
|
| 310 |
MOZ_ALWAYS_INLINE bool AutoCheckRecursionLimit::checkSystemDontReport( |
| 311 |
JSContext* cx) const { |
| 312 |
JS::NativeStackLimit limit = |
| 313 |
getStackLimitHelper(cx, JS::StackForSystemCode, 0); |
| 314 |
return checkLimitImpl(limit, __builtin_frame_address(0)); |
| 315 |
} |
| 316 |
|
| 317 |
MOZ_ALWAYS_INLINE bool AutoCheckRecursionLimit::checkConservative( |
| 318 |
JSContext* cx) const { |
| 319 |
if (MOZ_UNLIKELY(!checkConservativeDontReport(cx))) { |
| 320 |
ReportOverRecursed(cx); |
| 321 |
return false; |
| 322 |
} |
| 323 |
return true; |
| 324 |
} |
| 325 |
|
| 326 |
MOZ_ALWAYS_INLINE bool AutoCheckRecursionLimit::checkConservativeDontReport( |
| 327 |
JSContext* cx) const { |
| 328 |
JS::NativeStackLimit limit = getStackLimitHelper( |
| 329 |
cx, JS::StackForUntrustedScript, -4096 * int(sizeof(size_t))); |
| 330 |
return checkLimitImpl(limit, __builtin_frame_address(0)); |
| 331 |
} |
| 332 |
|
| 333 |
MOZ_ALWAYS_INLINE bool AutoCheckRecursionLimit::checkConservativeDontReport( |
| 334 |
JS::NativeStackLimit limit) const { |
| 335 |
return checkLimitImpl(limit, __builtin_frame_address(0)); |
| 336 |
} |
| 337 |
|
| 338 |
} // namespace js |
| 339 |
|
| 340 |
#endif // js_friend_StackLimits_h |