Rust is a memory-safe programming language that disallows undefined behavior. Its safety guarantees have been extensively examined by the community through empirical studies, which has led to its remarkable success. However, unsafe code remains a critical concern in Rust. By reviewing the safety design of Rust and analyzing real-world Rust projects, this paper establishes a systematic framework for understanding unsafe code and undefined behavior, and summarizes the soundness criteria for Rust code. It further derives actionable guidance for achieving sound encapsulation.
Rust is a memory-safe programming language that prohibits undefined behavior. Its safety guarantees have been verified through extensive empirical research by the community, which is a key reason for its remarkable success. However, unsafe code remains a critical issue in Rust. This paper establishes a systematic framework for understanding unsafe code and undefined behavior by reviewing Rust's safety design and analyzing real-world Rust projects. It summarizes soundness standards for Rust code and further proposes actionable guidance for implementing sound encapsulation.
Limitations of Rust's Safety Promises: Although Rust guarantees that safe code will not lead to undefined behavior, unsafe code may still introduce security risks
Lack of Systematic Framework: Existing research lacks systematic theoretical analysis of the relationship between unsafe code and undefined behavior
Difficulty in Verifying Encapsulation Soundness: Lack of actionable methods to verify the soundness of functions and structures containing unsafe code
Establishment of Main Theorem: Formally proves the relationship between undefined behavior and unsafe code, establishing the core principle that "undefined behavior originates solely from unsafe code and is entirely determined by its safety constraints"
Proposal of Soundness Standards: Establishes soundness judgment criteria for safe and unsafe functions, structures, and modules respectively
Development of Encapsulation Guidance: Derives actionable principles and corollaries for implementing sound encapsulation
Construction of Audit Framework: Proposes a systematic audit method based on Unsafe Propagation Graphs (UPG)
Main Theorem (Theorem 1): For a well-typed Rust program P, undefined behavior occurs only when P contains unsafe code and violates its associated safety constraints:
P ⊢ UB ⇒ (P ∋ UC) ∧ (P ⊬ SC_UC)
where UC denotes unsafe code and SC_UC denotes the safety constraints of unsafe code.
Soundness requirements for structure S = {C, F, M, d}:
Static Methods: All constructors and static methods must satisfy the function encapsulation principle
Dynamic Methods: Consider the impact of destructive methods, ensuring safety constraints are satisfied across all combinations of constructors and methods
Rust Team. Soundness (of code / a library). Rust Unsafe Code Guidelines.
Zihao Rao, et al. Annotating and Auditing the Safety Properties of Unsafe Rust. arXiv preprint arXiv:2504.21312, 2025.
Overall Assessment: This paper makes important theoretical contributions to the safety analysis of Rust unsafe code, establishing a systematic analysis framework. Although experimental validation and tool implementation require further enhancement, its theoretical value and practical potential are noteworthy. This work provides a solid theoretical foundation for Rust safety research and practice.