Skip to content

[Bug] 引擎原生模块回调脚本时访问 LSE 接口触发空指针访问 #383

Description

@engsr6982

Describe the bug

目前 lse 的 LegacyRemoteCall 接口类型太贫乏,如果 C++ 想导出大量接口难以使用,且没有提供任何对引擎原生操作的接口。

目前发现 lse 可以完整加载 quickjs c 模块、node C++ 插件 (node addon)

经过验证,quickjs c 模块 和 node addon 均可以正常同步调用。

但是在异步调用时崩溃,具体问题出现在 lse 内对所有 EngineOwnData 获取是无条件信任指针有效。
但是第三方触发的回调里,由于没有任何人能压入 script::EngineScope,导致 TLS 栈里没有 EngineScope。

请问能否对第三方扩展提供一些接口支持?
目前确认这个问题在 quickjs addon、node addon、以及 pybind11 绑定的 py 原生模块都能复现。
而 addon 是第三方扩展与引擎直接交互最近的方式。

如果愿意提供支持,能否在构建流程中将 lse 引擎的 lib 开放出来?这对 ABI 稳定很有帮助,虽然目前各自链接的 quickjs.lib 能正常处理调用,但官方的库总归更好一点。

当然,这需要您慎重决定,因为这同样取决于第三方扩展的开发者的代码质量,如果处理不好,这很容易在脚本调用时引发其它原因的崩溃或者损坏引擎,以及内存泄漏等。

相关堆栈:

legacy-script-engine-quickjs.dll!LoggerClass::info(const script::Arguments & args) Line 82 (d:\a\LegacyScriptEngine\LegacyScriptEngine\src\legacy\api\LoggerAPI.cpp:82)
[Inline Frame] legacy-script-engine-quickjs.dll!std::invoke(script::Local<script::Value>(*)(const script::Arguments &) & _Obj, const script::Arguments & _Arg1) Line 1811 (c:\Program Files\Microsoft Visual Studio\18\Enterprise\VC\Tools\MSVC\14.51.36231\include\type_traits:1811)
legacy-script-engine-quickjs.dll!std::_Func_impl_no_alloc<script::Local<script::Value> (*)(const script::Arguments &),script::Local<script::Value>,const script::Arguments &>::_Do_call(const script::Arguments & _Args) Line 1007 (c:\Program Files\Microsoft Visual Studio\18\Enterprise\VC\Tools\MSVC\14.51.36231\include\functional:1007)
legacy-script-engine-quickjs.dll!script::utils::MessageQueue::removeMessageIf(class std::function<enum script::utils::MessageQueue::RemoveMessagePredReturnType (class script::utils::Message &)> const &) (未知源:0)
legacy-script-engine-quickjs.dll!script::internal::ClassDefineState::validateClassDefine(bool) (未知源:0)
legacy-script-engine-quickjs.dll!script::internal::ClassDefineState::validateClassDefine(bool) (未知源:0)
legacy-script-engine-quickjs.dll!JS_WriteObject2() (未知源:0)
pland-napi-quickjs.dll!JS_CallConstructor() (未知源:0)
pland-napi-quickjs.dll!JS_CallConstructor() (未知源:0)
pland-napi-quickjs.dll!JS_Call() (未知源:0)
pland-napi-quickjs.dll!jspp::Local<class jspp::Function>::call(class jspp::Local<class jspp::Value> const &,class std::span<class jspp::Local<class jspp::Value> const ,-1>) (未知源:0)
pland-napi-quickjs.dll!jspp::binding::adapter::wrapScriptCallback<void,land::events::infra::ConfigReloadEvent &>::<lambda_1>::operator()(land::events::infra::ConfigReloadEvent & args) Line 735 (c:\Users\x\AppData\Local\.xmake\packages\j\jspp\979bba54\6c30eba150624455a8a77b24f4f3ea9d\include\jspp\binding\TypeConverter.h:735)
LeviLamina.dll!ll::event::CallbackStream::publish(ll::event::Event & event) Line 33 (d:\a\LeviLamina\LeviLamina\src\ll\api\event\EventBus.cpp:33)
LeviLamina.dll!ll::event::EventBus::publish(ll::event::Event & event, const ll::event::EventIdView & eventId) Line 183 (d:\a\LeviLamina\LeviLamina\src\ll\api\event\EventBus.cpp:183)
PLand.dll!ll::event::EventBus::publish<land::events::infra::ConfigReloadEvent>(land::events::infra::ConfigReloadEvent && event) Line 56 (c:\Users\x\AppData\Local\.xmake\packages\l\levilamina\26.20.7\ab359e40a25640f4a2101edfb5e95b01\include\ll\api\event\EventBus.h:56)
PLand.dll!land::internal::handlers::reload(const CommandOrigin & ori, CommandOutput & out) Line 222 (d:\Project\PLand\src\pland\internal\command\Command.cc:222)
PLand.dll!ll::command::Command<ll::command::EmptyParam,void (*)(const CommandOrigin &, CommandOutput &)>::execute(const CommandOrigin & origin, CommandOutput & output) Line 39 (c:\Users\x\AppData\Local\.xmake\packages\l\levilamina\26.20.7\ab359e40a25640f4a2101edfb5e95b01\include\ll\api\command\Command.h:39)
bedrock_server_mod.exe!00007ff781c73cba() (未知源:0)
bedrock_server_mod.exe!00007ff781ce2e19() (未知源:0)
[Inline Frame] LeviLamina.dll!ll::event::command::ExecutingCommandEventHook::origin(CommandContext & params, bool & params) Line 36 (d:\a\LeviLamina\LeviLamina\src\ll\api\event\command\ExecuteCommandEvent.cpp:36)
LeviLamina.dll!ll::event::command::ExecutingCommandEventHook::detour(CommandContext & context, bool suppressOutput) Line 50 (d:\a\LeviLamina\LeviLamina\src\ll\api\event\command\ExecuteCommandEvent.cpp:50)
bedrock_server_mod.exe!00007ff78141de07() (未知源:0)
bedrock_server_mod.exe!00007ff781c55050() (未知源:0)
bedrock_server_mod.exe!00007ff781c53b35() (未知源:0)
ucrtbase.dll!00007ff812a31bb2() (未知源:0)
kernel32.dll!00007ff814657374() (未知源:0)
ntdll.dll!00007ff81513cc91() (未知源:0)

