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

Scylla:将C的适用子集翻译为安全Rust

Scylla: Translating an Applicative Subset of C to Safe Rust

Inria(法国国家信息与自动化研究所);Google(谷歌)

Aymeric Fromherz, Jonathan Protzenko

AI总结:

Scylla通过类型引导翻译将C的子集转换为安全Rust,利用分裂树分析和指针类型编译策略,提升代码安全性并优化性能。

摘要:

Rust语言的流行持续增长;然而,许多关键代码库仍然由C编写。自动将C翻译为Rust是一种有吸引力的选择。一些工作已经沿着这条路前进,通过各种Rust特性处理越来越多的C子集,例如不安全。尽管自动化前景令人向往,但生成依赖于不安全的代码会否定Rust提供的内存安全保证,因此无法充分发挥将现有代码库转换为内存安全语言的优势。我们主张采取不同的方法,程序员在原始C代码上逐步进行重设计和重写,使代码更加结构化,直到其能够编译为安全Rust。这意味着重设计和重写可以逐步针对现有测试套件和生产环境评估性能和正确性。编译结构化的C到安全Rust依赖于以下贡献:一种类型引导的翻译,将(C的一个子集)转换为安全Rust;一种基于“分裂树”的新型静态分析,允许用Rust的切片和分裂操作表达C的指针算术;一种推断需要哪些借入必须可变的分析;以及一种适用于C指针类型的编译策略,与Rust的非拥有和拥有分配区分相兼容。我们对实际的密码学库、二进制解析器和序列器以及文件压缩库进行了评估。我们展示了这些库可以通过对原始C代码进行小幅度重写转换为Rust,并且生成的Rust代码具有与原始C代码相似的性能特征。在翻译过程中,我们还识别并报告了在bzip2压缩库和微软实现的FrodoKEM密码原语中的未定义行为。

英文摘要:

The popularity of the Rust language continues to explode; yet, many critical codebases remain authored in C. Automatically translating C to Rust is thus an appealing course of action. Several works have gone down this path, handling an ever-increasing subset of C through a variety of Rust features, such as unsafe. While the prospect of automation is appealing, producing code that relies on unsafe negates the memory safety guarantees offered by Rust, and therefore the main advantages of porting existing codebases to memory-safe languages. We instead advocate for a different approach, where the programmer iterates on the original C, gradually making the code more structured until it becomes eligible for compilation to safe Rust. This means that redesigns and rewrites can be evaluated incrementally for performance and correctness against existing test suites and production environments. Compiling structured C to safe Rust relies on the following contributions: a type-directed translation from (a subset of) C to safe Rust; a novel static analysis based on "split trees" which allows expressing C's pointer arithmetic using Rust's slices and splitting operations; an analysis that infers which borrows need to be mutable; and a compilation strategy for C pointer types that is compatible with Rust's distinction between non-owned and owned allocations. We evaluate our approach on real-world cryptographic libraries, binary parsers and serializers, and a file compression library. We show that these can be rewritten to Rust with small refactors of the original C code, and that the resulting Rust code exhibits similar performance characteristics as the original C code. As part of our translation process, we also identify and report undefined behaviors in the bzip2 compression library and in Microsoft's implementation of the FrodoKEM cryptographic primitive.

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

↑