Bug 1853881 - Part 1: Add JS::ThreadStackQuotaForSize and use it everywhere. r=nbp

Differential Revision: https://phabricator.services.mozilla.com/D191905
This commit is contained in:
Tooru Fujisawa 2023-10-26 14:10:55 +00:00
parent c51359e1e6
commit 62e38eaac8
7 changed files with 26 additions and 41 deletions

View File

@ -26,7 +26,7 @@
#include "js/CompileOptions.h" // JS::CompileOptions, JS::OwningCompileOptions, JS::DecodeOptions, JS::OwningDecodeOptions, JS::DelazificationOption
#include "js/ContextOptions.h" // JS::ContextOptionsRef
#include "js/experimental/JSStencil.h" // JS::Stencil, JS::InstantiationStorage
#include "js/experimental/CompileScript.h" // JS::FrontendContext, JS::NewFrontendContext, JS::DestroyFrontendContext, JS::SetNativeStackQuota, JS::CompilationStorage, JS::CompileGlobalScriptToStencil, JS::CompileModuleScriptToStencil, JS::DecodeStencil, JS::PrepareForInstantiate
#include "js/experimental/CompileScript.h" // JS::FrontendContext, JS::NewFrontendContext, JS::DestroyFrontendContext, JS::SetNativeStackQuota, JS::ThreadStackQuotaForSize, JS::CompilationStorage, JS::CompileGlobalScriptToStencil, JS::CompileModuleScriptToStencil, JS::DecodeStencil, JS::PrepareForInstantiate
#include "js/friend/ErrorMessages.h" // js::GetErrorMessage, JSMSG_*
#include "js/loader/ScriptLoadRequest.h"
#include "ScriptCompression.h"
@ -1871,16 +1871,10 @@ class ScriptOrModuleCompileTask final : public CompileOrDecodeTask {
}
private:
static size_t ThreadStackQuotaForSize(size_t size) {
// Set the stack quota to 10% less that the actual size.
// NOTE: This follows what JS helper thread does.
return size_t(double(size) * 0.9);
}
already_AddRefed<JS::Stencil> Compile() {
size_t stackSize = TaskController::GetThreadStackSize();
JS::SetNativeStackQuota(mFrontendContext,
ThreadStackQuotaForSize(stackSize));
JS::ThreadStackQuotaForSize(stackSize));
JS::CompilationStorage compileStorage;
auto compile = [&](auto& source) {

View File

@ -22,7 +22,7 @@
#include "XULTreeElement.h"
#include "js/CompilationAndEvaluation.h"
#include "js/CompileOptions.h" // JS::CompileOptions, JS::OwningCompileOptions, , JS::ReadOnlyCompileOptions, JS::ReadOnlyDecodeOptions, JS::DecodeOptions
#include "js/experimental/CompileScript.h" // JS::NewFrontendContext, JS::DestroyFrontendContext, JS::SetNativeStackQuota, JS::CompileGlobalScriptToStencil, JS::CompilationStorage
#include "js/experimental/CompileScript.h" // JS::NewFrontendContext, JS::DestroyFrontendContext, JS::SetNativeStackQuota, JS::ThreadStackQuotaForSize, JS::CompileGlobalScriptToStencil, JS::CompilationStorage
#include "js/experimental/JSStencil.h" // JS::Stencil, JS::FrontendContext
#include "js/SourceText.h"
#include "js/Transcoding.h"
@ -1856,17 +1856,11 @@ class ScriptCompileTask final : public Task {
}
private:
static size_t ThreadStackQuotaForSize(size_t size) {
// Set the stack quota to 10% less that the actual size.
// NOTE: This follows what JS helper thread does.
return size_t(double(size) * 0.9);
}
void Compile() {
// NOTE: The stack limit must be set from the same thread that compiles.
size_t stackSize = TaskController::GetThreadStackSize();
JS::SetNativeStackQuota(mFrontendContext,
ThreadStackQuotaForSize(stackSize));
JS::ThreadStackQuotaForSize(stackSize));
JS::SourceText<Utf8Unit> srcBuf;
if (NS_WARN_IF(!srcBuf.init(mFrontendContext, mText.get(), mTextLength,

View File

@ -42,6 +42,12 @@ JS_PUBLIC_API void DestroyFrontendContext(JS::FrontendContext* fc);
JS_PUBLIC_API void SetNativeStackQuota(JS::FrontendContext* fc,
JS::NativeStackSize stackSize);
// Return the stack quota that can be passed to SetNativeStackQuota, for given
// stack size.
// This subtracts a margin from given stack size, to make sure the stack quota
// check performed internally is sufficient.
JS_PUBLIC_API JS::NativeStackSize ThreadStackQuotaForSize(size_t stackSize);
// Returns true if there was any error reported to given FrontendContext.
JS_PUBLIC_API bool HadFrontendErrors(JS::FrontendContext* fc);

View File

@ -31,6 +31,13 @@ JS_PUBLIC_API void JS::SetNativeStackQuota(JS::FrontendContext* fc,
fc->setStackQuota(stackSize);
}
JS_PUBLIC_API JS::NativeStackSize JS::ThreadStackQuotaForSize(
size_t stackSize) {
// Set the stack quota to 10% less that the actual size.
static constexpr double RatioWithoutMargin = 0.9;
return JS::NativeStackSize(double(stackSize) * RatioWithoutMargin);
}
JS_PUBLIC_API bool JS::HadFrontendErrors(JS::FrontendContext* fc) {
return fc->hadErrors();
}

View File

@ -20,7 +20,8 @@
#include "jit/JitRuntime.h"
#include "jit/JitScript.h"
#include "js/CompileOptions.h" // JS::PrefableCompileOptions, JS::ReadOnlyCompileOptions
#include "js/friend/StackLimits.h" // js::ReportOverRecursed
#include "js/experimental/CompileScript.h" // JS::ThreadStackQuotaForSize
#include "js/friend/StackLimits.h" // js::ReportOverRecursed
#include "js/HelperThreadAPI.h"
#include "js/Stack.h"
#include "js/UniquePtr.h"
@ -118,11 +119,6 @@ void JS::SetProfilingThreadCallbacks(
HelperThreadState().unregisterThread = unregisterThread;
}
static size_t ThreadStackQuotaForSize(size_t size) {
// Set the stack quota to 10% less that the actual size.
return size_t(double(size) * 0.9);
}
// Bug 1630189: Without MOZ_NEVER_INLINE, Windows PGO builds have a linking
// error for HelperThreadTaskCallback.
JS_PUBLIC_API MOZ_NEVER_INLINE void JS::SetHelperThreadTaskCallback(
@ -142,7 +138,7 @@ void GlobalHelperThreadState::setDispatchTaskCallback(
dispatchTaskCallback = callback;
this->threadCount = threadCount;
this->stackQuota = ThreadStackQuotaForSize(stackSize);
this->stackQuota = JS::ThreadStackQuotaForSize(stackSize);
}
bool js::StartOffThreadWasmCompile(wasm::CompileTask* task,

View File

@ -16,7 +16,7 @@
#include "jsfriendapi.h"
#include "js/CompileOptions.h" // JS::CompileOptions, JS::OwningCompileOptions
#include "js/CompilationAndEvaluation.h"
#include "js/experimental/CompileScript.h" // JS::CompileGlobalScriptToStencil, JS::NewFrontendContext, JS::DestroyFrontendContext, JS::SetNativeStackQuota, JS::HadFrontendErrors, JS::ConvertFrontendErrorsToRuntimeErrors
#include "js/experimental/CompileScript.h" // JS::CompileGlobalScriptToStencil, JS::NewFrontendContext, JS::DestroyFrontendContext, JS::SetNativeStackQuota, JS::ThreadStackQuotaForSize, JS::HadFrontendErrors, JS::ConvertFrontendErrorsToRuntimeErrors
#include "js/experimental/JSStencil.h" // JS::Stencil, JS::CompileGlobalScriptToStencil, JS::InstantiateGlobalStencil, JS::CompilationStorage
#include "js/SourceText.h" // JS::SourceText
#include "js/Utility.h"
@ -113,17 +113,11 @@ class AsyncScriptCompileTask final : public Task {
}
private:
static size_t ThreadStackQuotaForSize(size_t size) {
// Set the stack quota to 10% less that the actual size.
// NOTE: This follows what JS helper thread does.
return size_t(double(size) * 0.9);
}
void Compile() {
// NOTE: The stack limit must be set from the same thread that compiles.
size_t stackSize = TaskController::GetThreadStackSize();
JS::SetNativeStackQuota(mFrontendContext,
ThreadStackQuotaForSize(stackSize));
JS::ThreadStackQuotaForSize(stackSize));
JS::CompilationStorage compileStorage;
mStencil = JS::CompileGlobalScriptToStencil(mFrontendContext, mOptions,

View File

@ -32,9 +32,9 @@
#include "mozilla/scache/StartupCache.h"
#include "crc32c.h"
#include "js/CompileOptions.h" // JS::ReadOnlyCompileOptions
#include "js/experimental/JSStencil.h" // JS::Stencil, JS::DecodeStencil
#include "js/experimental/CompileScript.h" // JS::NewFrontendContext, JS::DestroyFrontendContext
#include "js/CompileOptions.h" // JS::ReadOnlyCompileOptions
#include "js/experimental/JSStencil.h" // JS::Stencil, JS::DecodeStencil
#include "js/experimental/CompileScript.h" // JS::NewFrontendContext, JS::DestroyFrontendContext, JS::SetNativeStackQuota, JS::ThreadStackQuotaForSize
#include "js/Transcoding.h"
#include "MainThreadUtils.h"
#include "nsDebug.h"
@ -1202,12 +1202,6 @@ bool ScriptPreloader::StartDecodeTask(
return NS_SUCCEEDED(rv);
}
static size_t ThreadStackQuotaForSize(size_t size) {
// Set the stack quota to 10% less that the actual size.
// NOTE: This follows what JS helper thread does.
return size_t(double(size) * 0.9);
}
NS_IMETHODIMP ScriptPreloader::DecodeTask::Run() {
auto failure = [&]() {
RefPtr<JS::Stencil> stencil;
@ -1225,7 +1219,7 @@ NS_IMETHODIMP ScriptPreloader::DecodeTask::Run() {
auto cleanup = MakeScopeExit([&]() { JS::DestroyFrontendContext(fc); });
size_t stackSize = TaskController::GetThreadStackSize();
JS::SetNativeStackQuota(fc, ThreadStackQuotaForSize(stackSize));
JS::SetNativeStackQuota(fc, JS::ThreadStackQuotaForSize(stackSize));
size_t remaining = mDecodingSources.length();
for (auto& source : mDecodingSources) {