Bug 1716324 part 1 - Set default stack quota when creating the JSContext. r=tcampbell

Uses the stack limit values currently used for the JS shell. This fixes confusing
behavior when the embedding forgets to call JS_SetNativeStackQuota.

Differential Revision: https://phabricator.services.mozilla.com/D117655
This commit is contained in:
Jan de Mooij 2021-06-17 07:59:01 +00:00
Родитель b02492de66
Коммит 5bd1917c8c
1 изменённых файлов: 22 добавлений и 0 удалений

Просмотреть файл

@ -28,6 +28,7 @@
# include <processthreadsapi.h>
#endif // XP_WIN
#include "jsapi.h" // JS_SetNativeStackQuota
#include "jsexn.h"
#include "jspubtd.h"
#include "jstypes.h"
@ -154,6 +155,21 @@ bool JSContext::init(ContextKind kind) {
return true;
}
static void InitDefaultStackQuota(JSContext* cx) {
// Initialize stack quota to a reasonable default. Embedders can override this
// by calling JS_SetNativeStackQuota.
//
// NOTE: Firefox overrides these values. For the main thread this happens in
// XPCJSContext::Initialize.
#if defined(MOZ_ASAN) || (defined(DEBUG) && !defined(XP_WIN))
static constexpr size_t MaxStackSize = 2 * 128 * sizeof(size_t) * 1024;
#else
static constexpr size_t MaxStackSize = 128 * sizeof(size_t) * 1024;
#endif
JS_SetNativeStackQuota(cx, MaxStackSize);
}
JSContext* js::NewContext(uint32_t maxBytes, JSRuntime* parentRuntime) {
AutoNoteSingleThreadedRegion anstr;
@ -188,6 +204,12 @@ JSContext* js::NewContext(uint32_t maxBytes, JSRuntime* parentRuntime) {
return nullptr;
}
// Initialize stack quota last because simulators rely on the JSRuntime having
// been initialized.
if (cx->isMainThreadContext()) {
InitDefaultStackQuota(cx);
}
return cx;
}