1
0
forked from 0ad/0ad

Set a stack quota for JS scripts to prevent crashes from infinite loops.

Infinite loop will instead trigger JS exceptions, which will make error
reports much nicer.

Differential Revision: https://code.wildfiregames.com/D3851
This was SVN commit r25280.
This commit is contained in:
wraitii 2021-04-16 16:49:18 +00:00
parent 89032c4d2b
commit 518fb7eafa

View File

@ -19,6 +19,7 @@
#include "ScriptContext.h" #include "ScriptContext.h"
#include "lib/alignment.h"
#include "ps/GameSetup/Config.h" #include "ps/GameSetup/Config.h"
#include "ps/Profile.h" #include "ps/Profile.h"
#include "scriptinterface/ScriptExtraHeaders.h" #include "scriptinterface/ScriptExtraHeaders.h"
@ -92,6 +93,15 @@ ScriptContext::ScriptContext(int contextSize, int heapGrowthBytesGCTrigger):
m_cx = JS_NewContext(contextSize); m_cx = JS_NewContext(contextSize);
ENSURE(m_cx); // TODO: error handling ENSURE(m_cx); // TODO: error handling
// Set stack quota limits - JS scripts will stop with a "too much recursion" exception.
// This seems to refer to the program's actual stack size, so it should be lower than the lowest common denominator
// of the various stack sizes of supported OS.
// From SM78's jsapi.h:
// - "The stack quotas for each kind of code should be monotonically descending"
// - "This function may only be called immediately after the runtime is initialized
// and before any code is executed and/or interrupts requested"
JS_SetNativeStackQuota(m_cx, 950 * KiB, 900 * KiB, 850 * KiB);
ENSURE(JS::InitSelfHostedCode(m_cx)); ENSURE(JS::InitSelfHostedCode(m_cx));
JS::SetGCSliceCallback(m_cx, GCSliceCallbackHook); JS::SetGCSliceCallback(m_cx, GCSliceCallbackHook);