TL;DR
SACTOR is a novel, LLM-driven tool that automates the translation of legacy C code into safe, idiomatic Rust. By decoupling the translation into a two-stage pipeline—first preserving the interface using unsafe Rust, then refining it into idiomatic safe Rust—and validating the results via FFI-based end-to-end testing, SACTOR achieves high correctness while eliminating unsafe code.
What Problem Does This Paper Address?
Migrating legacy C codebases to Rust is a highly effective way to eliminate memory safety vulnerabilities like buffer overflows and use-after-free errors. However, doing this manually is slow, expensive, and requires deep expertise in both languages.
Existing automated tools generally fall into two camps, both with major drawbacks:
- Rule-based transpilers (e.g., C2Rust): These generate syntactically correct Rust, but the output is essentially "C code written in Rust syntax," heavily reliant on
unsafeblocks and raw pointers. This defeats the purpose of moving to Rust. - Naive LLM prompting: Large Language Models are great at writing idiomatic code, but they suffer from hallucinations and offer zero correctness guarantees, which is unacceptable for systems-level software.
SACTOR bridges this gap by combining the creative, idiomatic coding capabilities of LLMs with the rigorous verification of static analysis and automated testing.
Methods
SACTOR employs a structured, four-stage neuro-symbolic pipeline to translate code incrementally:
- Task Division: A custom C Parser (built on
libclang) analyzes the source code, extracts semantic dependencies, and breaks the program down into smaller fragments (types, globals, and functions) to process in dependency order. - Stage 1: C $\rightarrow$ Unidiomatic Rust: The LLM translates C fragments into Rust while strictly preserving the original Application Binary Interface (ABI). This stage allows the use of
unsafeblocks and raw pointers to ensure semantic equivalence. - Stage 2: Unidiomatic $\rightarrow$ Idiomatic Rust: SACTOR uses Crown (a static ownership tracking tool) to provide the LLM with "hints" about pointer mutability, fatness (arrays), and ownership. The LLM uses this context to strip away
unsafeblocks, replacing raw pointers with safe Rust abstractions like references,Box, or vectors. - FFI-Based Verification: To guarantee correctness without relying on heavy formal verification, SACTOR links the translated Rust code back into the original C environment using the Foreign Function Interface (FFI). It then runs the codebase's existing end-to-end (E2E) tests. If a test fails, compiler diagnostics and test traces are fed back into the LLM for up to six repair attempts.
Datasets
The authors evaluated SACTOR across four distinct datasets using five different LLMs (GPT-4o, Claude 3.5 Sonnet, Gemini 2.0 Flash, Llama 3.3 70B, and DeepSeek-R1):
- TransCoder-IR & Project CodeNet: 200 small-scale programs derived from online coding challenges.
- CRust-Bench: A 50-sample subset of real-world, repository-level C benchmarks paired with safe Rust interfaces.
- libogg: A real-world C multimedia container library consisting of 77 functions and roughly 2,041 lines of code, heavily utilizing complex structs and pointer manipulation.
Key Insights and Ideas
- The Power of Two-Step Decomposition: Trying to jump directly from C to idiomatic Rust forces the LLM to handle syntax translation, architectural refactoring, and safety proofs all at once. Separating the process into semantic preservation (Stage 1) and idiomatic refinement (Stage 2) makes the problem significantly more manageable for the model.
- Overcoming Information-Theoretic Limits: Local C syntax is fundamentally ambiguous; a
char*could be a single character reference, a null-terminated string, or a dynamic array. SACTOR proves that LLMs cannot reliably guess the correct Rust type from local context alone. Providing whole-program static analysis hints (via Crown) is essential for accurate type migration. - Harnessing Reasoning Models: Reasoning-focused models like DeepSeek-R1 excel at handling complex code logic, string manipulation, and array bounds checking, outperforming traditional models on intricate translation tasks.
Key Results
SACTOR demonstrated exceptional performance across all benchmarks, consistently outperforming rule-based baselines in code quality:
- High Success Rates: On the TransCoder-IR dataset, DeepSeek-R1 achieved a 93% end-to-end success rate for idiomatic translation. On Project CodeNet, it reached 84%.
- Real-World Codebases: For the complex
libogglibrary, SACTOR achieved a 100% success rate in unidiomatic translation across all models, and up to a 78% idiomatic success rate using GPT-5 (compared to 53% with GPT-4o). - Superior Idiomaticity: Successful idiomatic translations achieved a 100% unsafe-free rate (0% unsafe tokens) and generated up to 7$\times$ fewer Clippy warnings than baseline approaches, proving the code is clean, maintainable, and truly idiomatic.
Limitations and Future Work
Despite its success, the authors note several limitations that pave the way for future research:
- Test Coverage Dependence: SACTOR relies on "soft equivalence" via existing E2E tests. If the test suite is shallow, subtle semantic bugs might slip through. Integrating automated fuzzing or test generation could mitigate this.
- Unsupported C Features: Complex macros, pervasive function pointers, global state, and inline assembly are currently out of scope or only partially handled.
- Harness Generation Stability: The rule-based harness generator can occasionally produce brittle adapters when faced with highly unusual pointer shapes or complex length expressions, causing verification to fail even if the underlying translation is correct.
Summary updated