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.


翻译:Rust语言的流行度持续激增;然而,许多关键代码库仍以C语言编写。因此,将C语言自动转换为Rust成为极具吸引力的解决方案。已有若干研究沿此路径展开,通过运用unsafe等Rust特性处理日益增多的C语言子集。尽管自动化转换前景诱人,但生成依赖unsafe的代码会抵消Rust提供的内存安全保证,从而丧失将现有代码库移植到内存安全语言的主要优势。我们主张采用不同方法:程序员通过迭代修改原始C代码,逐步增强代码结构性,直至其符合安全Rust的编译条件。这意味着可以在现有测试套件和生产环境中,逐步评估重构方案在性能与正确性方面的表现。将结构化C代码编译为安全Rust依赖于以下贡献:基于类型导向的C语言(子集)到安全Rust的转换方法;基于"分裂树"的新型静态分析技术,可利用Rust的切片与分裂操作表达C语言的指针运算;推断可变借用需求的静态分析;以及与Rust非拥有/拥有分配机制兼容的C指针类型编译策略。我们在真实世界的密码学库、二进制解析器/序列化器和文件压缩库上评估该方法,证明通过适度重构原始C代码即可将其转换为Rust,且生成的Rust代码展现出与原始C代码相近的性能特征。在转换过程中,我们还发现并报告了bzip2压缩库及微软FrodoKEM密码原语实现中存在的未定义行为。

0
下载
关闭预览

相关内容

Rust 是一种注重高效、安全、并行的系统程序语言。
【经典书】现代C语言教程,408页pdf手写教你写C代码
专知会员服务
66+阅读 · 2022年8月5日
代码注释最详细的Transformer
专知会员服务
113+阅读 · 2022年6月30日
【干货书】C++实战编程指南,附549页pdf与Slides
专知会员服务
84+阅读 · 2021年4月23日
【2020新书】C语言编程傻瓜式入门,第二版,464页pdf
专知会员服务
66+阅读 · 2020年10月15日
社区分享 | TensorFlow Lite C++ API 开源案例教程
TensorFlow
10+阅读 · 2020年11月7日
82页《现代C++教程》:高速上手C++ 11/14/17/20
专知
21+阅读 · 2020年10月19日
手把手教你用Python实现“坦克大战”,附详细代码!
机器学习算法与Python学习
12+阅读 · 2019年6月8日
百闻不如一码!手把手教你用Python搭一个Transformer
大数据文摘
18+阅读 · 2019年4月22日
福利 | 当Python遇上大数据与机器学习,入门so easy!
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
31+阅读 · 2015年12月31日
国家自然科学基金
1+阅读 · 2015年12月31日
国家自然科学基金
1+阅读 · 2014年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
国家自然科学基金
8+阅读 · 2014年12月31日
Arxiv
0+阅读 · 3月7日
Arxiv
0+阅读 · 2月25日
VIP会员
最新内容
《新兴技术武器化及其对全球风险的影响》
专知会员服务
7+阅读 · 4月29日
《帕兰泰尔平台介绍:信息分析平台》
专知会员服务
17+阅读 · 4月29日
智能体化世界建模:基础、能力、规律及展望
专知会员服务
11+阅读 · 4月28日
美海警海上态势感知无人系统
专知会员服务
6+阅读 · 4月28日
相关基金
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
31+阅读 · 2015年12月31日
国家自然科学基金
1+阅读 · 2015年12月31日
国家自然科学基金
1+阅读 · 2014年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
国家自然科学基金
8+阅读 · 2014年12月31日
Top
微信扫码咨询专知VIP会员