arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2506.01427cs.SE
收藏

Forcrat:通过起源与能力分析实现从C到Rust的I/O API自动翻译

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

KAIST(韩国科学技术院);Yale University(耶鲁大学)

Jaemin Hong, Sukyoung Ryu

AI总结:

针对现有C到Rust翻译器C2Rust保留libc函数调用的问题,提出起源与能力分析、错误源分析两种静态分析技术,实现I/O API自动替换,在正确性、效率和适用性上表现优异。

摘要:

将C代码翻译为Rust是提升遗留系统程序可靠性的一种极具前景的途径。尽管业界已经开发出了自动C到Rust翻译器C2Rust,但它的翻译效果仍不尽如人意。一个主要原因是C2Rust保留了C标准库(libc)的函数调用,而没有将其替换为Rust标准库(Rust std)中的函数。然而,目前针对C2Rust生成代码中的库函数替换工作的研究还很少。\n在本研究中,我们聚焦于库函数的一个重要子集——I/O API的替换。由于libc和Rust std中的I/O API在设计上存在语义差异,这项工作面临诸多挑战。首先,两类API提供的类型集合不同,这些类型用于表示I/O流的起源(如标准输入、文件)和能力(如读、写)。其次,它们采用不同的错误检查机制:libc使用内部指示器,而Rust std使用返回值。\n为应对这些挑战,我们提出了两种静态分析技术:起源与能力分析以及错误源分析,并利用其分析结果完成I/O API的替换。我们的评估表明,所提出的方法具备以下特性:(1)正确性:所有32个带有测试套件的程序在转换后均通过测试;(2)高效性:在14秒内完成了42.2万行代码的分析与转换;(3)广泛适用性:可替换82%的I/O API调用。

英文摘要:

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.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