Forcrat: Automatic I/O API Translation from C to Rust via Origin and Capability Analysis

📅 2025-06-02
📈 Citations: 0
Influential: 0
📄 PDF
🤖 AI Summary
This work addresses the semantic mismatch between C and Rust I/O APIs—arising from differences in stream provenance, capability models, and error-handling mechanisms—in automated C-to-Rust translation. We propose the first systematic, semantics-aware cross-language I/O API replacement method. Our approach integrates three complementary static analyses: origin analysis, capability analysis, and error-source analysis, to construct a type-aware API mapping rule engine. This engine drives safe, semantics-preserving translation via Rust macros and trait-based adapter generation. Evaluated on 32 test-equipped programs, our method achieves 100% regression test pass rate. It processes 422 KLOC in just 14 seconds, with an I/O API call replacement rate of 82%. The results demonstrate substantial improvements in translation correctness and engineering practicality over prior approaches.

Technology Category

Application Category

📝 Abstract
Translating C to Rust is a promising way to enhance the reliability of legacy system programs. Although the industry has developed an automatic C-to-Rust translator, C2Rust, its translation remains unsatisfactory. One major reason is that C2Rust retains C standard library (libc) function calls instead of replacing them with functions from the Rust standard library (Rust std). However, little work has been done on replacing library functions in C2Rust-generated code. In this work, we focus on replacing the I/O API, an important subset of library functions. This poses challenges due to the semantically different designs of I/O APIs in libc and Rust std. First, the two APIs offer different sets of types that represent the origins (e.g., standard input, files) and capabilities (e.g., read, write) of streams used for I/O. Second, they use different error-checking mechanisms: libc uses internal indicators, while Rust std uses return values. To address these challenges, we propose two static analysis techniques, origin and capability analysis and error source analysis, and use their results to replace the I/O API. Our evaluation shows that the proposed approach is (1) correct, with all 32 programs that have test suites passing the tests after transformation, (2) efficient, analyzing and transforming 422k LOC in 14 seconds, and (3) widely applicable, replacing 82% of I/O API calls.
Problem

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

Automatically translate C I/O API calls to Rust equivalents
Address semantic differences in stream types and error handling
Enhance reliability of C-to-Rust translated code via static analysis
Innovation

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

Origin and capability analysis for I/O API
Error source analysis for API translation
Efficient C-to-Rust I/O API replacement
🔎 Similar Papers
No similar papers found.