In Rust, unsafe code is the sole source of potential undefined behaviors. To avoid misuse, Rust developers should clarify the safety properties for each unsafe API. However, the community currently lacks a key standard for safety documentation: existing safety comments in the source code and safety documentation can be ad hoc and incomplete. This paper presents a tag-centric methodology for auditing the consistency and completeness of safety documentation. We first derive a taxonomy of Safety Tags to formalize natural-language requirements. Second, because API soundness frequently relies on struct invariants, we propose a set of empirical rules to systematically audit the structural consistency of safety documentation. We implemented this methodology in safety-tool, a static linter that automatically enforces structural consistency between local safety annotations and callee requirements. Our approach was applied to the Rust standard library, fixing documentation issues on 27 APIs with 61 safety tags and identifying safety tags that are applicable to 96.1% of the public unsafe APIs in libstd. Furthermore, we have formalized the tagging idea through a Rust RFC to the wider community. We believe that the approach establishes a standardized practice of safety documentation and helps significantly reduce safety perils.
翻译:在Rust中,不安全代码是潜在未定义行为的唯一来源。为避免误用,Rust开发者应明确每个不安全API的安全属性。然而,当前社区缺乏安全文档的关键标准:源代码中的现有安全注释及安全文档可能具有临时性和不完整性。本文提出了一种以标签为核心的方法论,用于审计安全文档的一致性与完整性。我们首先推导出安全标签的分类体系以形式化自然语言需求。其次,鉴于API健全性常依赖结构体不变量,我们提出了一套经验规则以系统审计安全文档的结构一致性。我们通过静态检查工具safety-tool实现了该方法论,该工具能自动强化局部安全注释与被调用方需求之间的结构一致性。我们将该方法应用于Rust标准库,修复了涉及61个安全标签的27个API的文档问题,并识别出适用于libstd中96.1%公有不安全API的安全标签。此外,我们通过Rust RFC将标签思想形式化以推广至更广泛的社区。我们认为该方法建立了安全文档的标准化实践,有助于显著降低安全风险。