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

标记还是不标记:将C语言的联合体(Union)转换为Rust的标记联合体(Tagged Union)

To Tag, or Not to Tag: Translating C's Unions to Rust's Tagged Unions

Jaemin Hong, Sukyoung Ryu

AI总结:

针对C2Rust翻译生成代码中未解决的联合体不安全问题,提出基于静态分析的替换技术,通过识别标记字段将C联合体转为Rust标记联合体,在真实程序上验证了其精确性、正确性与高效性。

摘要:

自动C到Rust翻译是提升遗留系统软件可靠性的一种很有前景的方法。然而,工业界开发的翻译器C2Rust生成的Rust代码带有不安全特性,这削弱了翻译的目标。虽然研究人员已经提出了多种技术来消除C2Rust生成代码中的不安全特性,但这些工作仅针对有限的不安全特性子集。一个尚未解决的重要不安全特性是联合体(union)——一种由多个共享同一内存存储空间的字段组成的类型。程序员通常会将联合体和一个标记(tag)一同放入结构体中,以记录最后写入的字段,但他们仍可能访问错误的字段。相比之下,Rust的标记联合体在语言层面结合了标记和联合体,确保了值访问的正确性。在本研究中,我们提出了在C到Rust翻译过程中用标记联合体替换联合体的技术。我们开发了一种静态分析方法,通过识别标记字段及对应的标记值来推进这类替换。该分析包含一项计算结构体字段值的必须指向分析(must-points-to analysis),以及一种解释这些结果的启发式方法。为提升效率,我们采用过程内的逐函数分析,支持对函数进行选择性分析。我们在36个真实世界C程序上的评估表明,所提出的方法具备以下特点:(1)精确性:识别出74个标记字段,无误报,仅5个漏报;(2)基本正确性:23个程序中有17个在转换后通过了测试;(3)高效性:能够在4910秒内分析并转换14.1万行代码(LOC)。

英文摘要:

Automatic C-to-Rust translation is a promising way to enhance the reliability of legacy system software. However, C2Rust, an industrially developed translator, generates Rust code with unsafe features, undermining the translation's objective. While researchers have proposed techniques to remove unsafe features in C2Rust-generated code, these efforts have targeted only a limited subset of unsafe features. One important unsafe feature remaining unaddressed is a union, a type consisting of multiple fields sharing the same memory storage. Programmers often place a union with a tag in a struct to record the last-written field, but they can still access wrong fields. In contrast, Rust's tagged unions combine tags and unions at the language level, ensuring correct value access. In this work, we propose techniques to replace unions with tagged unions during C-to-Rust translation. We develop a static analysis that facilitates such replacement by identifying tag fields and the corresponding tag values. The analysis involves a must-points-to analysis computing struct field values and a heuristic interpreting these results. To enhance efficiency, we adopt intraprocedural function-wise analysis, allowing selective analysis of functions. Our evaluation on 36 real-world C programs shows that the proposed approach is (1) precise, identifying 74 tag fields with no false positives and only five false negatives, (2) mostly correct, with 17 out of 23 programs passing tests post-transformation, and (3) efficient, capable of analyzing and transforming 141k LOC in 4,910 seconds.

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

↑