2025-11-14T08:10:11.695880

Project-Level C-to-Rust Translation via Synergistic Integration of Knowledge Graphs and Large Language Models

Yuan, Mao, Chen et al.
Translating C code into safe Rust is an effective way to ensure its memory safety. Compared to rule-based translation which produces Rust code that remains largely unsafe, LLM-based methods can generate more idiomatic and safer Rust code because LLMs have been trained on vast amount of human-written idiomatic code. Although promising, existing LLM-based methods still struggle with project-level C-to-Rust translation. They typically partition a C project into smaller units (\eg{} functions) based on call graphs and translate them bottom-up to resolve program dependencies. However, this bottom-up, unit-by-unit paradigm often fails to translate pointers due to the lack of a global perspective on their usage. To address this problem, we propose a novel C-Rust Pointer Knowledge Graph (KG) that enriches a code-dependency graph with two types of pointer semantics: (i) pointer-usage information which record global behaviors such as points-to flows and map lower-level struct usage to higher-level units; and (ii) Rust-oriented annotations which encode ownership, mutability, nullability, and lifetime. Synthesizing the \kg{} with LLMs, we further propose \ourtool{}, which implements a project-level C-to-Rust translation technique. In \ourtool{}, the \kg{} provides LLMs with comprehensive pointer semantics from a global perspective, thus guiding LLMs towards generating safe and idiomatic Rust code from a given C project. Our experiments show that \ourtool{} reduces unsafe usages in translated Rust by 99.9\% compared to both rule-based translation and traditional LLM-based rewriting, while achieving an average 29.3\% higher functional correctness than those fuzzing-enhanced LLM methods.
academic

Project-Level C-to-Rust Translation via Synergistic Integration of Knowledge Graphs and Large Language Models

Basic Information

  • Paper ID: 2510.10956
  • Title: Project-Level C-to-Rust Translation via Synergistic Integration of Knowledge Graphs and Large Language Models
  • Authors: Zhiqiang Yuan, Wenjun Mao, Zhuo Chen, Xiyue Shang, Chong Wang, Yiling Lou, Xin Peng
  • Classification: cs.SE (Software Engineering), cs.AI (Artificial Intelligence)
  • Publication Date: October 13, 2025
  • Paper Link: https://arxiv.org/abs/2510.10956

Abstract

Translating C code to safe Rust code is an effective approach for ensuring memory safety. Compared to rule-based translation methods that produce substantial unsafe code, Large Language Model (LLM)-based approaches can generate more idiomatic and safer Rust code. However, existing LLM methods still struggle with project-level C-to-Rust translation, particularly lacking a global perspective when handling pointers. To address this challenge, this paper proposes a novel C-Rust Pointer Knowledge Graph (KG) that enriches code dependency graphs with two types of pointer semantics: (1) pointer usage information recording global behavior, and (2) Rust-oriented annotations encoding ownership, mutability, nullability, and lifetime. Based on this knowledge graph, the PTRMAPPER technique is proposed to enable project-level C-to-Rust translation. Experimental results demonstrate that PTRMAPPER reduces unsafe code usage by 99.9% compared to rule-based and conventional LLM methods, and improves functional correctness by an average of 29.3%.

Research Background and Motivation

1. Problem Definition

C language is widely used in operating systems, embedded systems, and performance-critical applications; however, its manual memory management and direct pointer operations frequently lead to security vulnerabilities such as buffer overflows and memory leaks. Rust, as a modern alternative, ensures memory safety while maintaining C's performance characteristics. Therefore, automatic translation of legacy C code to Rust has become an urgent necessity.

2. Limitations of Existing Approaches

  • Rule-based methods: Rely on predefined rules and produce Rust code with extensive use of unsafe blocks, raw pointers, and foreign function calls, still posing safety risks
  • Existing LLM methods: Adopt a bottom-up unit translation paradigm, lacking a global perspective on pointer usage, frequently encountering definition-use conflicts in pointer translation

3. Core Challenges

Fundamental differences exist between C and Rust in pointer usage:

  • C Language: Pointers are highly flexible; at definition, only the type is specified, and at usage, reads, writes, and modifications are freely permitted
  • Rust Language: Pointer usage is strictly constrained; at definition, ownership, mutability, and lifetime must be explicitly specified, and at usage, strict adherence to borrow checker rules is mandatory

Core Contributions

  1. Proposed a novel C-Rust Pointer Knowledge Graph (KG): Comprehensively models code unit dependencies, pointer usage information, and Rust-oriented annotations from project-level context
  2. Designed the PTRMAPPER project-level translation technique: Leverages synergistic integration of C-Rust Pointer KG and LLM to translate C projects into safe and idiomatic Rust code
  3. Achieved significant performance improvements: Generates more idiomatic, safer, and more correct Rust code
  4. Provided comprehensive experimental validation: Ablation studies demonstrate the importance of pointer usage information and Rust-oriented annotations for improving translation performance

