Skip to content

Commit 4ed5a1a

Browse files
authored
Merge pull request #354 from ehuss/static-mut-alternatives
Add alternatives for static-mut-refs
2 parents d56e0f3 + 2db0946 commit 4ed5a1a

File tree

1 file changed

+302
-0
lines changed

1 file changed

+302
-0
lines changed

src/rust-2024/static-mut-references.md

Lines changed: 302 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,308 @@ In situations where no locally-reasoned abstraction is possible and you are ther
4848
[`addr_of_mut!`]: https://docs.rust-lang.org/core/ptr/macro.addr_of_mut.html
4949
[raw]: ../../reference/expressions/operator-expr.html#raw-borrow-operators
5050

51+
Note that the following examples are just illustrations and are not intended as full-fledged implementations. Do not copy these as-is. There are details for your specific situation that may require alterations to fit your needs. These are intended to help you see different ways to approach your problem.
52+
53+
It is recommended to read the documentation for the specific types in the standard library, the reference on [undefined behavior], the [Rustonomicon], and if you are having questions to reach out on one of the Rust forums such as the [Users Forum].
54+
55+
[undefined behavior]: ../../reference/behavior-considered-undefined.html
56+
[Rustonomicon]: ../../nomicon/index.html
57+
[Users Forum]: https://users.rust-lang.org/
58+
59+
### Don't use globals
60+
61+
This is probably something you already know, but if possible it is best to avoid mutable global state. Of course this can be a little more awkward or difficult at times, particularly if you need to pass a mutable reference around between many functions.
62+
63+
### Atomics
64+
65+
The [atomic types][atomics] provide integers, pointers, and booleans that can be used in a `static` (without `mut`).
66+
67+
```rust,edition2024
68+
# use std::sync::atomic::Ordering;
69+
# use std::sync::atomic::AtomicU64;
70+
71+
// Chnage from this:
72+
// static mut COUNTER: u64 = 0;
73+
// to this:
74+
static COUNTER: AtomicU64 = AtomicU64::new(0);
75+
76+
fn main() {
77+
// Be sure to analyze your use case to determine the correct Ordering to use.
78+
COUNTER.fetch_add(1, Ordering::Relaxed);
79+
}
80+
```
81+
82+
[atomics]: ../../std/sync/atomic/index.html
83+
84+
### Mutex or RwLock
85+
86+
When your type is more complex than an atomic, consider using a [`Mutex`] or [`RwLock`] to ensure proper access to the global value.
87+
88+
```rust,edition2024
89+
# use std::sync::Mutex;
90+
# use std::collections::VecDeque;
91+
92+
// Change from this:
93+
// static mut QUEUE: VecDeque<String> = VecDeque::new();
94+
// to this:
95+
static QUEUE: Mutex<VecDeque<String>> = Mutex::new(VecDeque::new());
96+
97+
fn main() {
98+
QUEUE.lock().unwrap().push_back(String::from("abc"));
99+
let first = QUEUE.lock().unwrap().pop_front();
100+
}
101+
```
102+
103+
[`Mutex`]: ../../std/sync/struct.Mutex.html
104+
[`RwLock`]: ../../std/sync/struct.RwLock.html
105+
106+
### OnceLock or LazyLock
107+
108+
If you are using a `static mut` because you need to do some one-time initialization that can't be `const`, you can instead reach for [`OnceLock`] or [`LazyLock`] instead.
109+
110+
```rust,edition2024
111+
# use std::sync::LazyLock;
112+
#
113+
# struct GlobalState;
114+
#
115+
# impl GlobalState {
116+
# fn new() -> GlobalState {
117+
# GlobalState
118+
# }
119+
# fn example(&self) {}
120+
# }
121+
122+
// Instead of some temporary or uninitialized type like:
123+
// static mut STATE: Option<GlobalState> = None;
124+
// use this instead:
125+
static STATE: LazyLock<GlobalState> = LazyLock::new(|| {
126+
GlobalState::new()
127+
});
128+
129+
fn main() {
130+
STATE.example();
131+
}
132+
```
133+
134+
[`OnceLock`] is similar to [`LazyLock`], but can be used if you need to pass information into the constructor, which can work well with single initialization points (like `main`), or if the inputs are available wherever you access the global.
135+
136+
```rust,edition2024
137+
# use std::sync::OnceLock;
138+
#
139+
# struct GlobalState;
140+
#
141+
# impl GlobalState {
142+
# fn new(verbose: bool) -> GlobalState {
143+
# GlobalState
144+
# }
145+
# fn example(&self) {}
146+
# }
147+
#
148+
# struct Args {
149+
# verbose: bool
150+
# }
151+
# fn parse_arguments() -> Args {
152+
# Args { verbose: true }
153+
# }
154+
155+
static STATE: OnceLock<GlobalState> = OnceLock::new();
156+
157+
fn main() {
158+
let args = parse_arguments();
159+
let state = GlobalState::new(args.verbose);
160+
let _ = STATE.set(state);
161+
// ...
162+
STATE.get().unwrap().example();
163+
}
164+
```
165+
166+
[`OnceLock`]: ../../std/sync/struct.OnceLock.html
167+
[`LazyLock`]: ../../std/sync/struct.LazyLock.html
168+
169+
### `no_std` one-time initialization
170+
171+
This example is similar to [`OnceLock`] in that it provides one-time initialization of a global, but it does not require `std` which is useful in a `no_std` context. Assuming your target supports atomics, then you can use an atomic to check for the initialization of the global. The pattern might look something like this:
172+
173+
```rust,edition2024
174+
# use core::sync::atomic::AtomicUsize;
175+
# use core::sync::atomic::Ordering;
176+
#
177+
# struct Args {
178+
# verbose: bool,
179+
# }
180+
# fn parse_arguments() -> Args {
181+
# Args { verbose: true }
182+
# }
183+
#
184+
# struct GlobalState {
185+
# verbose: bool,
186+
# }
187+
#
188+
# impl GlobalState {
189+
# const fn default() -> GlobalState {
190+
# GlobalState { verbose: false }
191+
# }
192+
# fn new(verbose: bool) -> GlobalState {
193+
# GlobalState { verbose }
194+
# }
195+
# fn example(&self) {}
196+
# }
197+
198+
const UNINITIALIZED: usize = 0;
199+
const INITIALIZING: usize = 1;
200+
const INITIALIZED: usize = 2;
201+
202+
static STATE_INITIALIZED: AtomicUsize = AtomicUsize::new(UNINITIALIZED);
203+
static mut STATE: GlobalState = GlobalState::default();
204+
205+
fn set_global_state(state: GlobalState) {
206+
if STATE_INITIALIZED
207+
.compare_exchange(
208+
UNINITIALIZED,
209+
INITIALIZING,
210+
Ordering::SeqCst,
211+
Ordering::SeqCst,
212+
)
213+
.is_ok()
214+
{
215+
// SAFETY: The reads and writes to STATE are guarded with the INITIALIZED guard.
216+
unsafe {
217+
STATE = state;
218+
}
219+
STATE_INITIALIZED.store(INITIALIZED, Ordering::SeqCst);
220+
} else {
221+
panic!("already initialized, or concurrent initialization");
222+
}
223+
}
224+
225+
fn get_state() -> &'static GlobalState {
226+
if STATE_INITIALIZED.load(Ordering::Acquire) != INITIALIZED {
227+
panic!("not initialized");
228+
} else {
229+
// SAFETY: Mutable access is not possible after state has been initialized.
230+
unsafe { &*&raw const STATE }
231+
}
232+
}
233+
234+
fn main() {
235+
let args = parse_arguments();
236+
let state = GlobalState::new(args.verbose);
237+
set_global_state(state);
238+
// ...
239+
let state = get_state();
240+
state.example();
241+
}
242+
```
243+
244+
This example assumes you can put some default value in the static before it is initialized (the const `default` constructor in this example). If that is not possible, consider using either [`MaybeUninit`], or dynamic trait dispatch (with a dummy type that implements a trait), or some other approach to have a default placeholder.
245+
246+
There are community-provided crates that can provide similar one-time initialization, such as the [`static-cell`] crate (which supports targets that do not have atomics by using [`portable-atomic`]).
247+
248+
[`MaybeUninit`]: ../../core/mem/union.MaybeUninit.html
249+
[`static-cell`]: https://crates.io/crates/static_cell
250+
[`portable-atomic`]: https://crates.io/crates/portable-atomic
251+
252+
### Raw pointers
253+
254+
In some cases you can continue to use `static mut`, but avoid creating references. For example, if you just need to pass [raw pointers] into a C library, don't create an intermediate reference. Instead you can use [raw borrow operators], like in the following example:
255+
256+
```rust,edition2024,no_run
257+
# #[repr(C)]
258+
# struct GlobalState {
259+
# value: i32
260+
# }
261+
#
262+
# impl GlobalState {
263+
# const fn new() -> GlobalState {
264+
# GlobalState { value: 0 }
265+
# }
266+
# }
267+
268+
static mut STATE: GlobalState = GlobalState::new();
269+
270+
unsafe extern "C" {
271+
fn example_ffi(state: *mut GlobalState);
272+
}
273+
274+
fn main() {
275+
unsafe {
276+
// Change from this:
277+
// example_ffi(&mut STATE as *mut GlobalState);
278+
// to this:
279+
example_ffi(&raw mut STATE);
280+
}
281+
}
282+
```
283+
284+
Just beware that you still need to uphold the aliasing constraints around mutable pointers. This may require some internal or external synchronization or proofs about how it is used across threads, interrupt handlers, and reentrancy.
285+
286+
[raw borrow operators]: ../../reference/expressions/operator-expr.html#raw-borrow-operators
287+
[raw pointers]: ../../reference/types/pointer.html#raw-pointers-const-and-mut
288+
289+
### `UnsafeCell` with `Sync`
290+
291+
[`UnsafeCell`] does not impl `Sync`, so it cannot be used in a `static`. You can create your own wrapper around [`UnsafeCell`] to add a `Sync` impl so that it can be used in a `static` to implement interior mutability. This approach can be useful if you have external locks or other guarantees that uphold the safety invariants required for mutable pointers.
292+
293+
Note that this is largely the same as the [raw pointers](#raw-pointers) example. The wrapper helps to emphasize how you are using the type, and focus on which safety requirements you should be careful of. But otherwise they are roughly the same.
294+
295+
```rust,edition2024
296+
# use std::cell::UnsafeCell;
297+
#
298+
# fn with_interrupts_disabled<T: Fn()>(f: T) {
299+
# // A real example would disable interrupts.
300+
# f();
301+
# }
302+
#
303+
# #[repr(C)]
304+
# struct GlobalState {
305+
# value: i32,
306+
# }
307+
#
308+
# impl GlobalState {
309+
# const fn new() -> GlobalState {
310+
# GlobalState { value: 0 }
311+
# }
312+
# }
313+
314+
#[repr(transparent)]
315+
pub struct SyncUnsafeCell<T>(UnsafeCell<T>);
316+
317+
unsafe impl<T: Sync> Sync for SyncUnsafeCell<T> {}
318+
319+
static STATE: SyncUnsafeCell<GlobalState> = SyncUnsafeCell(UnsafeCell::new(GlobalState::new()));
320+
321+
fn set_value(value: i32) {
322+
with_interrupts_disabled(|| {
323+
let state = STATE.0.get();
324+
unsafe {
325+
// SAFETY: This value is only ever read in our interrupt handler,
326+
// and interrupts are disabled, and we only use this in one thread.
327+
(*state).value = value;
328+
}
329+
});
330+
}
331+
```
332+
333+
The standard library has a nightly-only (unstable) variant of [`UnsafeCell`] called [`SyncUnsafeCell`]. This example above shows a very simplified version of the standard library type, but would be used roughly the same way. It can provide even better isolation, so do check out its implementation for more details.
334+
335+
This example includes a fictional `with_interrupts_disabled` function which is the type of thing you might see in an embedded environment. For example, the [`critical-section`] crate provides a similar kind of functionality that could be used for an embedded environment.
336+
337+
[`critical-section`]: https://crates.io/crates/critical-section
338+
[`UnsafeCell`]: ../../std/cell/struct.UnsafeCell.html
339+
[`SyncUnsafeCell`]: ../../std/cell/struct.SyncUnsafeCell.html
340+
341+
### Safe references
342+
343+
In some cases it may be safe to create a reference of a `static mut`. The whole point of the [`static_mut_refs`] lint is that this is very hard to do correctly! However, that's not to say it is *impossible*. If you have a situation where you can guarantee that the aliasing requirements are upheld, such as guaranteeing the static is narrowly scoped (only used in a small module or function), has some internal or external synchronization, accounts for interrupt handlers and reentrancy, panic safety, drop handlers, etc., then taking a reference may be fine.
344+
345+
There are two approaches you can take for this. You can either allow the [`static_mut_refs`] lint (preferably as narrowly as you can), or convert raw pointers to a reference, as with `&mut *&raw mut MY_STATIC`.
346+
347+
<!-- TODO: Should we prefer one or the other here? -->
348+
349+
#### Short-lived references
350+
351+
If you must create a reference to a `static mut`, then it is recommended to minimize the scope of how long that reference exists. Avoid squirreling the reference away somewhere, or keeping it alive through a large section of code. Keeping it short-lived helps with auditing, and verifying that exclusive access is maintained for the duration. Using pointers should be your default unit, and only convert the pointer to a reference on demand when absolutely required.
352+
51353
## Migration
52354

53355
There is no automatic migration to fix these references to `static mut`. To avoid undefined behavior you must rewrite your code to use a different approach as recommended in the [Alternatives](#alternatives) section.

0 commit comments

Comments
 (0)