Abstract
Migrating legacy C codebases to Rust is a primary strategy for eliminating memory safety vulnerabilities in systems programming. However, automated migration remains challenging: rule-based tools often produce non-idiomatic, "unsafe" code, while Large Language Models (LLMs) frequently struggle with Rust’s strict borrow checker and complex type system. This paper introduces IRENE (Integrates RulEs aNd sEmantics), a novel framework that synergizes static analysis rules with the reasoning capabilities of LLMs. By utilizing rule-aware example retrieval, structured code summarization, and iterative compiler-feedback loops, IRENE achieves significant improvements in translation accuracy and safety. Evaluation on the xCodeEval benchmark and an industrial dataset (HW-Bench) shows an 8–12% boost in accuracy and a drastic reduction in the generation of unsafe Rust code compared to state-of-the-art baselines.
Introduction
The transition from C to Rust is often described as the "holy grail" for systems engineers seeking to eliminate memory safety bugs at the source. Despite the clear benefits, manual migration is prohibitively expensive for large-scale legacy systems. Existing automated solutions generally fall into two categories, both of which possess significant drawbacks. Rule-based tools, such as C2Rust, provide efficient transliteration but often rely heavily on unsafe blocks to preserve C-style pointer semantics, thereby bypassing Rust's safety guarantees. Conversely, LLM-based translation models like GPT-4 or DeepSeek-Coder understand high-level intent but often fail to satisfy the Rust compiler, leading to "hallucinated" logic or compilation errors.
This paper identifies two primary gaps in current LLM-based approaches: a lack of safety-aware rule adaptation and a deficiency in deep semantic understanding. To address these, we present IRENE, a framework designed to provide LLMs with the necessary context and feedback to produce safe, idiomatic Rust code.
The IRENE Framework
IRENE operates through a three-pillared architecture designed to bridge the gap between rigid rules and fluid semantic reasoning.
1. Rule-Augmented Retrieval Module
Rather than relying on generic few-shot prompting, IRENE employs a custom static analyzer to identify specific C patterns within the source code, such as complex pointer arithmetic or specific I/O operations. The framework then uses the BM25 algorithm to retrieve the most relevant C-to-Rust translation pairs from a curated "Demonstration Corpus" of approximately 23,000 successful translations. This provides the LLM with a targeted "cheat sheet" of how similar technical challenges have been solved safely in Rust.
2. Structured Summarization Module
To prevent the model from becoming overwhelmed by low-level syntax mismatches, IRENE requires the LLM to generate a structured summary of the C code before attempting translation. This summary details I/O types and core functional logic. By forcing the model to "think" at a higher level of abstraction, the framework ensures that the resulting Rust code reflects the original program's intent rather than just its syntax.
3. Error-Driven Translation Module
Translation is treated as an iterative process rather than a single-shot task. IRENE attempts to compile the generated Rust code using the standard Rust compiler (rustc). If compilation fails, the specific diagnostic error messages are fed back into the LLM. This iterative refinement loop allows the model to self-correct, mimicking the workflow of a human developer debugging their way through a migration.
Experimental Evaluation
Datasets
The performance of IRENE was evaluated using two primary sources:
- xCodeEval: A large-scale public benchmark consisting of competitive programming problems.
- HW-Bench: A proprietary industrial dataset provided by Huawei, containing 100 C functions from real-world product lines, providing a measure of the framework's industrial viability.
Key Results
IRENE was tested across eight different LLMs, including DeepSeek-Coder and Qwen2.5-Coder. The results demonstrate a clear advantage over standard Retrieval-Augmented Generation (RAG) and zero-shot baselines:
- Accuracy and Compilation: On the xCodeEval benchmark, IRENE outperformed the strongest baseline by an average of 8.06% in computational accuracy and 12.74% in compilation success rate.
- Safety: One of the most significant findings was the reduction in the "Unsafe Rate." While traditional tools often produce code that is nearly 100% unsafe for complex functions, IRENE reduced this rate to just 1.70%.
- Industrial Performance: On the Huawei HW-Bench, IRENE improved the compilation success rate by 12.33% and reduced the use of
unsafeblocks by 26% compared to standard prompting.
Discussion and Insights
The research highlights that LLMs do not necessarily need to be pre-trained on every nuance of Rust if they are provided with the right context at the right time. A key insight was the importance of summary quality; while smaller models (e.g., Qwen-1.5B) struggled to summarize complex logic, using a larger model (e.g., Qwen-32B) to provide the summary significantly improved the smaller model's translation performance. Furthermore, the compiler feedback loop proved to be a "superpower," enabling models to resolve borrow-checker issues that were initially beyond their reasoning capabilities.
Conclusion and Future Work
IRENE represents a significant step forward in making automated C-to-Rust migration both practical and safe. By integrating static rules with semantic summaries, the framework produces code that is not only functional but also idiomatic. Future work will focus on expanding the framework's scope from function-level translation to entire projects with complex inter-file dependencies, as well as extending support to C++ to Rust translation, which involves more complex abstractions like templates and class hierarchies.
Summary updated