Nambers / MiraiCP

c++ SDK of Mirai
https://eritque-arcus.tech/MiraiCP/
GNU Affero General Public License v3.0
177 stars 31 forks source link

MiraiCP-Plugin 加载插件时抛出 [json.exception.type_error.316] invalid UTF-8 byte at index 1: 0xD2 #137

Closed DreamUniverse843 closed 1 year ago

DreamUniverse843 commented 1 year ago

Code of Conduct

问题描述

加载插件后抛出下列异常: terminate called after throwing an instance of 'nlohmann::detail::type_error' what(): [json.exception.type_error.316] invalid UTF-8 byte at index 1: 0xD2 然后 Mirai 进程终止。

而插件本应正常加载。(我用的文档内演示文件编译的插件,也不行)

日志

2022-10-20 23:49:13 I/main: Starting mirai-console...
2022-10-20 23:49:13 I/main: Backend: version 2.12.1, built on 2022-07-31 17:49:27.
2022-10-20 23:49:13 I/main: Frontend Terminal: version 2.12.1, provided by Mamoe Technologies
2022-10-20 23:49:13 I/main: Welcome to visit https://mirai.mamoe.net/
2022-10-20 23:49:14 W/stderr: SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
2022-10-20 23:49:14 W/stderr: SLF4J: Defaulting to no-operation (NOP) logger implementation
2022-10-20 23:49:14 W/stderr: SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
2022-10-20 23:49:14 I/plugin: Successfully loaded plugin MiraiCP v2.12.0-RC2
2022-10-20 23:49:14 I/plugin: Successfully loaded plugin MCL Addon v2.0.2
2022-10-20 23:49:14 I/main: Prepared built-in commands: autoLogin, help, login, logout, permission, status, stop
2022-10-20 23:49:15 I/MiraiCP: ⭐MiraiCP启动中⭐
2022-10-20 23:49:15 I/MiraiCP: ⭐github地址:https://github.com/Nambers/MiraiCP
2022-10-20 23:49:15 I/MiraiCP: ⭐MiraiCP-loader 版本: 2.12.0-RC2, 构建时间: Fri Jul 22 08:29:04 UTC 2022
2022-10-20 23:49:15 I/MiraiCP/LibLoader: ⭐libLoader 版本: v2.12.0-RC2
terminate called after throwing an instance of 'nlohmann::detail::type_error'
  what():  [json.exception.type_error.316] invalid UTF-8 byte at index 1: 0xD2

版本

2.12.0-RC2

组件

Plugin

Antares0982 commented 1 year ago

能提供一份插件和你的配置json(去掉密码)吗

Antares0982 commented 1 year ago

插件路径不支持中文,之后这里会加上异常捕获信息

Antares0982 commented 1 year ago

在尝试多种不同编码保存文件后,不能复现

lucas8485 commented 1 year ago

目前我也遇到了同样的问题 插件使用MSVC1933编译,使用OpenJDK 17.0.2运行MiraiLoader 2.12.0-RC2,在配置文件里把后缀名从dll改为dl后会提示

2022.10.21 20:53:28 [ERROR] MiraiCP/LibLoader - path don't exist or invalid C:\Users\Lucas\CLionProjects\Lucas-Bot\out\build\x64-Debug\MiraiCPPlugin.dl 2022.10.21 20:53:28 [ERROR] MiraiCP/LibLoader - 2022.10.21 20:53:28 [ERROR] MiraiCP/LibLoader - failed to load plugin at location C:\Users\Lucas\CLionProjects\Lucas-Bot\out\build\x64-Debug\MiraiCPPlugin.dl

但如果把后缀名改为dll后则会复现上述问题

2022.10.21 20:54:35 [INFO ] MiraiCP - ⭐MiraiCP启动中⭐ 2022.10.21 20:54:35 [INFO ] MiraiCP - ⭐github存储库:https://github.com/Nambers/MiraiCP 2022.10.21 20:54:35 [INFO ] MiraiCP - ⭐MiraiCP-plugin 版本: v2.12.0-RC2, 构建时间: Fri Jul 22 08:29:41 UTC 2022, mirai 版本: 2.12.0 2022.10.21 20:54:35 [INFO ] MiraiCP/LibLoader - ⭐libLoader 版本: v2.12.0-RC2 terminate called after throwing an instance of 'nlohmann::detail::type_error' what(): [json.exception.type_error.316] invalid UTF-8 byte at index 1: 0xD2

Antares0982 commented 1 year ago