Methodology Details

Task Definition

Given a C project, automatically translate it into a functionally equivalent, memory-safe, and idiomatic Rust project. Input consists of a collection of C source files; output is a compilable and executable Rust project.

Model Architecture

PTRMAPPER comprises three main phases:

1. C-Rust Pointer Knowledge Graph Construction

Knowledge Graph Schema Design:

  • Code Dependency Graph: Describes dependency relationships among code units (functions, structures, enumerations, etc.)
  • Pointer Usage Information: Captures project-level pointer behavior, including:
    • Entities: Param (parameters), Value (return values), Member (members), Pointer (pointers), etc.
    • Relations: derivesFrom (derivation), mayAlias/noAlias (aliasing), pointsTo (pointing), etc.
  • Rust-oriented Annotations: Contains semantics of ownership, mutability, nullability, and lifetime

Construction Process:

  1. Code Dependency Graph Extraction: Uses static analysis to extract code units and their dependency relationships
  2. Pointer Usage Information Extraction: Employs context-sensitive and field-sensitive analysis strategies
  3. Rust-oriented Annotation Extraction: Analyzes pointer lifetimes based on predefined rules and annotates corresponding semantics

2. KG-Guided Code Translation

Translation Unit Identification and Ordering:

  • Uses Tarjan's algorithm to detect strongly connected components (SCCs)
  • Treats each SCC as an independent translation unit
  • Topological sorting determines bottom-up translation order

Pointer Semantic Extraction: Extracts corresponding pointer semantic knowledge for each translation unit from the KG, provided to the LLM in the form of <entity1, relation, entity2> triples.

Incremental Translation and Verification:

  • Integrates translation units, pointer semantic knowledge, and previously translated Rust context into the translation prompt
  • Immediately integrates and compiles each translated unit into the Rust project for verification
  • Triggers error correction mechanisms upon compilation errors

3. KG-Guided Error Correction

Leverages the Rust version of the pointer KG to provide project-level semantic context to the LLM:

  • Dependency relationships help precisely identify code units related to errors
  • Rust-oriented annotations provide precise program semantics to guide effective fixes

Technical Innovations

  1. Global Pointer Semantic Modeling: First integrates project-level pointer usage information and Rust-specific semantics into a knowledge graph
  2. Synergistic Translation Paradigm: Combines the global perspective of knowledge graphs with the generative capabilities of LLMs
  3. Incremental Verification Mechanism: Timely detection and correction of errors, preventing error accumulation
  4. Intelligent Error Correction: Based on KG semantic information rather than relying solely on compiler error messages

Experimental Setup

Dataset

Uses 16 real C projects from the CROWN dataset, with code ranging from 154 to 14,829 lines, including:

  • Small projects: avl, buffer, genann, quadtree, rgba, urlparser, etc.
  • Large projects: bzip2, heman, lodepng, etc.

To evaluate functional equivalence, unit tests were manually constructed, achieving line coverage of 81.4%-97.7% and function coverage of 92.9%-100.0%.

Evaluation Metrics

Idiomaticity Assessment:

  • Lint Alert Count: Uses Rust-Clippy to count non-idiomatic code warnings
  • Unsafe Usage Count: Uses Cargo-geiger to count unsafe code usage instances

Correctness Assessment:

  • Compiled: Proportion of functions that compile successfully
  • Equiv.: Proportion of functions passing unit tests

Comparison Methods

  • Idiomaticity Comparison: CROWN (rule-based), PR2 (LLM rewriting)
  • Correctness Comparison: FLOURINE (fuzzing-enhanced LLM method)
  • Ablation Studies: PTRTRANSPS, PTRTRANSPU, PTRTRANSRA, PTRTRANSEC variants, etc.

Implementation Details

  • LLM: ChatGPT-4o, temperature=0
  • Static Analysis: Clang, Doxygen, SVF framework
  • Error Correction: Maximum 5 iterations
  • Compilation Verification: cargo check

Experimental Results

Main Results

Significant Idiomaticity Improvements:

  • Compared to CROWN: Lint warnings reduced by 94.9% (6,802→349), unsafe usage reduced by 99.9% (141,866→85)
  • Compared to PR2: Lint warnings reduced by 91.8% (4,272→349), unsafe usage reduced by 99.9% (134,185→85)

Substantial Correctness Improvements:

  • Compared to FLOURINE: Compilation success rate improved by 28.4% (98.3% vs 69.9%), functional equivalence rate improved by 29.3% (81.6% vs 52.3%)
  • Small projects (avl, rgba, ht, bst) achieve 100% functional equivalence

