PR2: Peephole Raw Pointer Rewriting with LLMs for Translating C to Safer Rust
Yifei Gao, Chengpeng Wang, Pengxiang Huang, Xuwei Liu, Mingwei Zheng, Xiangyu Zhang
TL;DR
PR2 presents a peephole, LLM-guided pointer rewriting framework to convert C-to-Rust translations produced by C2Rust into memory-safe Rust code. By combining a decision-tree prompted pointer lifting with statement refactoring and code-change–driven repair, PR2 effectively reduces local raw pointers while preserving semantics on test suites. Evaluation over 28 real-world projects shows PR2 eliminates 2,057 raw pointers (13.22% of locals) at an average cost of $1.46 and 5.44 hours per project, outperforming the prior Laertes baseline. The work demonstrates how targeted, function-local rewrites guided by prompts and repair signals can substantially strengthen cross-language safety, with insights into data-structure choices and the importance of prompt design.
Abstract
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 peephole 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. Additionally, it 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 as a prototype and evaluate it using gpt-4o-mini on 28 real-world C projects. The results show that PR2 successfully eliminates 13.22% 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.44 hours, at an average cost of $1.46.
