arXiv:2503.12511cs.SEcs.AI2025-03ACL被引 11

用LLM+静态分析实现安全且符合习惯的C到Rust翻译

SACTOR: LLM-Driven Correct and Idiomatic C to Rust Translation with Static Analysis and FFI-Based Verification

  • 分两步翻译:先保语义,再优化为符合Rust规范的代码
  • 在多个数据集上达到85%语义正确率,78%代码风格正确率
  • 适合需要迁移老旧C代码至Rust的开发者使用

将C语言程序翻译为Rust可显著提升内存安全性。然而手动翻译繁琐易错,且常产生不自然的代码。大语言模型(LLMs)虽能生成符合习惯的代码,但无法保证正确性。我们提出SACTOR,一种基于LLM的C-to-Rust翻译工具,采用两阶段流程:首先生成语义保持的非习惯性代码,再进行习惯化优化以符合Rust规范。为验证函数级增量翻译的正确性,我们通过外部函数接口(FFI)进行端到端测试。在两个公开数据集共200个程序以及两个真实场景(CRust-Bench的50个样本和libogg库)上评估多个LLM。结果显示,SACTOR在各项数据集上均实现高可靠性端到端正确性,生成代码安全且符合习惯,最多减少7倍Clippy警告;在CRust-Bench上平均获得85%语义正确率和52%习惯化成功率;在libogg上,对GPT-5实现了完整的语义覆盖和最高达78%的习惯化覆盖率。

原文摘要 · Abstract (English)

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转RustLLM代码生成内存安全

Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。