/rust/registry/src/index.crates.io-1949cf8c6b5b557f/zerocopy-0.8.27/src/wrappers.rs
Line | Count | Source |
1 | | // Copyright 2023 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 | | use core::{fmt, hash::Hash}; |
10 | | |
11 | | use super::*; |
12 | | |
13 | | /// A type with no alignment requirement. |
14 | | /// |
15 | | /// An `Unalign` wraps a `T`, removing any alignment requirement. `Unalign<T>` |
16 | | /// has the same size and bit validity as `T`, but not necessarily the same |
17 | | /// alignment [or ABI]. This is useful if a type with an alignment requirement |
18 | | /// needs to be read from a chunk of memory which provides no alignment |
19 | | /// guarantees. |
20 | | /// |
21 | | /// Since `Unalign` has no alignment requirement, the inner `T` may not be |
22 | | /// properly aligned in memory. There are five ways to access the inner `T`: |
23 | | /// - by value, using [`get`] or [`into_inner`] |
24 | | /// - by reference inside of a callback, using [`update`] |
25 | | /// - fallibly by reference, using [`try_deref`] or [`try_deref_mut`]; these can |
26 | | /// fail if the `Unalign` does not satisfy `T`'s alignment requirement at |
27 | | /// runtime |
28 | | /// - unsafely by reference, using [`deref_unchecked`] or |
29 | | /// [`deref_mut_unchecked`]; it is the caller's responsibility to ensure that |
30 | | /// the `Unalign` satisfies `T`'s alignment requirement |
31 | | /// - (where `T: Unaligned`) infallibly by reference, using [`Deref::deref`] or |
32 | | /// [`DerefMut::deref_mut`] |
33 | | /// |
34 | | /// [or ABI]: https://github.com/google/zerocopy/issues/164 |
35 | | /// [`get`]: Unalign::get |
36 | | /// [`into_inner`]: Unalign::into_inner |
37 | | /// [`update`]: Unalign::update |
38 | | /// [`try_deref`]: Unalign::try_deref |
39 | | /// [`try_deref_mut`]: Unalign::try_deref_mut |
40 | | /// [`deref_unchecked`]: Unalign::deref_unchecked |
41 | | /// [`deref_mut_unchecked`]: Unalign::deref_mut_unchecked |
42 | | /// |
43 | | /// # Example |
44 | | /// |
45 | | /// In this example, we need `EthernetFrame` to have no alignment requirement - |
46 | | /// and thus implement [`Unaligned`]. `EtherType` is `#[repr(u16)]` and so |
47 | | /// cannot implement `Unaligned`. We use `Unalign` to relax `EtherType`'s |
48 | | /// alignment requirement so that `EthernetFrame` has no alignment requirement |
49 | | /// and can implement `Unaligned`. |
50 | | /// |
51 | | /// ```rust |
52 | | /// use zerocopy::*; |
53 | | /// # use zerocopy_derive::*; |
54 | | /// # #[derive(FromBytes, KnownLayout, Immutable, Unaligned)] #[repr(C)] struct Mac([u8; 6]); |
55 | | /// |
56 | | /// # #[derive(PartialEq, Copy, Clone, Debug)] |
57 | | /// #[derive(TryFromBytes, KnownLayout, Immutable)] |
58 | | /// #[repr(u16)] |
59 | | /// enum EtherType { |
60 | | /// Ipv4 = 0x0800u16.to_be(), |
61 | | /// Arp = 0x0806u16.to_be(), |
62 | | /// Ipv6 = 0x86DDu16.to_be(), |
63 | | /// # /* |
64 | | /// ... |
65 | | /// # */ |
66 | | /// } |
67 | | /// |
68 | | /// #[derive(TryFromBytes, KnownLayout, Immutable, Unaligned)] |
69 | | /// #[repr(C)] |
70 | | /// struct EthernetFrame { |
71 | | /// src: Mac, |
72 | | /// dst: Mac, |
73 | | /// ethertype: Unalign<EtherType>, |
74 | | /// payload: [u8], |
75 | | /// } |
76 | | /// |
77 | | /// let bytes = &[ |
78 | | /// # 0, 1, 2, 3, 4, 5, |
79 | | /// # 6, 7, 8, 9, 10, 11, |
80 | | /// # /* |
81 | | /// ... |
82 | | /// # */ |
83 | | /// 0x86, 0xDD, // EtherType |
84 | | /// 0xDE, 0xAD, 0xBE, 0xEF // Payload |
85 | | /// ][..]; |
86 | | /// |
87 | | /// // PANICS: Guaranteed not to panic because `bytes` is of the right |
88 | | /// // length, has the right contents, and `EthernetFrame` has no |
89 | | /// // alignment requirement. |
90 | | /// let packet = EthernetFrame::try_ref_from_bytes(&bytes).unwrap(); |
91 | | /// |
92 | | /// assert_eq!(packet.ethertype.get(), EtherType::Ipv6); |
93 | | /// assert_eq!(packet.payload, [0xDE, 0xAD, 0xBE, 0xEF]); |
94 | | /// ``` |
95 | | /// |
96 | | /// # Safety |
97 | | /// |
98 | | /// `Unalign<T>` is guaranteed to have the same size and bit validity as `T`, |
99 | | /// and to have [`UnsafeCell`]s covering the same byte ranges as `T`. |
100 | | /// `Unalign<T>` is guaranteed to have alignment 1. |
101 | | // NOTE: This type is sound to use with types that need to be dropped. The |
102 | | // reason is that the compiler-generated drop code automatically moves all |
103 | | // values to aligned memory slots before dropping them in-place. This is not |
104 | | // well-documented, but it's hinted at in places like [1] and [2]. However, this |
105 | | // also means that `T` must be `Sized`; unless something changes, we can never |
106 | | // support unsized `T`. [3] |
107 | | // |
108 | | // [1] https://github.com/rust-lang/rust/issues/54148#issuecomment-420529646 |
109 | | // [2] https://github.com/google/zerocopy/pull/126#discussion_r1018512323 |
110 | | // [3] https://github.com/google/zerocopy/issues/209 |
111 | | #[allow(missing_debug_implementations)] |
112 | | #[derive(Default, Copy)] |
113 | | #[cfg_attr(any(feature = "derive", test), derive(Immutable, FromBytes, IntoBytes, Unaligned))] |
114 | | #[repr(C, packed)] |
115 | | pub struct Unalign<T>(T); |
116 | | |
117 | | // We do not use `derive(KnownLayout)` on `Unalign`, because the derive is not |
118 | | // smart enough to realize that `Unalign<T>` is always sized and thus emits a |
119 | | // `KnownLayout` impl bounded on `T: KnownLayout.` This is overly restrictive. |
120 | | impl_known_layout!(T => Unalign<T>); |
121 | | |
122 | | // SAFETY: |
123 | | // - `Unalign<T>` promises to have alignment 1, and so we don't require that `T: |
124 | | // Unaligned`. |
125 | | // - `Unalign<T>` has the same bit validity as `T`, and so it is `FromZeros`, |
126 | | // `FromBytes`, or `IntoBytes` exactly when `T` is as well. |
127 | | // - `Immutable`: `Unalign<T>` has the same fields as `T`, so it contains |
128 | | // `UnsafeCell`s exactly when `T` does. |
129 | | // - `TryFromBytes`: `Unalign<T>` has the same the same bit validity as `T`, so |
130 | | // `T::is_bit_valid` is a sound implementation of `is_bit_valid`. |
131 | | #[allow(unused_unsafe)] // Unused when `feature = "derive"`. |
132 | | const _: () = unsafe { |
133 | | impl_or_verify!(T => Unaligned for Unalign<T>); |
134 | | impl_or_verify!(T: Immutable => Immutable for Unalign<T>); |
135 | | impl_or_verify!( |
136 | | T: TryFromBytes => TryFromBytes for Unalign<T>; |
137 | | |c| T::is_bit_valid(c.transmute()) |
138 | | ); |
139 | | impl_or_verify!(T: FromZeros => FromZeros for Unalign<T>); |
140 | | impl_or_verify!(T: FromBytes => FromBytes for Unalign<T>); |
141 | | impl_or_verify!(T: IntoBytes => IntoBytes for Unalign<T>); |
142 | | }; |
143 | | |
144 | | // Note that `Unalign: Clone` only if `T: Copy`. Since the inner `T` may not be |
145 | | // aligned, there's no way to safely call `T::clone`, and so a `T: Clone` bound |
146 | | // is not sufficient to implement `Clone` for `Unalign`. |
147 | | impl<T: Copy> Clone for Unalign<T> { |
148 | | #[inline(always)] |
149 | | fn clone(&self) -> Unalign<T> { |
150 | | *self |
151 | | } |
152 | | } |
153 | | |
154 | | impl<T> Unalign<T> { |
155 | | /// Constructs a new `Unalign`. |
156 | | #[inline(always)] |
157 | | pub const fn new(val: T) -> Unalign<T> { |
158 | | Unalign(val) |
159 | | } |
160 | | |
161 | | /// Consumes `self`, returning the inner `T`. |
162 | | #[inline(always)] |
163 | 0 | pub const fn into_inner(self) -> T { |
164 | | // SAFETY: Since `Unalign` is `#[repr(C, packed)]`, it has the same size |
165 | | // and bit validity as `T`. |
166 | | // |
167 | | // We do this instead of just destructuring in order to prevent |
168 | | // `Unalign`'s `Drop::drop` from being run, since dropping is not |
169 | | // supported in `const fn`s. |
170 | | // |
171 | | // FIXME(https://github.com/rust-lang/rust/issues/73255): Destructure |
172 | | // instead of using unsafe. |
173 | 0 | unsafe { crate::util::transmute_unchecked(self) } |
174 | 0 | } Unexecuted instantiation: <zerocopy::wrappers::Unalign<td_shim_interface::acpi::Ccel>>::into_inner Unexecuted instantiation: <zerocopy::wrappers::Unalign<td_shim::e820::E820Entry>>::into_inner Unexecuted instantiation: <zerocopy::wrappers::Unalign<cc_measurement::CcEventHeader>>::into_inner Unexecuted instantiation: <zerocopy::wrappers::Unalign<cc_measurement::TcgEfiSpecIdevent>>::into_inner Unexecuted instantiation: <zerocopy::wrappers::Unalign<cc_measurement::TcgPcrEventHeader>>::into_inner Unexecuted instantiation: <zerocopy::wrappers::Unalign<migtd::migration::data::ServiceQueryResponse>>::into_inner |
175 | | |
176 | | /// Attempts to return a reference to the wrapped `T`, failing if `self` is |
177 | | /// not properly aligned. |
178 | | /// |
179 | | /// If `self` does not satisfy `align_of::<T>()`, then `try_deref` returns |
180 | | /// `Err`. |
181 | | /// |
182 | | /// If `T: Unaligned`, then `Unalign<T>` implements [`Deref`], and callers |
183 | | /// may prefer [`Deref::deref`], which is infallible. |
184 | | #[inline(always)] |
185 | | pub fn try_deref(&self) -> Result<&T, AlignmentError<&Self, T>> { |
186 | | let inner = Ptr::from_ref(self).transmute(); |
187 | | match inner.try_into_aligned() { |
188 | | Ok(aligned) => Ok(aligned.as_ref()), |
189 | | Err(err) => Err(err.map_src(|src| src.into_unalign().as_ref())), |
190 | | } |
191 | | } |
192 | | |
193 | | /// Attempts to return a mutable reference to the wrapped `T`, failing if |
194 | | /// `self` is not properly aligned. |
195 | | /// |
196 | | /// If `self` does not satisfy `align_of::<T>()`, then `try_deref` returns |
197 | | /// `Err`. |
198 | | /// |
199 | | /// If `T: Unaligned`, then `Unalign<T>` implements [`DerefMut`], and |
200 | | /// callers may prefer [`DerefMut::deref_mut`], which is infallible. |
201 | | #[inline(always)] |
202 | | pub fn try_deref_mut(&mut self) -> Result<&mut T, AlignmentError<&mut Self, T>> { |
203 | | let inner = Ptr::from_mut(self).transmute::<_, _, (_, (_, _))>(); |
204 | | match inner.try_into_aligned() { |
205 | | Ok(aligned) => Ok(aligned.as_mut()), |
206 | | Err(err) => Err(err.map_src(|src| src.into_unalign().as_mut())), |
207 | | } |
208 | | } |
209 | | |
210 | | /// Returns a reference to the wrapped `T` without checking alignment. |
211 | | /// |
212 | | /// If `T: Unaligned`, then `Unalign<T>` implements[ `Deref`], and callers |
213 | | /// may prefer [`Deref::deref`], which is safe. |
214 | | /// |
215 | | /// # Safety |
216 | | /// |
217 | | /// The caller must guarantee that `self` satisfies `align_of::<T>()`. |
218 | | #[inline(always)] |
219 | | pub const unsafe fn deref_unchecked(&self) -> &T { |
220 | | // SAFETY: `Unalign<T>` is `repr(transparent)`, so there is a valid `T` |
221 | | // at the same memory location as `self`. It has no alignment guarantee, |
222 | | // but the caller has promised that `self` is properly aligned, so we |
223 | | // know that it is sound to create a reference to `T` at this memory |
224 | | // location. |
225 | | // |
226 | | // We use `mem::transmute` instead of `&*self.get_ptr()` because |
227 | | // dereferencing pointers is not stable in `const` on our current MSRV |
228 | | // (1.56 as of this writing). |
229 | | unsafe { mem::transmute(self) } |
230 | | } |
231 | | |
232 | | /// Returns a mutable reference to the wrapped `T` without checking |
233 | | /// alignment. |
234 | | /// |
235 | | /// If `T: Unaligned`, then `Unalign<T>` implements[ `DerefMut`], and |
236 | | /// callers may prefer [`DerefMut::deref_mut`], which is safe. |
237 | | /// |
238 | | /// # Safety |
239 | | /// |
240 | | /// The caller must guarantee that `self` satisfies `align_of::<T>()`. |
241 | | #[inline(always)] |
242 | | pub unsafe fn deref_mut_unchecked(&mut self) -> &mut T { |
243 | | // SAFETY: `self.get_mut_ptr()` returns a raw pointer to a valid `T` at |
244 | | // the same memory location as `self`. It has no alignment guarantee, |
245 | | // but the caller has promised that `self` is properly aligned, so we |
246 | | // know that the pointer itself is aligned, and thus that it is sound to |
247 | | // create a reference to a `T` at this memory location. |
248 | | unsafe { &mut *self.get_mut_ptr() } |
249 | | } |
250 | | |
251 | | /// Gets an unaligned raw pointer to the inner `T`. |
252 | | /// |
253 | | /// # Safety |
254 | | /// |
255 | | /// The returned raw pointer is not necessarily aligned to |
256 | | /// `align_of::<T>()`. Most functions which operate on raw pointers require |
257 | | /// those pointers to be aligned, so calling those functions with the result |
258 | | /// of `get_ptr` will result in undefined behavior if alignment is not |
259 | | /// guaranteed using some out-of-band mechanism. In general, the only |
260 | | /// functions which are safe to call with this pointer are those which are |
261 | | /// explicitly documented as being sound to use with an unaligned pointer, |
262 | | /// such as [`read_unaligned`]. |
263 | | /// |
264 | | /// Even if the caller is permitted to mutate `self` (e.g. they have |
265 | | /// ownership or a mutable borrow), it is not guaranteed to be sound to |
266 | | /// write through the returned pointer. If writing is required, prefer |
267 | | /// [`get_mut_ptr`] instead. |
268 | | /// |
269 | | /// [`read_unaligned`]: core::ptr::read_unaligned |
270 | | /// [`get_mut_ptr`]: Unalign::get_mut_ptr |
271 | | #[inline(always)] |
272 | | pub const fn get_ptr(&self) -> *const T { |
273 | | ptr::addr_of!(self.0) |
274 | | } |
275 | | |
276 | | /// Gets an unaligned mutable raw pointer to the inner `T`. |
277 | | /// |
278 | | /// # Safety |
279 | | /// |
280 | | /// The returned raw pointer is not necessarily aligned to |
281 | | /// `align_of::<T>()`. Most functions which operate on raw pointers require |
282 | | /// those pointers to be aligned, so calling those functions with the result |
283 | | /// of `get_ptr` will result in undefined behavior if alignment is not |
284 | | /// guaranteed using some out-of-band mechanism. In general, the only |
285 | | /// functions which are safe to call with this pointer are those which are |
286 | | /// explicitly documented as being sound to use with an unaligned pointer, |
287 | | /// such as [`read_unaligned`]. |
288 | | /// |
289 | | /// [`read_unaligned`]: core::ptr::read_unaligned |
290 | | // FIXME(https://github.com/rust-lang/rust/issues/57349): Make this `const`. |
291 | | #[inline(always)] |
292 | | pub fn get_mut_ptr(&mut self) -> *mut T { |
293 | | ptr::addr_of_mut!(self.0) |
294 | | } |
295 | | |
296 | | /// Sets the inner `T`, dropping the previous value. |
297 | | // FIXME(https://github.com/rust-lang/rust/issues/57349): Make this `const`. |
298 | | #[inline(always)] |
299 | | pub fn set(&mut self, t: T) { |
300 | | *self = Unalign::new(t); |
301 | | } |
302 | | |
303 | | /// Updates the inner `T` by calling a function on it. |
304 | | /// |
305 | | /// If [`T: Unaligned`], then `Unalign<T>` implements [`DerefMut`], and that |
306 | | /// impl should be preferred over this method when performing updates, as it |
307 | | /// will usually be faster and more ergonomic. |
308 | | /// |
309 | | /// For large types, this method may be expensive, as it requires copying |
310 | | /// `2 * size_of::<T>()` bytes. \[1\] |
311 | | /// |
312 | | /// \[1\] Since the inner `T` may not be aligned, it would not be sound to |
313 | | /// invoke `f` on it directly. Instead, `update` moves it into a |
314 | | /// properly-aligned location in the local stack frame, calls `f` on it, and |
315 | | /// then moves it back to its original location in `self`. |
316 | | /// |
317 | | /// [`T: Unaligned`]: Unaligned |
318 | | #[inline] |
319 | | pub fn update<O, F: FnOnce(&mut T) -> O>(&mut self, f: F) -> O { |
320 | | if mem::align_of::<T>() == 1 { |
321 | | // While we advise callers to use `DerefMut` when `T: Unaligned`, |
322 | | // not all callers will be able to guarantee `T: Unaligned` in all |
323 | | // cases. In particular, callers who are themselves providing an API |
324 | | // which is generic over `T` may sometimes be called by *their* |
325 | | // callers with `T` such that `align_of::<T>() == 1`, but cannot |
326 | | // guarantee this in the general case. Thus, this optimization may |
327 | | // sometimes be helpful. |
328 | | |
329 | | // SAFETY: Since `T`'s alignment is 1, `self` satisfies its |
330 | | // alignment by definition. |
331 | | let t = unsafe { self.deref_mut_unchecked() }; |
332 | | return f(t); |
333 | | } |
334 | | |
335 | | // On drop, this moves `copy` out of itself and uses `ptr::write` to |
336 | | // overwrite `slf`. |
337 | | struct WriteBackOnDrop<T> { |
338 | | copy: ManuallyDrop<T>, |
339 | | slf: *mut Unalign<T>, |
340 | | } |
341 | | |
342 | | impl<T> Drop for WriteBackOnDrop<T> { |
343 | | fn drop(&mut self) { |
344 | | // SAFETY: We never use `copy` again as required by |
345 | | // `ManuallyDrop::take`. |
346 | | let copy = unsafe { ManuallyDrop::take(&mut self.copy) }; |
347 | | // SAFETY: `slf` is the raw pointer value of `self`. We know it |
348 | | // is valid for writes and properly aligned because `self` is a |
349 | | // mutable reference, which guarantees both of these properties. |
350 | | unsafe { ptr::write(self.slf, Unalign::new(copy)) }; |
351 | | } |
352 | | } |
353 | | |
354 | | // SAFETY: We know that `self` is valid for reads, properly aligned, and |
355 | | // points to an initialized `Unalign<T>` because it is a mutable |
356 | | // reference, which guarantees all of these properties. |
357 | | // |
358 | | // Since `T: !Copy`, it would be unsound in the general case to allow |
359 | | // both the original `Unalign<T>` and the copy to be used by safe code. |
360 | | // We guarantee that the copy is used to overwrite the original in the |
361 | | // `Drop::drop` impl of `WriteBackOnDrop`. So long as this `drop` is |
362 | | // called before any other safe code executes, soundness is upheld. |
363 | | // While this method can terminate in two ways (by returning normally or |
364 | | // by unwinding due to a panic in `f`), in both cases, `write_back` is |
365 | | // dropped - and its `drop` called - before any other safe code can |
366 | | // execute. |
367 | | let copy = unsafe { ptr::read(self) }.into_inner(); |
368 | | let mut write_back = WriteBackOnDrop { copy: ManuallyDrop::new(copy), slf: self }; |
369 | | |
370 | | let ret = f(&mut write_back.copy); |
371 | | |
372 | | drop(write_back); |
373 | | ret |
374 | | } |
375 | | } |
376 | | |
377 | | impl<T: Copy> Unalign<T> { |
378 | | /// Gets a copy of the inner `T`. |
379 | | // FIXME(https://github.com/rust-lang/rust/issues/57349): Make this `const`. |
380 | | #[inline(always)] |
381 | | pub fn get(&self) -> T { |
382 | | let Unalign(val) = *self; |
383 | | val |
384 | | } |
385 | | } |
386 | | |
387 | | impl<T: Unaligned> Deref for Unalign<T> { |
388 | | type Target = T; |
389 | | |
390 | | #[inline(always)] |
391 | | fn deref(&self) -> &T { |
392 | | Ptr::from_ref(self).transmute().bikeshed_recall_aligned().as_ref() |
393 | | } |
394 | | } |
395 | | |
396 | | impl<T: Unaligned> DerefMut for Unalign<T> { |
397 | | #[inline(always)] |
398 | | fn deref_mut(&mut self) -> &mut T { |
399 | | Ptr::from_mut(self).transmute::<_, _, (_, (_, _))>().bikeshed_recall_aligned().as_mut() |
400 | | } |
401 | | } |
402 | | |
403 | | impl<T: Unaligned + PartialOrd> PartialOrd<Unalign<T>> for Unalign<T> { |
404 | | #[inline(always)] |
405 | | fn partial_cmp(&self, other: &Unalign<T>) -> Option<Ordering> { |
406 | | PartialOrd::partial_cmp(self.deref(), other.deref()) |
407 | | } |
408 | | } |
409 | | |
410 | | impl<T: Unaligned + Ord> Ord for Unalign<T> { |
411 | | #[inline(always)] |
412 | | fn cmp(&self, other: &Unalign<T>) -> Ordering { |
413 | | Ord::cmp(self.deref(), other.deref()) |
414 | | } |
415 | | } |
416 | | |
417 | | impl<T: Unaligned + PartialEq> PartialEq<Unalign<T>> for Unalign<T> { |
418 | | #[inline(always)] |
419 | | fn eq(&self, other: &Unalign<T>) -> bool { |
420 | | PartialEq::eq(self.deref(), other.deref()) |
421 | | } |
422 | | } |
423 | | |
424 | | impl<T: Unaligned + Eq> Eq for Unalign<T> {} |
425 | | |
426 | | impl<T: Unaligned + Hash> Hash for Unalign<T> { |
427 | | #[inline(always)] |
428 | | fn hash<H>(&self, state: &mut H) |
429 | | where |
430 | | H: Hasher, |
431 | | { |
432 | | self.deref().hash(state); |
433 | | } |
434 | | } |
435 | | |
436 | | impl<T: Unaligned + Debug> Debug for Unalign<T> { |
437 | | #[inline(always)] |
438 | | fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { |
439 | | Debug::fmt(self.deref(), f) |
440 | | } |
441 | | } |
442 | | |
443 | | impl<T: Unaligned + Display> Display for Unalign<T> { |
444 | | #[inline(always)] |
445 | | fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { |
446 | | Display::fmt(self.deref(), f) |
447 | | } |
448 | | } |
449 | | |
450 | | /// A wrapper type to construct uninitialized instances of `T`. |
451 | | /// |
452 | | /// `MaybeUninit` is identical to the [standard library |
453 | | /// `MaybeUninit`][core-maybe-uninit] type except that it supports unsized |
454 | | /// types. |
455 | | /// |
456 | | /// # Layout |
457 | | /// |
458 | | /// The same layout guarantees and caveats apply to `MaybeUninit<T>` as apply to |
459 | | /// the [standard library `MaybeUninit`][core-maybe-uninit] with one exception: |
460 | | /// for `T: !Sized`, there is no single value for `T`'s size. Instead, for such |
461 | | /// types, the following are guaranteed: |
462 | | /// - Every [valid size][valid-size] for `T` is a valid size for |
463 | | /// `MaybeUninit<T>` and vice versa |
464 | | /// - Given `t: *const T` and `m: *const MaybeUninit<T>` with identical fat |
465 | | /// pointer metadata, `t` and `m` address the same number of bytes (and |
466 | | /// likewise for `*mut`) |
467 | | /// |
468 | | /// [core-maybe-uninit]: core::mem::MaybeUninit |
469 | | /// [valid-size]: crate::KnownLayout#what-is-a-valid-size |
470 | | #[repr(transparent)] |
471 | | #[doc(hidden)] |
472 | | pub struct MaybeUninit<T: ?Sized + KnownLayout>( |
473 | | // SAFETY: `MaybeUninit<T>` has the same size as `T`, because (by invariant |
474 | | // on `T::MaybeUninit`) `T::MaybeUninit` has `T::LAYOUT` identical to `T`, |
475 | | // and because (invariant on `T::LAYOUT`) we can trust that `LAYOUT` |
476 | | // accurately reflects the layout of `T`. By invariant on `T::MaybeUninit`, |
477 | | // it admits uninitialized bytes in all positions. Because `MaybeUninit` is |
478 | | // marked `repr(transparent)`, these properties additionally hold true for |
479 | | // `Self`. |
480 | | T::MaybeUninit, |
481 | | ); |
482 | | |
483 | | #[doc(hidden)] |
484 | | impl<T: ?Sized + KnownLayout> MaybeUninit<T> { |
485 | | /// Constructs a `MaybeUninit<T>` initialized with the given value. |
486 | | #[inline(always)] |
487 | | pub fn new(val: T) -> Self |
488 | | where |
489 | | T: Sized, |
490 | | Self: Sized, |
491 | | { |
492 | | // SAFETY: It is valid to transmute `val` to `MaybeUninit<T>` because it |
493 | | // is both valid to transmute `val` to `T::MaybeUninit`, and it is valid |
494 | | // to transmute from `T::MaybeUninit` to `MaybeUninit<T>`. |
495 | | // |
496 | | // First, it is valid to transmute `val` to `T::MaybeUninit` because, by |
497 | | // invariant on `T::MaybeUninit`: |
498 | | // - For `T: Sized`, `T` and `T::MaybeUninit` have the same size. |
499 | | // - All byte sequences of the correct size are valid values of |
500 | | // `T::MaybeUninit`. |
501 | | // |
502 | | // Second, it is additionally valid to transmute from `T::MaybeUninit` |
503 | | // to `MaybeUninit<T>`, because `MaybeUninit<T>` is a |
504 | | // `repr(transparent)` wrapper around `T::MaybeUninit`. |
505 | | // |
506 | | // These two transmutes are collapsed into one so we don't need to add a |
507 | | // `T::MaybeUninit: Sized` bound to this function's `where` clause. |
508 | | unsafe { crate::util::transmute_unchecked(val) } |
509 | | } |
510 | | |
511 | | /// Constructs an uninitialized `MaybeUninit<T>`. |
512 | | #[must_use] |
513 | | #[inline(always)] |
514 | | pub fn uninit() -> Self |
515 | | where |
516 | | T: Sized, |
517 | | Self: Sized, |
518 | | { |
519 | | let uninit = CoreMaybeUninit::<T>::uninit(); |
520 | | // SAFETY: It is valid to transmute from `CoreMaybeUninit<T>` to |
521 | | // `MaybeUninit<T>` since they both admit uninitialized bytes in all |
522 | | // positions, and they have the same size (i.e., that of `T`). |
523 | | // |
524 | | // `MaybeUninit<T>` has the same size as `T`, because (by invariant on |
525 | | // `T::MaybeUninit`) `T::MaybeUninit` has `T::LAYOUT` identical to `T`, |
526 | | // and because (invariant on `T::LAYOUT`) we can trust that `LAYOUT` |
527 | | // accurately reflects the layout of `T`. |
528 | | // |
529 | | // `CoreMaybeUninit<T>` has the same size as `T` [1] and admits |
530 | | // uninitialized bytes in all positions. |
531 | | // |
532 | | // [1] Per https://doc.rust-lang.org/1.81.0/std/mem/union.MaybeUninit.html#layout-1: |
533 | | // |
534 | | // `MaybeUninit<T>` is guaranteed to have the same size, alignment, |
535 | | // and ABI as `T` |
536 | | unsafe { crate::util::transmute_unchecked(uninit) } |
537 | | } |
538 | | |
539 | | /// Creates a `Box<MaybeUninit<T>>`. |
540 | | /// |
541 | | /// This function is useful for allocating large, uninit values on the heap |
542 | | /// without ever creating a temporary instance of `Self` on the stack. |
543 | | /// |
544 | | /// # Errors |
545 | | /// |
546 | | /// Returns an error on allocation failure. Allocation failure is guaranteed |
547 | | /// never to cause a panic or an abort. |
548 | | #[cfg(feature = "alloc")] |
549 | | #[inline] |
550 | | pub fn new_boxed_uninit(meta: T::PointerMetadata) -> Result<Box<Self>, AllocError> { |
551 | | // SAFETY: `alloc::alloc::alloc_zeroed` is a valid argument of |
552 | | // `new_box`. The referent of the pointer returned by `alloc` (and, |
553 | | // consequently, the `Box` derived from it) is a valid instance of |
554 | | // `Self`, because `Self` is `MaybeUninit` and thus admits arbitrary |
555 | | // (un)initialized bytes. |
556 | | unsafe { crate::util::new_box(meta, alloc::alloc::alloc) } |
557 | | } |
558 | | |
559 | | /// Extracts the value from the `MaybeUninit<T>` container. |
560 | | /// |
561 | | /// # Safety |
562 | | /// |
563 | | /// The caller must ensure that `self` is in an bit-valid state. Depending |
564 | | /// on subsequent use, it may also need to be in a library-valid state. |
565 | | #[inline(always)] |
566 | | pub unsafe fn assume_init(self) -> T |
567 | | where |
568 | | T: Sized, |
569 | | Self: Sized, |
570 | | { |
571 | | // SAFETY: The caller guarantees that `self` is in an bit-valid state. |
572 | | unsafe { crate::util::transmute_unchecked(self) } |
573 | | } |
574 | | } |
575 | | |
576 | | impl<T: ?Sized + KnownLayout> fmt::Debug for MaybeUninit<T> { |
577 | | #[inline] |
578 | | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
579 | | f.pad(core::any::type_name::<Self>()) |
580 | | } |
581 | | } |
582 | | |
583 | | #[cfg(test)] |
584 | | mod tests { |
585 | | use core::panic::AssertUnwindSafe; |
586 | | |
587 | | use super::*; |
588 | | use crate::util::testutil::*; |
589 | | |
590 | | #[test] |
591 | | fn test_unalign() { |
592 | | // Test methods that don't depend on alignment. |
593 | | let mut u = Unalign::new(AU64(123)); |
594 | | assert_eq!(u.get(), AU64(123)); |
595 | | assert_eq!(u.into_inner(), AU64(123)); |
596 | | assert_eq!(u.get_ptr(), <*const _>::cast::<AU64>(&u)); |
597 | | assert_eq!(u.get_mut_ptr(), <*mut _>::cast::<AU64>(&mut u)); |
598 | | u.set(AU64(321)); |
599 | | assert_eq!(u.get(), AU64(321)); |
600 | | |
601 | | // Test methods that depend on alignment (when alignment is satisfied). |
602 | | let mut u: Align<_, AU64> = Align::new(Unalign::new(AU64(123))); |
603 | | assert_eq!(u.t.try_deref().unwrap(), &AU64(123)); |
604 | | assert_eq!(u.t.try_deref_mut().unwrap(), &mut AU64(123)); |
605 | | // SAFETY: The `Align<_, AU64>` guarantees proper alignment. |
606 | | assert_eq!(unsafe { u.t.deref_unchecked() }, &AU64(123)); |
607 | | // SAFETY: The `Align<_, AU64>` guarantees proper alignment. |
608 | | assert_eq!(unsafe { u.t.deref_mut_unchecked() }, &mut AU64(123)); |
609 | | *u.t.try_deref_mut().unwrap() = AU64(321); |
610 | | assert_eq!(u.t.get(), AU64(321)); |
611 | | |
612 | | // Test methods that depend on alignment (when alignment is not |
613 | | // satisfied). |
614 | | let mut u: ForceUnalign<_, AU64> = ForceUnalign::new(Unalign::new(AU64(123))); |
615 | | assert!(matches!(u.t.try_deref(), Err(AlignmentError { .. }))); |
616 | | assert!(matches!(u.t.try_deref_mut(), Err(AlignmentError { .. }))); |
617 | | |
618 | | // Test methods that depend on `T: Unaligned`. |
619 | | let mut u = Unalign::new(123u8); |
620 | | assert_eq!(u.try_deref(), Ok(&123)); |
621 | | assert_eq!(u.try_deref_mut(), Ok(&mut 123)); |
622 | | assert_eq!(u.deref(), &123); |
623 | | assert_eq!(u.deref_mut(), &mut 123); |
624 | | *u = 21; |
625 | | assert_eq!(u.get(), 21); |
626 | | |
627 | | // Test that some `Unalign` functions and methods are `const`. |
628 | | const _UNALIGN: Unalign<u64> = Unalign::new(0); |
629 | | const _UNALIGN_PTR: *const u64 = _UNALIGN.get_ptr(); |
630 | | const _U64: u64 = _UNALIGN.into_inner(); |
631 | | // Make sure all code is considered "used". |
632 | | // |
633 | | // FIXME(https://github.com/rust-lang/rust/issues/104084): Remove this |
634 | | // attribute. |
635 | | #[allow(dead_code)] |
636 | | const _: () = { |
637 | | let x: Align<_, AU64> = Align::new(Unalign::new(AU64(123))); |
638 | | // Make sure that `deref_unchecked` is `const`. |
639 | | // |
640 | | // SAFETY: The `Align<_, AU64>` guarantees proper alignment. |
641 | | let au64 = unsafe { x.t.deref_unchecked() }; |
642 | | match au64 { |
643 | | AU64(123) => {} |
644 | | _ => const_unreachable!(), |
645 | | } |
646 | | }; |
647 | | } |
648 | | |
649 | | #[test] |
650 | | fn test_unalign_update() { |
651 | | let mut u = Unalign::new(AU64(123)); |
652 | | u.update(|a| a.0 += 1); |
653 | | assert_eq!(u.get(), AU64(124)); |
654 | | |
655 | | // Test that, even if the callback panics, the original is still |
656 | | // correctly overwritten. Use a `Box` so that Miri is more likely to |
657 | | // catch any unsoundness (which would likely result in two `Box`es for |
658 | | // the same heap object, which is the sort of thing that Miri would |
659 | | // probably catch). |
660 | | let mut u = Unalign::new(Box::new(AU64(123))); |
661 | | let res = std::panic::catch_unwind(AssertUnwindSafe(|| { |
662 | | u.update(|a| { |
663 | | a.0 += 1; |
664 | | panic!(); |
665 | | }) |
666 | | })); |
667 | | assert!(res.is_err()); |
668 | | assert_eq!(u.into_inner(), Box::new(AU64(124))); |
669 | | |
670 | | // Test the align_of::<T>() == 1 optimization. |
671 | | let mut u = Unalign::new([0u8, 1]); |
672 | | u.update(|a| a[0] += 1); |
673 | | assert_eq!(u.get(), [1u8, 1]); |
674 | | } |
675 | | |
676 | | #[test] |
677 | | fn test_unalign_copy_clone() { |
678 | | // Test that `Copy` and `Clone` do not cause soundness issues. This test |
679 | | // is mainly meant to exercise UB that would be caught by Miri. |
680 | | |
681 | | // `u.t` is definitely not validly-aligned for `AU64`'s alignment of 8. |
682 | | let u = ForceUnalign::<_, AU64>::new(Unalign::new(AU64(123))); |
683 | | #[allow(clippy::clone_on_copy)] |
684 | | let v = u.t.clone(); |
685 | | let w = u.t; |
686 | | assert_eq!(u.t.get(), v.get()); |
687 | | assert_eq!(u.t.get(), w.get()); |
688 | | assert_eq!(v.get(), w.get()); |
689 | | } |
690 | | |
691 | | #[test] |
692 | | fn test_unalign_trait_impls() { |
693 | | let zero = Unalign::new(0u8); |
694 | | let one = Unalign::new(1u8); |
695 | | |
696 | | assert!(zero < one); |
697 | | assert_eq!(PartialOrd::partial_cmp(&zero, &one), Some(Ordering::Less)); |
698 | | assert_eq!(Ord::cmp(&zero, &one), Ordering::Less); |
699 | | |
700 | | assert_ne!(zero, one); |
701 | | assert_eq!(zero, zero); |
702 | | assert!(!PartialEq::eq(&zero, &one)); |
703 | | assert!(PartialEq::eq(&zero, &zero)); |
704 | | |
705 | | fn hash<T: Hash>(t: &T) -> u64 { |
706 | | let mut h = std::collections::hash_map::DefaultHasher::new(); |
707 | | t.hash(&mut h); |
708 | | h.finish() |
709 | | } |
710 | | |
711 | | assert_eq!(hash(&zero), hash(&0u8)); |
712 | | assert_eq!(hash(&one), hash(&1u8)); |
713 | | |
714 | | assert_eq!(format!("{:?}", zero), format!("{:?}", 0u8)); |
715 | | assert_eq!(format!("{:?}", one), format!("{:?}", 1u8)); |
716 | | assert_eq!(format!("{}", zero), format!("{}", 0u8)); |
717 | | assert_eq!(format!("{}", one), format!("{}", 1u8)); |
718 | | } |
719 | | |
720 | | #[test] |
721 | | #[allow(clippy::as_conversions)] |
722 | | fn test_maybe_uninit() { |
723 | | // int |
724 | | { |
725 | | let input = 42; |
726 | | let uninit = MaybeUninit::new(input); |
727 | | // SAFETY: `uninit` is in an initialized state |
728 | | let output = unsafe { uninit.assume_init() }; |
729 | | assert_eq!(input, output); |
730 | | } |
731 | | |
732 | | // thin ref |
733 | | { |
734 | | let input = 42; |
735 | | let uninit = MaybeUninit::new(&input); |
736 | | // SAFETY: `uninit` is in an initialized state |
737 | | let output = unsafe { uninit.assume_init() }; |
738 | | assert_eq!(&input as *const _, output as *const _); |
739 | | assert_eq!(input, *output); |
740 | | } |
741 | | |
742 | | // wide ref |
743 | | { |
744 | | let input = [1, 2, 3, 4]; |
745 | | let uninit = MaybeUninit::new(&input[..]); |
746 | | // SAFETY: `uninit` is in an initialized state |
747 | | let output = unsafe { uninit.assume_init() }; |
748 | | assert_eq!(&input[..] as *const _, output as *const _); |
749 | | assert_eq!(input, *output); |
750 | | } |
751 | | } |
752 | | } |