SACTOR: LLM-Driven Correct and Idiomatic C to Rust Translation with Static Analysis and FFI-Based Verification

Tianyang Zhou, Ziyi Zhang, Haowen Lin, Somesh Jha, Mihai Christodorescu, Kirill Levchenko, Varun Chandrasekaran

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:

  1. 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 unsafe blocks and raw pointers. This defeats the purpose of moving to Rust.
  2. 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:

  1. 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.
  2. 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 unsafe blocks and raw pointers to ensure semantic equivalence.
  3. 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 unsafe blocks, replacing raw pointers with safe Rust abstractions like references, Box, or vectors.
  4. 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.
Figure 1: Overview of the SACTOR methodology.

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 libogg library, 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.
Figure 2: Success rates (SR) across different LLM models for the TransCoder-IR and CodeNet datasets. SR 1-6 represent the number of attempts made to achieve a successful translation. Unid. and Idiom. denote unidiomatic and idiomatic translation steps, respectively.

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

Abstract

Translating software written in C to Rust has significant benefits in improving memory safety. However, manual translation is cumbersome, error-prone, and often produces unidiomatic code. Large language models (LLMs) have demonstrated promise in producing idiomatic translations, but offer no correctness guarantees. We propose SACTOR, an LLM-driven C-to-Rust translation tool that employs a two-step process: an initial "unidiomatic" translation to preserve semantics, followed by an "idiomatic" refinement to align with Rust standards. To validate correctness of our function-wise incremental translation that mixes C and Rust, we use end-to-end testing via the foreign function interface. We evaluate SACTOR on 200 programs from two public datasets and on two more real-world scenarios (a 50-sample subset of CRust-Bench and the libogg library), comparing multiple LLMs. Across datasets, SACTOR delivers high end-to-end correctness and produces safe, idiomatic Rust with up to 7 times fewer Clippy warnings; On CRust-Bench, SACTOR achieves an average (across samples) of 85% unidiomatic and 52% idiomatic success, and on libogg it attains full unidiomatic and up to 78% idiomatic coverage on GPT-5.
BibTeX
@inproceedings{zhou2026sactor,
  author = {Tianyang Zhou and Ziyi Zhang and Haowen Lin and Somesh Jha and Mihai Christodorescu and Kirill Levchenko and Varun Chandrasekaran},
  title = {{SACTOR: LLM-Driven Correct and Idiomatic C to Rust Translation with Static Analysis and FFI-Based Verification}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2026},
  doi = {10.18653/v1/2026.acl-long.28},
}
Figure