目前我也遇到了同样的问题 插件使用MSVC1933编译,使用OpenJDK 17.0.2运行MiraiLoader 2.12.0-RC2,在配置文件里把后缀名从dll改为dl后会提示

2022.10.21 20:53:28 [ERROR] MiraiCP/LibLoader - path don't exist or invalid C:\Users\Lucas\CLionProjects\Lucas-Bot\out\build\x64-Debug\MiraiCPPlugin.dl 2022.10.21 20:53:28 [ERROR] MiraiCP/LibLoader - 2022.10.21 20:53:28 [ERROR] MiraiCP/LibLoader - failed to load plugin at location C:\Users\Lucas\CLionProjects\Lucas-Bot\out\build\x64-Debug\MiraiCPPlugin.dl

但如果把后缀名改为dll后则会复现上述问题

2022.10.21 20:54:35 [INFO ] MiraiCP - ⭐MiraiCP启动中⭐ 2022.10.21 20:54:35 [INFO ] MiraiCP - ⭐github存储库:https://github.com/Nambers/MiraiCP 2022.10.21 20:54:35 [INFO ] MiraiCP - ⭐MiraiCP-plugin 版本: v2.12.0-RC2, 构建时间: Fri Jul 22 08:29:41 UTC 2022, mirai 版本: 2.12.0 2022.10.21 20:54:35 [INFO ] MiraiCP/LibLoader - ⭐libLoader 版本: v2.12.0-RC2 terminate called after throwing an instance of 'nlohmann::detail::type_error' what(): [json.exception.type_error.316] invalid UTF-8 byte at index 1: 0xD2

我明白了,之前理解错了出bug的位置,我晚点步进排查一下。先问一下你们编译的时候有没有确保/utf8在编译参数里,如果没有的话加上之后还能复现吗

DreamUniverse843 commented 1 year ago

抱歉,今天上课来着,才回复。 我用的是 MiraiCP-template 模板库构建的,里面的 CMakeList.txt 里面已经携带了 /utf8 编译参数。 image

Antares0982 commented 1 year ago

dll文件能发一份吗,还是不能复现,可能和你那边的编译器有一定关系,直接发插件dll就行

DreamUniverse843 commented 1 year ago

按照 https://eritque-arcus.tech/MiraiCP-docs/html/a00599.html 示例构建: MiraiCPPlugin.zip

Antares0982 commented 1 year ago

还是没办法复现,可能真的和每台设备的编译运行环境有关系……麻烦看一下这个仓库,按照这里的方法试一下看看是在什么位置发生崩溃,谢谢!

lucas8485 commented 1 year ago

编译参数确认了,是带了UTF-8的 压缩包内一共有多个DLL,其中MiraiCPPlugin.dll为主DLL文件 MiraiLoader.zip

Antares0982 commented 1 year ago

编译参数确认了,是带了UTF-8的 压缩包内一共有多个DLL,其中MiraiCPPlugin.dll为主DLL文件 MiraiLoader.zip

二位的插件在我本地都正常加载了,除了您的插件加载完了之后

[ERROR] MiraiCP/LibLoader - Error: plugin 0 crashed (caused by throwing an unknown exception)

可以确定该问题不是在插件端了。麻烦提供一些额外信息,以及参考

还是没办法复现,可能真的和每台设备的编译运行环境有关系……麻烦看一下这个仓库,按照这里的方法试一下看看是在什么位置发生崩溃,谢谢!

谢谢!

Antares0982 commented 1 year ago

libLoader.zip

如果不想debug的话,麻烦试下这个libLoader,在dev-hotfix分支上打的

DreamUniverse843 commented 1 year ago

实在抱歉,周末在备考,没注意这边的消息... 我晚些给您发 debug 哈,刚到家

Antares0982 commented 1 year ago

实在抱歉,周末在备考,没注意这边的消息... 我晚些给您发 debug 哈,刚到家

不着急的,慢慢来吧

DreamUniverse843 commented 1 year ago

刚才我试着在服务器端装了一组开发环境就好了...神奇

DreamUniverse843 commented 1 year ago

确实补齐 Visual Studio 开发工具链就好了...之前的 dll 直接加载也没事了 image 现在已经没有问题了

Antares0982 commented 1 year ago

草……虽然解决了但还是很好奇为什么会这样,太诡异了

DreamUniverse843 commented 1 year ago

草,似乎也不是 libLoader 的问题... 无论是 debug 那边构建出来的还是 Releases 里面的都不出问题了... 真是玄学

DreamUniverse843 commented 1 year ago

先暂时关掉 Issue 吧,毕竟没有问题了... 之后如果问题再次复现了,再打开叭

