Fearless Unsafe. A More User-friendly Document for Unsafe Rust Programming Base on Refined Safety Properties.

Mohan Cui, Penglei Mao, Shuran Sun, Yangfan Zhou, Hui Xu · 2024

Read Fearless Unsafe. A More User-friendly Document for Unsafe Rust Programming Base on Refined Safety Properties. by Mohan Cui, Penglei Mao, Shuran Sun, Yangfan Zhou, Hui Xu (2024) in the Tower of Babel, a free online library of public-domain works preserved in clean, searchable text. No sign-up. The full text is served as a plain-text file at a predictable URL, where text is present: https://antarctic-labs.com/tower-of-babel/arxiv-2412-06251.txt.

Introduction

Mohan Cui and co-authors wrote this software paper, published in two thousand twenty-four, on making unsafe Rust code easier to write correctly. Rust is fast and memory-safe, but its unsafe mode hands control back to the programmer, and the official docs state the safety rules unevenly.

The team distilled twenty-two safety properties from the standard library's own pages. They then tagged all four hundred thirty-eight public unsafe functions in the library and studied how the tags relate. With that scheme they rewrote the unsafe docs and built a helper plugin for a common Rust editor. They also sorted the root causes of all known Rust security flaws up to early two thousand twenty-four by the same properties. Usage counts from the public crate store round out the picture. The goal is guidance a working programmer can actually follow.

Catalog record

CREATORMohan Cui, Penglei Mao, Shuran Sun, Yangfan Zhou, Hui Xu
YEAR2024
COLLECTIONPAPERS
FORMATTXT
FILE SIZE66 KB
VERSION1.0
LICENSEhttp://creativecommons.org/licenses/by/4.0/
RIGHTSCC BY
SOURCEarXiv

Source: https://arxiv.org/abs/2412-06251

Browse the full library · About the Tower of Babel

Full text

The opening of the work, from the library's hosted text:

Fearless Unsafe. A More User-friendly Document for Unsafe Rust Programming Base on Refined Safety Properties. Mohan Cui, Penglei Mao, Shuran Sun, Yangfan Zhou, Hui Xu Abstract . Rust, a popular systems-level programming language, has garnered widespread attention due to its features of achieving run-time efficiency and memory safety. With an increasing number of real-world projects adopting Rust, understanding how to assist programmers in correctly writing unsafe code poses a significant challenge. Based on our observations, the current standard library has many unsafe APIs, but their descriptions are not uniform, complete, and intuitive, especially in describing safety requirements. Therefore, we advocate establishing a systematic category of safety requirements for revising those documents. In this paper, we extended and refined our study in ICSE 2024. We defined a category of Safety Properties (22 items in total) that learned from the documents of unsafe APIs in the standard library. Then, we labeled all public unsafe APIs (438 in total) and analyzed their correlations. Based on the safety properties, we reorganized all the unsafe documents in the standard library and designed a consultation plugin into rust-analyzer as a complementary tool to assist Rust developers in writing unsafe code. To validate the practical significance, we categorized the root causes of all Rust CVEs up to 2024-01-31 (419 in total) into safety properties and further counted the real-world usage of unsafe APIs in the crates.io ecosystem. Conference: arxiv pre-print versions; 2024; CCS: Software and its engineering General programming languages CCS: Software and its engineering Development frameworks and environments 1. Introduction Rust is a popular system programming language designed for achieving memory safety and efficiency ( Matsakis and Klock, 2014 ; Klabnik and Nichols, 2019 ) . This is attributed to extensive static analysis during compilation ( Fulton et al., 2021 ) ; various zero-cost abstractions collectively provide those guarantees ( Zhu et al., 2022 ) , and the typical examples are ownership and generics ( Jung et al., 2017 ) . Limited by using static analysis in the compiler, programmers must invest significant effort in writing code that can satisfy the syntax requirement to pass the compilation, leading to a steep learning curve ( Fulton et al., 2021 ) . However, due to the enticing rewards ( Kolakowski, 2019 ) , Rust has attracted numerous real-world projects to code translations, especially for the system programs ( Shen et al., 2020 ; Levy et al., 2017a ; Levy et al., 2017b ) . Since 2016, Rust has consistently been the most popular programming language in the open-source community ( Overflow, 2016 ; Overflow, 2017 ; Overflow, 2018 ; Overflow, 2019 ; Overflow, 2020 ; Overflow, 2021 ; Overflow, 2022 ) . Source code of ManuallyDrop::take in Rust std. ⬇ 1 // impl<T> ManuallyDrop<T> 2 pub unsafe fn take ( slot : & mut ManuallyDrop < T >) -> T { 3 // SAFETY: we are reading from a reference, which is 4 // guaranteed to be valid for reads. 5 unsafe { ptr :: read (& slot . value ) } 6 } Document of ManuallyDrop::take in Rust 1.75. ⬇ 1 Takes the value from the ManuallyDrop < T > container out . 2 This method is primarily intended for moving out values in drop . Instead of using ManuallyDrop :: drop to manually drop the value , you can use this method to take the value and use it however desired . 3 Whenever possible , it is preferable to use into_inner instead , which prevents duplicating the content of the ManuallyDrop < T >. 4 Safety 5 This function semantically moves out the contained value without preventing further usage , leaving the state of this container unchanged . It is your responsibility to ensure that this ManuallyDrop is not used again . Figure 1 . Example of an unsafe API in Rust std. Listing provides the source code of an unsafe method of struct ManuallyDrop<T>. Listing extracts the document in Rust 1.75. This document introduces the usage, functionality, and safety requirements to comply with by using a section labeled Safety . Table 1 . A list of APIs with the same side effects and their document slices in Rust 1.75. These APIs input mutable pointers and return a typed owner. In previous research, it has been reported that misuse of these APIs can incur double free. The Safety section is highlighted, the description of the safety requirement is underlined, and the side effect is bolded. Implemented Type Unsafe Method Safety Description Slices in API Documents. impl < T: ?Sized > *mut T fn read ( self ) -> T read creates a bitwise copy of T, regardless of whether T is Copy. If T is not Copy, using both the returned value and the value at *src can violate memory safety . Note that assigning to *src counts as a use because it will attempt to drop the value at *src. impl < T > ManuallyDrop < T > fn take ( &mut ManuallyDrop < T >) -> T Safety : This function semantically moves out the contained value without preventing further usage, leaving the state of this container unchanged. It is your responsibility to ensure that this ManuallyDrop is not used again. impl < T: ?Sized > Box < T > fn from_raw ( *mut T ) -> Self This function is unsafe because improper use may lead to memory problems . For example, a double-free may occur if the function is called twice on the same raw pointer. impl < T: ?Sized > Rc < T > fn from_raw ( *const T ) -> Self The raw pointer must have been previously returned by a call to Rc<U>::into_raw where U must have the same size and alignment as T. The user of from_raw has to make sure a specific value of T is only dropped once. impl < T: ?Sized > Arc < T > fn from_raw ( *const T ) -> Self The raw pointer must have been previously returned by a call to Arc<U>::into_raw where U must have the same size and alignment as T. The user of from_raw has to make sure a specific value of T is only dropped once. impl CString fn from_raw ( *mut c_char ) -> Self Safety : This should only ever be called with a pointer that was earlier obtained by calling CString::into_raw. Other usage (e.g., trying to take ownership of a string that was allocated by foreign code ) is likely to lead to undefined behavior or allocator corruption . impl < T > Vec < T > fn from_raw_parts ( *mut T , usize , usize ) -> Self Safety : The ownership of ptr is effectively transferred to the Vec<T> which may then deallocate, reallocate or change the contents of memory pointed to by the pointer at will. Ensure that nothing else uses the pointer after calling this function. impl String fn from_raw_parts ( *mut u8 , usize , usize ) -> Self Safety : The ownership of buf is effectively transferred to the String which may then deallocate, reallocate or change the contents of memory pointed to by the pointer at will. Ensure that nothing else uses the pointer after

Read the complete text