TLDR: PTRMAPPER is a novel framework for translating entire C projects into safe and idiomatic Rust. It addresses the challenges of pointer handling in C-to-Rust translation by integrating a C-Rust Pointer Knowledge Graph (KG) with Large Language Models (LLMs). The KG captures code dependencies, global pointer usage, and Rust-oriented annotations (ownership, mutability, lifetime), providing LLMs with comprehensive context. This KG-guided approach, combined with incremental verification and error correction, significantly improves the idiomaticity, safety, and functional correctness of translated Rust code compared to existing rule-based and LLM-based methods, especially for complex, large-scale projects.
The world of software development constantly seeks ways to enhance code safety and efficiency. A significant challenge lies in migrating legacy C code, widely used in critical systems, to modern, memory-safe languages like Rust. C, while powerful, is notorious for vulnerabilities such as buffer overflows and memory leaks due to its manual memory management and direct pointer manipulation. Rust offers a compelling alternative, providing performance comparable to C while guaranteeing memory safety through its unique ownership and borrowing system.
However, automatically translating C code to Rust, especially at a project level, has proven difficult. Existing methods, whether rule-based or those relying on Large Language Models (LLMs), often fall short. Rule-based approaches tend to produce unsafe and unidiomatic Rust code, while LLM-based methods, despite generating more natural Rust, struggle with the complexities of project-level translation. A major hurdle is the fundamental difference in how C and Rust handle pointers – variables that directly reference memory locations. C’s flexible pointer usage contrasts sharply with Rust’s strict rules around ownership, mutability, and lifetimes, leading to frequent translation errors when LLMs attempt to translate code unit-by-unit without a global understanding.
Introducing PTRMAPPER: A Novel Approach
To overcome these limitations, researchers have developed PTRMAPPER, a groundbreaking technique for project-level C-to-Rust translation. PTRMAPPER synergistically combines the power of a novel C-Rust Pointer Knowledge Graph (KG) with Large Language Models to produce safe and idiomatic Rust code. The core idea is to provide LLMs with a comprehensive, global perspective on pointer semantics, which is often missing in traditional unit-by-unit translation.
The C-Rust Pointer Knowledge Graph
At the heart of PTRMAPPER is the C-Rust Pointer KG. This knowledge graph acts as a structured repository of information about the C project, going beyond simple code dependencies. It captures three crucial types of information:
- Code-Dependency Graph: This forms the skeleton, mapping relationships between different code units like functions, structs, and enums.
- Pointer-Usage Information: This is vital for understanding how pointers behave across the entire project. It records global behaviors such as where pointers point, how they are accessed within data structures, and how lower-level data structures are used by higher-level functions. This global view helps resolve ambiguities that arise when looking at individual code units in isolation.
- Rust-Oriented Annotations: These annotations explicitly encode Rust-specific pointer semantics, including ownership (whether a pointer owns the data or just borrows it), mutability (whether the data can be changed), nullability (whether a pointer can be null), and lifetimes (how long a reference is valid). These are determined by analyzing the pointer’s lifecycle throughout the project using predefined rules.
How PTRMAPPER Works
PTRMAPPER operates in three main stages:
1. C-Rust Pointer KG Construction: First, the C project undergoes preprocessing (macro expansion) and static analysis to build the code-dependency graph. Then, sophisticated context-sensitive and field-sensitive analyses are performed to extract detailed pointer-usage information and apply Rust-oriented annotations based on a set of predefined rules for ownership, mutability, nullability, and lifetimes.
2. KG-Guided Translation: The constructed KG guides the translation process. The code-dependency graph helps identify translation units (groups of interdependent code) and determines a bottom-up translation order, ensuring dependencies are handled first. Crucially, PTRMAPPER removes manual memory deallocation calls (like free) from the C code, aligning with Rust’s automatic memory management. For each translation unit, PTRMAPPER extracts relevant pointer semantic knowledge from the KG and integrates it into the LLM’s prompt. This enriched prompt guides the LLM to generate accurate and idiomatic Rust code. The generated Rust code is then incrementally integrated into a new Rust project and immediately compiled for verification.
3. KG-Guided Error Correction: If compilation errors occur, PTRMAPPER doesn’t just rely on generic error messages. Instead, it uses a Rust copy of the pointer KG to provide the LLM with project-level semantic context. This allows the LLM to precisely identify problematic code units, understand inter-component interactions, and leverage Rust-oriented annotations to guide effective fixes. The process is iterative, with the LLM generating patches until the code compiles successfully or a predefined limit is reached. If an error persists, PTRMAPPER generates a function stub to maintain project integrity and compilability.
Also Read:
- Optimizing LLM Code Translation for Longer Programs with Identifier Replacement
- Unlocking Large Codebases: A Vector Graph System for Smarter File Retrieval
Experimental Results
PTRMAPPER was evaluated on 16 real-world C projects, demonstrating significant improvements over existing methods:
- Idiomaticity: PTRMAPPER drastically reduced Lint warnings (by 94.9% compared to CROWN and 91.8% compared to PR2) and unsafe code usage (by 99.9% against both CROWN and PR2). This indicates that PTRMAPPER generates Rust code that is not only safer but also adheres more closely to Rust’s best practices.
- Correctness: Compared to FLOURINE, another LLM-based translation approach, PTRMAPPER achieved an average 28.4% higher compilation success rate and a 29.3% higher functional correctness rate. This highlights PTRMAPPER’s ability to produce functionally equivalent and compilable Rust projects.
- Ablation Study: The study confirmed that both pointer-usage information and Rust-oriented annotations from the C-Rust Pointer KG, as well as the KG-guided error correction phase, are essential for PTRMAPPER’s superior performance.
- Performance Distribution: PTRMAPPER maintained stable performance even as code complexity (lines of code, function dependencies, pointer counts) increased, a notable advantage over FLOURINE, which saw its performance decline sharply with complexity.
In conclusion, PTRMAPPER represents a significant leap forward in automated C-to-Rust translation. By integrating a sophisticated C-Rust Pointer Knowledge Graph with Large Language Models, it provides the necessary global context to generate safer, more idiomatic, and functionally correct Rust code from complex C projects. This work paves the way for more efficient and reliable migration of legacy C systems to the memory-safe Rust ecosystem. For more details, you can refer to the full research paper here.


