LLM-Driven Multi-step Translation from C to Rust using Static Analysis

πŸ“… 2025-03-16
πŸ“ˆ Citations: 0
✨ Influential: 0
πŸ“„ PDF
πŸ€– AI Summary
This work addresses the challenge of securely, efficiently, and idiomatically migrating C programs to Rust. We propose a static-analysis-guided, two-stage LLM-based translation framework: Stage I generates semantically equivalent yet non-idiomatic Rust code; Stage II performs fine-grained refactoring into idiomatic Rust by jointly leveraging Rust’s ownership model and Clang-based static analysis. We introduce the first zero-shot, formal-verification-enhanced translation paradigm, integrating semantic equivalence checking with end-to-end FFI testing to overcome the correctness assurance gap inherent in pure LLM approaches. Evaluated on 200 real-world C programs, DeepSeek-R1 achieves 93% functional correctness; GPT-4o and Claude 3.5 both attain 84%, significantly outperforming existing tools. Moreover, our outputs strictly adhere to Rust idioms and memory safety guarantees.

Technology Category

Application Category

πŸ“ Abstract
Translating software written in legacy languages to modern languages, such as C to Rust, has significant benefits in improving memory safety while maintaining high performance. However, manual translation is cumbersome, error-prone, and produces unidiomatic code. Large language models (LLMs) have demonstrated promise in producing idiomatic translations, but offer no correctness guarantees as they lack the ability to capture all the semantics differences between the source and target languages. To resolve this issue, we propose SACTOR, an LLM-driven C-to-Rust zero-shot translation tool using a two-step translation methodology: an"unidiomatic"step to translate C into Rust while preserving semantics, and an"idiomatic"step to refine the code to follow Rust's semantic standards. SACTOR utilizes information provided by static analysis of the source C program to address challenges such as pointer semantics and dependency resolution. To validate the correctness of the translated result from each step, we use end-to-end testing via the foreign function interface to embed our translated code segment into the original code. We evaluate the translation of 200 programs from two datasets and two case studies, comparing the performance of GPT-4o, Claude 3.5 Sonnet, Gemini 2.0 Flash, Llama 3.3 70B and DeepSeek-R1 in SACTOR. Our results demonstrate that SACTOR achieves high correctness and improved idiomaticity, with the best-performing model (DeepSeek-R1) reaching 93% and (GPT-4o, Claude 3.5, DeepSeek-R1) reaching 84% correctness (on each dataset, respectively), while producing more natural and Rust-compliant translations compared to existing methods.
Problem

Research questions and friction points this paper is trying to address.

Translate legacy C code to modern Rust for memory safety.
Ensure correctness and idiomaticity in C-to-Rust translation.
Use static analysis and LLMs for accurate, idiomatic Rust code.
Innovation

Methods, ideas, or system contributions that make the work stand out.

LLM-driven C-to-Rust translation tool
Two-step translation: unidiomatic and idiomatic
Static analysis for pointer semantics resolution
πŸ”Ž Similar Papers
No similar papers found.