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