arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2603.28686cs.SE
收藏

C2RustXW: 通过程序分析和LLM实现的程序结构感知的C到Rust翻译

C2RustXW: Program-Structure-Aware C-to-Rust Translation via Program Analysis and LLM

State Key Laboratory for Novel Software Technology, Nanjing University(南京大学新型软件技术重点实验室);Jiangsu Police Institute(江苏警官学院)

Yanyan Yan, Yang Feng, Jiangshan Liu, Di Liu, Zixi Liu, Hao Teng, Baowen Xu

AI总结:

本文提出C2RustXW工具,通过程序分析与LLM结合,实现结构感知的C到Rust翻译,提高代码正确性和可维护性,实验显示在CodeNet和GitHub上达到高语法正确率和代码压缩效果。

摘要:

随着Rust因其内存安全性和性能而被越来越多采用,对遗留C代码库的有效迁移需求增加。然而,现有基于规则的翻译器(如ctorust)常生成冗长且非idiomatic的代码,保留unsafe C语义,限制可读性、可维护性和实际应用。此外,此类输出的手动后处理劳动强度大且 rarely yields 高质量Rust代码,成为大规模迁移的显著障碍。为解决这些限制,我们提出了tool,一种程序结构感知的C到Rust翻译方法,整合程序分析与大语言模型(LLMs)。tool提取多级程序结构,包括全局符号、函数依赖性和控制-数据流信息,并将这些编码为结构化文本表示注入到LLM提示中以指导翻译和修复。基于此设计,tool执行依赖感知的翻译,并采用多阶段修复流程,结合基于规则和结构引导的LLM技术,确保语法正确性。对于语义正确性,tool进一步整合执行验证与结构引导推理以定位和修复行为不一致。实验结果表明,tool在CodeNet上实现100%语法正确性,在GitHub上实现97.78%的正确性,同时显著减少代码大小(最高43.70%)和不安全使用(降至5.75%)。在项目层面,tool实现完美语法正确性,平均语义正确性为78.87%,证明其在实际和可扩展的C到Rust迁移中的有效性。

英文摘要:

The growing adoption of Rust for its memory safety and performance has increased the demand for effective migration of legacy C codebases. However, existing rule-based translators (e.g., \ctorust) often generate verbose, non-idiomatic code that preserves unsafe C semantics, limiting readability, maintainability, and practical adoption. Moreover, manual post-processing of such outputs is labor-intensive and rarely yields high-quality Rust code, posing a significant barrier to large-scale migration. To address these limitations, we present \tool, a program-structure-aware C-to-Rust translation approach that integrates program analysis with Large Language Models (LLMs). \tool extracts the multi-level program structure, including global symbols, function dependencies, and control- and data-flow information, and encodes these as structured textual representations injected into LLM prompts to guide translation and repair. Based on this design, \tool performs dependency-aware translation and adopts a multi-stage repair pipeline that combines rule-based and structure-guided LLM-based techniques to ensure syntactic correctness. For semantic correctness, \tool further integrates execution-based validation with structure-guided reasoning to localize and repair behavioral inconsistencies. Experimental results show that \tool achieves 100\% syntactic correctness on CodeNet and 97.78\% on GitHub, while significantly reducing code size (up to 43.70\%) and unsafe usage (to 5.75\%). At the project level, \tool achieves perfect syntactic correctness and an average semantic correctness of 78.87\%, demonstrating its effectiveness for practical and scalable C-to-Rust migration.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