DreamUniverse843 commented 1 year ago

也谢谢 @Antares0982 ,这几天辛苦了

Antares0982 commented 1 year ago

ok,之后如果复现再讨论~

DreamUniverse843 commented 1 year ago

我又来了/捂脸 刚以为没问题结果又出问题了,不过这次捕获到了 image

DreamUniverse843 commented 1 year ago

这是日志:

2022-10-25 00:24:26 I/main: Starting mirai-console...
2022-10-25 00:24:26 I/main: Backend: version 2.12.1, built on 2022-07-31 17:49:27.
2022-10-25 00:24:26 I/main: Frontend Terminal: version 2.12.1, provided by Mamoe Technologies
2022-10-25 00:24:26 I/main: Welcome to visit https://mirai.mamoe.net/
2022-10-25 00:24:27 W/stderr: SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
2022-10-25 00:24:27 W/stderr: SLF4J: Defaulting to no-operation (NOP) logger implementation
2022-10-25 00:24:27 W/stderr: SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
2022-10-25 00:24:27 I/plugin: Successfully loaded plugin MiraiCP v2.12.0-RC2
2022-10-25 00:24:27 I/plugin: Successfully loaded plugin MCL Addon v2.0.2
2022-10-25 00:24:28 I/main: Prepared built-in commands: autoLogin, help, login, logout, permission, status, stop
2022-10-25 00:24:28 I/MiraiCP: ⭐MiraiCP启动中⭐
2022-10-25 00:24:28 I/MiraiCP: ⭐github地址:https://github.com/Nambers/MiraiCP
2022-10-25 00:24:28 I/MiraiCP: ⭐MiraiCP-loader 版本: 2.12.0-RC2, 构建时间: Fri Jul 22 08:29:04 UTC 2022
2022-10-25 00:24:28 I/MiraiCP/LibLoader: ⭐libLoader 版本: v2.12.0-RC2
terminate called after throwing an instance of 'nlohmann::detail::type_error'
  what():  [json.exception.type_error.316] invalid UTF-8 byte at index 0: 0xB6

这是构建文件: cmake-build-debug.zip

Antares0982 commented 1 year ago

你得把那个调用堆栈打出来,或者断点的代码行

Antares0982 commented 1 year ago

以及pdb文件为啥不见了,调试就是要pdb才看得到断点呀,插件需要用debug模式编译。现在只知道问题是在插件端发生,但是没有调试信息不知道发生在哪

DreamUniverse843 commented 1 year ago

抱歉,之前没用过调试,不太熟😂 image 引发了未经处理的异常:读取访问权限冲突。 LibLoader::LoaderApi::loader_apis 是 nullptr。

Nambers commented 1 year ago

抱歉,之前没用过调试,不太熟😂 image 引发了未经处理的异常:读取访问权限冲突。 LibLoader::LoaderApi::loader_apis 是 nullptr。

看起来像全局变量的问题 #126 , 可以试一下检查下有没有构造什么全局变量(改为指针,然后用的时候构造之类的), 或者等下个版本就修复了

DreamUniverse843 commented 1 year ago

话说,我确实有用全局变量预留了群对象...那我先写到局部吧

Antares0982 commented 1 year ago

这就是全局变量那个issue,倒数第五个调用栈初始化一个全局group变量

晓宇致梦Universe @.***> 于 2022年10月26日周三 00:16写道:

话说,我确实有用全局变量预留了群对象...那我先写到局部吧

— Reply to this email directly, view it on GitHub https://github.com/Nambers/MiraiCP/issues/137#issuecomment-1290819095, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHYUJZXP3OCEKIBFYN55YATWFABURANCNFSM6AAAAAARKJYZ7Y . You are receiving this because you were mentioned.Message ID: @.***>

DreamUniverse843 commented 1 year ago

好的,我暂时用局部变量去了😂 谢谢 @Nambers 和 @Antares0982 ,这两天辛苦了~

duplicate of #126.

Antares0982 commented 1 year ago

终于找到问题了!首先是当error发生的时候,load_plugin调用了GetLastErrorAsString()来获得系统异常并尝试输出到日志。 image 但是我们默认用的是utf-8编码,Windows系统默认不是utf-8,获取到的std::string不是按照这样编码的,下图对比,正常能发送的string内容在CLion里是显示为乱码的那个,正常显示的那个是从GetLastErrorAsString()获得的。这时候确实可以正常地构造json,但是一旦调用j.dump()就会抛出异常而崩溃。 image 目前快速fix的方法:删掉GetLastErrorAsString()的调用。