Ablation Studies

Ablation experiments validate the importance of each component:

  • Without Pointer Semantics (PTRTRANSPS): Equivalence rate drops from 81.6% to 59.5%, a decrease of 22.1%
  • Pointer Usage Information Only (PTRTRANSRA): Performance inferior to the complete version
  • Rust Annotations Only (PTRTRANSPU): Similar performance degradation
  • Without Error Correction (PTRTRANSEC): Equivalence rate drops from 81.6% to 50.8%, a decrease of 30.8%

Performance Distribution Analysis

PTRMAPPER demonstrates stable performance across different complexity levels:

  • Code Lines: Performance remains stable with increasing complexity; FLOURINE shows sharp decline
  • Dependency Relationships: Equivalence rate of 57.1% when dependencies [16,44); FLOURINE achieves 0%
  • Pointer Quantity: Equivalence rate of 50.0% when pointers [20,50); FLOURINE achieves 0%

Case Analysis

Using the Quadtree project as an example, demonstrating how PTRMAPPER resolves complex borrowing conflicts:

  1. Identifies tree parameter should be immutable borrow &Quadtree
  2. Separates root as mutable borrow &mut QuadtreeNode
  3. Extracts key_free as independent parameter to avoid borrowing conflicts
  4. Correctly handles ownership semantics

C-to-Rust Translation Research

  1. Rule-based Methods: Corrode, C2RUST, etc., producing substantial unsafe code
  2. LLM-based Methods: SPECTRA, FLOURINE, etc., lacking global perspective
  3. Unit Translation Methods: SYZYGY, etc., with pointer semantic loss issues

Advantages of This Work

  • First systematic modeling of project-level pointer semantics
  • Synergistic approach combining static analysis and LLM generation
  • Significant improvements in safety and correctness

Conclusions and Discussion

Main Conclusions

  1. The C-Rust Pointer Knowledge Graph effectively addresses pointer semantic loss in project-level translation
  2. Synergistic integration of static analysis and LLM significantly improves translation quality
  3. Incremental verification and intelligent error correction mechanisms ensure translation reliability

Limitations

  1. Construction Overhead: Knowledge graph construction requires additional static analysis time
  2. Rule Dependency: Rust annotation extraction depends on predefined rules, potentially with incomplete coverage
  3. Project Scale: Scalability for ultra-large projects remains to be verified
  4. Manual Intervention: Complex cases may still require limited manual adjustment

Future Directions

  1. Optimize knowledge graph construction efficiency
  2. Extend to additional programming language pairs
  3. Integrate additional program analysis techniques
  4. Enhance automation degree

In-Depth Evaluation

Strengths

  1. Strong Innovation: First combines knowledge graphs with LLMs for code translation, addressing fundamental problems of existing methods
  2. Systematic Methodology: Complete from problem analysis to solution design, with clear technical roadmap
  3. Comprehensive Experiments: 16 real projects, multiple comparison methods, detailed ablation studies
  4. Significant Results: Achieves substantial improvements across safety, idiomaticity, and correctness dimensions
  5. High Practical Value: Solves real engineering problems with strong application prospects

Weaknesses

  1. Scalability Concerns: Computational complexity of static analysis may limit application to ultra-large projects
  2. Rule Completeness: Rust annotation extraction rules may not cover all edge cases
  3. Evaluation Limitations: Primarily validated on medium-sized projects; performance on large projects requires further verification
  4. Quality Dependency: Method effectiveness largely depends on static analysis accuracy

Impact

  1. Academic Contribution: Provides new research paradigm for code translation field, combining program analysis and generative AI
  2. Engineering Value: Provides practical tools for C-to-Rust migration, helping improve software safety
  3. Technical Inspiration: Knowledge graph-guided LLM approach can be generalized to other code generation tasks

Applicable Scenarios

  1. Legacy System Migration: Suitable for projects requiring C codebase migration to Rust
  2. Safety-Critical Applications: Systems with high memory safety requirements
  3. Automation Tools: Can be integrated into IDE or CI/CD pipelines
  4. Education and Training: Helps developers learn Rust memory safety concepts

References

The paper cites 76 relevant references covering multiple domains including code translation, program analysis, and large language models, providing solid theoretical foundation for the research.


Overall Assessment: This is a high-quality software engineering research paper that innovatively combines knowledge graphs and large language models to address key challenges in C-to-Rust translation. The methodology is well-designed, experimental validation is comprehensive, and results are convincing. Although certain limitations exist, the work opens new research directions in the code translation field and possesses significant academic value and practical significance.