Abstract
Migrating legacy C codebases to Rust is a critical strategy for eliminating memory safety vulnerabilities. However, automated transpilation tools often produce "unsafe" Rust code that relies heavily on raw pointers, bypassing Rust's safety guarantees. This paper presents PR² (Peephole Raw Pointer Rewriting), a framework that leverages Large Language Models (LLMs) to refactor raw pointers into safe, idiomatic Rust abstractions. By combining a decision-tree prompting strategy with iterative repair guided by code-diff analysis, PR² successfully eliminated over 13% of local raw pointers across 28 real-world projects, outperforming traditional static analysis tools by more than 3x.
1. Introduction
The industry is currently undergoing a massive push to migrate legacy C codebases to Rust to eliminate memory safety vulnerabilities like the Heartbleed bug. While manual rewriting is labor-intensive, existing automated tools like C2Rust perform "syntax-guided transpilation," which results in code that is essentially C written with Rust syntax. This code relies heavily on raw pointers, bypassing Rust’s borrow checker and ownership model. Previous attempts to use static analysis to "lift" these pointers into safe references have struggled with complex loops, library API calls, and understanding high-level programmer intent.
2. Methodology
PR² treats pointer refactoring as a two-phase "peephole" optimization problem, focusing on local pointers within function scopes to ensure safety and prevent cascading breaks across codebases.
2.1 Phase 1: Peephole Rewriting
Instead of a naive "fix this code" prompt, PR² employs a decision-tree-based prompting strategy. This forces the LLM to reason through three specific properties of a pointer:
- Ownership: Does this pointer own the memory (requiring
BoxorVec) or just borrow it? - Shape: Is it a single value (singleton) or a collection (array)?
- Access: Is it read-only or read-write?
Based on these properties, the LLM selects a safe Rust equivalent, such as Option<Box<T>> or &[T], and refactors the function body accordingly.
2.2 Phase 2: Peephole Repair
LLMs may introduce compilation errors or break logic during refactoring. PR² enters an iterative loop using the Rust compiler and existing test suites as feedback. To prevent the model from being overwhelmed by large functions, the authors utilize Code Change Analysis. By feeding the LLM only the specific "diff" between the original and refactored code, the model can focus its attention on the likely source of the error, significantly improving the success rate of the repair phase.
3. Experimental Evaluation
The authors evaluated PR² on 28 real-world C projects sourced from the NOPCRAT benchmark, totaling over 136,000 lines of code. These projects range from small utilities to complex systems like wget, grep, and gawk.
3.1 Key Results
- Effectiveness: PR² successfully eliminated 2,057 raw pointers, representing 13.22% of all local raw pointers in the dataset.
- Comparison: The framework eliminated 3.48x more pointers than LAERTES, the current state-of-the-art static analysis tool for this task.
- Efficiency: Using
gpt-4o-mini, the average cost to process an entire project was just $1.46, with an average automated processing time of 5.44 hours. - Semantic Preservation: A user study with ten Rust developers confirmed that the refactored code preserved the original program's semantics in all sampled cases.
4. Discussion and Limitations
PR² demonstrates that LLMs can recognize patterns—such as slice traversals in convoluted pointer logic—that traditional static analysis cannot. By constraining the LLM's "creativity" through a decision tree, the framework prevents hallucinations and ensures logical refactoring paths.
However, several limitations remain. The current "peephole" approach is limited to local scope; it does not modify function signatures to avoid breaking external dependencies. Additionally, the system relies heavily on the quality of a project's test suite for the repair phase. Future work will explore expanding the decision tree to handle complex data structures like linked lists and propagating changes across function boundaries through inter-procedural analysis.
5. Conclusion
PR² represents a significant step forward in automated C-to-Rust migration. By combining the pattern-recognition strengths of LLMs with structured decision logic and iterative, diff-based repair, it offers a cost-effective and highly efficient method for cleaning up unsafe raw pointers in transpiled Rust code.
Summary updated