arXiv:2610.34141cs.SEOct 2026

Manuscript Offprint

arXiv:2610.34141

SpeC2Rust: Specification-Driven Repository-Level C-to-Rust Translation

Junjie Yao, Xiongzheng Guan, Jia Feng, Yujia Chen, Hongliang Tian, Cuiyun Gao

Junjie Yao ∗^{*}

Harbin Institute of Technology

Shenzhen, China

220110716@stu.hit.edu.cn

Xiongzheng Guan ∗^{*}

Harbin Institute of Technology

Shenzhen, China

nancicv@163.com

Jia Feng

Harbin Institute of Technology

Shenzhen, China

jiafeng@stu.hit.edu.cn

Yujia Chen

Harbin Institute of Technology

Shenzhen, China

yujiachen@stu.hit.edu.cn

Hongliang Tian

Ant Group, China

tate.thl@antgroup.com

Cuiyun Gao

Harbin Institute of Technology

Shenzhen, China

gaocuiyun@hit.edu.cn

Abstract—Legacy C codebases remain central to system software and performance-critical infrastructure, while their weak memory-safety guarantees continue to impose substantial security and maintenance risks. Recent C-to-Rust translation systems, including translators based on large language models (LLMs), have made automated migration increasingly feasible. However, LLM-based translation still faces several challenges. Existing methods often preserve the original C skeleton, making it difficult to recover project intent and module functionalities at repository scale. The translated Rust code may still rely on raw pointers and unsafe constructs. Cross-file interfaces and dependency relations are also difficult to maintain consistently across generated Rust modules. Even when initial translation succeeds, the resulting project still requires costly compilation repair and end-to-end validation, while naively accumulated context during this process can degrade repair stability and efficiency.

To address these challenges, we propose SpeC2Rust, a specification-driven pipeline for repository-level C-to-Rust translation. SpeC2Rust consists of three stages: (1) Project-Intent Structuring, which decomposes the C repository into semantically coherent modules and generates persistent repository-level and module-level specification documents; (2) Spec-Guided Generation, which translates each Rust module against these specifications with a live symbol registry to maintain cross-file consistency instead of collapsing the translated system into poorly structured monolithic Rust files; (3) Layered-Feedback Repair, which organizes repair into targeted batches and injects only failure-relevant diagnostics, specification context, and source evidence to support efficient compilation and test-driven correction.

We evaluate SpeC2Rust on eight repository-level C projects. On eight repository-level C projects, SpeC2Rust generates Rust projects that compile and pass the original end-to-end tests. The generated code contains no raw-pointer declarations, raw-pointer dereferences, or unsafe lines. On the projects shared with the compared LLM-based baseline, SpeC2Rust reduces cost by 66.1% and input-token usage by 84.7%.

Index Terms—C-to-Rust translation, large language models, specification-driven translation, program repair, memory safety

I. INTRODUCTION

Modernizing legacy C code is a long-standing problem in software engineering. C provides weak memory-safety guarantees and a limited type system, which lead to high maintenance cost and frequent security vulnerabilities [1]. By contrast, Rust is increasingly regarded as a practical replacement for C because of its ownership and borrowing mechanisms, together with its strong memory-safety guarantees [2].

Rule-based translation tools such as C2Rust [3] can quickly translate C code into Rust. However, prior studies show that eliminating unsafe code while preserving translation quality remains difficult in practice [4], [5]. In parallel, LLMs have demonstrated strong capability in software engineering tasks, especially code understanding, code generation [6], and program repair [7]. Systems such as C2SaferRust [8] and SmartC2Rust [9] use LLMs to generate safer Rust code, but LLM-based translation still faces several major challenges.

Project-level C-to-Rust migration raises four challenges that are not adequately addressed by existing approaches:

  • C Project Intent. LLM-generated Rust code may pass end-to-end tests while still being semantically incomplete or even degenerating into a pseudo implementation. In such cases, the translated result no longer preserves the original algorithmic logic of the C project.

• Memory Safety. Previous approaches often construct a Rust skeleton that closely matches the original C project layout, or directly perform function-level translation over the unsafe Rust generated by C2Rust. As a result, the translated Rust code remains centered around unsafe pointers and blocks, with limited memory-safety guarantees.

• Cross-file Dependencies. Rust projects are organized around modules with explicit dependency relationships. Existing methods sometimes place most or all translated Rust code into a single .rs file. Although this design

∗^{*}Co-first authors.

// Original C decomposition void next() {} void expr(int lev) {} void stmt() {} int main(int argc, char **argv) {}

