自动化将完整C项目翻译为等效Rust代码,兼顾安全与可读性。
EvoC2Rust: A Skeleton-guided Framework for Project-Level C-to-Rust Translation
- 用骨架引导策略分三步完成项目级转换:模块分解、函数逐步翻译、错误修复。
- 在开源与工业项目上,语法准确率比最强LLM基线高17.24%,语义准确率高14.32%。
- 适合需要高安全性的嵌入式或系统级开发人员,尤其关注大规模迁移场景。
将遗留的C代码库转换为Rust的需求日益增长,用于构建安全关键系统。尽管已有多种方法出现,但普遍存在固有权衡:基于规则的方法难以满足代码安全性和惯用性要求,而基于大模型的方法常因整个代码库中模块间依赖复杂,无法生成语义等价的Rust代码。近期研究指出,现有方案仅适用于小规模程序。本文提出EvoC2Rust,一个面向完整C项目到等效Rust项目的自动化框架。该框架采用骨架引导翻译策略,包含三个阶段:首先将C项目分解为功能模块,利用增强特征映射的大模型转换定义与宏,并生成类型检查通过的函数桩,构成可编译的Rust骨架;其次逐步翻译函数,替换对应桩位;最后结合大模型与静态分析修复编译错误。通过进化增强,该方法融合了规则与大模型的优势。在开源基准和六个工业项目上的评估显示,EvoC2Rust在项目级转换中表现卓越,其语法准确率比最强的基于大模型基线提升17.24%,语义准确率提升14.32%,代码安全率比最佳规则工具高出43.59%。
原文摘要 · Abstract (English)
Translating legacy C codebases to Rust is increasingly demanded for building safety-critical systems. While various approaches have emerged for this task, they face inherent trade-offs: rule-based methods often struggle to satisfy code safety and idiomaticity requirements, while LLM-based methods frequently fail to generate semantically equivalent Rust code, due to the heavy dependencies of modules across the entire codebase. Recent studies have revealed that both solutions are limited to small-scale programs. In this paper, we propose EvoC2Rust, an automated framework for converting complete C projects to equivalent Rust ones. EvoC2Rust employs a skeleton-guided translation strategy for project-level translation. The pipeline consists of three stages: 1) it first decomposes the C project into functional modules, employs a feature-mapping-enhanced LLM to transform definitions and macros, and generates type-checked function stubs, which form a compilable Rust skeleton; 2) it then incrementally translates functions, replacing the corresponding stub placeholders; 3) finally, it repairs compilation errors by integrating LLM and static analysis. Through evolutionary augmentation, EvoC2Rust combines the advantages of both rule-based and LLM-based solutions. Our evaluation on open-source benchmarks and six industrial projects demonstrates the superior performance of EvoC2Rust in project-level C-to-Rust translation. The results show that our approach outperforms the strongest LLM-based baseline by 17.24% in syntax accuracy and 14.32% in semantic accuracy, while also achieving a 43.59% higher code safety rate than the best rule-based tool.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。