Translating software written in C to Rust has significant benefits in improving memory safety. However, manual translation is cumbersome, error-prone, and often produces unidiomatic code. Large language models (LLMs) have demonstrated promise in producing idiomatic translations, but offer no correctness guarantees. We propose SACTOR, an LLM-driven C-to-Rust translation tool that employs a two-step process: an initial "unidiomatic" translation to preserve semantics, followed by an "idiomatic" refinement to align with Rust standards. To validate correctness of our function-wise incremental translation that mixes C and Rust, we use end-to-end testing via the foreign function interface. We evaluate SACTOR on 200 programs from two public datasets and on two more real-world scenarios (a 50-sample subset of CRust-Bench and the libogg library), comparing multiple LLMs. Across datasets, SACTOR delivers high end-to-end correctness and produces safe, idiomatic Rust with up to 7 times fewer Clippy warnings; On CRust-Bench, SACTOR achieves an average (across samples) of 85% unidiomatic and 52% idiomatic success, and on libogg it attains full unidiomatic and up to 78% idiomatic coverage on GPT-5.
翻译:将C语言编写的软件翻译为Rust语言对提升内存安全性具有显著优势。然而,人工翻译过程繁琐且易出错,生成的代码往往不符合语言习惯。大型语言模型(LLMs)在生成地道翻译方面展现出潜力,但无法提供正确性保证。本文提出SACTOR,一种基于LLM的C到Rust翻译工具,采用两步处理流程:首先进行保持语义的“非地道”初始翻译,随后执行符合Rust标准的“地道化”精炼。为验证混合C与Rust的逐函数增量翻译的正确性,我们通过外部函数接口进行端到端测试。我们在两个公开数据集的200个程序及两个真实场景(CRust-Bench的50个样本子集和libogg库)上评估SACTOR,并比较多种LLM。在所有数据集中,SACTOR均实现较高的端到端正确性,生成安全地道的Rust代码,其Clippy警告数量最多可减少7倍;在CRust-Bench上,SACTOR平均(跨样本)达到85%的非地道翻译成功率和52%的地道翻译成功率;在libogg库中,GPT-5模型实现了100%的非地道翻译覆盖率和最高78%的地道翻译覆盖率。