// Skeleton-preserving Rust output #[no_mangle] pub unsafe extern "C" fn next() {} #[no_mangle] pub unsafe extern "C" fn expr( mut lev: libc::c_longlong) {} #[no_mangle] pub unsafe extern "C" fn stmt() {}

unsafe fn main_0( mut argc: libc::c_longlong, mut argv: *mut *mut libc::c_char, ) -> libc::c_longlong {} pub fn main() {}

// Lexer state remains a global raw pointer #[no_mangle] pub static mut p: *mut libc::c_char = 0 as *const libc::c_char as *mut libc::c_char; #[no_mangle] pub unsafe extern "C" fn next() { loop { tk = *p as libc::c_longlong; p = p.offset(1); ... else if is_alpha(tk) { id.offset(Name) = pp as _; } else if / Conditions / { while / Conditions */ { *data = *p as _; } } } }

#[derive(Clone, Debug, PartialEq, Eq)] pub struct C4 { source: String, position: usize, // Other fields... }

#[derive(Clone, Debug, PartialEq, Eq)] pub struct C4Error { line: usize, message: String, }

impl C4 { pub fn next(&mut self) -> Result<(), C4Error> {} pub fn expr(&mut self, lev:i32) -> Result<(), C4Error> {} pub fn stmt(&mut self) -> Result<(), C4Error> {} fn scan_number(&mut self) -> Result<(), C4Error> {} fn precedence(tok: Token) -> i32 {} // Other func... }

(a) Skeleton-preserving structure

(b) Pointer-centric implementation

(c) SpeC2Rust: Rust-native redesign

Fig. 1: Code comparison from a real C interpreter (c4) in our evaluation dataset. Light red background highlights mark C-centric structure and unsafe carryover in skeleton-preserving translation, while light teal highlights mark the Rust-native abstractions recovered by SpeC2Rust.

avoids dependency management across files, it yields poor code organization and weak maintainability. Moreover, as the size of the C project increases, the translation success rate tends to decrease [10].

• Program Repair. The translated Rust project must be repaired through compilation and testing to ensure behavioral consistency with the original C project. Current methods often feed compiler outputs together with Rust source files back to the LLM, which causes excessive context accumulation and reduces repair effectiveness.

To address these challenges, we present SpeC2Rust, a specification-driven pipeline for repository-level C-to-Rust translation. The main idea of SpeC2Rust is to explicitly externalize repository-level knowledge before code generation, preserve it as reusable specification artifacts, and reinject it on demand during subsequent generation and repair.

During repair, SpeC2Rust organizes compilation diagnostics and test failures into structured repair batches, and uses iterative repair to preserve semantic consistency between the translated Rust project and the original C project. To prevent the repair model from bypassing real failures by directly modifying the tests, dataset test scripts are treated as read-only artifacts throughout repair. To address the four challenges above, SpeC2Rust uses three technical components. First, Project-Intent Structuring partitions the repository into functional modules and generates repository-level and module-level specifications from the recovered project facts. Second, Spec-Guided Generation translates each target module against these specifications and uses a live symbol registry with demand-driven evidence retrieval to maintain cross-file consistency. Third, Layered-Feedback Repair resolves build and test failures through targeted evidence injection, structured repair batching, and read-only test boundaries, thereby improving repair focus and preventing spurious success through test modification.

We evaluate SpeC2Rust on eight GitHub C projects. Across all eight projects, SpeC2Rust produces compilable Rust translations that pass the original end-to-end test scripts and contain no raw-pointer declarations, raw-pointer dereferences, or unsafe lines of code. On the five projects shared with SmartC2Rust, SpeC2Rust reduces monetary cost by 66.1% and input-token usage by 84.7%.

Our contributions are summarized as follows:

• Specification-driven Pipeline for repository-level C-to-Rust translation. We propose SpeC2Rust, a three-stage workflow consisting of Project-Intent Structuring, Spec-Guided Generation, and Layered-Feedback Repair, which externalizes project intent before code generation and reuses it throughout translation and repair.

• Specification-mediated generation and layered repair. SpeC2Rust translates against reusable specification documents rather than raw source fragments alone, and repairs Rust projects through targeted knowledge reinjection with read-only test boundaries, improving safety, cross-file consistency, and repair focus.

• Clearer and safer Rust project reconstruction. By breaking away from rigid C-oriented skeletons, SpeC2Rust produces Rust projects with clearer module organization, more idiomatic abstractions, and higher safety in the final translated repositories.

• Repository-level empirical validation. On eight open-source C projects, SpeC2Rust produces compilable Rust translations that pass the original end-to-end test scripts while eliminating raw-pointer declarations/dereferences and unsafe lines of code. On the five projects shared with SmartC2Rust, it further reduces monetary cost by 66.1% and input-token usage by 84.7%.

References (38)