| 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 |
#include "frontend/FrontendContext.h" |
| 8 |
|
| 9 |
#ifdef _WIN32 |
| 10 |
# include "util/WindowsWrapper.h" |
| 11 |
# include <process.h> // GetCurrentThreadId |
| 12 |
#else |
| 13 |
# include <pthread.h> // pthread_self |
| 14 |
#endif |
| 15 |
|
| 16 |
#include "gc/GC.h" |
| 17 |
#include "js/AllocPolicy.h" // js::ReportOutOfMemory |
| 18 |
#include "js/experimental/CompileScript.h" |
| 19 |
#include "js/friend/StackLimits.h" // js::ReportOverRecursed, js::MinimumStackLimitMargin, js::StackLimitMargin |
| 20 |
#include "js/Modules.h" |
| 21 |
#include "util/DifferentialTesting.h" |
| 22 |
#include "util/NativeStack.h" // GetNativeStackBase |
| 23 |
#include "vm/JSContext.h" |
| 24 |
|
| 25 |
using namespace js; |
| 26 |
|
| 27 |
void FrontendErrors::clearErrors() { |
| 28 |
error.reset(); |
| 29 |
warnings.clear(); |
| 30 |
overRecursed = false; |
| 31 |
outOfMemory = false; |
| 32 |
allocationOverflow = false; |
| 33 |
} |
| 34 |
|
| 35 |
void FrontendErrors::clearWarnings() { warnings.clear(); } |
| 36 |
|
| 37 |
void FrontendAllocator::reportAllocationOverflow() { |
| 38 |
fc_->onAllocationOverflow(); |
| 39 |
} |
| 40 |
|
| 41 |
void* FrontendAllocator::onOutOfMemory(AllocFunction allocFunc, |
| 42 |
arena_id_t arena, size_t nbytes, |
| 43 |
void* reallocPtr) { |
| 44 |
return fc_->onOutOfMemory(allocFunc, arena, nbytes, reallocPtr); |
| 45 |
} |
| 46 |
|
| 47 |
FrontendContext::~FrontendContext() { |
| 48 |
if (ownNameCollectionPool_) { |
| 49 |
MOZ_ASSERT(nameCollectionPool_); |
| 50 |
js_delete(nameCollectionPool_); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
void FrontendContext::setStackQuota(JS::NativeStackSize stackSize) { |
| 55 |
#ifdef __wasi__ |
| 56 |
stackLimit_ = JS::WASINativeStackLimit; |
| 57 |
#else // __wasi__ |
| 58 |
if (stackSize == 0) { |
| 59 |
stackLimit_ = JS::NativeStackLimitMax; |
| 60 |
} else { |
| 61 |
stackLimit_ = JS::GetNativeStackLimit(GetNativeStackBase(), stackSize - 1); |
| 62 |
} |
| 63 |
#endif // !__wasi__ |
| 64 |
|
| 65 |
#ifdef DEBUG |
| 66 |
setNativeStackLimitThread(); |
| 67 |
#endif |
| 68 |
} |
| 69 |
|
| 70 |
JS_PUBLIC_API void JS::SetNativeStackQuota(JS::FrontendContext* fc, |
| 71 |
JS::NativeStackSize stackSize) { |
| 72 |
fc->setStackQuota(stackSize); |
| 73 |
} |
| 74 |
|
| 75 |
JS_PUBLIC_API JS::NativeStackSize JS::ThreadStackQuotaForSize( |
| 76 |
size_t stackSize) { |
| 77 |
// Set the stack quota to 10% less that the actual size. |
| 78 |
static constexpr double RatioWithoutMargin = 0.9; |
| 79 |
|
| 80 |
MOZ_ASSERT(double(stackSize) * (1 - RatioWithoutMargin) > |
| 81 |
js::MinimumStackLimitMargin); |
| 82 |
|
| 83 |
return JS::NativeStackSize(double(stackSize) * RatioWithoutMargin); |
| 84 |
} |
| 85 |
|
| 86 |
bool FrontendContext::allocateOwnedPool() { |
| 87 |
MOZ_ASSERT(!nameCollectionPool_); |
| 88 |
|
| 89 |
nameCollectionPool_ = js_new<frontend::NameCollectionPool>(); |
| 90 |
if (!nameCollectionPool_) { |
| 91 |
return false; |
| 92 |
} |
| 93 |
ownNameCollectionPool_ = true; |
| 94 |
return true; |
| 95 |
} |
| 96 |
|
| 97 |
bool FrontendContext::hadErrors() const { |
| 98 |
// All errors must be reported to FrontendContext. |
| 99 |
MOZ_ASSERT_IF(maybeCx_, !maybeCx_->isExceptionPending()); |
| 100 |
|
| 101 |
return errors_.hadErrors(); |
| 102 |
} |
| 103 |
|
| 104 |
JS_PUBLIC_API bool JS::HadFrontendErrors(JS::FrontendContext* fc) { |
| 105 |
return fc->hadErrors(); |
| 106 |
} |
| 107 |
|
| 108 |
void FrontendContext::clearErrors() { |
| 109 |
MOZ_ASSERT(!maybeCx_); |
| 110 |
return errors_.clearErrors(); |
| 111 |
} |
| 112 |
|
| 113 |
JS_PUBLIC_API void JS::ClearFrontendErrors(JS::FrontendContext* fc) { |
| 114 |
fc->clearErrors(); |
| 115 |
} |
| 116 |
|
| 117 |
void FrontendContext::clearWarnings() { return errors_.clearWarnings(); } |
| 118 |
|
| 119 |
void* FrontendContext::onOutOfMemory(AllocFunction allocFunc, arena_id_t arena, |
| 120 |
size_t nbytes, void* reallocPtr) { |
| 121 |
addPendingOutOfMemory(); |
| 122 |
return nullptr; |
| 123 |
} |
| 124 |
|
| 125 |
void FrontendContext::onAllocationOverflow() { |
| 126 |
errors_.allocationOverflow = true; |
| 127 |
} |
| 128 |
|
| 129 |
void FrontendContext::onOutOfMemory() { addPendingOutOfMemory(); } |
| 130 |
|
| 131 |
void FrontendContext::onOverRecursed() { errors_.overRecursed = true; } |
| 132 |
|
| 133 |
void FrontendContext::recoverFromOutOfMemory() { |
| 134 |
MOZ_ASSERT_IF(maybeCx_, !maybeCx_->isThrowingOutOfMemory()); |
| 135 |
|
| 136 |
errors_.outOfMemory = false; |
| 137 |
} |
| 138 |
|
| 139 |
const JSErrorFormatString* FrontendContext::gcSafeCallback( |
| 140 |
JSErrorCallback callback, void* userRef, const unsigned errorNumber) { |
| 141 |
mozilla::Maybe<gc::AutoSuppressGC> suppressGC; |
| 142 |
if (maybeCx_) { |
| 143 |
suppressGC.emplace(maybeCx_); |
| 144 |
} |
| 145 |
|
| 146 |
return callback(userRef, errorNumber); |
| 147 |
} |
| 148 |
|
| 149 |
void FrontendContext::reportError(CompileError&& err) { |
| 150 |
if (errors_.error) { |
| 151 |
errors_.error.reset(); |
| 152 |
} |
| 153 |
|
| 154 |
// When compiling off thread, save the error so that the thread finishing the |
| 155 |
// parse can report it later. |
| 156 |
errors_.error.emplace(std::move(err)); |
| 157 |
} |
| 158 |
|
| 159 |
bool FrontendContext::reportWarning(CompileError&& err) { |
| 160 |
if (!errors_.warnings.append(std::move(err))) { |
| 161 |
ReportOutOfMemory(); |
| 162 |
return false; |
| 163 |
} |
| 164 |
|
| 165 |
return true; |
| 166 |
} |
| 167 |
|
| 168 |
void FrontendContext::ReportOutOfMemory() { |
| 169 |
/* |
| 170 |
* OOMs are non-deterministic, especially across different execution modes |
| 171 |
* (e.g. interpreter vs JIT). When doing differential testing, print to |
| 172 |
* stderr so that the fuzzers can detect this. |
| 173 |
*/ |
| 174 |
if (SupportDifferentialTesting()) { |
| 175 |
fprintf(stderr, "ReportOutOfMemory called\n"); |
| 176 |
} |
| 177 |
|
| 178 |
addPendingOutOfMemory(); |
| 179 |
} |
| 180 |
|
| 181 |
void FrontendContext::addPendingOutOfMemory() { errors_.outOfMemory = true; } |
| 182 |
|
| 183 |
void FrontendContext::setCurrentJSContext(JSContext* cx) { |
| 184 |
MOZ_ASSERT(!nameCollectionPool_); |
| 185 |
|
| 186 |
maybeCx_ = cx; |
| 187 |
nameCollectionPool_ = &cx->frontendCollectionPool(); |
| 188 |
scriptDataTableHolder_ = &cx->runtime()->scriptDataTableHolder(); |
| 189 |
stackLimit_ = cx->stackLimitForCurrentPrincipal(); |
| 190 |
|
| 191 |
#ifdef DEBUG |
| 192 |
setNativeStackLimitThread(); |
| 193 |
#endif |
| 194 |
} |
| 195 |
|
| 196 |
bool FrontendContext::convertToRuntimeError( |
| 197 |
JSContext* cx, Warning warning /* = Warning::Report */) { |
| 198 |
// Report out of memory errors eagerly, or errors could be malformed. |
| 199 |
if (hadOutOfMemory()) { |
| 200 |
js::ReportOutOfMemory(cx); |
| 201 |
return false; |
| 202 |
} |
| 203 |
|
| 204 |
if (maybeError()) { |
| 205 |
if (!maybeError()->throwError(cx)) { |
| 206 |
return false; |
| 207 |
} |
| 208 |
} |
| 209 |
if (warning == Warning::Report) { |
| 210 |
for (CompileError& error : warnings()) { |
| 211 |
if (!error.throwError(cx)) { |
| 212 |
return false; |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
if (hadOverRecursed()) { |
| 217 |
js::ReportOverRecursed(cx); |
| 218 |
} |
| 219 |
if (hadAllocationOverflow()) { |
| 220 |
js::ReportAllocationOverflow(cx); |
| 221 |
} |
| 222 |
|
| 223 |
MOZ_ASSERT(!extraBindingsAreNotUsed(), |
| 224 |
"extraBindingsAreNotUsed shouldn't escape from FrontendContext"); |
| 225 |
return true; |
| 226 |
} |
| 227 |
|
| 228 |
JS_PUBLIC_API bool JS::ConvertFrontendErrorsToRuntimeErrors( |
| 229 |
JSContext* cx, JS::FrontendContext* fc, |
| 230 |
const JS::ReadOnlyCompileOptions& options) { |
| 231 |
return fc->convertToRuntimeError(cx); |
| 232 |
} |
| 233 |
|
| 234 |
#ifdef DEBUG |
| 235 |
static size_t GetTid() { |
| 236 |
# if defined(_WIN32) |
| 237 |
return size_t(GetCurrentThreadId()); |
| 238 |
# elif defined(__wasm__) |
| 239 |
return 1; |
| 240 |
# else |
| 241 |
return size_t(pthread_self()); |
| 242 |
# endif |
| 243 |
} |
| 244 |
|
| 245 |
void FrontendContext::setNativeStackLimitThread() { |
| 246 |
stackLimitThreadId_.emplace(GetTid()); |
| 247 |
} |
| 248 |
|
| 249 |
void FrontendContext::assertNativeStackLimitThread() { |
| 250 |
if (!stackLimitThreadId_.isSome()) { |
| 251 |
return; |
| 252 |
} |
| 253 |
|
| 254 |
MOZ_ASSERT(*stackLimitThreadId_ == GetTid()); |
| 255 |
} |
| 256 |
#endif |
| 257 |
|
| 258 |
#ifdef __wasi__ |
| 259 |
void FrontendContext::incWasiRecursionDepth() { |
| 260 |
if (maybeCx_) { |
| 261 |
IncWasiRecursionDepth(maybeCx_); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
void FrontendContext::decWasiRecursionDepth() { |
| 266 |
if (maybeCx_) { |
| 267 |
DecWasiRecursionDepth(maybeCx_); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
bool FrontendContext::checkWasiRecursionLimit() { |
| 272 |
if (maybeCx_) { |
| 273 |
return CheckWasiRecursionLimit(maybeCx_); |
| 274 |
} |
| 275 |
return true; |
| 276 |
} |
| 277 |
|
| 278 |
JS_PUBLIC_API void js::IncWasiRecursionDepth(FrontendContext* fc) { |
| 279 |
fc->incWasiRecursionDepth(); |
| 280 |
} |
| 281 |
|
| 282 |
JS_PUBLIC_API void js::DecWasiRecursionDepth(FrontendContext* fc) { |
| 283 |
fc->decWasiRecursionDepth(); |
| 284 |
} |
| 285 |
|
| 286 |
JS_PUBLIC_API bool js::CheckWasiRecursionLimit(FrontendContext* fc) { |
| 287 |
return fc->checkWasiRecursionLimit(); |
| 288 |
} |
| 289 |
#endif // __wasi__ |
| 290 |
|
| 291 |
FrontendContext* js::NewFrontendContext() { |
| 292 |
UniquePtr<FrontendContext> fc = MakeUnique<FrontendContext>(); |
| 293 |
if (!fc) { |
| 294 |
return nullptr; |
| 295 |
} |
| 296 |
|
| 297 |
if (!fc->allocateOwnedPool()) { |
| 298 |
return nullptr; |
| 299 |
} |
| 300 |
|
| 301 |
return fc.release(); |
| 302 |
} |
| 303 |
|
| 304 |
JS_PUBLIC_API FrontendContext* JS::NewFrontendContext() { |
| 305 |
MOZ_ASSERT(JS::detail::libraryInitState == JS::detail::InitState::Running, |
| 306 |
"must call JS_Init prior to creating any FrontendContexts"); |
| 307 |
|
| 308 |
return js::NewFrontendContext(); |
| 309 |
} |
| 310 |
|
| 311 |
void js::DestroyFrontendContext(FrontendContext* fc) { js_delete_poison(fc); } |
| 312 |
|
| 313 |
JS_PUBLIC_API void JS::DestroyFrontendContext(FrontendContext* fc) { |
| 314 |
return js::DestroyFrontendContext(fc); |
| 315 |
} |
| 316 |
|
| 317 |
#ifdef DEBUG |
| 318 |
void FrontendContext::checkAndUpdateFrontendContextRecursionLimit(void* sp) { |
| 319 |
// For the js::MinimumStackLimitMargin to be effective, it should be larger |
| 320 |
// than the largest stack space which might be consumed by successive calls |
| 321 |
// to AutoCheckRecursionLimit::check. |
| 322 |
// |
| 323 |
// This function asserts that this property holds by recalling the stack |
| 324 |
// pointer of the previous call and comparing the consumed stack size with |
| 325 |
// the minimum margin. |
| 326 |
// |
| 327 |
// If this property does not hold, either the stack limit should be increased |
| 328 |
// or more calls to check for recursion should be added. |
| 329 |
if (previousStackPointer_ != nullptr) { |
| 330 |
# if JS_STACK_GROWTH_DIRECTION > 0 |
| 331 |
if (sp > previousStackPointer_) { |
| 332 |
size_t diff = uintptr_t(sp) - uintptr_t(previousStackPointer_); |
| 333 |
MOZ_ASSERT(diff < js::MinimumStackLimitMargin); |
| 334 |
} |
| 335 |
# else |
| 336 |
if (sp < previousStackPointer_) { |
| 337 |
size_t diff = uintptr_t(previousStackPointer_) - uintptr_t(sp); |
| 338 |
MOZ_ASSERT(diff < js::MinimumStackLimitMargin); |
| 339 |
} |
| 340 |
# endif |
| 341 |
} |
| 342 |
previousStackPointer_ = sp; |
| 343 |
} |
| 344 |
|
| 345 |
void js::CheckAndUpdateFrontendContextRecursionLimit(FrontendContext* fc, |
| 346 |
void* sp) { |
| 347 |
fc->checkAndUpdateFrontendContextRecursionLimit(sp); |
| 348 |
} |
| 349 |
#endif |
| 350 |
|
| 351 |
JS_PUBLIC_API const JSErrorReport* JS::GetFrontendErrorReport( |
| 352 |
JS::FrontendContext* fc, const JS::ReadOnlyCompileOptions& options) { |
| 353 |
if (!fc->maybeError().isSome()) { |
| 354 |
return nullptr; |
| 355 |
} |
| 356 |
return fc->maybeError().ptr(); |
| 357 |
} |
| 358 |
|
| 359 |
JS_PUBLIC_API bool JS::HadFrontendOverRecursed(JS::FrontendContext* fc) { |
| 360 |
return fc->hadOverRecursed(); |
| 361 |
} |
| 362 |
|
| 363 |
JS_PUBLIC_API bool JS::HadFrontendOutOfMemory(JS::FrontendContext* fc) { |
| 364 |
return fc->hadOutOfMemory(); |
| 365 |
} |
| 366 |
|
| 367 |
JS_PUBLIC_API bool JS::HadFrontendAllocationOverflow(JS::FrontendContext* fc) { |
| 368 |
return fc->hadAllocationOverflow(); |
| 369 |
} |
| 370 |
|
| 371 |
JS_PUBLIC_API size_t JS::GetFrontendWarningCount(JS::FrontendContext* fc) { |
| 372 |
return fc->warnings().length(); |
| 373 |
} |
| 374 |
|
| 375 |
JS_PUBLIC_API const JSErrorReport* JS::GetFrontendWarningAt( |
| 376 |
JS::FrontendContext* fc, size_t index, |
| 377 |
const JS::ReadOnlyCompileOptions& options) { |
| 378 |
return &fc->warnings()[index]; |
| 379 |
} |