arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2607.28835cs.SEcs.OScs.PL

从C到地道Rust:Theseus之船智能体翻译

From C to Idiomatic Rust: A Ship-of-Theseus Agentic Translation

Vasily A. Sartakov

AI总结:

本文提出一种智能体AI驱动的结构化C到Rust迁移工作流,先生成语义保留的非地道Rust基线,再逐步重写为地道Rust,经12.5千行代码的iodine验证可实现可靠迁移。

AI中文摘要:

C语言是操作系统、嵌入式平台和网络基础设施的基础,因为其抽象直接对应机器行为。它明确的内存模型、可预测的数据表示和最小化的运行时,使编译器能生成快速、确定的代码。但这些特性也将正确性和内存安全完全交给程序员,导致未定义行为、指针误用和生命周期错误成为长期C代码库中缺陷和安全漏洞的持续来源。Rust通过静态所有权与借用模型消除了多数故障模式,该模型在编译时强制内存安全和别名约束。然而,成熟的C系统无法直接翻译:必须先重构隐式布局假设、别名模式和未定义行为,才能生成安全的Rust。本文提出一种迁移方法,先生成语义保留的非地道Rust基线,再用智能体AI逐步将其重写为地道Rust,每一步通过编译和行为测试验证。将该方法应用于含12500行源代码的iodine,结果表明可靠的C到Rust迁移是结构化转换工作流,而非单一翻译步骤。

英文摘要:

C underpins operating systems, embedded platforms, and network infrastructure as its abstractions map directly to machine behaviour. Its explicit memory model, predictable data representations, and minimal runtime allow compilers to generate fast, deterministic code. These properties also leave correctness and memory safety entirely to the programmer, making undefined behaviour, pointer misuse, and lifetime errors persistent sources of defects and security vulnerabilities in long-lived C codebases. Rust eliminates most failure modes through a static ownership and borrowing model that enforces memory safety and aliasing constraints at compile time. However, mature C systems cannot be translated directly: implicit layout assumptions, aliasing patterns, and undefined behaviour must be reconstructed before safe Rust can be produced. This paper presents a migration methodology that first generates a semantics-preserving, non-idiomatic Rust baseline and then incrementally rewrites it into idiomatic Rust using agentic AI, validating each step through compilation and behavioural testing. Applied to iodine, a real-world DNS tunnel, the approach demonstrates that reliable C-to-Rust migration is a structured transformation workflow rather than a single translation step.

↑