PR2: Peephole Raw Pointer Rewriting with LLMs for Translating C to Safer Rust

Yifei Gao, Chengpeng Wang, Pengxiang Huang, Xuwei Liu, Mingwei Zheng +1 more
5/7/2025
cs.SEcs.AIcs.PL

No confident code implementation found for this paper.

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.

Show full abstract

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.

View on arXivView PDF

Code Implementations

No confident code match yet

We couldn't find an author-owned or strongly-evidenced community implementation for this paper. Any repos shown below are weak matches — verify before relying on them.

No code implementations found yet.

Know of an implementation? Let us know in the comments below!

Similar Papers

Cite this paper

@article{gao2025pr,
  title  = {PR2: Peephole Raw Pointer Rewriting with LLMs for Translating C to Safer Rust},
  author = {Yifei Gao and Chengpeng Wang and Pengxiang Huang and Xuwei Liu and Mingwei Zheng and Xiangyu Zhang},
  year   = {2025},
  eprint = {2505.04852},
  archivePrefix = {arXiv},
  url    = {http://arxiv.org/abs/2505.04852v2}
}

Add a badge to your README

Shows whether official code is linked for this paper.

Code of Paper badge
[![Code of Paper](https://codeofpaper.com/badge/2505.04852.svg)](https://codeofpaper.com/paper/2505.04852)

Discussion