forked from rosebudai/OpenRCT2-Web
Fix use-after-free when unregistering script classes twice (#27143)
ScBase::Unregister() freed the class prototype but kept proto and protoCtx. The script classes are globals and the JS runtime is freed when a context is destroyed, so these values become dangling pointers. The script classes are registered only by ScriptEngine::Initialise(), but ~ScriptEngine() always unregisters them. Destroying a context whose script engine was never initialised, after an initialised one had been destroyed, therefore freed the stale prototype again through the freed JSContext. On Windows this crashed WaterObjectTest when it ran after TileElementsViewTests in the same test process. Reset proto and protoCtx after freeing them, and add a test that destroys an uninitialised context after an initialised one. Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
b2357d4695
commit
9521e6cdf9
2 files changed
+19
No files matched your search
@@ -333,6 +333,8 @@ namespace OpenRCT2::Scripting
|
||||
classId = JS_INVALID_CLASS_ID;
|
||||
if (protoCtx != nullptr)
|
||||
JS_FreeValue(protoCtx, proto);
|
||||
proto = JS_NULL;
|
||||
protoCtx = nullptr;
|
||||
}
|
||||
|
||||
[[nodiscard]] JSValue GetProto() const
|
||||
|
||||
@@ -73,4 +73,21 @@ TEST_F(ScriptingTests, MultipleSubscribersToSameEventShouldNotCrash)
|
||||
hookEngine.Call(HookType::intervalTick, arg, false);
|
||||
}
|
||||
|
||||
TEST(ScriptEngineLifetimeTests, UninitialisedContextCanBeDestroyedAfterInitialisedOne)
|
||||
{
|
||||
gOpenRCT2Headless = true;
|
||||
gOpenRCT2NoGraphics = true;
|
||||
|
||||
// Registers the global script classes and then frees the JS runtime they belong to.
|
||||
{
|
||||
auto context = CreateContext();
|
||||
context->GetScriptEngine().Initialise();
|
||||
}
|
||||
|
||||
// Unregisters the global script classes again without registering them first.
|
||||
{
|
||||
auto context = CreateContext();
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user