English

Raw Pointer Rewriting with LLMs for Translating C to Safer Rust

Software Engineering 2026-04-21 v3 Artificial Intelligence Programming Languages

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 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.

Keywords

Cite

@article{arxiv.2505.04852,
  title  = {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},
  journal= {arXiv preprint arXiv:2505.04852},
  year   = {2026}
}
R2 v1 2026-06-28T23:25:08.948Z