| 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 |
/* |
| 8 |
* JS execution context. |
| 9 |
*/ |
| 10 |
|
| 11 |
#include "vm/JSContext-inl.h" |
| 12 |
|
| 13 |
#include "mozilla/CheckedInt.h" |
| 14 |
#include "mozilla/DebugOnly.h" |
| 15 |
#include "mozilla/MemoryReporting.h" |
| 16 |
#include "mozilla/Sprintf.h" |
| 17 |
#include "mozilla/Utf8.h" // mozilla::ConvertUtf16ToUtf8 |
| 18 |
|
| 19 |
#include <string.h> |
| 20 |
#ifdef ANDROID |
| 21 |
# include <android/log.h> |
| 22 |
# include <fstream> |
| 23 |
# include <string> |
| 24 |
#endif // ANDROID |
| 25 |
#ifdef XP_WIN |
| 26 |
# include <processthreadsapi.h> |
| 27 |
#endif // XP_WIN |
| 28 |
|
| 29 |
#include "jsapi.h" // JS_SetNativeStackQuota |
| 30 |
#include "jsexn.h" |
| 31 |
#include "jstypes.h" |
| 32 |
|
| 33 |
#include "builtin/RegExp.h" // js::RegExpSearcherLastLimitSentinel |
| 34 |
#ifdef MOZ_EXECUTION_TRACING |
| 35 |
# include "debugger/ExecutionTracer.h" |
| 36 |
#endif |
| 37 |
#include "frontend/FrontendContext.h" |
| 38 |
#include "gc/GC.h" |
| 39 |
#include "gc/PublicIterators.h" // js::RealmsIter |
| 40 |
#include "irregexp/RegExpAPI.h" |
| 41 |
#include "jit/Simulator.h" |
| 42 |
#include "js/CallAndConstruct.h" // JS::Call |
| 43 |
#include "js/CharacterEncoding.h" |
| 44 |
#include "js/ContextOptions.h" // JS::ContextOptions |
| 45 |
#include "js/ErrorInterceptor.h" // JSErrorInterceptor |
| 46 |
#include "js/friend/ErrorMessages.h" // js::GetErrorMessage, JSMSG_* |
| 47 |
#include "js/friend/StackLimits.h" // js::ReportOverRecursed |
| 48 |
#include "js/MemoryCallbacks.h" |
| 49 |
#include "js/Prefs.h" |
| 50 |
#include "js/Printf.h" |
| 51 |
#include "js/PropertyAndElement.h" // JS_GetProperty |
| 52 |
#include "js/Stack.h" // JS::NativeStackSize, JS::NativeStackLimit, JS::NativeStackLimitMin |
| 53 |
#include "util/DiagnosticAssertions.h" |
| 54 |
#include "util/DifferentialTesting.h" |
| 55 |
#include "util/DoubleToString.h" |
| 56 |
#include "util/NativeStack.h" |
| 57 |
#include "util/Text.h" |
| 58 |
#include "util/WindowsWrapper.h" |
| 59 |
#include "vm/BytecodeUtil.h" // JSDVG_IGNORE_STACK |
| 60 |
#include "vm/ErrorObject.h" |
| 61 |
#include "vm/ErrorReporting.h" |
| 62 |
#include "vm/FrameIter.h" |
| 63 |
#include "vm/JSFunction.h" |
| 64 |
#include "vm/JSObject.h" |
| 65 |
#include "vm/PlainObject.h" // js::PlainObject |
| 66 |
#include "vm/Realm.h" |
| 67 |
#include "vm/StringType.h" // StringToNewUTF8CharsZ |
| 68 |
#include "vm/ToSource.h" // js::ValueToSource |
| 69 |
|
| 70 |
#include "vm/Compartment-inl.h" |
| 71 |
#include "vm/Stack-inl.h" |
| 72 |
|
| 73 |
using namespace js; |
| 74 |
|
| 75 |
#ifdef DEBUG |
| 76 |
JSContext* js::MaybeGetJSContext() { |
| 77 |
if (!TlsContext.init()) { |
| 78 |
return nullptr; |
| 79 |
} |
| 80 |
return TlsContext.get(); |
| 81 |
} |
| 82 |
#endif |
| 83 |
|
| 84 |
bool js::AutoCycleDetector::init() { |
| 85 |
MOZ_ASSERT(cyclic); |
| 86 |
|
| 87 |
AutoCycleDetector::Vector& vector = cx->cycleDetectorVector(); |
| 88 |
|
| 89 |
for (JSObject* obj2 : vector) { |
| 90 |
if (MOZ_UNLIKELY(obj == obj2)) { |
| 91 |
return true; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
if (!vector.append(obj)) { |
| 96 |
return false; |
| 97 |
} |
| 98 |
|
| 99 |
cyclic = false; |
| 100 |
return true; |
| 101 |
} |
| 102 |
|
| 103 |
js::AutoCycleDetector::~AutoCycleDetector() { |
| 104 |
if (MOZ_LIKELY(!cyclic)) { |
| 105 |
AutoCycleDetector::Vector& vec = cx->cycleDetectorVector(); |
| 106 |
MOZ_ASSERT(vec.back() == obj); |
| 107 |
if (vec.length() > 1) { |
| 108 |
vec.popBack(); |
| 109 |
} else { |
| 110 |
// Avoid holding on to unused heap allocations. |
| 111 |
vec.clearAndFree(); |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
bool JSContext::init() { |
| 117 |
TlsContext.set(this); |
| 118 |
nativeStackBase_.emplace(GetNativeStackBase()); |
| 119 |
|
| 120 |
if (!fx.initInstance()) { |
| 121 |
return false; |
| 122 |
} |
| 123 |
|
| 124 |
#ifdef JS_SIMULATOR |
| 125 |
simulator_ = jit::Simulator::Create(); |
| 126 |
if (!simulator_) { |
| 127 |
return false; |
| 128 |
} |
| 129 |
#endif |
| 130 |
|
| 131 |
isolate = irregexp::CreateIsolate(this); |
| 132 |
if (!isolate) { |
| 133 |
return false; |
| 134 |
} |
| 135 |
|
| 136 |
#ifdef DEBUG |
| 137 |
// Set the initialized_ last, so that ProtectedData checks will allow us to |
| 138 |
// initialize this context before it becomes the runtime's active context. |
| 139 |
initialized_ = true; |
| 140 |
#endif |
| 141 |
|
| 142 |
return true; |
| 143 |
} |
| 144 |
|
| 145 |
static void InitDefaultStackQuota(JSContext* cx) { |
| 146 |
// Initialize stack quota to a reasonable default. Embedders can override this |
| 147 |
// by calling JS_SetNativeStackQuota. |
| 148 |
// |
| 149 |
// NOTE: Firefox overrides these values. For the main thread this happens in |
| 150 |
// XPCJSContext::Initialize. |
| 151 |
|
| 152 |
#if defined(MOZ_ASAN) || (defined(DEBUG) && !defined(XP_WIN)) |
| 153 |
static constexpr JS::NativeStackSize MaxStackSize = |
| 154 |
2 * 128 * sizeof(size_t) * 1024; |
| 155 |
#else |
| 156 |
static constexpr JS::NativeStackSize MaxStackSize = |
| 157 |
128 * sizeof(size_t) * 1024; |
| 158 |
#endif |
| 159 |
JS_SetNativeStackQuota(cx, MaxStackSize); |
| 160 |
} |
| 161 |
|
| 162 |
JSContext* js::NewContext(uint32_t maxBytes, JSRuntime* parentRuntime) { |
| 163 |
AutoNoteSingleThreadedRegion anstr; |
| 164 |
|
| 165 |
MOZ_RELEASE_ASSERT(!TlsContext.get()); |
| 166 |
|
| 167 |
#if defined(DEBUG) || defined(JS_OOM_BREAKPOINT) |
| 168 |
js::oom::SetThreadType(!parentRuntime ? js::THREAD_TYPE_MAIN |
| 169 |
: js::THREAD_TYPE_WORKER); |
| 170 |
#endif |
| 171 |
|
| 172 |
JSRuntime* runtime = js_new<JSRuntime>(parentRuntime); |
| 173 |
if (!runtime) { |
| 174 |
return nullptr; |
| 175 |
} |
| 176 |
|
| 177 |
JSContext* cx = js_new<JSContext>(runtime, JS::ContextOptions()); |
| 178 |
if (!cx) { |
| 179 |
js_delete(runtime); |
| 180 |
return nullptr; |
| 181 |
} |
| 182 |
|
| 183 |
if (!cx->init()) { |
| 184 |
js_delete(cx); |
| 185 |
js_delete(runtime); |
| 186 |
return nullptr; |
| 187 |
} |
| 188 |
|
| 189 |
if (!runtime->init(cx, maxBytes)) { |
| 190 |
runtime->destroyRuntime(); |
| 191 |
js_delete(cx); |
| 192 |
js_delete(runtime); |
| 193 |
return nullptr; |
| 194 |
} |
| 195 |
|
| 196 |
// Initialize stack quota last because simulators rely on the JSRuntime having |
| 197 |
// been initialized. |
| 198 |
InitDefaultStackQuota(cx); |
| 199 |
|
| 200 |
return cx; |
| 201 |
} |
| 202 |
|
| 203 |
void js::DestroyContext(JSContext* cx) { |
| 204 |
JS_AbortIfWrongThread(cx); |
| 205 |
|
| 206 |
MOZ_ASSERT(!cx->realm(), "Shouldn't destroy context with active realm"); |
| 207 |
MOZ_ASSERT(!cx->activation(), "Shouldn't destroy context with activations"); |
| 208 |
|
| 209 |
cx->checkNoGCRooters(); |
| 210 |
|
| 211 |
// Cancel all off thread compiles. Completed compiles may try to |
| 212 |
// interrupt this context. See HelperThread::handleIonWorkload. |
| 213 |
CancelOffThreadCompile(cx->runtime()); |
| 214 |
|
| 215 |
cx->jobQueue = nullptr; |
| 216 |
cx->internalJobQueue = nullptr; |
| 217 |
SetContextProfilingStack(cx, nullptr); |
| 218 |
|
| 219 |
JSRuntime* rt = cx->runtime(); |
| 220 |
|
| 221 |
// Flush promise tasks executing in helper threads early, before any parts |
| 222 |
// of the JSRuntime that might be visible to helper threads are torn down. |
| 223 |
rt->offThreadPromiseState.ref().shutdown(cx); |
| 224 |
|
| 225 |
// Destroy the runtime along with its last context. |
| 226 |
js::AutoNoteSingleThreadedRegion nochecks; |
| 227 |
rt->destroyRuntime(); |
| 228 |
js_delete_poison(cx); |
| 229 |
js_delete_poison(rt); |
| 230 |
} |
| 231 |
|
| 232 |
void JS::RootingContext::checkNoGCRooters() { |
| 233 |
#ifdef DEBUG |
| 234 |
for (auto const& stackRootPtr : stackRoots_) { |
| 235 |
MOZ_ASSERT(stackRootPtr == nullptr); |
| 236 |
} |
| 237 |
#endif |
| 238 |
} |
| 239 |
|
| 240 |
bool AutoResolving::alreadyStartedSlow() const { |
| 241 |
MOZ_ASSERT(link); |
| 242 |
AutoResolving* cursor = link; |
| 243 |
do { |
| 244 |
MOZ_ASSERT(this != cursor); |
| 245 |
if (object.get() == cursor->object && id.get() == cursor->id) { |
| 246 |
return true; |
| 247 |
} |
| 248 |
} while (!!(cursor = cursor->link)); |
| 249 |
return false; |
| 250 |
} |
| 251 |
|
| 252 |
static void MaybeReportOutOfMemoryForDifferentialTesting() { |
| 253 |
/* |
| 254 |
* OOMs are non-deterministic, especially across different execution modes |
| 255 |
* (e.g. interpreter vs JIT). When doing differential testing, print to stderr |
| 256 |
* so that the fuzzers can detect this. |
| 257 |
*/ |
| 258 |
if (js::SupportDifferentialTesting()) { |
| 259 |
fprintf(stderr, "ReportOutOfMemory called\n"); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
/* |
| 264 |
* Since memory has been exhausted, avoid the normal error-handling path which |
| 265 |
* allocates an error object, report and callstack. Instead simply throw the |
| 266 |
* static atom "out of memory". |
| 267 |
* |
| 268 |
* Furthermore, callers of ReportOutOfMemory (viz., malloc) assume a GC does |
| 269 |
* not occur, so GC must be avoided or suppressed. |
| 270 |
*/ |
| 271 |
void JSContext::onOutOfMemory() { |
| 272 |
runtime()->hadOutOfMemory = true; |
| 273 |
gc::AutoSuppressGC suppressGC(this); |
| 274 |
|
| 275 |
/* Report the oom. */ |
| 276 |
if (JS::OutOfMemoryCallback oomCallback = runtime()->oomCallback) { |
| 277 |
oomCallback(this, runtime()->oomCallbackData); |
| 278 |
} |
| 279 |
|
| 280 |
// If we OOM early in process startup, this may be unavailable so just return |
| 281 |
// instead of crashing unexpectedly. |
| 282 |
if (MOZ_UNLIKELY(!runtime()->hasInitializedSelfHosting())) { |
| 283 |
return; |
| 284 |
} |
| 285 |
|
| 286 |
RootedValue oomMessage(this, StringValue(names().out_of_memory_)); |
| 287 |
setPendingException(oomMessage, nullptr); |
| 288 |
MOZ_ASSERT(status == JS::ExceptionStatus::Throwing); |
| 289 |
status = JS::ExceptionStatus::OutOfMemory; |
| 290 |
|
| 291 |
reportResourceExhaustion(); |
| 292 |
} |
| 293 |
|
| 294 |
JS_PUBLIC_API void js::ReportOutOfMemory(JSContext* cx) { |
| 295 |
MaybeReportOutOfMemoryForDifferentialTesting(); |
| 296 |
|
| 297 |
cx->onOutOfMemory(); |
| 298 |
} |
| 299 |
|
| 300 |
JS_PUBLIC_API void js::ReportLargeOutOfMemory(JSContext* cx) { |
| 301 |
js::ReportOutOfMemory(cx); |
| 302 |
} |
| 303 |
|
| 304 |
JS_PUBLIC_API void js::ReportOutOfMemory(FrontendContext* fc) { |
| 305 |
MaybeReportOutOfMemoryForDifferentialTesting(); |
| 306 |
|
| 307 |
fc->onOutOfMemory(); |
| 308 |
} |
| 309 |
|
| 310 |
static void MaybeReportOverRecursedForDifferentialTesting() { |
| 311 |
/* |
| 312 |
* We cannot make stack depth deterministic across different |
| 313 |
* implementations (e.g. JIT vs. interpreter will differ in |
| 314 |
* their maximum stack depth). |
| 315 |
* However, we can detect externally when we hit the maximum |
| 316 |
* stack depth which is useful for external testing programs |
| 317 |
* like fuzzers. |
| 318 |
*/ |
| 319 |
if (js::SupportDifferentialTesting()) { |
| 320 |
fprintf(stderr, "ReportOverRecursed called\n"); |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
void JSContext::onOverRecursed() { |
| 325 |
// Try to construct an over-recursed error and then update the exception |
| 326 |
// status to `OverRecursed`. Creating the error can fail, so check there |
| 327 |
// is a reasonable looking exception pending before updating status. |
| 328 |
JS_ReportErrorNumberASCII(this, GetErrorMessage, nullptr, |
| 329 |
JSMSG_OVER_RECURSED); |
| 330 |
if (isExceptionPending() && !isThrowingOutOfMemory()) { |
| 331 |
MOZ_ASSERT(unwrappedException().isObject()); |
| 332 |
MOZ_ASSERT(status == JS::ExceptionStatus::Throwing); |
| 333 |
status = JS::ExceptionStatus::OverRecursed; |
| 334 |
} |
| 335 |
|
| 336 |
reportResourceExhaustion(); |
| 337 |
} |
| 338 |
|
| 339 |
JS_PUBLIC_API void js::ReportOverRecursed(JSContext* maybecx) { |
| 340 |
MaybeReportOverRecursedForDifferentialTesting(); |
| 341 |
|
| 342 |
if (!maybecx) { |
| 343 |
return; |
| 344 |
} |
| 345 |
|
| 346 |
maybecx->onOverRecursed(); |
| 347 |
} |
| 348 |
|
| 349 |
JS_PUBLIC_API void js::ReportOverRecursed(FrontendContext* fc) { |
| 350 |
MaybeReportOverRecursedForDifferentialTesting(); |
| 351 |
|
| 352 |
fc->onOverRecursed(); |
| 353 |
} |
| 354 |
|
| 355 |
void js::ReportOversizedAllocation(JSContext* cx, const unsigned errorNumber) { |
| 356 |
// The JIT may optimize away allocations if it determines that they aren't |
| 357 |
// used. This can affect whether we throw an exception when the size of an |
| 358 |
// allocation exceeds implementation-defined limits (eg JSString::MAX_LENGTH). |
| 359 |
// These errors aren't interesting for the purposes of differential fuzzing. |
| 360 |
// We print a message so that fuzzers can detect this case. To simplify |
| 361 |
// tooling updates, we use the same message as ReportOutOfMemory. |
| 362 |
if (js::SupportDifferentialTesting()) { |
| 363 |
fprintf(stderr, "ReportOutOfMemory called\n"); |
| 364 |
} |
| 365 |
|
| 366 |
gc::AutoSuppressGC suppressGC(cx); |
| 367 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, errorNumber); |
| 368 |
|
| 369 |
cx->reportResourceExhaustion(); |
| 370 |
} |
| 371 |
|
| 372 |
void js::ReportAllocationOverflow(JSContext* cx) { |
| 373 |
if (js::SupportDifferentialTesting()) { |
| 374 |
fprintf(stderr, "ReportAllocationOverflow called\n"); |
| 375 |
} |
| 376 |
|
| 377 |
if (!cx) { |
| 378 |
return; |
| 379 |
} |
| 380 |
|
| 381 |
cx->reportAllocationOverflow(); |
| 382 |
} |
| 383 |
|
| 384 |
void js::ReportAllocationOverflow(FrontendContext* fc) { |
| 385 |
fc->onAllocationOverflow(); |
| 386 |
} |
| 387 |
|
| 388 |
/* |callee| requires a usage string provided by JS_DefineFunctionsWithHelp. */ |
| 389 |
void js::ReportUsageErrorASCII(JSContext* cx, HandleObject callee, |
| 390 |
const char* msg) { |
| 391 |
RootedValue usage(cx); |
| 392 |
if (!JS_GetProperty(cx, callee, "usage", &usage)) { |
| 393 |
return; |
| 394 |
} |
| 395 |
|
| 396 |
if (!usage.isString()) { |
| 397 |
JS_ReportErrorASCII(cx, "%s", msg); |
| 398 |
} else { |
| 399 |
RootedString usageStr(cx, usage.toString()); |
| 400 |
UniqueChars str = JS_EncodeStringToUTF8(cx, usageStr); |
| 401 |
if (!str) { |
| 402 |
return; |
| 403 |
} |
| 404 |
JS_ReportErrorUTF8(cx, "%s. Usage: %s", msg, str.get()); |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
enum class PrintErrorKind { Error, Warning, Note }; |
| 409 |
|
| 410 |
static void PrintErrorLine(FILE* file, const char* prefix, |
| 411 |
JSErrorReport* report) { |
| 412 |
if (const char16_t* linebuf = report->linebuf()) { |
| 413 |
UniqueChars line; |
| 414 |
size_t n; |
| 415 |
{ |
| 416 |
size_t linebufLen = report->linebufLength(); |
| 417 |
|
| 418 |
// This function is only used for shell command-line sorts of stuff where |
| 419 |
// performance doesn't really matter, so just encode into max-sized |
| 420 |
// memory. |
| 421 |
mozilla::CheckedInt<size_t> utf8Len(linebufLen); |
| 422 |
utf8Len *= 3; |
| 423 |
if (utf8Len.isValid()) { |
| 424 |
line = UniqueChars(js_pod_malloc<char>(utf8Len.value())); |
| 425 |
if (line) { |
| 426 |
n = mozilla::ConvertUtf16toUtf8({linebuf, linebufLen}, |
| 427 |
{line.get(), utf8Len.value()}); |
| 428 |
} |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
const char* utf8buf; |
| 433 |
if (line) { |
| 434 |
utf8buf = line.get(); |
| 435 |
} else { |
| 436 |
static const char unavailableStr[] = "<context unavailable>"; |
| 437 |
utf8buf = unavailableStr; |
| 438 |
n = js_strlen(unavailableStr); |
| 439 |
} |
| 440 |
|
| 441 |
fputs(":\n", file); |
| 442 |
if (prefix) { |
| 443 |
fputs(prefix, file); |
| 444 |
} |
| 445 |
|
| 446 |
for (size_t i = 0; i < n; i++) { |
| 447 |
fputc(utf8buf[i], file); |
| 448 |
} |
| 449 |
|
| 450 |
// linebuf/utf8buf usually ends with a newline. If not, add one here. |
| 451 |
if (n == 0 || utf8buf[n - 1] != '\n') { |
| 452 |
fputc('\n', file); |
| 453 |
} |
| 454 |
|
| 455 |
if (prefix) { |
| 456 |
fputs(prefix, file); |
| 457 |
} |
| 458 |
|
| 459 |
n = report->tokenOffset(); |
| 460 |
for (size_t i = 0, j = 0; i < n; i++) { |
| 461 |
if (utf8buf[i] == '\t') { |
| 462 |
for (size_t k = (j + 8) & ~7; j < k; j++) { |
| 463 |
fputc('.', file); |
| 464 |
} |
| 465 |
continue; |
| 466 |
} |
| 467 |
fputc('.', file); |
| 468 |
j++; |
| 469 |
} |
| 470 |
fputc('^', file); |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
static void PrintErrorLine(FILE* file, const char* prefix, |
| 475 |
JSErrorNotes::Note* note) {} |
| 476 |
|
| 477 |
template <typename T> |
| 478 |
static void PrintSingleError(FILE* file, JS::ConstUTF8CharsZ toStringResult, |
| 479 |
T* report, PrintErrorKind kind) { |
| 480 |
UniqueChars prefix; |
| 481 |
if (report->filename) { |
| 482 |
prefix = JS_smprintf("%s:", report->filename.c_str()); |
| 483 |
} |
| 484 |
|
| 485 |
if (report->lineno) { |
| 486 |
prefix = JS_smprintf("%s%u:%u ", prefix ? prefix.get() : "", report->lineno, |
| 487 |
report->column.oneOriginValue()); |
| 488 |
} |
| 489 |
|
| 490 |
if (kind != PrintErrorKind::Error) { |
| 491 |
const char* kindPrefix = nullptr; |
| 492 |
switch (kind) { |
| 493 |
case PrintErrorKind::Error: |
| 494 |
MOZ_CRASH("unreachable"); |
| 495 |
case PrintErrorKind::Warning: |
| 496 |
kindPrefix = "warning"; |
| 497 |
break; |
| 498 |
case PrintErrorKind::Note: |
| 499 |
kindPrefix = "note"; |
| 500 |
break; |
| 501 |
} |
| 502 |
|
| 503 |
prefix = JS_smprintf("%s%s: ", prefix ? prefix.get() : "", kindPrefix); |
| 504 |
} |
| 505 |
|
| 506 |
const char* message = |
| 507 |
toStringResult ? toStringResult.c_str() : report->message().c_str(); |
| 508 |
|
| 509 |
/* embedded newlines -- argh! */ |
| 510 |
const char* ctmp; |
| 511 |
while ((ctmp = strchr(message, '\n')) != 0) { |
| 512 |
ctmp++; |
| 513 |
if (prefix) { |
| 514 |
fputs(prefix.get(), file); |
| 515 |
} |
| 516 |
(void)fwrite(message, 1, ctmp - message, file); |
| 517 |
message = ctmp; |
| 518 |
} |
| 519 |
|
| 520 |
/* If there were no filename or lineno, the prefix might be empty */ |
| 521 |
if (prefix) { |
| 522 |
fputs(prefix.get(), file); |
| 523 |
} |
| 524 |
fputs(message, file); |
| 525 |
|
| 526 |
PrintErrorLine(file, prefix.get(), report); |
| 527 |
fputc('\n', file); |
| 528 |
|
| 529 |
fflush(file); |
| 530 |
} |
| 531 |
|
| 532 |
static void PrintErrorImpl(FILE* file, JS::ConstUTF8CharsZ toStringResult, |
| 533 |
JSErrorReport* report, bool reportWarnings) { |
| 534 |
MOZ_ASSERT(report); |
| 535 |
|
| 536 |
/* Conditionally ignore reported warnings. */ |
| 537 |
if (report->isWarning() && !reportWarnings) { |
| 538 |
return; |
| 539 |
} |
| 540 |
|
| 541 |
PrintErrorKind kind = PrintErrorKind::Error; |
| 542 |
if (report->isWarning()) { |
| 543 |
kind = PrintErrorKind::Warning; |
| 544 |
} |
| 545 |
PrintSingleError(file, toStringResult, report, kind); |
| 546 |
|
| 547 |
if (report->notes) { |
| 548 |
for (auto&& note : *report->notes) { |
| 549 |
PrintSingleError(file, JS::ConstUTF8CharsZ(), note.get(), |
| 550 |
PrintErrorKind::Note); |
| 551 |
} |
| 552 |
} |
| 553 |
} |
| 554 |
|
| 555 |
JS_PUBLIC_API void JS::PrintError(FILE* file, JSErrorReport* report, |
| 556 |
bool reportWarnings) { |
| 557 |
PrintErrorImpl(file, JS::ConstUTF8CharsZ(), report, reportWarnings); |
| 558 |
} |
| 559 |
|
| 560 |
JS_PUBLIC_API void JS::PrintError(FILE* file, |
| 561 |
const JS::ErrorReportBuilder& builder, |
| 562 |
bool reportWarnings) { |
| 563 |
PrintErrorImpl(file, builder.toStringResult(), builder.report(), |
| 564 |
reportWarnings); |
| 565 |
} |
| 566 |
|
| 567 |
void js::ReportIsNotDefined(JSContext* cx, HandleId id) { |
| 568 |
if (UniqueChars printable = |
| 569 |
IdToPrintableUTF8(cx, id, IdToPrintableBehavior::IdIsIdentifier)) { |
| 570 |
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, JSMSG_NOT_DEFINED, |
| 571 |
printable.get()); |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
void js::ReportIsNotDefined(JSContext* cx, Handle<PropertyName*> name) { |
| 576 |
RootedId id(cx, NameToId(name)); |
| 577 |
ReportIsNotDefined(cx, id); |
| 578 |
} |
| 579 |
|
| 580 |
const char* NullOrUndefinedToCharZ(HandleValue v) { |
| 581 |
MOZ_ASSERT(v.isNullOrUndefined()); |
| 582 |
return v.isNull() ? "null" : "undefined"; |
| 583 |
} |
| 584 |
|
| 585 |
void js::ReportIsNullOrUndefinedForPropertyAccess(JSContext* cx, HandleValue v, |
| 586 |
int vIndex) { |
| 587 |
MOZ_ASSERT(v.isNullOrUndefined()); |
| 588 |
|
| 589 |
if (vIndex == JSDVG_IGNORE_STACK) { |
| 590 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 591 |
JSMSG_CANT_CONVERT_TO, NullOrUndefinedToCharZ(v), |
| 592 |
"object"); |
| 593 |
return; |
| 594 |
} |
| 595 |
|
| 596 |
UniqueChars bytes = DecompileValueGenerator(cx, vIndex, v, nullptr); |
| 597 |
if (!bytes) { |
| 598 |
return; |
| 599 |
} |
| 600 |
|
| 601 |
if (strcmp(bytes.get(), "undefined") == 0 || |
| 602 |
strcmp(bytes.get(), "null") == 0) { |
| 603 |
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, JSMSG_NO_PROPERTIES, |
| 604 |
bytes.get()); |
| 605 |
} else { |
| 606 |
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, |
| 607 |
JSMSG_UNEXPECTED_TYPE, bytes.get(), |
| 608 |
NullOrUndefinedToCharZ(v)); |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
void js::ReportIsNullOrUndefinedForPropertyAccess(JSContext* cx, HandleValue v, |
| 613 |
int vIndex, HandleId key) { |
| 614 |
MOZ_ASSERT(v.isNullOrUndefined()); |
| 615 |
|
| 616 |
if (!JS::Prefs::property_error_message_fix()) { |
| 617 |
ReportIsNullOrUndefinedForPropertyAccess(cx, v, vIndex); |
| 618 |
return; |
| 619 |
} |
| 620 |
|
| 621 |
RootedValue idVal(cx, IdToValue(key)); |
| 622 |
RootedString idStr(cx, ValueToSource(cx, idVal)); |
| 623 |
if (!idStr) { |
| 624 |
return; |
| 625 |
} |
| 626 |
|
| 627 |
UniqueChars keyStr = StringToNewUTF8CharsZ(cx, *idStr); |
| 628 |
if (!keyStr) { |
| 629 |
return; |
| 630 |
} |
| 631 |
|
| 632 |
if (vIndex == JSDVG_IGNORE_STACK) { |
| 633 |
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, JSMSG_PROPERTY_FAIL, |
| 634 |
keyStr.get(), NullOrUndefinedToCharZ(v)); |
| 635 |
return; |
| 636 |
} |
| 637 |
|
| 638 |
UniqueChars bytes = DecompileValueGenerator(cx, vIndex, v, nullptr); |
| 639 |
if (!bytes) { |
| 640 |
return; |
| 641 |
} |
| 642 |
|
| 643 |
if (strcmp(bytes.get(), "undefined") == 0 || |
| 644 |
strcmp(bytes.get(), "null") == 0) { |
| 645 |
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, JSMSG_PROPERTY_FAIL, |
| 646 |
keyStr.get(), bytes.get()); |
| 647 |
return; |
| 648 |
} |
| 649 |
|
| 650 |
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, |
| 651 |
JSMSG_PROPERTY_FAIL_EXPR, keyStr.get(), bytes.get(), |
| 652 |
NullOrUndefinedToCharZ(v)); |
| 653 |
} |
| 654 |
|
| 655 |
bool js::ReportValueError(JSContext* cx, const unsigned errorNumber, |
| 656 |
int spindex, HandleValue v, HandleString fallback, |
| 657 |
const char* arg1, const char* arg2) { |
| 658 |
MOZ_ASSERT(js_ErrorFormatString[errorNumber].argCount >= 1); |
| 659 |
MOZ_ASSERT(js_ErrorFormatString[errorNumber].argCount <= 3); |
| 660 |
UniqueChars bytes = DecompileValueGenerator(cx, spindex, v, fallback); |
| 661 |
if (!bytes) { |
| 662 |
return false; |
| 663 |
} |
| 664 |
|
| 665 |
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, errorNumber, |
| 666 |
bytes.get(), arg1, arg2); |
| 667 |
return false; |
| 668 |
} |
| 669 |
|
| 670 |
JSObject* js::CreateErrorNotesArray(JSContext* cx, JSErrorReport* report) { |
| 671 |
Rooted<ArrayObject*> notesArray(cx, NewDenseEmptyArray(cx)); |
| 672 |
if (!notesArray) { |
| 673 |
return nullptr; |
| 674 |
} |
| 675 |
|
| 676 |
if (!report->notes) { |
| 677 |
return notesArray; |
| 678 |
} |
| 679 |
|
| 680 |
for (auto&& note : *report->notes) { |
| 681 |
Rooted<PlainObject*> noteObj(cx, NewPlainObject(cx)); |
| 682 |
if (!noteObj) { |
| 683 |
return nullptr; |
| 684 |
} |
| 685 |
|
| 686 |
RootedString messageStr(cx, note->newMessageString(cx)); |
| 687 |
if (!messageStr) { |
| 688 |
return nullptr; |
| 689 |
} |
| 690 |
RootedValue messageVal(cx, StringValue(messageStr)); |
| 691 |
if (!DefineDataProperty(cx, noteObj, cx->names().message, messageVal)) { |
| 692 |
return nullptr; |
| 693 |
} |
| 694 |
|
| 695 |
RootedValue filenameVal(cx); |
| 696 |
if (const char* filename = note->filename.c_str()) { |
| 697 |
JS::UTF8Chars utf8chars(filename, strlen(filename)); |
| 698 |
Rooted<JSString*> filenameStr(cx, NewStringCopyUTF8N(cx, utf8chars)); |
| 699 |
if (!filenameStr) { |
| 700 |
return nullptr; |
| 701 |
} |
| 702 |
filenameVal = StringValue(filenameStr); |
| 703 |
} |
| 704 |
if (!DefineDataProperty(cx, noteObj, cx->names().fileName, filenameVal)) { |
| 705 |
return nullptr; |
| 706 |
} |
| 707 |
|
| 708 |
RootedValue linenoVal(cx, Int32Value(note->lineno)); |
| 709 |
if (!DefineDataProperty(cx, noteObj, cx->names().lineNumber, linenoVal)) { |
| 710 |
return nullptr; |
| 711 |
} |
| 712 |
RootedValue columnVal(cx, Int32Value(note->column.oneOriginValue())); |
| 713 |
if (!DefineDataProperty(cx, noteObj, cx->names().columnNumber, columnVal)) { |
| 714 |
return nullptr; |
| 715 |
} |
| 716 |
|
| 717 |
if (!NewbornArrayPush(cx, notesArray, ObjectValue(*noteObj))) { |
| 718 |
return nullptr; |
| 719 |
} |
| 720 |
} |
| 721 |
|
| 722 |
return notesArray; |
| 723 |
} |
| 724 |
|
| 725 |
void JSContext::recoverFromOutOfMemory() { |
| 726 |
if (isExceptionPending()) { |
| 727 |
MOZ_ASSERT(isThrowingOutOfMemory()); |
| 728 |
clearPendingException(); |
| 729 |
} |
| 730 |
} |
| 731 |
|
| 732 |
void JSContext::reportAllocationOverflow() { |
| 733 |
gc::AutoSuppressGC suppressGC(this); |
| 734 |
JS_ReportErrorNumberASCII(this, GetErrorMessage, nullptr, |
| 735 |
JSMSG_ALLOC_OVERFLOW); |
| 736 |
} |
| 737 |
|
| 738 |
JS::StackKind JSContext::stackKindForCurrentPrincipal() { |
| 739 |
return runningWithTrustedPrincipals() ? JS::StackForTrustedScript |
| 740 |
: JS::StackForUntrustedScript; |
| 741 |
} |
| 742 |
|
| 743 |
JS::NativeStackLimit JSContext::stackLimitForCurrentPrincipal() { |
| 744 |
return stackLimit(stackKindForCurrentPrincipal()); |
| 745 |
} |
| 746 |
|
| 747 |
JS_PUBLIC_API bool js::UseInternalJobQueues(JSContext* cx) { |
| 748 |
// Internal job queue handling must be set up very early. Self-hosting |
| 749 |
// initialization is as good a marker for that as any. |
| 750 |
MOZ_RELEASE_ASSERT( |
| 751 |
!cx->runtime()->hasInitializedSelfHosting(), |
| 752 |
"js::UseInternalJobQueues must be called early during runtime startup."); |
| 753 |
MOZ_ASSERT(!cx->jobQueue); |
| 754 |
auto queue = MakeUnique<InternalJobQueue>(cx); |
| 755 |
if (!queue) { |
| 756 |
return false; |
| 757 |
} |
| 758 |
|
| 759 |
cx->internalJobQueue = std::move(queue); |
| 760 |
cx->jobQueue = cx->internalJobQueue.ref().get(); |
| 761 |
|
| 762 |
cx->runtime()->offThreadPromiseState.ref().initInternalDispatchQueue(); |
| 763 |
MOZ_ASSERT(cx->runtime()->offThreadPromiseState.ref().initialized()); |
| 764 |
|
| 765 |
return true; |
| 766 |
} |
| 767 |
|
| 768 |
#ifdef DEBUG |
| 769 |
JSObject* InternalJobQueue::copyJobs(JSContext* cx) { |
| 770 |
Rooted<ArrayObject*> jobs(cx, NewDenseEmptyArray(cx)); |
| 771 |
if (!jobs) { |
| 772 |
return nullptr; |
| 773 |
} |
| 774 |
|
| 775 |
for (const JSObject* unwrappedJob : queue.get()) { |
| 776 |
RootedObject job(cx, const_cast<JSObject*>(unwrappedJob)); |
| 777 |
if (!cx->compartment()->wrap(cx, &job)) { |
| 778 |
return nullptr; |
| 779 |
} |
| 780 |
|
| 781 |
if (!NewbornArrayPush(cx, jobs, ObjectValue(*job))) { |
| 782 |
return nullptr; |
| 783 |
} |
| 784 |
} |
| 785 |
|
| 786 |
return jobs; |
| 787 |
} |
| 788 |
|
| 789 |
JS_PUBLIC_API JSObject* js::GetJobsInInternalJobQueue(JSContext* cx) { |
| 790 |
MOZ_ASSERT(cx->internalJobQueue.ref()); |
| 791 |
return cx->internalJobQueue->copyJobs(cx); |
| 792 |
} |
| 793 |
#endif |
| 794 |
|
| 795 |
JS_PUBLIC_API bool js::EnqueueJob(JSContext* cx, JS::HandleObject job) { |
| 796 |
MOZ_ASSERT(cx->jobQueue); |
| 797 |
return cx->jobQueue->enqueuePromiseJob(cx, nullptr, job, nullptr, nullptr); |
| 798 |
} |
| 799 |
|
| 800 |
JS_PUBLIC_API void js::StopDrainingJobQueue(JSContext* cx) { |
| 801 |
MOZ_ASSERT(cx->internalJobQueue.ref()); |
| 802 |
cx->internalJobQueue->interrupt(); |
| 803 |
} |
| 804 |
|
| 805 |
JS_PUBLIC_API void js::RestartDrainingJobQueue(JSContext* cx) { |
| 806 |
MOZ_ASSERT(cx->internalJobQueue.ref()); |
| 807 |
cx->internalJobQueue->uninterrupt(); |
| 808 |
} |
| 809 |
|
| 810 |
JS_PUBLIC_API void js::RunJobs(JSContext* cx) { |
| 811 |
MOZ_ASSERT(cx->jobQueue); |
| 812 |
MOZ_ASSERT(cx->isEvaluatingModule == 0); |
| 813 |
cx->jobQueue->runJobs(cx); |
| 814 |
JS::ClearKeptObjects(cx); |
| 815 |
} |
| 816 |
|
| 817 |
bool InternalJobQueue::getHostDefinedData( |
| 818 |
JSContext* cx, JS::MutableHandle<JSObject*> data) const { |
| 819 |
data.set(nullptr); |
| 820 |
return true; |
| 821 |
} |
| 822 |
|
| 823 |
bool InternalJobQueue::enqueuePromiseJob(JSContext* cx, |
| 824 |
JS::HandleObject promise, |
| 825 |
JS::HandleObject job, |
| 826 |
JS::HandleObject allocationSite, |
| 827 |
JS::HandleObject hostDefinedData) { |
| 828 |
MOZ_ASSERT(job); |
| 829 |
if (!queue.pushBack(job)) { |
| 830 |
ReportOutOfMemory(cx); |
| 831 |
return false; |
| 832 |
} |
| 833 |
|
| 834 |
JS::JobQueueMayNotBeEmpty(cx); |
| 835 |
return true; |
| 836 |
} |
| 837 |
|
| 838 |
void InternalJobQueue::runJobs(JSContext* cx) { |
| 839 |
if (draining_ || interrupted_) { |
| 840 |
return; |
| 841 |
} |
| 842 |
|
| 843 |
while (true) { |
| 844 |
cx->runtime()->offThreadPromiseState.ref().internalDrain(cx); |
| 845 |
|
| 846 |
// It doesn't make sense for job queue draining to be reentrant. At the |
| 847 |
// same time we don't want to assert against it, because that'd make |
| 848 |
// drainJobQueue unsafe for fuzzers. We do want fuzzers to test this, |
| 849 |
// so we simply ignore nested calls of drainJobQueue. |
| 850 |
draining_ = true; |
| 851 |
|
| 852 |
RootedObject job(cx); |
| 853 |
JS::HandleValueArray args(JS::HandleValueArray::empty()); |
| 854 |
RootedValue rval(cx); |
| 855 |
|
| 856 |
// Execute jobs in a loop until we've reached the end of the queue. |
| 857 |
while (!queue.empty()) { |
| 858 |
// A previous job might have set this flag. E.g., the js shell |
| 859 |
// sets it if the `quit` builtin function is called. |
| 860 |
if (interrupted_) { |
| 861 |
break; |
| 862 |
} |
| 863 |
|
| 864 |
cx->runtime()->offThreadPromiseState.ref().internalDrain(cx); |
| 865 |
|
| 866 |
job = queue.front(); |
| 867 |
queue.popFront(); |
| 868 |
|
| 869 |
// If the next job is the last job in the job queue, allow |
| 870 |
// skipping the standard job queuing behavior. |
| 871 |
if (queue.empty()) { |
| 872 |
JS::JobQueueIsEmpty(cx); |
| 873 |
} |
| 874 |
|
| 875 |
AutoRealm ar(cx, &job->as<JSFunction>()); |
| 876 |
{ |
| 877 |
if (!JS::Call(cx, UndefinedHandleValue, job, args, &rval)) { |
| 878 |
// Nothing we can do about uncatchable exceptions. |
| 879 |
if (!cx->isExceptionPending()) { |
| 880 |
continue; |
| 881 |
} |
| 882 |
|
| 883 |
// Always clear the exception, because |
| 884 |
// PrepareScriptEnvironmentAndInvoke will assert that we don't have |
| 885 |
// one. |
| 886 |
RootedValue exn(cx); |
| 887 |
bool success = cx->getPendingException(&exn); |
| 888 |
cx->clearPendingException(); |
| 889 |
if (success) { |
| 890 |
js::ReportExceptionClosure reportExn(exn); |
| 891 |
PrepareScriptEnvironmentAndInvoke(cx, cx->global(), reportExn); |
| 892 |
} |
| 893 |
} |
| 894 |
} |
| 895 |
} |
| 896 |
|
| 897 |
draining_ = false; |
| 898 |
|
| 899 |
if (interrupted_) { |
| 900 |
break; |
| 901 |
} |
| 902 |
|
| 903 |
queue.clear(); |
| 904 |
|
| 905 |
// It's possible a job added a new off-thread promise task. |
| 906 |
if (!cx->runtime()->offThreadPromiseState.ref().internalHasPending()) { |
| 907 |
break; |
| 908 |
} |
| 909 |
} |
| 910 |
} |
| 911 |
|
| 912 |
bool InternalJobQueue::empty() const { return queue.empty(); } |
| 913 |
|
| 914 |
JSObject* InternalJobQueue::maybeFront() const { |
| 915 |
if (queue.empty()) { |
| 916 |
return nullptr; |
| 917 |
} |
| 918 |
|
| 919 |
return queue.get().front(); |
| 920 |
} |
| 921 |
|
| 922 |
class js::InternalJobQueue::SavedQueue : public JobQueue::SavedJobQueue { |
| 923 |
public: |
| 924 |
SavedQueue(JSContext* cx, Queue&& saved, bool draining) |
| 925 |
: cx(cx), saved(cx, std::move(saved)), draining_(draining) { |
| 926 |
MOZ_ASSERT(cx->internalJobQueue.ref()); |
| 927 |
} |
| 928 |
|
| 929 |
~SavedQueue() { |
| 930 |
MOZ_ASSERT(cx->internalJobQueue.ref()); |
| 931 |
cx->internalJobQueue->queue = std::move(saved.get()); |
| 932 |
cx->internalJobQueue->draining_ = draining_; |
| 933 |
} |
| 934 |
|
| 935 |
private: |
| 936 |
JSContext* cx; |
| 937 |
PersistentRooted<Queue> saved; |
| 938 |
bool draining_; |
| 939 |
}; |
| 940 |
|
| 941 |
js::UniquePtr<JS::JobQueue::SavedJobQueue> InternalJobQueue::saveJobQueue( |
| 942 |
JSContext* cx) { |
| 943 |
auto saved = |
| 944 |
js::MakeUnique<SavedQueue>(cx, std::move(queue.get()), draining_); |
| 945 |
if (!saved) { |
| 946 |
// When MakeUnique's allocation fails, the SavedQueue constructor is never |
| 947 |
// called, so this->queue is still initialized. (The move doesn't occur |
| 948 |
// until the constructor gets called.) |
| 949 |
ReportOutOfMemory(cx); |
| 950 |
return nullptr; |
| 951 |
} |
| 952 |
|
| 953 |
queue = Queue(SystemAllocPolicy()); |
| 954 |
draining_ = false; |
| 955 |
return saved; |
| 956 |
} |
| 957 |
|
| 958 |
mozilla::GenericErrorResult<OOM> JSContext::alreadyReportedOOM() { |
| 959 |
MOZ_ASSERT(isThrowingOutOfMemory()); |
| 960 |
return mozilla::Err(JS::OOM()); |
| 961 |
} |
| 962 |
|
| 963 |
mozilla::GenericErrorResult<JS::Error> JSContext::alreadyReportedError() { |
| 964 |
return mozilla::Err(JS::Error()); |
| 965 |
} |
| 966 |
|
| 967 |
JSContext::JSContext(JSRuntime* runtime, const JS::ContextOptions& options) |
| 968 |
: RootingContext(runtime ? &runtime->gc.nursery() : nullptr), |
| 969 |
runtime_(runtime), |
| 970 |
options_(this, options), |
| 971 |
measuringExecutionTime_(this, false), |
| 972 |
jitActivation(this, nullptr), |
| 973 |
isolate(this, nullptr), |
| 974 |
activation_(this, nullptr), |
| 975 |
profilingActivation_(nullptr), |
| 976 |
noExecuteDebuggerTop(this, nullptr), |
| 977 |
#ifdef JS_CHECK_UNSAFE_CALL_WITH_ABI |
| 978 |
inUnsafeCallWithABI(this, false), |
| 979 |
hasAutoUnsafeCallWithABI(this, false), |
| 980 |
#endif |
| 981 |
#ifdef DEBUG |
| 982 |
liveArraySortDataInstances(this, 0), |
| 983 |
#endif |
| 984 |
#ifdef JS_SIMULATOR |
| 985 |
simulator_(this, nullptr), |
| 986 |
#endif |
| 987 |
dtoaState(this, nullptr), |
| 988 |
suppressGC(this, 0), |
| 989 |
#ifdef FUZZING_JS_FUZZILLI |
| 990 |
executionHash(1), |
| 991 |
executionHashInputs(0), |
| 992 |
#endif |
| 993 |
#ifdef DEBUG |
| 994 |
noNurseryAllocationCheck(this, 0), |
| 995 |
disableStrictProxyCheckingCount(this, 0), |
| 996 |
#endif |
| 997 |
#if defined(DEBUG) || defined(JS_OOM_BREAKPOINT) |
| 998 |
runningOOMTest(this, false), |
| 999 |
#endif |
| 1000 |
inUnsafeRegion(this, 0), |
| 1001 |
generationalDisabled(this, 0), |
| 1002 |
compactingDisabledCount(this, 0), |
| 1003 |
#ifdef DEBUG |
| 1004 |
regExpSearcherLastLimit(this, RegExpSearcherLastLimitSentinel), |
| 1005 |
#else |
| 1006 |
regExpSearcherLastLimit(this, 0), |
| 1007 |
#endif |
| 1008 |
isEvaluatingModule(this, 0), |
| 1009 |
frontendCollectionPool_(this), |
| 1010 |
suppressProfilerSampling(false), |
| 1011 |
tempLifoAlloc_(this, (size_t)TEMP_LIFO_ALLOC_PRIMARY_CHUNK_SIZE, |
| 1012 |
js::MallocArena), |
| 1013 |
debuggerMutations(this, 0), |
| 1014 |
status(this, JS::ExceptionStatus::None), |
| 1015 |
unwrappedException_(this), |
| 1016 |
unwrappedExceptionStack_(this), |
| 1017 |
#ifdef DEBUG |
| 1018 |
hadResourceExhaustion_(this, false), |
| 1019 |
hadUncatchableException_(this, false), |
| 1020 |
#endif |
| 1021 |
reportGranularity(this, JS_DEFAULT_JITREPORT_GRANULARITY), |
| 1022 |
resolvingList(this, nullptr), |
| 1023 |
#ifdef DEBUG |
| 1024 |
enteredPolicy(this, nullptr), |
| 1025 |
#endif |
| 1026 |
generatingError(this, false), |
| 1027 |
cycleDetectorVector_(this, this), |
| 1028 |
data(nullptr), |
| 1029 |
asyncStackForNewActivations_(this), |
| 1030 |
asyncCauseForNewActivations(this, nullptr), |
| 1031 |
asyncCallIsExplicit(this, false), |
| 1032 |
interruptCallbacks_(this), |
| 1033 |
interruptCallbackDisabled(this, false), |
| 1034 |
interruptBits_(0), |
| 1035 |
inlinedICScript_(this, nullptr), |
| 1036 |
jitStackLimit(JS::NativeStackLimitMin), |
| 1037 |
jitStackLimitNoInterrupt(this, JS::NativeStackLimitMin), |
| 1038 |
jobQueue(this, nullptr), |
| 1039 |
internalJobQueue(this), |
| 1040 |
canSkipEnqueuingJobs(this, false), |
| 1041 |
promiseRejectionTrackerCallback(this, nullptr), |
| 1042 |
promiseRejectionTrackerCallbackData(this, nullptr), |
| 1043 |
insideExclusiveDebuggerOnEval(this, nullptr) { |
| 1044 |
MOZ_ASSERT(static_cast<JS::RootingContext*>(this) == |
| 1045 |
JS::RootingContext::get(this)); |
| 1046 |
} |
| 1047 |
|
| 1048 |
#ifdef ENABLE_WASM_JSPI |
| 1049 |
bool js::IsSuspendableStackActive(JSContext* cx) { |
| 1050 |
return cx->wasm().suspendableStackLimit != JS::NativeStackLimitMin; |
| 1051 |
} |
| 1052 |
|
| 1053 |
JS::NativeStackLimit js::GetSuspendableStackLimit(JSContext* cx) { |
| 1054 |
return cx->wasm().suspendableStackLimit; |
| 1055 |
} |
| 1056 |
#endif |
| 1057 |
|
| 1058 |
JSContext::~JSContext() { |
| 1059 |
#ifdef DEBUG |
| 1060 |
// Clear the initialized_ first, so that ProtectedData checks will allow us to |
| 1061 |
// destroy this context even if the runtime is already gone. |
| 1062 |
initialized_ = false; |
| 1063 |
#endif |
| 1064 |
|
| 1065 |
/* Free the stuff hanging off of cx. */ |
| 1066 |
MOZ_ASSERT(!resolvingList); |
| 1067 |
|
| 1068 |
// Ensure we didn't leak memory for the ArraySortData vector. |
| 1069 |
MOZ_ASSERT(liveArraySortDataInstances == 0); |
| 1070 |
|
| 1071 |
if (dtoaState) { |
| 1072 |
DestroyDtoaState(dtoaState); |
| 1073 |
} |
| 1074 |
|
| 1075 |
fx.destroyInstance(); |
| 1076 |
|
| 1077 |
#ifdef JS_SIMULATOR |
| 1078 |
js::jit::Simulator::Destroy(simulator_); |
| 1079 |
#endif |
| 1080 |
|
| 1081 |
if (isolate) { |
| 1082 |
irregexp::DestroyIsolate(isolate.ref()); |
| 1083 |
} |
| 1084 |
|
| 1085 |
TlsContext.set(nullptr); |
| 1086 |
} |
| 1087 |
|
| 1088 |
void JSContext::setRuntime(JSRuntime* rt) { |
| 1089 |
MOZ_ASSERT(!resolvingList); |
| 1090 |
MOZ_ASSERT(!compartment()); |
| 1091 |
MOZ_ASSERT(!activation()); |
| 1092 |
MOZ_ASSERT(!unwrappedException_.ref().initialized()); |
| 1093 |
MOZ_ASSERT(!unwrappedExceptionStack_.ref().initialized()); |
| 1094 |
MOZ_ASSERT(!asyncStackForNewActivations_.ref().initialized()); |
| 1095 |
|
| 1096 |
runtime_ = rt; |
| 1097 |
} |
| 1098 |
|
| 1099 |
#if defined(NIGHTLY_BUILD) |
| 1100 |
static bool IsOutOfMemoryException(JSContext* cx, const Value& v) { |
| 1101 |
return v == StringValue(cx->names().out_of_memory_); |
| 1102 |
} |
| 1103 |
#endif |
| 1104 |
|
| 1105 |
void JSContext::setPendingException(HandleValue v, Handle<SavedFrame*> stack) { |
| 1106 |
#if defined(NIGHTLY_BUILD) |
| 1107 |
do { |
| 1108 |
// Do not intercept exceptions if we are already |
| 1109 |
// in the exception interceptor. That would lead |
| 1110 |
// to infinite recursion. |
| 1111 |
if (this->runtime()->errorInterception.isExecuting) { |
| 1112 |
break; |
| 1113 |
} |
| 1114 |
|
| 1115 |
// Check whether we have an interceptor at all. |
| 1116 |
if (!this->runtime()->errorInterception.interceptor) { |
| 1117 |
break; |
| 1118 |
} |
| 1119 |
|
| 1120 |
// Don't report OOM exceptions. The interceptor isn't interested in those |
| 1121 |
// and they can confuse the interceptor because OOM can be thrown when we |
| 1122 |
// are not in a realm (atom allocation, for example). |
| 1123 |
if (IsOutOfMemoryException(this, v)) { |
| 1124 |
break; |
| 1125 |
} |
| 1126 |
|
| 1127 |
// Make sure that we do not call the interceptor from within |
| 1128 |
// the interceptor. |
| 1129 |
this->runtime()->errorInterception.isExecuting = true; |
| 1130 |
|
| 1131 |
// The interceptor must be infallible. |
| 1132 |
const mozilla::DebugOnly<bool> wasExceptionPending = |
| 1133 |
this->isExceptionPending(); |
| 1134 |
this->runtime()->errorInterception.interceptor->interceptError(this, v); |
| 1135 |
MOZ_ASSERT(wasExceptionPending == this->isExceptionPending()); |
| 1136 |
|
| 1137 |
this->runtime()->errorInterception.isExecuting = false; |
| 1138 |
} while (false); |
| 1139 |
#endif // defined(NIGHTLY_BUILD) |
| 1140 |
|
| 1141 |
// overRecursed_ is set after the fact by ReportOverRecursed. |
| 1142 |
this->status = JS::ExceptionStatus::Throwing; |
| 1143 |
this->unwrappedException() = v; |
| 1144 |
this->unwrappedExceptionStack() = stack; |
| 1145 |
} |
| 1146 |
|
| 1147 |
void JSContext::setPendingException(HandleValue value, |
| 1148 |
ShouldCaptureStack captureStack) { |
| 1149 |
Rooted<SavedFrame*> nstack(this); |
| 1150 |
if (captureStack == ShouldCaptureStack::Always || |
| 1151 |
realm()->shouldCaptureStackForThrow()) { |
| 1152 |
RootedObject stack(this); |
| 1153 |
if (!CaptureStack(this, &stack)) { |
| 1154 |
clearPendingException(); |
| 1155 |
} |
| 1156 |
if (stack) { |
| 1157 |
nstack = &stack->as<SavedFrame>(); |
| 1158 |
} |
| 1159 |
} |
| 1160 |
setPendingException(value, nstack); |
| 1161 |
} |
| 1162 |
|
| 1163 |
bool JSContext::getPendingException(MutableHandleValue rval) { |
| 1164 |
MOZ_ASSERT(isExceptionPending()); |
| 1165 |
|
| 1166 |
RootedValue exception(this, unwrappedException()); |
| 1167 |
if (zone()->isAtomsZone()) { |
| 1168 |
rval.set(exception); |
| 1169 |
return true; |
| 1170 |
} |
| 1171 |
|
| 1172 |
Rooted<SavedFrame*> stack(this, unwrappedExceptionStack()); |
| 1173 |
JS::ExceptionStatus prevStatus = status; |
| 1174 |
clearPendingException(); |
| 1175 |
if (!compartment()->wrap(this, &exception)) { |
| 1176 |
return false; |
| 1177 |
} |
| 1178 |
this->check(exception); |
| 1179 |
setPendingException(exception, stack); |
| 1180 |
status = prevStatus; |
| 1181 |
|
| 1182 |
rval.set(exception); |
| 1183 |
return true; |
| 1184 |
} |
| 1185 |
|
| 1186 |
bool JSContext::getPendingExceptionStack(MutableHandleValue rval) { |
| 1187 |
MOZ_ASSERT(isExceptionPending()); |
| 1188 |
|
| 1189 |
Rooted<SavedFrame*> exceptionStack(this, unwrappedExceptionStack()); |
| 1190 |
if (!exceptionStack) { |
| 1191 |
rval.setNull(); |
| 1192 |
return true; |
| 1193 |
} |
| 1194 |
if (zone()->isAtomsZone()) { |
| 1195 |
rval.setObject(*exceptionStack); |
| 1196 |
return true; |
| 1197 |
} |
| 1198 |
|
| 1199 |
RootedValue stack(this, ObjectValue(*exceptionStack)); |
| 1200 |
RootedValue exception(this, unwrappedException()); |
| 1201 |
JS::ExceptionStatus prevStatus = status; |
| 1202 |
clearPendingException(); |
| 1203 |
if (!compartment()->wrap(this, &exception) || |
| 1204 |
!compartment()->wrap(this, &stack)) { |
| 1205 |
return false; |
| 1206 |
} |
| 1207 |
this->check(stack); |
| 1208 |
setPendingException(exception, exceptionStack); |
| 1209 |
status = prevStatus; |
| 1210 |
|
| 1211 |
rval.set(stack); |
| 1212 |
return true; |
| 1213 |
} |
| 1214 |
|
| 1215 |
SavedFrame* JSContext::getPendingExceptionStack() { |
| 1216 |
return unwrappedExceptionStack(); |
| 1217 |
} |
| 1218 |
|
| 1219 |
#ifdef DEBUG |
| 1220 |
const JS::Value& JSContext::getPendingExceptionUnwrapped() { |
| 1221 |
MOZ_ASSERT(isExceptionPending()); |
| 1222 |
return unwrappedException(); |
| 1223 |
} |
| 1224 |
#endif |
| 1225 |
|
| 1226 |
bool JSContext::isClosingGenerator() { |
| 1227 |
return isExceptionPending() && |
| 1228 |
unwrappedException().isMagic(JS_GENERATOR_CLOSING); |
| 1229 |
} |
| 1230 |
|
| 1231 |
bool JSContext::isThrowingDebuggeeWouldRun() { |
| 1232 |
return isExceptionPending() && unwrappedException().isObject() && |
| 1233 |
unwrappedException().toObject().is<ErrorObject>() && |
| 1234 |
unwrappedException().toObject().as<ErrorObject>().type() == |
| 1235 |
JSEXN_DEBUGGEEWOULDRUN; |
| 1236 |
} |
| 1237 |
|
| 1238 |
bool JSContext::isRuntimeCodeGenEnabled( |
| 1239 |
JS::RuntimeCode kind, JS::Handle<JSString*> codeString, |
| 1240 |
JS::CompilationType compilationType, |
| 1241 |
JS::Handle<JS::StackGCVector<JSString*>> parameterStrings, |
| 1242 |
JS::Handle<JSString*> bodyString, |
| 1243 |
JS::Handle<JS::StackGCVector<JS::Value>> parameterArgs, |
| 1244 |
JS::Handle<JS::Value> bodyArg, bool* outCanCompileStrings) { |
| 1245 |
// Make sure that the CSP callback is installed and that it permits runtime |
| 1246 |
// code generation. |
| 1247 |
if (JSCSPEvalChecker allows = |
| 1248 |
runtime()->securityCallbacks->contentSecurityPolicyAllows) { |
| 1249 |
return allows(this, kind, codeString, compilationType, parameterStrings, |
| 1250 |
bodyString, parameterArgs, bodyArg, outCanCompileStrings); |
| 1251 |
} |
| 1252 |
|
| 1253 |
// Default implementation from the "Dynamic Code Brand Checks" spec. |
| 1254 |
// https://tc39.es/proposal-dynamic-code-brand-checks/#sec-hostensurecancompilestrings |
| 1255 |
*outCanCompileStrings = true; |
| 1256 |
return true; |
| 1257 |
} |
| 1258 |
|
| 1259 |
bool JSContext::getCodeForEval(HandleObject code, |
| 1260 |
JS::MutableHandle<JSString*> outCode) { |
| 1261 |
if (JSCodeForEvalOp gets = runtime()->securityCallbacks->codeForEvalGets) { |
| 1262 |
return gets(this, code, outCode); |
| 1263 |
} |
| 1264 |
// Default implementation from the "Dynamic Code Brand Checks" spec. |
| 1265 |
// https://tc39.es/proposal-dynamic-code-brand-checks/#sec-hostgetcodeforeval |
| 1266 |
outCode.set(nullptr); |
| 1267 |
return true; |
| 1268 |
} |
| 1269 |
|
| 1270 |
size_t JSContext::sizeOfExcludingThis( |
| 1271 |
mozilla::MallocSizeOf mallocSizeOf) const { |
| 1272 |
/* |
| 1273 |
* There are other JSContext members that could be measured; the following |
| 1274 |
* ones have been found by DMD to be worth measuring. More stuff may be |
| 1275 |
* added later. |
| 1276 |
*/ |
| 1277 |
return cycleDetectorVector().sizeOfExcludingThis(mallocSizeOf) + |
| 1278 |
irregexp::IsolateSizeOfIncludingThis(isolate, mallocSizeOf); |
| 1279 |
} |
| 1280 |
|
| 1281 |
size_t JSContext::sizeOfIncludingThis( |
| 1282 |
mozilla::MallocSizeOf mallocSizeOf) const { |
| 1283 |
return mallocSizeOf(this) + sizeOfExcludingThis(mallocSizeOf); |
| 1284 |
} |
| 1285 |
|
| 1286 |
#ifdef DEBUG |
| 1287 |
bool JSContext::inAtomsZone() const { return zone_->isAtomsZone(); } |
| 1288 |
#endif |
| 1289 |
|
| 1290 |
void JSContext::trace(JSTracer* trc) { |
| 1291 |
cycleDetectorVector().trace(trc); |
| 1292 |
geckoProfiler().trace(trc); |
| 1293 |
if (isolate) { |
| 1294 |
irregexp::TraceIsolate(trc, isolate.ref()); |
| 1295 |
} |
| 1296 |
#ifdef ENABLE_WASM_JSPI |
| 1297 |
wasm().promiseIntegration.trace(trc); |
| 1298 |
#endif |
| 1299 |
} |
| 1300 |
|
| 1301 |
JS::NativeStackLimit JSContext::stackLimitForJitCode(JS::StackKind kind) { |
| 1302 |
#ifdef JS_SIMULATOR |
| 1303 |
return simulator()->stackLimit(); |
| 1304 |
#else |
| 1305 |
return stackLimit(kind); |
| 1306 |
#endif |
| 1307 |
} |
| 1308 |
|
| 1309 |
void JSContext::resetJitStackLimit() { |
| 1310 |
// Note that, for now, we use the untrusted limit for ion. This is fine, |
| 1311 |
// because it's the most conservative limit, and if we hit it, we'll bail |
| 1312 |
// out of ion into the interpreter, which will do a proper recursion check. |
| 1313 |
#ifdef JS_SIMULATOR |
| 1314 |
jitStackLimit = jit::Simulator::StackLimit(); |
| 1315 |
#else |
| 1316 |
jitStackLimit = nativeStackLimit[JS::StackForUntrustedScript]; |
| 1317 |
#endif |
| 1318 |
jitStackLimitNoInterrupt = jitStackLimit; |
| 1319 |
} |
| 1320 |
|
| 1321 |
void JSContext::initJitStackLimit() { resetJitStackLimit(); } |
| 1322 |
|
| 1323 |
JSScript* JSContext::currentScript(jsbytecode** ppc, |
| 1324 |
AllowCrossRealm allowCrossRealm) { |
| 1325 |
if (ppc) { |
| 1326 |
*ppc = nullptr; |
| 1327 |
} |
| 1328 |
|
| 1329 |
// Fast path: there are no JS frames on the stack if there's no activation. |
| 1330 |
if (!activation()) { |
| 1331 |
return nullptr; |
| 1332 |
} |
| 1333 |
|
| 1334 |
FrameIter iter(this); |
| 1335 |
if (iter.done() || !iter.hasScript()) { |
| 1336 |
return nullptr; |
| 1337 |
} |
| 1338 |
|
| 1339 |
JSScript* script = iter.script(); |
| 1340 |
if (allowCrossRealm == AllowCrossRealm::DontAllow && |
| 1341 |
script->realm() != realm()) { |
| 1342 |
return nullptr; |
| 1343 |
} |
| 1344 |
|
| 1345 |
if (ppc) { |
| 1346 |
*ppc = iter.pc(); |
| 1347 |
} |
| 1348 |
return script; |
| 1349 |
} |
| 1350 |
|
| 1351 |
#ifdef JS_CRASH_DIAGNOSTICS |
| 1352 |
void ContextChecks::check(AbstractFramePtr frame, int argIndex) { |
| 1353 |
if (frame) { |
| 1354 |
check(frame.realm(), argIndex); |
| 1355 |
} |
| 1356 |
} |
| 1357 |
#endif |
| 1358 |
|
| 1359 |
void AutoEnterOOMUnsafeRegion::crash_impl(const char* reason) { |
| 1360 |
char msgbuf[1024]; |
| 1361 |
js::NoteIntentionalCrash(); |
| 1362 |
SprintfLiteral(msgbuf, "[unhandlable oom] %s", reason); |
| 1363 |
#ifndef DEBUG |
| 1364 |
// In non-DEBUG builds MOZ_CRASH normally doesn't print to stderr so we have |
| 1365 |
// to do this explicitly (the jit-test allow-unhandlable-oom annotation and |
| 1366 |
// fuzzers depend on it). |
| 1367 |
fprintf(stderr, "Hit MOZ_CRASH(%s) at %s:%d\n", msgbuf, __FILE__, __LINE__); |
| 1368 |
#endif |
| 1369 |
MOZ_CRASH_UNSAFE(msgbuf); |
| 1370 |
} |
| 1371 |
|
| 1372 |
mozilla::Atomic<AutoEnterOOMUnsafeRegion::AnnotateOOMAllocationSizeCallback, |
| 1373 |
mozilla::Relaxed> |
| 1374 |
AutoEnterOOMUnsafeRegion::annotateOOMSizeCallback(nullptr); |
| 1375 |
|
| 1376 |
void AutoEnterOOMUnsafeRegion::crash_impl(size_t size, const char* reason) { |
| 1377 |
{ |
| 1378 |
JS::AutoSuppressGCAnalysis suppress; |
| 1379 |
if (annotateOOMSizeCallback) { |
| 1380 |
annotateOOMSizeCallback(size); |
| 1381 |
} |
| 1382 |
} |
| 1383 |
crash_impl(reason); |
| 1384 |
} |
| 1385 |
|
| 1386 |
void ExternalValueArray::trace(JSTracer* trc) { |
| 1387 |
if (Value* vp = begin()) { |
| 1388 |
TraceRootRange(trc, length(), vp, "js::ExternalValueArray"); |
| 1389 |
} |
| 1390 |
} |
| 1391 |
|
| 1392 |
#ifdef MOZ_EXECUTION_TRACING |
| 1393 |
|
| 1394 |
bool JSContext::enableExecutionTracing() { |
| 1395 |
if (!executionTracer_) { |
| 1396 |
for (RealmsIter realm(runtime()); !realm.done(); realm.next()) { |
| 1397 |
if (realm->debuggerObservesCoverage()) { |
| 1398 |
JS_ReportErrorNumberASCII( |
| 1399 |
this, GetErrorMessage, nullptr, |
| 1400 |
JSMSG_DEBUG_EXCLUSIVE_EXECUTION_TRACE_COVERAGE); |
| 1401 |
return false; |
| 1402 |
} |
| 1403 |
} |
| 1404 |
|
| 1405 |
executionTracer_ = js::MakeUnique<ExecutionTracer>(); |
| 1406 |
|
| 1407 |
if (!executionTracer_) { |
| 1408 |
return false; |
| 1409 |
} |
| 1410 |
|
| 1411 |
if (!executionTracer_->init()) { |
| 1412 |
executionTracer_ = nullptr; |
| 1413 |
return false; |
| 1414 |
} |
| 1415 |
|
| 1416 |
for (RealmsIter realm(runtime()); !realm.done(); realm.next()) { |
| 1417 |
if (realm->isSystem()) { |
| 1418 |
continue; |
| 1419 |
} |
| 1420 |
realm->enableExecutionTracing(); |
| 1421 |
} |
| 1422 |
} |
| 1423 |
|
| 1424 |
executionTracerSuspended_ = false; |
| 1425 |
return true; |
| 1426 |
} |
| 1427 |
|
| 1428 |
void JSContext::cleanUpExecutionTracingState() { |
| 1429 |
MOZ_ASSERT(executionTracer_); |
| 1430 |
|
| 1431 |
for (RealmsIter realm(runtime()); !realm.done(); realm.next()) { |
| 1432 |
if (realm->isSystem()) { |
| 1433 |
continue; |
| 1434 |
} |
| 1435 |
realm->disableExecutionTracing(); |
| 1436 |
} |
| 1437 |
|
| 1438 |
caches().tracingCaches.clearAll(); |
| 1439 |
} |
| 1440 |
|
| 1441 |
void JSContext::disableExecutionTracing() { |
| 1442 |
if (executionTracer_) { |
| 1443 |
cleanUpExecutionTracingState(); |
| 1444 |
executionTracer_ = nullptr; |
| 1445 |
} |
| 1446 |
} |
| 1447 |
|
| 1448 |
void JSContext::suspendExecutionTracing() { |
| 1449 |
if (executionTracer_) { |
| 1450 |
cleanUpExecutionTracingState(); |
| 1451 |
executionTracerSuspended_ = true; |
| 1452 |
} |
| 1453 |
} |
| 1454 |
|
| 1455 |
#endif |
| 1456 |
|
| 1457 |
#ifdef JS_CHECK_UNSAFE_CALL_WITH_ABI |
| 1458 |
|
| 1459 |
AutoUnsafeCallWithABI::AutoUnsafeCallWithABI(UnsafeABIStrictness strictness) |
| 1460 |
: cx_(TlsContext.get()), |
| 1461 |
nested_(cx_ ? cx_->hasAutoUnsafeCallWithABI : false), |
| 1462 |
nogc(cx_) { |
| 1463 |
if (!cx_) { |
| 1464 |
// This is a helper thread doing Ion or Wasm compilation - nothing to do. |
| 1465 |
return; |
| 1466 |
} |
| 1467 |
switch (strictness) { |
| 1468 |
case UnsafeABIStrictness::NoExceptions: |
| 1469 |
MOZ_ASSERT(!JS_IsExceptionPending(cx_)); |
| 1470 |
checkForPendingException_ = true; |
| 1471 |
break; |
| 1472 |
case UnsafeABIStrictness::AllowPendingExceptions: |
| 1473 |
checkForPendingException_ = !JS_IsExceptionPending(cx_); |
| 1474 |
break; |
| 1475 |
case UnsafeABIStrictness::AllowThrownExceptions: |
| 1476 |
checkForPendingException_ = false; |
| 1477 |
break; |
| 1478 |
} |
| 1479 |
|
| 1480 |
cx_->hasAutoUnsafeCallWithABI = true; |
| 1481 |
} |
| 1482 |
|
| 1483 |
AutoUnsafeCallWithABI::~AutoUnsafeCallWithABI() { |
| 1484 |
if (!cx_) { |
| 1485 |
return; |
| 1486 |
} |
| 1487 |
MOZ_ASSERT(cx_->hasAutoUnsafeCallWithABI); |
| 1488 |
if (!nested_) { |
| 1489 |
cx_->hasAutoUnsafeCallWithABI = false; |
| 1490 |
cx_->inUnsafeCallWithABI = false; |
| 1491 |
} |
| 1492 |
MOZ_ASSERT_IF(checkForPendingException_, !JS_IsExceptionPending(cx_)); |
| 1493 |
} |
| 1494 |
|
| 1495 |
#endif // JS_CHECK_UNSAFE_CALL_WITH_ABI |
| 1496 |
|
| 1497 |
#ifdef __wasi__ |
| 1498 |
JS_PUBLIC_API void js::IncWasiRecursionDepth(JSContext* cx) { |
| 1499 |
++JS::RootingContext::get(cx)->wasiRecursionDepth; |
| 1500 |
} |
| 1501 |
|
| 1502 |
JS_PUBLIC_API void js::DecWasiRecursionDepth(JSContext* cx) { |
| 1503 |
MOZ_ASSERT(JS::RootingContext::get(cx)->wasiRecursionDepth > 0); |
| 1504 |
--JS::RootingContext::get(cx)->wasiRecursionDepth; |
| 1505 |
} |
| 1506 |
|
| 1507 |
JS_PUBLIC_API bool js::CheckWasiRecursionLimit(JSContext* cx) { |
| 1508 |
// WASI has two limits: |
| 1509 |
// 1) The stack pointer in linear memory that grows to zero. See |
| 1510 |
// --stack-first in js/src/shell/moz.build. |
| 1511 |
// 2) The JS::RootingContext::wasiRecursionDepth that counts recursion depth. |
| 1512 |
// Here we should check both. |
| 1513 |
if (JS::RootingContext::get(cx)->wasiRecursionDepth >= |
| 1514 |
JS::RootingContext::wasiRecursionDepthLimit) { |
| 1515 |
return false; |
| 1516 |
} |
| 1517 |
return true; |
| 1518 |
} |
| 1519 |
#endif // __wasi__ |