使用LLM的原始指针重写用于将C翻译为更安全的Rust
Raw Pointer Rewriting with LLMs for Translating C to Safer Rust
AI总结:
本文提出PR2技术,通过决策树提示引导指针提升,消除C到Rust翻译中的原始指针,提升代码安全性。
摘要:
由于Rust的内存和线程安全保证,将C代码转换为Rust的兴趣日益增长。C2RUST等工具能指导语法引导的转换。然而,生成的Rust程序往往依赖大量不安全构造,特别是原始指针,这削弱了Rust的安全保证。本文旨在通过消除原始指针来提高C2RUST生成的Rust程序的内存安全性。具体而言,我们提出了一种原始指针重写技术,将单个函数中的原始指针提升到适当的Rust数据结构。技术上,PR2利用基于决策树的提示引导指针提升过程。它还利用代码更改分析来指导重写过程中引入的错误修复,有效解决编译和测试用例执行期间遇到的错误。我们实现了PR2,并在28个真实世界C项目上使用gpt-4o-mini进行评估。结果显示,PR2成功消除了这些项目中18.57%的本地原始指针,显著提高了翻译Rust代码的安全性。平均而言,PR2完成一个项目的转换需要5.02小时,成本为1.13美元。
英文摘要:
There has been a growing interest in translating C code to Rust due to Rust's robust memory and thread safety guarantees. Tools such as C2RUST enable syntax-guided transpilation from C to semantically equivalent Rust code. However, the resulting Rust programs often rely heavily on unsafe constructs, particularly raw pointers, which undermines Rust's safety guarantees. This paper aims to improve the memory safety of Rust programs generated by C2RUST by eliminating raw pointers. Specifically, we propose a raw pointer rewriting technique that lifts raw pointers in individual functions to appropriate Rust data structures. Technically, PR2 employs decision-tree-based prompting to guide the pointer lifting process. It also leverages code change analysis to guide the repair of errors introduced during rewriting, effectively addressing errors encountered during compilation and test case execution. We implement PR2 and evaluate it using gpt-4o-mini on 28 real-world C projects. It is shown that PR2 successfully eliminates 18.57% of local raw pointers across these projects, significantly enhancing the safety of the translated Rust code. On average, PR2 completes the transformation of a project in 5.02 hours, at a cost of $1.13.
机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。