Files
OpenRCT2-Web/test/tests/ScriptingTests.cpp
T
itu-itis24-iyigun24andClaude Opus 5 9521e6cdf9 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>
2026-10-26 11:00:41 +11:00

94 lines
2.7 KiB
C++

/*****************************************************************************
* Copyright (c) 2014-2026 OpenRCT2 developers
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#include <gtest/gtest.h>
#include <openrct2/Context.h>
#include <openrct2/GameState.h>
#include <openrct2/OpenRCT2.h>
#include <openrct2/scripting/ScriptEngine.h>
#include <quickjs.h>
using namespace OpenRCT2;
using namespace OpenRCT2::Scripting;
class ScriptingTests : public testing::Test
{
protected:
void SetUp() override
{
gOpenRCT2Headless = true;
gOpenRCT2NoGraphics = true;
_context = CreateContext();
_context->Initialise();
}
std::unique_ptr<IContext> _context;
};
#ifdef ENABLE_SCRIPTING
TEST_F(ScriptingTests, MultipleSubscribersToSameEventShouldNotCrash)
{
auto& scriptEngine = static_cast<ScriptEngine&>(_context->GetScriptEngine());
// Register a plugin that subscribes twice to the same event
const char* pluginCode = R"(
registerPlugin({
name: 'test-plugin-multiple-subscribers',
version: '1.0.0',
authors: ['openrct2-test'],
type: 'remote',
licence: 'MIT',
minApiVersion: 110, // deliberately the version before quickjs
targetApiVersion: 110,
main: function () {
context.subscribe('interval.tick', function (e) {
// first subscriber
});
context.subscribe('interval.tick', function (e) {
// second subscriber
});
}
});
)";
scriptEngine.AddNetworkPlugin(pluginCode);
scriptEngine.LoadTransientPlugins();
scriptEngine.Tick();
auto& hookEngine = scriptEngine.GetHookEngine();
// We need a JSValue to pass to Call.
JSContext* ctx = scriptEngine.GetContext();
JSValue arg = JS_NewObject(ctx);
JS_SetPropertyStr(ctx, arg, "test", JS_NewInt32(ctx, 1));
// This should NOT crash.
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