Skip to content

Add cargo rapx audit unsafe-apis and cargo rapx audit std-unsafe-apis commands#231

Merged
hxuhack merged 4 commits intomainfrom
copilot/add-cargo-rapx-audit-command
Mar 4, 2026
Merged

Add cargo rapx audit unsafe-apis and cargo rapx audit std-unsafe-apis commands#231
hxuhack merged 4 commits intomainfrom
copilot/add-cargo-rapx-audit-command

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 3, 2026

Adds a new audit top-level subcommand that enumerates all pub unsafe APIs in either the current crate or the Rust standard library (std/core/alloc) and emits a stable JSON document to stderr.

CLI

cargo rapx audit unsafe-apis         # current crate
cargo rapx audit std-unsafe-apis     # std/core/alloc

JSON schema

[
  {
    "module": "crate_name::module_path",
    "name": "function_name",
    "params": [{ "name": "ptr", "ty": "*const u8" }],
    "safety_doc": "The pointer must be valid and non-null."
  }
]
  • module – qualified module path (unsafe_apis_test, std::ptr, etc.)
  • params – names sourced from HIR body params for local items; argN fallback for external
  • safety_doc – text of the # Safety / ## Safety doc section, or null

Implementation

  • cli.rs – new Audit { kind: AuditKind } variant in Commands; AuditKind::{UnsafeApis, StdUnsafeApis}
  • analysis/audit/mod.rs – new module with:
    • UnsafeApiEntry / ParamInfo serde structs
    • extract_safety_doc() – pure string parser for # Safety sections with an is_heading_stop helper; covered by 5 unit tests
    • AuditUnsafeApis – iterates mir_keys() (local) or get_all_std_fns_by_rustc_public (std), filters on pub + unsafe, emits JSON via eprintln!
  • lib.rs – dispatch branch for Commands::Audit
  • tests/audit/unsafe_apis_test/ – fixture crate; integration test asserts JSON contains known pub unsafe fn deref_raw with its safety doc
  • help.rs / README.md – usage examples added
Original prompt

请在仓库 safer-rust/RAPx 中新增一个命令:

  • cargo rapx audit unsafe-apis

    • 输出当前被分析 crate 中所有 pub unsafe API 的 JSON 文档
    • JSON 至少包含:
      • module 路径(例如 std::ptr / crate::foo::bar 这种可读的模块路径)
      • 函数/方法/trait/struct 名称
      • 参数列表(名称与类型;若拿不到名称也要给出占位/序号)
      • Safety 文档(来自 Rust doc comment 中的 # Safety 段落;若不存在则为空/None)
    • 覆盖对象:
      • pub unsafe fn(free functions)
      • pub struct/enum/union:如果该类型自身是 public,则需要收集其所有 unsafe 方法(pub unsafe fn associated methods)以及相关的 free functions(若有与类型相关的定义,至少保证类型的 unsafe methods 被输出)。
      • pub unsafe trait:需要单独输出(trait 本身、以及 trait 中的 unsafe fn / unsafe items 如适用)。
    • 输出格式要求:
      • 机器可读 JSON(建议 serde_json pretty 或 compact 皆可,但要稳定)。
      • 建议输出到 stdout;如已有项目惯例可复用。
  • cargo rapx audit std-unsafe-apis

    • 输出 Rust 标准库std/core/alloc,与项目现有的 UpgStd / verify_std 类似的“分析标准库”的方式保持一致)中所有 pub unsafe API 的 JSON 文档,字段同上。

实现约束/建议:

  1. 需要把 audit 作为 cargo rapx 的一个新顶层 subcommand(类似 analyze / check),或作为 analyze 下的一个子命令也可,但用户期望是:cargo rapx audit unsafe-apis
  2. 复用现有的 rustc driver/callback 架构:在 rapx/src/cli.rs 扩展 clap 命令,并在 rapx/src/lib.rs::start_analyzer 增加分支调用新的分析器。
  3. 新增一个分析模块(例如 rapx/src/analysis/audit_unsafe_apis.rsrapx/src/audit/...):
    • 使用 TyCtxt / HIR / tcx 的 API 遍历 crate items,筛选 public 可见性与 unsafe
    • 获取 def-path/module path:建议使用已有工具函数(例如类似 get_cleaned_def_path_name 之类)或新增 util。
    • 获取函数签名参数:用 tcx.fn_sig(def_id) 等。
    • 提取 # Safety 文档:从 tcx.get_attrs(def_id) 读取 doc comments,拼接成 markdown/string,然后解析 # Safety section(简单解析:找到以 # Safety## Safety 开头的标题,抓取到下一个同级标题前的文本)。
  4. 标准库模式:实现与现有 UpgStd / verify_std 类似的逻辑(例如切换 TargetCrate::Std 或启用 build-std / rust-src),确保能够遍历标准库 crate。
  5. 增加必要的单元测试/集成测试:
    • 至少包含对 # Safety 提取逻辑的测试(纯字符串解析即可)。
    • 若仓库已有 test harness,也可添加一个小的 fixture crate 来验证 JSON 中包含某个 pub unsafe fn 的条目。
  6. 更新帮助/README:让 cargo rapx --help 能看到新命令的说明,并在 README 简要添加用法示例。

参考与现有代码位置(可先查看并按需要调整):

  • rapx/src/cli.rs 定义 clap Commands
  • rapx/src/lib.rsstart_analyzer match 分发
  • rapx/src/bin/cargo-rapx/main.rs / rapx/src/bin/rapx.rs 入口
  • 现有标准库分析相关:AnalysisKind::UpgStd, verify_std

验收标准:

  • cargo rapx audit unsafe-apis -- ... 能在分析目标 crate 时输出 JSON,且包含至少一个已知 pub unsafe fn 的记录。
  • cargo rapx audit std-unsafe-apis -- ... 能输出标准库 unsafe API 的 JSON(允许数量很大,但命令应能运行并输出)。
  • JSON 字段齐全、结构清晰、稳定。

This pull request was created from Copilot chat.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits March 3, 2026 15:39
…ration test

Co-authored-by: hxuhack <13302178+hxuhack@users.noreply.github.com>
Co-authored-by: hxuhack <13302178+hxuhack@users.noreply.github.com>
Copilot AI changed the title [WIP] Add cargo rapx audit command for unsafe APIs Add cargo rapx audit unsafe-apis and cargo rapx audit std-unsafe-apis commands Mar 3, 2026
@hxuhack hxuhack marked this pull request as ready for review March 4, 2026 01:30
Removed tests for the extract_safety_doc function.
@hxuhack hxuhack merged commit e9090f6 into main Mar 4, 2026
2 checks passed
@hxuhack hxuhack deleted the copilot/add-cargo-rapx-audit-command branch March 4, 2026 02:22
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.

2 participants