paperbot · PL 论文追踪

全部论文 待读 收藏

P2 Don’t Write, but Return: Replacing Output Parameters with Algebraic Data Types in C-to-Rust Translation

不要写,而是返回:在 C 到 Rust 翻译中用代数数据类型替换输出参数
PLDI 8(PLDI)2024匹配度 3/10 · 该论文是程序分析与抽象解释在C-to-Rust翻译中的应用,与用户关注的核心方向(静态分析用于ML/Agentic程序、LLM改进静态分析、浮点程序分析或贝叶斯程序分析)没有直接关联;仅在‘静态分析’这一宽泛主题上相关,且不涉及LLM/Agent或浮点/贝叶斯交叉。引用 34 inbox
Jaemin Hong, Sukyoung Ryu
dblp: 2026-09-10crossref: 2026-09-10

AI 速览

一句话:提出基于抽象解释的静态分析技术,在 C 到 Rust 翻译中识别并消除输出参数,改用元组与 Option/Result 等代数数据类型返回。

将遗留 C 系统程序自动翻译为 Rust 可提升可靠性,但现有翻译器未能充分利用 Rust 的代数数据类型,常保留 C 中用于返回多值或表示失败的输出参数(指针型参数),导致代码可读性差且易出错。本文提出在 C-to-Rust 翻译中消除输出参数的技术,分三步:先用现有翻译器将 C 语法翻译为 Rust;再分析 Rust 代码提取输出参数相关信息;最后依据分析结果变换代码。第二步的挑战在于输出参数的识别与分类,为此作者提出基于抽象解释的静态分析,引入抽象读/写集来近似读/写指针集合,并采用写集敏感性与空值敏感性。评估表明该技术可扩展(190k 行代码 213 秒内完成分析与变换)、有效(在 55 个真实 C 程序中检测出 1,670 个输出参数)、且基本正确(26 个程序中有 25 个变换后通过测试套件)。

问题
现有 C-to-Rust 翻译器未充分利用 Rust 的代数数据类型,导致翻译结果保留 C 风格的输出参数,降低可读性并增加出错风险。
方法
先做语法翻译,再对 Rust 代码进行基于抽象解释的静态分析,借助抽象读/写集以及写集敏感性和空值敏感性来识别与分类输出参数,最后据此变换代码。
评估
在 55 个真实 C 程序上检测出 1,670 个输出参数,对 190k 行代码的分析与变换耗时 213 秒,26 个程序中有 25 个在变换后通过原有测试套件。
原文摘要(Abstract)

Translating legacy system programs from C to Rust is a promising way to enhance their reliability. To alleviate the burden of manual translation, automatic C-to-Rust translation is desirable. However, existing translators fail to generate Rust code fully utilizing Rust’s language features, including algebraic data types. In this work, we focus on tuples and Option/Result types, an important subset of algebraic data types. They are used as functions’ return types to represent those returning multiple values and those that may fail. Due to the absence of these types, C programs use output parameters , i.e., pointer-type parameters for producing outputs, to implement such functions. As output parameters make code less readable and more error-prone, their use is discouraged in Rust. To address this problem, this paper presents a technique for removing output parameters during C-to-Rust translation. This involves three steps: (1) syntactically translating C code to Rust using an existing translator; (2) analyzing the Rust code to extract information related to output parameters; and (3) transforming the Rust code using the analysis result. The second step poses several challenges, including the identification and classification of output parameters. To overcome these challenges, we propose a static analysis based on abstract interpretation, complemented by the notion of abstract read/write sets , which approximate the sets of read/written pointers, and two sensitivities: write set sensitivity and nullity sensitivity . Our evaluation shows that the proposed technique is (1) scalable, with the analysis and transformation of 190k LOC within 213 seconds, (2) useful, with the detection of 1,670 output parameters across 55 real-world C programs, and (3) mostly correct, with 25 out of 26 programs passing their test suites after the transformation.

链接与引用

DOI 原文 · PDF(开放获取) · DBLP

BibTeX
@article{HongR24,
  title = {Don’t Write, but Return: Replacing Output Parameters with Algebraic Data Types in C-to-Rust Translation},
  author = {Jaemin Hong and Sukyoung Ryu},
  journal = {Proceedings of the ACM on Programming Languages},
  volume = {8},
  number = {PLDI},
  year = {2024},
  doi = {10.1145/3656406}
}