一句话:提出 Stacked Borrows 操作语义,为 Rust 定义别名纪律,并用 Coq 证明其足以支撑内存访问重排优化。
Rust 的类型系统对指针别名施加严格纪律,编译器希望利用这些别名信息重排内存访问以优化程序,但 unsafe 代码可绕过检查破坏该纪律。本文提出 Stacked Borrows:一种针对 Rust 内存访问的操作语义,它定义明确的别名纪律,并将违反者判定为未定义行为,使编译器在优化时无需考虑此类程序。作者用 Coq 机器化证明了该模型排除了足够多的程序,从而支持仅基于过程内推理、围绕未知代码与函数调用重排内存访问的优化;同时将操作模型实现于 Rust 解释器,并运行标准库测试套件的大部分用例,验证模型对真实 unsafe Rust 代码的容许度。
Type systems are useful not just for the safety guarantees they provide, but also for helping compilers generate more efficient code by simplifying important program analyses. In Rust, the type system imposes a strict discipline on pointer aliasing, and it is an express goal of the Rust compiler developers to make use of that alias information for the purpose of program optimizations that reorder memory accesses. The problem is that Rust also supports unsafe code, and programmers can write unsafe code that bypasses the usual compiler checks to violate the aliasing discipline. To strike a balance between optimizations and unsafe code, the language needs to provide a set of rules such that unsafe code authors can be sure, if they are following these rules, that the compiler will preserve the semantics of their code despite all the optimizations it is doing. In this work, we propose Stacked Borrows , an operational semantics for memory accesses in Rust. Stacked Borrows defines an aliasing discipline and declares programs violating it to have undefined behavior , meaning the compiler does not have to consider such programs when performing optimizations. We give formal proofs (mechanized in Coq) showing that this rules out enough programs to enable optimizations that reorder memory accesses around unknown code and function calls, based solely on intraprocedural reasoning. We also implemented this operational model in an interpreter for Rust and ran large parts of the Rust standard library test suite in the interpreter to validate that the model permits enough real-world unsafe Rust code.
@article{paperbot519,
title = {Stacked borrows: an aliasing model for Rust},
author = {Ralf Jung and Hoang-Hai Dang and Jeehoon Kang and Derek Dreyer},
journal = {Proceedings of the ACM on Programming Languages},
volume = {4},
number = {POPL},
year = {2019},
doi = {10.1145/3371109}
}