Skip to content

fix(core): guard runtime use during version queries - #627

Merged
wangl-cc merged 1 commit into
mainfrom
codex/core-runtime-guard
Oct 23, 2026
Merged

wangl-cc merged 1 commit into
mainfrom
codex/core-runtime-guard

Conversation

@wangl-cc

@wangl-cc wangl-cc commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

动机 / Motivation

runtime 模式下,get_version() 在 binding 的读锁释放后才复制库内字符串,可能与动态库卸载或替换并发。

方案 / Solution

将现有使用计数封装为 RAII Guard:实例持有到销毁结束,版本查询持有到字符串复制完成。链接模式使用空 guard。

影响与验证 / Impact and testing

无 CLI 或配置变更。macOS 下工作区测试(跳过真实 MaaCore 和 ignored 测试)、Clippy、格式检查及链接模式编译检查通过;未验证真实 MaaCore 运行。

AI 辅助 / AI assistance

Codex 根据作者讨论的方案编写了实现、测试和本 PR 描述。

  • 我已审阅全部改动,能解释每一处修改 / I have reviewed all changes and can explain each of them

Sourcery 摘要

在 MaaCore 的所有运行时使用期间保持保护,直到相关操作完成,以确保运行时变更期间的版本查询安全。

Bug Fixes:

  • 防止在版本查询复制返回的版本字符串时卸载或替换运行时库。

Enhancements:

  • 为助手实例和版本查询使用统一的 RAII 运行时使用保护,并提供更清晰的运行时生命周期错误信息和文档。

Tests:

  • 扩展运行时生命周期测试,覆盖保护的生命周期、并发运行时变更,以及在运行时已卸载或正在变更时拒绝版本查询的情况。
Original summary in English

Sourcery 摘要

在每项操作完成前保护所有 MaaCore 运行时的使用,确保运行时发生变化时仍可安全查询版本信息。

Bug 修复:

  • 在查询和复制 MaaCore 版本字符串期间,防止运行时库被卸载或替换。

增强功能:

  • 使用 RAII 运行时使用保护统一管理助手实例和版本查询,并更新生命周期错误信息和文档。

测试:

  • 扩展运行时生命周期测试,覆盖保护的生命周期、并发运行时变更,以及运行时不可用或正在变更时拒绝版本查询的情况。
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:

  • Prevent runtime library unloading or replacement while MaaCore version strings are being queried and copied.

Enhancements:

  • Unify assistant instances and version queries under RAII runtime-use guards, with updated lifecycle errors and documentation.

Tests:

  • Expand runtime lifecycle tests to cover guard lifetimes, concurrent runtime changes, and rejected version queries when the runtime is unavailable or changing.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗨——我发现了 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>

Sourcery 对开源项目免费使用——如果您喜欢我们的审查,请考虑分享 ✨
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>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread crates/maa-core/src/lib.rs
@codecov

codecov Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.84615% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 76.81%. Comparing base (bf3774e) to head (093fe1f).

Files with missing lines Patch % Lines
crates/maa-core/src/lib.rs 20.00% 4 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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
wangl-cc force-pushed the codex/core-runtime-guard branch from b6aea02 to 093fe1f Compare October 22, 2026 23:43
@wangl-cc
wangl-cc added this pull request to the merge queue Oct 23, 2026
Merged via the queue into main with commit 4695c10 Oct 23, 2026
27 checks passed
@wangl-cc
wangl-cc deleted the codex/core-runtime-guard branch October 23, 2026 08:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant