refactor(axdevice): replace errno contracts#1595
Merged
Merged
Conversation
ZR233
marked this pull request as ready for review
July 13, 2026 09:29
Contributor
There was a problem hiding this comment.
审查总结
本 PR 将虚拟化设备路径中 axdevice_base、axdevice、arm_vgic、riscv_vplic 以及测试 crate 的 ax-errno 依赖替换为领域类型错误枚举(通过 thiserror),并建立完整的错误传播链。变更范围清晰,实现质量高。
实现逻辑
axdevice_base:扩展DeviceError新增InvalidInput、InvalidData、InvalidState、Unsupported、OutOfMemory、ResourceBusy、Backend等变体,并为DeviceError、RegistryError、InvalidResourceReason添加thiserror::Errorderive 与 Display 实现。新增DeviceResult类型别名。同时为IrqLine/IrqSink新增IrqError/IrqResult,将原先的AxError::InvalidInput替换为携带 IRQ line、触发模式、操作类型等上下文的IrqError::InvalidTriggerMode。axdevice:新增DeviceManagerError/DeviceManagerResult,覆盖配置错误、资源冲突、分配失败、总线访问失败、IRQ 路由失败等场景,并实现From<DeviceManagerError> for DeviceError做上层映射。arm_vgic:新增VgicError/VgicResult,将panic!替换为可匹配的InvalidIrq错误,assign_irq返回VgicResult而非无返回值;内部handle_read/handle_write改为?传播错误。riscv_vplic:新增VplicError/VplicResult,覆盖非法 IRQ、上下文字段、访问宽度和区域配置等场景。axvm:新增From<DeviceError> for AxVmError、From<IrqError> for AxVmError、From<RegistryError> for AxVmError、From<DeviceManagerError> for AxVmError四个转换实现,将下层领域错误映射到 VM 层语义,保留可匹配的错误类型。- 所有原先的
expect()调用替换为通过config_argument()辅助方法的?传播;ax_err!宏调用替换为类型化错误构造。
验证结果
| 验证项 | 结果 |
|---|---|
cargo fmt --check |
✅ 通过 |
cargo test -p axdevice_base --all-features |
✅ 7 测试通过 |
cargo test -p axdevice --all-features |
✅ 23 测试通过 |
cargo test -p arm_vgic --all-features |
✅ 3 测试通过 |
cargo test -p riscv_vplic --all-features |
✅ 11 测试通过 |
cargo test -p axvm |
✅ 22 测试通过 |
cargo test -p virtualization-tests --test axdevice |
✅ 25 测试通过 |
cargo xtask clippy --package axdevice_base |
✅ 3 检查通过 |
cargo xtask clippy --package axdevice |
✅ 1 检查通过 |
cargo xtask clippy --package arm_vgic |
✅ 2 检查通过 |
cargo xtask clippy --package riscv_vplic |
✅ 1 检查通过 |
cargo xtask clippy --package axvm |
✅ 6 检查通过 |
CI 状态
GitHub Actions CI workflow run #29239061906 结论为 success。个别 job 显示 skipped 是因为矩阵中 run_host/run_container 互斥择一或路径过滤的正常行为,不影响整体 CI 结论。
测试覆盖
PR 为 4 个 crate(axdevice_base、axdevice、arm_vgic、riscv_vplic)新增了 error_contract 测试,验证:
Cargo.toml和源码中不再依赖ax-errno- 错误变体可正确匹配和转换
- 原先
panic!的路径现在返回可匹配错误
这些测试正确地放置在各自 crate 的 tests/ 目录中,被 cargo test 自动发现和执行。
重复/重叠分析
- 在当前
origin/dev基础分支上搜索,未发现等价的类型化错误实现。 - 在开放 PR 中搜索,未发现与本 PR 冲突或重叠的 PR。PR #1076(Starry 自编译)和 #1594(JPU pipeline)与本次变更完全无关。
审查意见
无阻塞性问题。该 PR 设计合理:
- 错误类型层次清晰,
DeviceError→DeviceManagerError→AxVmError的转换链保留了可匹配性和上下文 IrqError::InvalidTriggerMode携带 line/operation/expected/actual 比原先AxError::InvalidInput更有诊断价值assign_irq从panic!改为返回Result是正确性改进- 无
[patch.crates-io]变更 axhvc和axvmconfig中仍保留ax-errno依赖属于本次变更范围外的 crate,不应阻塞合并
环境限制
book/guideline/code-quality.md 读取被容器策略阻止,但代码变更已通过 Rust 编码标准检查(fmt + clippy),且错误类型设计符合 TGOSKits 分层模式。未能在当前 x86_64 环境中执行 cargo xtask axvisor build --arch aarch64/riscv64/loongarch64 的交叉编译验证,但 CI 已涵盖这些架构的构建与测试。
Powered by deepseek-v4-pro
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.
问题
虚拟化设备路径中的
axdevice_base、axdevice、arm_vgic、riscv_vplic与测试 crate 仍直接依赖ax-errno,导致设备访问、资源注册和 IRQ 路由错误被压缩为通用 errno,调用方无法按领域分类处理,也缺少总线、地址、宽度、IRQ line 等诊断上下文。改动
axdevice_base中完善DeviceError与RegistryError,新增DeviceResult、IrqError和IrqResult,并将设备及 IRQ trait 迁移到领域结果类型。axdevice中新增DeviceManagerError/DeviceManagerResult,覆盖配置、资源、分配、访问、IRQ、注册、fw_cfg 与 IVC 失败。arm_vgic和riscv_vplic分别新增VgicError/VgicResult与VplicError/VplicResult,将非法 IRQ、上下文、访问宽度和区域配置改为可匹配错误,避免断言或通用错误。AxVmError。ax-errno的直接依赖。实现逻辑
下层错误本身已经携带完整语义时,通过
From<LowerError> for UpperError配合?直接传播;只有需要补充当前操作、总线地址、访问宽度或 VM 领域分类时才在边界显式构造上层错误。这样减少重复的map_err,同时保留可匹配的错误类型与完整上下文。VM、设备、内存和中断的运行策略保持不变。验证
cargo fmt --all --checkcargo test -p axdevice_base -p axdevice -p arm_vgic -p riscv_vplic --features arm_vgic/vgicv3cargo test -p axvmcargo check -p virtualization-tests --testscargo test -p virtualization-tests --test axdevicecargo xtask clippy --package axdevice_basecargo xtask clippy --package axdevicecargo xtask clippy --package arm_vgiccargo xtask clippy --package riscv_vpliccargo xtask clippy --package axvmcargo xtask axvisor build --arch x86_64cargo xtask axvisor build --arch aarch64cargo xtask axvisor build --arch riscv64cargo xtask axvisor build --arch loongarch64