异常路径:

Local<Value> LoggerClass::info(Arguments const& args) {
CHECK_ARGS_COUNT(args, 1)
try {
auto globalConf = getEngineOwnData();
LogDataHelper(LogLevel::Info, args);
return Boolean::newBoolean(true);
}
CATCH_AND_THROW
}

void inline LogDataHelper(LogLevel level, Arguments const& args) {
std::string res;
for (int i = 0; i < args.size(); ++i) res += ValueToString(args[i]);
getEngineOwnData()->logger->log(level, res);
}

// Engine additional data
inline std::shared_ptr<EngineOwnData> getEngineOwnData() {
return std::static_pointer_cast<EngineOwnData>(EngineScope::currentEngine()->getData());
}

To Reproduce

创建一个对应引擎的原生模块,然后模块导出一个回调函数,并保存 Global 句柄,然后再回调它。

脚本在此回调函数内访问 lse 任意依赖 EngineScope 机制的 api,回调触发即触发空指针访问崩溃。

Expected behavior

rt

Screenshots

No response

Platform

No response

BDS Version

rt

LeviLamina Version

26.20.6

LegacyScriptEngine Version

0.19.6

Additional context

相关代码:

https://github.com/IceBlcokMC/PLand-NAPI/blob/main/src/api/event.cc

https://github.com/IceBlcokMC/PLand-NAPI/blob/6c7bd2e97a76f1046e8ed3881301f28ca21cd1f9/tests/test-qjs/event.js#L13-L22

22:11:10.813 INFO [PreLoader] Loading records from data...
22:11:12.632 INFO [PreLoader] Preloading: LeviLamina <LeviLamina.dll>
22:11:14.567 INFO [PreLoader] LeviLamina Loaded.
22:11:14.599 INFO [LeviLamina] 统计已启用,您可以在配置文件中禁用统计
22:11:14.691 INFO [LeviLamina]
22:11:14.691 INFO [LeviLamina]          _               _ _                    _
22:11:14.691 INFO [LeviLamina]         | |    _____   _(_) |    __ _ _ __ ___ (_)_ __   __ _
22:11:14.691 INFO [LeviLamina]         | |   / _ \ \ / / | |   / _` | '_ ` _ \| | '_ \ / _` |
22:11:14.691 INFO [LeviLamina]         | |__|  __/\ V /| | |__| (_| | | | | | | | | | | (_| |
22:11:14.691 INFO [LeviLamina]         |_____\___| \_/ |_|_____\__,_|_| |_| |_|_|_| |_|\__,_|
22:11:14.691 INFO [LeviLamina]
22:11:14.691 INFO [LeviLamina]         ------------   Light-Weight Mod Loader   -------------
22:11:14.691 INFO [LeviLamina]
22:11:14.691 INFO [LeviLamina]
22:11:14.691 INFO [LeviLamina] LeviLamina 是一个由 LGPLv3 授权的自由软件
22:11:14.691 INFO [LeviLamina] 帮助我们翻译和改进文本 -> https://translate.levimc.org/
22:11:14.764 INFO [LeviLamina] 正在加载模组……
22:11:14.768 INFO [LeviLamina] 正在加载 LegacyMoney v0.19.0
22:11:14.776 INFO [LeviLamina] LegacyMoney 已加载
22:11:14.776 INFO [LeviLamina] 正在加载 LegacyRemoteCall v0.19.0
22:11:14.779 INFO [LeviLamina] LegacyRemoteCall 已加载
22:11:14.779 INFO [LeviLamina] 正在加载 iListenAttentively v0.13.0
22:11:15.234 INFO [iListenAttentively]     ___   _          _
22:11:15.234 INFO [iListenAttentively]    |_ _| | |        / \
22:11:15.234 INFO [iListenAttentively]     | |  | |       / _ \
22:11:15.234 INFO [iListenAttentively]     | |  | |___   / ___ \
22:11:15.234 INFO [iListenAttentively]    |___| |_____| /_/   \_\
22:11:15.234 INFO [iListenAttentively]
22:11:15.234 INFO [iListenAttentively]  iListenAttentively v0.13.0
22:11:15.234 INFO [iListenAttentively]     Author: MiracleForest
22:11:15.234 INFO [LeviLamina] iListenAttentively 已加载
22:11:15.234 INFO [LeviLamina] 正在加载 PLand v0.21.0
22:11:15.543 INFO [PLand] v0.21.0-25-g422c12c-develop-422c12c
22:11:15.543 WARN [PLand] This is a development build (v0.21.0-25-g422c12c). It may not be stable.
22:11:15.559 WARN [PLand] Unknown item permission: bbb (aaa: bbb)
22:11:15.560 WARN [PLand] Unknown block permission: bbb (aaa: bbb)
22:11:15.582 INFO [PLand] 已加载 0 位管理员
22:11:15.582 INFO [PLand] 已加载 0 位玩家的个人设置
22:11:15.584 INFO [PLand] 已加载 0 个领地
22:11:15.586 INFO [PLand] 领地默认权限模板加载完成
22:11:15.586 INFO [PLand] 领地空间索引构建完成
22:11:15.586 INFO [PLand] 构建完成,共处理 0 个领地组
22:11:15.586 WARN [PLand] Debug Mode
22:11:15.586 INFO [LeviLamina] PLand 已加载
22:11:15.586 INFO [LeviLamina] 正在加载 legacy-script-engine-nodejs v0.19.6
22:11:17.296 INFO [LeviLamina] legacy-script-engine-nodejs 已加载
22:11:17.296 INFO [LeviLamina] 正在加载 legacy-script-engine-quickjs v0.19.6
22:11:17.397 INFO [LeviLamina] legacy-script-engine-quickjs 已加载
22:11:17.397 INFO [LeviLamina] 正在加载 pland-napi-nodejs v0.0.1
22:11:17.480 INFO [LeviLamina] pland-napi-nodejs 已加载
22:11:17.480 INFO [LeviLamina] 正在加载 pland-napi-quickjs v0.0.1
22:11:17.482 INFO [LeviLamina] pland-napi-quickjs 已加载
22:11:17.482 INFO [LeviLamina] 正在加载 test-node v0.0.0
22:11:17.686 INFO [test-node] aabb: (0,0,0) => (10,10,10)

22:11:17.686 INFO [LeviLamina] test-node 已加载
22:11:17.686 INFO [LeviLamina] 正在加载 test-qjs v0.0.0
22:11:17.693 INFO [LeviLamina] test-qjs 已加载
22:11:17.693 INFO [LeviLamina] 已加载 10 个模组
22:11:18.284 INFO [Server] setting up server logging...
22:11:18.285 INFO [Server] Starting Server
22:11:18.285 INFO [Server] Version: 1.26.20.5(ProtocolVersion 975) with LeviLamina-26.20.6
22:11:18.285 INFO [Server] Session ID: 4470d7b4-3544-4cb3-8aa9-267dba9fb396
22:11:18.285 INFO [Server] Build ID: 43875355
22:11:18.285 INFO [Server] Branch: r/26_u2
22:11:18.285 INFO [Server] Commit ID: 1f6d1b10fc43f4c331af96d99ef1fe30ea642ed3
22:11:18.285 INFO [Server] Configuration: Publish
22:11:18.286 INFO [Server] Level Name: Bedrock level
22:11:18.834 INFO [Server] No CDN config file found at: cdn_config.json for dedicated server
22:11:18.834 INFO [Server] Game mode: 0 Survival
22:11:18.834 INFO [Server] Difficulty: 1 EASY
22:11:18.836 WARN [Server] <LVL|4> Content logging to console is disabled.  Enable it with content-log-console-output-enabled=true in server.properties
22:11:19.798 INFO [Server] Opening level 'worlds/Bedrock level/db'
22:11:19.952 INFO [Server] Pack Stack - None
22:11:21.826 INFO [Server] IPv4 supported, port: 19132: Used for gameplay and LAN discovery
22:11:21.826 INFO [Server] IPv6 supported, port: 19133: Used for gameplay
22:11:21.940 INFO [Server] Waiting for Minecraft services...
22:11:22.163 INFO [Server] Server started in (7.6s)! For help, type "help" or "?"
22:11:22.164 INFO [Server] ================ TELEMETRY MESSAGE ===================
22:11:22.164 INFO [Server] Server Telemetry is currently not enabled.
22:11:22.164 INFO [Server] Enabling this telemetry helps us improve the game.
22:11:22.164 INFO [Server] To enable this feature, add the line 'emit-server-telemetry=true'
22:11:22.164 INFO [Server] to the server.properties file in the handheld/src-server directory
22:11:22.164 INFO [Server] ======================================================
22:11:22.333 INFO [CrashLogger] 已成功启用崩溃记录器
22:11:22.334 INFO [LeviLamina] 正在启用模组……
22:11:22.334 INFO [LeviLamina] 正在启用 LegacyMoney v0.19.0
22:11:22.334 INFO [LeviLamina] 正在启用 LegacyRemoteCall v0.19.0
22:11:22.334 INFO [LeviLamina] 正在启用 iListenAttentively v0.13.0
22:11:22.334 INFO [LeviLamina] 正在启用 PLand v0.21.0
22:11:26.024 INFO [LeviLamina] 正在启用 legacy-script-engine-nodejs v0.19.6
22:11:26.039 INFO [LeviLamina] 正在启用 legacy-script-engine-quickjs v0.19.6
22:11:26.039 INFO [LeviLamina] 正在启用 pland-napi-nodejs v0.0.1
22:11:26.039 INFO [LeviLamina] 正在启用 pland-napi-quickjs v0.0.1
22:11:26.039 INFO [LeviLamina] 正在启用 test-node v0.0.0
22:11:26.039 INFO [LeviLamina] 正在启用 test-qjs v0.0.0
22:11:26.039 INFO [LeviLamina] 10 个模组在 (3.7秒) 内启用
22:11:26.080 INFO [test-qjs] LandAABB test passed
22:11:26.080 INFO [test-qjs] enums ok
22:11:26.080 INFO [test-qjs] perm table semantics ok
22:11:26.081 INFO [test-qjs] perm table sweep ok (17 env + 51 role fields)
22:11:26.082 INFO [test-qjs] land defaults ok
22:11:26.082 INFO [test-qjs] land readonly ok
22:11:26.082 INFO [test-qjs] land 4-arg ctor ok
22:11:26.082 INFO [test-qjs] land members ok
22:11:26.082 INFO [test-qjs] land teleport ok
22:11:26.082 INFO [test-qjs] land collision ok
22:11:26.082 INFO [test-qjs] land perm flow ok
22:11:26.082 INFO [test-qjs] all tests passed
land reload
22:11:49.632 WARN [PLand] Unknown item permission: bbb (aaa: bbb)
22:11:49.632 WARN [PLand] Unknown block permission: bbb (aaa: bbb)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions