Skip to main content

core/panic/
unwind_safe.rs

1use crate::async_iter::AsyncIterator;
2use crate::cell::UnsafeCell;
3use crate::fmt;
4use crate::future::Future;
5use crate::marker::PointeeSized;
6use crate::ops::{Deref, DerefMut};
7use crate::pin::Pin;
8use crate::ptr::{NonNull, Unique};
9use crate::task::{Context, Poll};
10
11/// A marker trait which represents "panic safe" types in Rust.
12///
13/// This trait is implemented by default for many types and behaves similarly in
14/// terms of inference of implementation to the [`Send`] and [`Sync`] traits. The
15/// purpose of this trait is to encode what types are safe to cross a [`catch_unwind`]
16/// boundary with no fear of unwind safety.
17///
18/// [`catch_unwind`]: ../../std/panic/fn.catch_unwind.html
19///
20/// ## What is unwind safety?
21///
22/// In Rust a function can "return" early if it either panics or calls a
23/// function which transitively panics. This sort of control flow is not always
24/// anticipated, and has the possibility of causing subtle bugs through a
25/// combination of two critical components:
26///
27/// 1. A data structure is in a temporarily invalid state when the thread
28///    panics.
29/// 2. This broken invariant is then later observed.
30///
31/// Typically in Rust, it is difficult to perform step (2) because catching a
32/// panic involves either spawning a thread (which in turn makes it difficult
33/// to later witness broken invariants) or using the `catch_unwind` function in this
34/// module. Additionally, even if an invariant is witnessed, it typically isn't a
35/// problem in Rust because there are no uninitialized values (like in C or C++).
36///
37/// It is possible, however, for **logical** invariants to be broken in Rust,
38/// which can end up causing behavioral bugs. Another key aspect of unwind safety
39/// in Rust is that, in the absence of `unsafe` code, a panic cannot lead to
40/// memory unsafety.
41///
42/// That was a bit of a whirlwind tour of unwind safety, but for more information
43/// about unwind safety and how it applies to Rust, see an [associated RFC][rfc].
44///
45/// [rfc]: https://github.com/rust-lang/rfcs/blob/master/text/1236-stabilize-catch-panic.md
46///
47/// ## What is `UnwindSafe`?
48///
49/// Now that we've got an idea of what unwind safety is in Rust, it's also
50/// important to understand what this trait represents. As mentioned above, one
51/// way to witness broken invariants is through the `catch_unwind` function in this
52/// module as it allows catching a panic and then re-using the environment of
53/// the closure.
54///
55/// Simply put, a type `T` implements `UnwindSafe` if it cannot easily allow
56/// witnessing a broken invariant through the use of `catch_unwind` (catching a
57/// panic). This trait is an auto trait, so it is automatically implemented for
58/// many types, and it is also structurally composed (e.g., a struct is unwind
59/// safe if all of its components are unwind safe).
60///
61/// Note, however, that this is not an unsafe trait, so there is not a succinct
62/// contract that this trait is providing. Instead it is intended as more of a
63/// "speed bump" to alert users of `catch_unwind` that broken invariants may be
64/// witnessed and may need to be accounted for.
65///
66/// ## Who implements `UnwindSafe`?
67///
68/// Types such as `&mut T` and `&RefCell<T>` are examples which are **not**
69/// unwind safe. The general idea is that any mutable state which can be shared
70/// across `catch_unwind` is not unwind safe by default. This is because it is very
71/// easy to witness a broken invariant outside of `catch_unwind` as the data is
72/// simply accessed as usual.
73///
74/// Types like `&Mutex<T>`, however, are unwind safe because they implement
75/// poisoning by default. They still allow witnessing a broken invariant, but
76/// they already provide their own "speed bumps" to do so.
77///
78/// ## When should `UnwindSafe` be used?
79///
80/// It is not intended that most types or functions need to worry about this trait.
81/// It is only used as a bound on the `catch_unwind` function and as mentioned
82/// above, the lack of `unsafe` means it is mostly an advisory. The
83/// [`AssertUnwindSafe`] wrapper struct can be used to force this trait to be
84/// implemented for any closed over variables passed to `catch_unwind`.
85#[stable(feature = "catch_unwind", since = "1.9.0")]
86#[rustc_diagnostic_item = "unwind_safe_trait"]
87#[diagnostic::on_unimplemented(
88    message = "the type `{Self}` may not be safely transferred across an unwind boundary",
89    label = "`{Self}` may not be safely transferred across an unwind boundary"
90)]
91pub auto trait UnwindSafe {}
92
93/// A marker trait representing types where a shared reference is considered
94/// unwind safe.
95///
96/// This trait is namely not implemented by [`UnsafeCell`], the root of all
97/// interior mutability.
98///
99/// This is a "helper marker trait" used to provide impl blocks for the
100/// [`UnwindSafe`] trait, for more information see that documentation.
101#[stable(feature = "catch_unwind", since = "1.9.0")]
102#[rustc_diagnostic_item = "ref_unwind_safe_trait"]
103#[diagnostic::on_unimplemented(
104    message = "the type `{Self}` may contain interior mutability and a reference may not be safely \
105               transferable across a catch_unwind boundary",
106    label = "`{Self}` may contain interior mutability and a reference may not be safely \
107             transferable across a catch_unwind boundary"
108)]
109pub auto trait RefUnwindSafe {}
110
111/// A simple wrapper around a type to assert that it is unwind safe.
112///
113/// When using [`catch_unwind`] it may be the case that some of the closed over
114/// variables are not unwind safe. For example if `&mut T` is captured the
115/// compiler will generate a warning indicating that it is not unwind safe. It
116/// might not be the case, however, that this is actually a problem due to the
117/// specific usage of [`catch_unwind`] if unwind safety is specifically taken into
118/// account. This wrapper struct is useful for a quick and lightweight
119/// annotation that a variable is indeed unwind safe.
120///
121/// [`catch_unwind`]: ../../std/panic/fn.catch_unwind.html
122///
123/// # Examples
124///
125/// One way to use `AssertUnwindSafe` is to assert that the entire closure
126/// itself is unwind safe, bypassing all checks for all variables:
127///
128/// ```
129/// use std::panic::{self, AssertUnwindSafe};
130///
131/// let mut variable = 4;
132///
133/// // This code will not compile because the closure captures `&mut variable`
134/// // which is not considered unwind safe by default.
135///
136/// // panic::catch_unwind(|| {
137/// //     variable += 3;
138/// // });
139///
140/// // This, however, will compile due to the `AssertUnwindSafe` wrapper
141/// let result = panic::catch_unwind(AssertUnwindSafe(|| {
142///     variable += 3;
143/// }));
144/// // ...
145/// ```
146///
147/// Wrapping the entire closure amounts to a blanket assertion that all captured
148/// variables are unwind safe. This has the downside that if new captures are
149/// added in the future, they will also be considered unwind safe. Therefore,
150/// you may prefer to just wrap individual captures, as shown below. This is
151/// more annotation, but it ensures that if a new capture is added which is not
152/// unwind safe, you will get a compilation error at that time, which will
153/// allow you to consider whether that new capture in fact represent a bug or
154/// not.
155///
156/// ```
157/// use std::panic::{self, AssertUnwindSafe};
158///
159/// let mut variable = 4;
160/// let other_capture = 3;
161///
162/// let result = {
163///     let mut wrapper = AssertUnwindSafe(&mut variable);
164///     panic::catch_unwind(move || {
165///         **wrapper += other_capture;
166///     })
167/// };
168/// // ...
169/// ```
170#[stable(feature = "catch_unwind", since = "1.9.0")]
171pub struct AssertUnwindSafe<T>(#[stable(feature = "catch_unwind", since = "1.9.0")] pub T);
172
173// Implementations of the `UnwindSafe` trait:
174//
175// * By default everything is unwind safe
176// * pointers T contains mutability of some form are not unwind safe
177// * Unique, an owning pointer, lifts an implementation
178// * Types like Mutex/RwLock which are explicitly poisoned are unwind safe
179// * Our custom AssertUnwindSafe wrapper is indeed unwind safe
180
181#[stable(feature = "catch_unwind", since = "1.9.0")]
182impl<T: PointeeSized> !UnwindSafe for &mut T {}
183#[stable(feature = "catch_unwind", since = "1.9.0")]
184impl<T: RefUnwindSafe + PointeeSized> UnwindSafe for &T {}
185#[stable(feature = "catch_unwind", since = "1.9.0")]
186impl<T: RefUnwindSafe + PointeeSized> UnwindSafe for *const T {}
187#[stable(feature = "catch_unwind", since = "1.9.0")]
188impl<T: RefUnwindSafe + PointeeSized> UnwindSafe for *mut T {}
189#[unstable(feature = "ptr_internals", issue = "none")]
190impl<T: UnwindSafe + PointeeSized> UnwindSafe for Unique<T> {}
191#[stable(feature = "nonnull", since = "1.25.0")]
192impl<T: RefUnwindSafe + PointeeSized> UnwindSafe for NonNull<T> {}
193#[stable(feature = "catch_unwind", since = "1.9.0")]
194impl<T> UnwindSafe for AssertUnwindSafe<T> {}
195
196// Pretty simple implementations for the `RefUnwindSafe` marker trait,
197// basically just saying that `UnsafeCell` is the
198// only thing which doesn't implement it (which then transitively applies to
199// everything else).
200#[stable(feature = "catch_unwind", since = "1.9.0")]
201impl<T: ?Sized> !RefUnwindSafe for UnsafeCell<T> {}
202#[stable(feature = "catch_unwind", since = "1.9.0")]
203impl<T> RefUnwindSafe for AssertUnwindSafe<T> {}
204
205#[cfg(target_has_atomic_load_store = "ptr")]
206#[stable(feature = "unwind_safe_atomic_refs", since = "1.14.0")]
207impl RefUnwindSafe for crate::sync::atomic::AtomicIsize {}
208#[cfg(target_has_atomic_load_store = "8")]
209#[stable(feature = "integer_atomics_stable", since = "1.34.0")]
210impl RefUnwindSafe for crate::sync::atomic::AtomicI8 {}
211#[cfg(target_has_atomic_load_store = "16")]
212#[stable(feature = "integer_atomics_stable", since = "1.34.0")]
213impl RefUnwindSafe for crate::sync::atomic::AtomicI16 {}
214#[cfg(target_has_atomic_load_store = "32")]
215#[stable(feature = "integer_atomics_stable", since = "1.34.0")]
216impl RefUnwindSafe for crate::sync::atomic::AtomicI32 {}
217#[cfg(target_has_atomic_load_store = "64")]
218#[stable(feature = "integer_atomics_stable", since = "1.34.0")]
219impl RefUnwindSafe for crate::sync::atomic::AtomicI64 {}
220#[cfg(target_has_atomic_load_store = "128")]
221#[unstable(feature = "integer_atomics", issue = "99069")]
222impl RefUnwindSafe for crate::sync::atomic::AtomicI128 {}
223
224#[cfg(target_has_atomic_load_store = "ptr")]
225#[stable(feature = "unwind_safe_atomic_refs", since = "1.14.0")]
226impl RefUnwindSafe for crate::sync::atomic::AtomicUsize {}
227#[cfg(target_has_atomic_load_store = "8")]
228#[stable(feature = "integer_atomics_stable", since = "1.34.0")]
229impl RefUnwindSafe for crate::sync::atomic::AtomicU8 {}
230#[cfg(target_has_atomic_load_store = "16")]
231#[stable(feature = "integer_atomics_stable", since = "1.34.0")]
232impl RefUnwindSafe for crate::sync::atomic::AtomicU16 {}
233#[cfg(target_has_atomic_load_store = "32")]
234#[stable(feature = "integer_atomics_stable", since = "1.34.0")]
235impl RefUnwindSafe for crate::sync::atomic::AtomicU32 {}
236#[cfg(target_has_atomic_load_store = "64")]
237#[stable(feature = "integer_atomics_stable", since = "1.34.0")]
238impl RefUnwindSafe for crate::sync::atomic::AtomicU64 {}
239#[cfg(target_has_atomic_load_store = "128")]
240#[unstable(feature = "integer_atomics", issue = "99069")]
241impl RefUnwindSafe for crate::sync::atomic::AtomicU128 {}
242
243#[cfg(target_has_atomic_load_store = "8")]
244#[stable(feature = "unwind_safe_atomic_refs", since = "1.14.0")]
245impl RefUnwindSafe for crate::sync::atomic::AtomicBool {}
246
247#[cfg(target_has_atomic_load_store = "ptr")]
248#[stable(feature = "unwind_safe_atomic_refs", since = "1.14.0")]
249impl<T> RefUnwindSafe for crate::sync::atomic::AtomicPtr<T> {}
250
251#[stable(feature = "catch_unwind", since = "1.9.0")]
252#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
253impl<T> const Deref for AssertUnwindSafe<T> {
254    type Target = T;
255
256    fn deref(&self) -> &T {
257        &self.0
258    }
259}
260
261#[stable(feature = "catch_unwind", since = "1.9.0")]
262#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
263impl<T> const DerefMut for AssertUnwindSafe<T> {
264    fn deref_mut(&mut self) -> &mut T {
265        &mut self.0
266    }
267}
268
269#[stable(feature = "catch_unwind", since = "1.9.0")]
270impl<R, F: FnOnce() -> R> FnOnce<()> for AssertUnwindSafe<F> {
271    type Output = R;
272
273    #[inline]
274    extern "rust-call" fn call_once(self, _args: ()) -> R {
275        (self.0)()
276    }
277}
278
279#[stable(feature = "std_debug", since = "1.16.0")]
280impl<T: fmt::Debug> fmt::Debug for AssertUnwindSafe<T> {
281    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
282        f.debug_tuple("AssertUnwindSafe").field(&self.0).finish()
283    }
284}
285
286#[stable(feature = "assertunwindsafe_default", since = "1.62.0")]
287impl<T: Default> Default for AssertUnwindSafe<T> {
288    fn default() -> Self {
289        Self(Default::default())
290    }
291}
292
293#[stable(feature = "futures_api", since = "1.36.0")]
294impl<F: Future> Future for AssertUnwindSafe<F> {
295    type Output = F::Output;
296
297    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
298        // SAFETY: pin projection. AssertUnwindSafe follows structural pinning.
299        let pinned_field = unsafe { Pin::map_unchecked_mut(self, |x| &mut x.0) };
300        F::poll(pinned_field, cx)
301    }
302}
303
304#[unstable(feature = "async_iterator", issue = "79024")]
305impl<S: AsyncIterator> AsyncIterator for AssertUnwindSafe<S> {
306    type Item = S::Item;
307
308    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<S::Item>> {
309        // SAFETY: pin projection. AssertUnwindSafe follows structural pinning.
310        unsafe { self.map_unchecked_mut(|x| &mut x.0) }.poll_next(cx)
311    }
312
313    fn size_hint(&self) -> (usize, Option<usize>) {
314        self.0.size_hint()
315    }
316}
317
318/// If a value's type is already `UnwindSafe`,
319/// wrapping it in `AssertUnwindSafe` is never incorrect.
320#[stable(feature = "from_wrapper_impls", since = "CURRENT_RUSTC_VERSION")]
321impl<T> From<T> for AssertUnwindSafe<T>
322where
323    T: UnwindSafe,
324{
325    #[inline(always)]
326    fn from(value: T) -> Self {
327        Self(value)
328    }
329}