Repository navigation
fix(core): guard runtime use during version queries - #627
Merged
Merged
Conversation
There was a problem hiding this comment.
嗨——我发现了 1 个问题
给 AI 智能体的提示
请处理此代码审查中的评论:
## 单条评论
### 评论 1
<location path="crates/maa-core/src/lib.rs" line_range="90" />
<code_context>
- // Safety: AsstGetVersion returns a pointer into the loaded library's
- // read-only data segment, valid only while the library remains loaded.
- // `to_owned` copies the bytes before any unload can occur.
+ let _guard = runtime_lifecycle::Guard::acquire()?;
+ // Safety: MaaCore returns a valid, NUL-terminated string. The runtime
+ // guard prevents load or unload until the string has been copied.
</code_context>
<issue_to_address>
**原始卸载会使版本指针失效**
当调用方在 `Assistant::get_version()` 复制字符串时通过 `maa_sys::binding` 更改运行时,`Guard` 仅跟踪 `maa-core` 的运行时更改,因此对公开接口 `maa_sys::binding::unload()` 或 `load()` 的并发调用会绕过它,并可能在 `CStr::to_owned()` 复制版本字符串时卸载或替换库,从而导致未定义行为或崩溃。
确保原始绑定的加载和卸载也使用同一个生命周期保护机制,或者以其他方式防止它们在核心版本查询进行期间更改库。
</issue_to_address>Original comment in English
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="crates/maa-core/src/lib.rs" line_range="90" />
<code_context>
- // Safety: AsstGetVersion returns a pointer into the loaded library's
- // read-only data segment, valid only while the library remains loaded.
- // `to_owned` copies the bytes before any unload can occur.
+ let _guard = runtime_lifecycle::Guard::acquire()?;
+ // Safety: MaaCore returns a valid, NUL-terminated string. The runtime
+ // guard prevents load or unload until the string has been copied.
</code_context>
<issue_to_address>
**Raw unload invalidates version pointer**
When a caller changes the runtime through `maa_sys::binding` while `Assistant::get_version()` is copying the string, `Guard` tracks only `maa-core` runtime changes, so a concurrent call to the public `maa_sys::binding::unload()` or `load()` bypasses it and can unload or replace the library while `CStr::to_owned()` is copying its version string, causing undefined behavior or a crash.
Ensure raw binding load and unload participate in the same lifecycle guard, or otherwise prevent them from changing the library while a core version query is active.
</issue_to_address>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #627 +/- ##
==========================================
+ Coverage 76.67% 76.81% +0.13%
==========================================
Files 74 74
Lines 7748 7763 +15
Branches 7748 7763 +15
==========================================
+ Hits 5941 5963 +22
+ Misses 1478 1472 -6
+ Partials 329 328 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Hold runtime use in RAII guards owned by Assistant instances and temporary version queries. Keep runtime changes blocked until destruction or version string copying finishes.
wangl-cc
force-pushed
the
codex/core-runtime-guard
branch
from
October 22, 2026 23:43
b6aea02 to
093fe1f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
动机 / Motivation
runtime 模式下,
get_version()在 binding 的读锁释放后才复制库内字符串,可能与动态库卸载或替换并发。方案 / Solution
将现有使用计数封装为 RAII
Guard:实例持有到销毁结束,版本查询持有到字符串复制完成。链接模式使用空 guard。影响与验证 / Impact and testing
无 CLI 或配置变更。macOS 下工作区测试(跳过真实 MaaCore 和 ignored 测试)、Clippy、格式检查及链接模式编译检查通过;未验证真实 MaaCore 运行。
AI 辅助 / AI assistance
Codex 根据作者讨论的方案编写了实现、测试和本 PR 描述。
Sourcery 摘要
在 MaaCore 的所有运行时使用期间保持保护,直到相关操作完成,以确保运行时变更期间的版本查询安全。
Bug Fixes:
Enhancements:
Tests:
Original summary in English
Sourcery 摘要
在每项操作完成前保护所有 MaaCore 运行时的使用,确保运行时发生变化时仍可安全查询版本信息。
Bug 修复:
增强功能:
测试:
Original summary in English
Summary by Sourcery
Protect all MaaCore runtime use until each operation completes so version queries remain safe during runtime changes.
Bug Fixes:
Enhancements:
Tests: