| 1 |
<!-- Copyright 2025 The Fuchsia Authors |
| 2 |
|
| 3 |
Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0 |
| 4 |
<LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT |
| 5 |
license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. |
| 6 |
This file may not be copied, modified, or distributed except according to |
| 7 |
those terms. --> |
| 8 |
|
| 9 |
# Unsafe Code Guidelines |
| 10 |
|
| 11 |
`unsafe` code is extremely dangerous and should be avoided unless absolutely |
| 12 |
necessary. When it is absolutely necessary to write `unsafe` code, it should be |
| 13 |
done extremely carefully. |
| 14 |
|
| 15 |
This document covers guidelines for writing unsafe code, including pointer casts |
| 16 |
and safety comments. |
| 17 |
|
| 18 |
## Pointer Casts |
| 19 |
|
| 20 |
- **Avoid `&slice[0] as *const T`**: Use `slice.as_ptr()`. Accessing subsequent |
| 21 |
elements via pointer arithmetic on a single-element pointer is UB. |
| 22 |
```rust |
| 23 |
let slice = &[1, 2]; |
| 24 |
|
| 25 |
// BAD: Derived from reference to single element. |
| 26 |
let ptr = &slice[0] as *const i32; |
| 27 |
// SAFETY: UB! `ptr` has provenance only for the first element. |
| 28 |
// Accessing `ptr.add(1)` is out of bounds for this provenance. |
| 29 |
let val = unsafe { *ptr.add(1) }; |
| 30 |
|
| 31 |
// GOOD: Derived from the slice itself. |
| 32 |
let ptr = slice.as_ptr(); |
| 33 |
// SAFETY: Safe because `ptr` has provenance for the entire slice. |
| 34 |
let val = unsafe { *ptr.add(1) }; |
| 35 |
``` |
| 36 |
- **Avoid converting `&mut T` to `*const T`**: This reborrows as a shared |
| 37 |
reference, restricting permissions. Cast `&mut T` to `*mut T` first. |
| 38 |
```rust |
| 39 |
let mut val = 42; |
| 40 |
let r = &mut val; |
| 41 |
|
| 42 |
// BAD: `r as *const i32` creates a shared reborrow. |
| 43 |
// The resulting pointer loses write provenance. |
| 44 |
let ptr = r as *const i32 as *mut i32; |
| 45 |
// SAFETY: UB! Writing to a pointer derived from a shared reborrow. |
| 46 |
unsafe { *ptr = 0 }; |
| 47 |
|
| 48 |
// GOOD: `r as *mut i32` preserves write provenance. |
| 49 |
let ptr = r as *mut i32; |
| 50 |
// SAFETY: Safe because `ptr` retains mutable provenance. |
| 51 |
unsafe { *ptr = 0 }; |
| 52 |
``` |
| 53 |
|
| 54 |
## Safety Comments |
| 55 |
|
| 56 |
Every `unsafe` block must be documented with a `// SAFETY:` comment. |
| 57 |
- **Requirement:** The comment must prove soundness using *only* text from the |
| 58 |
stable [Rust Reference](https://doc.rust-lang.org/reference/) or [standard |
| 59 |
library documentation](https://doc.rust-lang.org/std/). |
| 60 |
- **Citation:** You must cite and quote the relevant text from the |
| 61 |
documentation. Citations must cite a specific version of the documentation |
| 62 |
(e.g. https://doc.rust-lang.org/1.91.0/reference/ or |
| 63 |
https://doc.rust-lang.org/1.91.0/std/). |
| 64 |
- **Prohibition:** Do not rely on "common sense" or behavior not guaranteed by |
| 65 |
the docs. |
| 66 |
```rust |
| 67 |
// BAD: Missing justification for "obvious" properties. |
| 68 |
// SAFETY: `ptr` and `field` are from the same object. |
| 69 |
let offset = unsafe { field.cast::<u8>().offset_from(ptr.cast::<u8>()) }; |
| 70 |
|
| 71 |
// GOOD: Explicitly justifies every requirement, even trivial ones. |
| 72 |
// SAFETY: |
| 73 |
// - `ptr` and `field` are derived from the same allocated object [1]. |
| 74 |
// - The distance between them is trivially a multiple of `u8`'s size (1) [2], |
| 75 |
// satisfying `offset_from`'s alignment requirement [1]. |
| 76 |
// |
| 77 |
// [1] Per https://doc.rust-lang.org/1.91.0/std/primitive.pointer.html#method.offset_from: |
| 78 |
// |
| 79 |
// Both pointers must be derived from the same allocated object, and the |
| 80 |
// distance between them must be a multiple of the element size. |
| 81 |
// |
| 82 |
// [2] https://doc.rust-lang.org/1.91.0/reference/type-layout.html#primitive-data-layout |
| 83 |
let offset = unsafe { field.cast::<u8>().offset_from(ptr.cast::<u8>()) }; |
| 84 |
``` |