From e4ba21257476730436882496bcb13ee57da937d4 Mon Sep 17 00:00:00 2001 From: mrmbernardi Date: Sun, 17 May 2026 01:26:29 +1000 Subject: [PATCH] Set qjs stack limit to 7MB (#26489) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Set qjs stack limit to 7MB * Update distribution/changelog.txt Co-authored-by: Michał Janiszewski --------- Co-authored-by: Michał Janiszewski --- distribution/changelog.txt | 1 + src/openrct2/Context.cpp | 19 ++++++------------- src/openrct2/scripting/ScriptEngine.cpp | 3 +-- src/openrct2/scripting/ScriptEngine.h | 7 +++++++ 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/distribution/changelog.txt b/distribution/changelog.txt index b7449a8091..2181b096f4 100644 --- a/distribution/changelog.txt +++ b/distribution/changelog.txt @@ -14,6 +14,7 @@ - Fix: [#26352] The label for path additions is using the wrong text colour in the tile inspector. - Fix: [#26360] Inverted Lay-down Roller Coaster helices are invisible when loading old saves. - Fix: [#26410] Tiles with water can draw incorrectly when there is something underwater and nothing above water. +- Fix: [#26418] Game crashes when a stack overflow occurs in plugin code. - Fix: [#26419] Drop count & negative g’s stat requirements for Flying Roller Coaster don’t get nullified by having an inversion. - Fix: [#26421] Wrong scenery tab highlighted when more than 64 scenery groups are selected. - Fix: [#26425] Benches don’t reduce watching spots from 4 to 2 while other path additions do (should be reversed). diff --git a/src/openrct2/Context.cpp b/src/openrct2/Context.cpp index 7e76c5f0c2..e64f31b078 100644 --- a/src/openrct2/Context.cpp +++ b/src/openrct2/Context.cpp @@ -546,14 +546,18 @@ namespace OpenRCT2 // TODO: preload the title scene in another (parallel) job. preloaderScene->AddJob([this]() { InitialiseRepositories(); }); - preloaderScene->AddJob([this]() { InitialiseScriptEngine(); }); } else { InitialiseRepositories(); - InitialiseScriptEngine(); } +#ifdef ENABLE_SCRIPTING + // quickjs script engine is single-threaded and must be set up on the main thread + _scriptEngine.Initialise(); + _uiContext->InitialiseScriptExtensions(); +#endif + return true; } @@ -618,17 +622,6 @@ namespace OpenRCT2 OpenProgress(STR_LOADING_GENERIC); } - void InitialiseScriptEngine() - { -#ifdef ENABLE_SCRIPTING - OpenProgress(STR_LOADING_PLUGIN_ENGINE); - _scriptEngine.Initialise(); - _uiContext->InitialiseScriptExtensions(); - - OpenProgress(STR_LOADING_GENERIC); -#endif - } - public: void InitialiseDrawingEngine() final override { diff --git a/src/openrct2/scripting/ScriptEngine.cpp b/src/openrct2/scripting/ScriptEngine.cpp index 396df8f98e..5bea65d1b3 100644 --- a/src/openrct2/scripting/ScriptEngine.cpp +++ b/src/openrct2/scripting/ScriptEngine.cpp @@ -490,8 +490,7 @@ void ScriptEngine::Initialise() #endif } - // Disable maximum stack size limit for the JS engine. - JS_SetMaxStackSize(_runtime, 0); + JS_SetMaxStackSize(_runtime, kJsStackSize); _replContext = JS_NewContext(_runtime); if (!_replContext) diff --git a/src/openrct2/scripting/ScriptEngine.h b/src/openrct2/scripting/ScriptEngine.h index fb7f5aaec9..4384593b22 100644 --- a/src/openrct2/scripting/ScriptEngine.h +++ b/src/openrct2/scripting/ScriptEngine.h @@ -51,6 +51,13 @@ namespace OpenRCT2::Scripting static constexpr int32_t kApiVersionCustomActionArgs = 68; static constexpr int32_t kApiVersionNetworkIDs = 77; + // This stack limit is not precise as quickjs-ng just sets a hard boundary offset from the stack frame + // where the limit is updated. The actual limit available to quickjs will be more or less depending on + // how big the native stack is relative to its size when the limit was first set. + // Calculating a precise limit is possible but requires platform specifc code for all possible platforms + // to get the end of the reserved stack space. + static constexpr size_t kJsStackSize = 1024 * 1024 * 7; + #ifndef DISABLE_NETWORK struct SocketDataBase; #endif