2025-08-07 · 专家 · 数据科学 · 机器学习

EVOC2RUST: A Skeleton-guided Framework for Project-Level C-to-Rust Translation

本文提出了一种名为EVOC2RUST的框架,用于将整个C项目自动转换为等价的Rust代码,通过结合规则和基于LLM的方法,实现了在语法正确性、语义等价性和内存安全方面的显著提升。

更新于 2025-08-08。作者:Chaofan Wang、Tingrui Yu、Jie Wang、Dong Chen、Wenrui Zhang、Yuling Shi、Xiaodong Gu、Beijun Shen。

中文摘要

Rust的编译时安全性保障使其非常适合用于安全关键系统,从而产生了将旧C代码库翻译成Rust的需求。虽然已经出现了各种完成这项任务的方法,但它们都面临固有的权衡:基于规则的解决方案在满足代码安全性和风格一致性要求方面面临挑战,而基于大型语言模型(LLM)的解决方案由于整个代码库中模块之间的重度依赖,往往无法生成语义等价的Rust代码。最近的研究表明,这两种解决方案都局限于小型程序。在本文中,我们提出了EvoC2Rust,这是一个将整个C项目转换成等效Rust项目的自动化框架。EvoC2Rust采用骨架引导的翻译策略进行项目级翻译。该流程由三个进化阶段组成:1)首先将C项目分解为功能模块,使用增强特征映射的LLM转换定义和宏,并生成类型检查的功能存根,这些存根形成一个可编译的Rust骨架;2)然后增量地翻译函数,替换相应的存根占位符;3)最后,通过整合LLM和静态分析来修复编译错误。通过进化增强,EvoC2Rust结合了基于规则和基于LLM解决方案的优点。我们对开源基准测试和六个工业项目进行的评估表明,EvoC2Rust在项目级C到Rust翻译方面的性能优于其他方法。平均而言,它在语法和语义准确性方面比基于LLM的方法提高了17.24%和14.32%,并且比基于规则的工具的代码安全性率高出96.79%。在模块级别,EvoC2Rust在工业项目中实现了92.25%的编译率和89.53%的测试通过率,即使是复杂代码库和长函数也是如此。

英文摘要

Rust's compile-time safety guarantees make it ideal for safety-critical systems, creating demand for translating legacy C codebases to Rust. While various approaches have emerged for this task, they face inherent trade-offs: rule-based solutions face challenges in meeting code safety and idiomaticity requirements, while LLM-based solutions often 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 entire C projects to equivalent Rust ones. EvoC2Rust employs a skeleton-guided translation strategy for project-level translation. The pipeline consists of three evolutionary 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 the function, replacing the corresponding stub placeholder; 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 EvoC2Rust's superior performance in project-level C-to-Rust translation. On average, it achieves 17.24% and 14.32% improvements in syntax and semantic accuracy over the LLM-based approaches, along with a 96.79% higher code safety rate than the rule-based tools. At the module level, EvoC2Rust reaches 92.25% compilation and 89.53% test pass rates on industrial projects, even for complex codebases and long functions.

解读

一句话总结

本文提出了一种名为EVOC2RUST的框架,用于将整个C项目自动转换为等价的Rust代码,通过结合规则和基于LLM的方法,实现了在语法正确性、语义等价性和内存安全方面的显著提升。

问题1:这篇论文想要解决什么具体问题?

问题2:论文的核心创新点是什么?

问题3:实验结果如何验证了方法的有效性?

问题4:这个研究的实际应用价值是什么?

来源

机器可读索引:llms.txt