Skip to content

Commit fa7be1b

Browse files
authored
Add Unalign::update method (#212)
Adds the `Unalign::update` method, which allows updating an `Unalign` in-place via a callback. This works by temporarily moving the `Unalign` into the local stack frame in order to call the callback. Closes #206
1 parent b5b30d0 commit fa7be1b

File tree

1 file changed

+85
-3
lines changed

1 file changed

+85
-3
lines changed

src/lib.rs

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,8 +1142,9 @@ mod simd {
11421142
/// guarantees.
11431143
///
11441144
/// Since `Unalign` has no alignment requirement, the inner `T` may not be
1145-
/// properly aligned in memory. There are four ways to access the inner `T`:
1145+
/// properly aligned in memory. There are five ways to access the inner `T`:
11461146
/// - by value, using [`get`] or [`into_inner`]
1147+
/// - by reference inside of a callback, using [`update`]
11471148
/// - fallibly by reference, using [`try_deref`] or [`try_deref_mut`]; these can
11481149
/// fail if the `Unalign` does not satisfy `T`'s alignment requirement at
11491150
/// runtime
@@ -1156,17 +1157,21 @@ mod simd {
11561157
/// [or ABI]: https://github.com/google/zerocopy/issues/164
11571158
/// [`get`]: Unalign::get
11581159
/// [`into_inner`]: Unalign::into_inner
1160+
/// [`update`]: Unalign::update
11591161
/// [`try_deref`]: Unalign::try_deref
11601162
/// [`try_deref_mut`]: Unalign::try_deref_mut
11611163
/// [`deref_unchecked`]: Unalign::deref_unchecked
11621164
/// [`deref_mut_unchecked`]: Unalign::deref_mut_unchecked
11631165
// NOTE: This type is sound to use with types that need to be dropped. The
11641166
// reason is that the compiler-generated drop code automatically moves all
11651167
// values to aligned memory slots before dropping them in-place. This is not
1166-
// well-documented, but it's hinted at in places like [1] and [2].
1168+
// well-documented, but it's hinted at in places like [1] and [2]. However, this
1169+
// also means that `T` must be `Sized`; unless something changes, we can never
1170+
// support unsized `T`. [3]
11671171
//
11681172
// [1] https://github.com/rust-lang/rust/issues/54148#issuecomment-420529646
11691173
// [2] https://github.com/google/zerocopy/pull/126#discussion_r1018512323
1174+
// [3] https://github.com/google/zerocopy/issues/209
11701175
#[allow(missing_debug_implementations)]
11711176
#[derive(FromZeroes, FromBytes, Unaligned, Default, Copy)]
11721177
#[repr(C, packed)]
@@ -1329,6 +1334,61 @@ impl<T> Unalign<T> {
13291334
pub fn set(&mut self, t: T) {
13301335
*self = Unalign::new(t);
13311336
}
1337+
1338+
/// Updates the inner `T` by calling a function on it.
1339+
///
1340+
/// For large types, this method may be expensive, as it requires copying
1341+
/// `2 * size_of::<T>()` bytes. \[1\]
1342+
///
1343+
/// \[1\] Since the inner `T` may not be aligned, it would not be sound to
1344+
/// invoke `f` on it directly. Instead, `update` moves it into a
1345+
/// properly-aligned location in the local stack frame, calls `f` on it, and
1346+
/// then moves it back to its original location in `self`.
1347+
pub fn update<O, F: FnOnce(&mut T) -> O>(&mut self, f: F) -> O {
1348+
// On drop, this moves `copy` out of itself and uses `ptr::write` to
1349+
// overwrite `slf`.
1350+
struct WriteBackOnDrop<T> {
1351+
copy: ManuallyDrop<T>,
1352+
slf: *mut Unalign<T>,
1353+
}
1354+
1355+
impl<T> Drop for WriteBackOnDrop<T> {
1356+
fn drop(&mut self) {
1357+
// SAFETY: See inline comments.
1358+
unsafe {
1359+
// SAFETY: We never use `copy` again as required by
1360+
// `ManuallyDrop::take`.
1361+
let copy = ManuallyDrop::take(&mut self.copy);
1362+
// SAFETY: `slf` is the raw pointer value of `self`. We know
1363+
// it is valid for writes and properly aligned because
1364+
// `self` is a mutable reference, which guarantees both of
1365+
// these properties.
1366+
ptr::write(self.slf, Unalign::new(copy));
1367+
}
1368+
}
1369+
}
1370+
1371+
// SAFETY: We know that `self` is valid for reads, properly aligned, and
1372+
// points to an initialized `Unalign<T>` because it is a mutable
1373+
// reference, which guarantees all of these properties.
1374+
//
1375+
// Since `T: !Copy`, it would be unsound in the general case to allow
1376+
// both the original `Unalign<T>` and the copy to be used by safe code.
1377+
// We guarantee that the copy is used to overwrite the original in the
1378+
// `Drop::drop` impl of `WriteBackOnDrop`. So long as this `drop` is
1379+
// called before any other safe code executes, soundness is upheld.
1380+
// While this method can terminate in two ways (by returning normally or
1381+
// by unwinding due to a panic in `f`), in both cases, `write_back` is
1382+
// dropped - and its `drop` called - before any other safe code can
1383+
// execute.
1384+
let copy = unsafe { ptr::read(self) }.into_inner();
1385+
let mut write_back = WriteBackOnDrop { copy: ManuallyDrop::new(copy), slf: self };
1386+
1387+
let ret = f(&mut write_back.copy);
1388+
1389+
drop(write_back);
1390+
ret
1391+
}
13321392
}
13331393

13341394
impl<T: Copy> Unalign<T> {
@@ -2949,7 +3009,7 @@ pub use alloc_support::*;
29493009
mod tests {
29503010
#![allow(clippy::unreadable_literal)]
29513011

2952-
use core::ops::Deref;
3012+
use core::{ops::Deref, panic::AssertUnwindSafe};
29533013

29543014
use static_assertions::assert_impl_all;
29553015

@@ -3129,6 +3189,28 @@ mod tests {
31293189
};
31303190
}
31313191

3192+
#[test]
3193+
fn test_unalign_update() {
3194+
let mut u = Unalign::new(AU64(123));
3195+
u.update(|a| a.0 += 1);
3196+
assert_eq!(u.get(), AU64(124));
3197+
3198+
// Test that, even if the callback panics, the original is still
3199+
// correctly overwritten. Use a `Box` so that Miri is more likely to
3200+
// catch any unsoundness (which would likely result in two `Box`es for
3201+
// the same heap object, which is the sort of thing that Miri would
3202+
// probably catch).
3203+
let mut u = Unalign::new(Box::new(AU64(123)));
3204+
let res = std::panic::catch_unwind(AssertUnwindSafe(|| {
3205+
u.update(|a| {
3206+
a.0 += 1;
3207+
panic!();
3208+
})
3209+
}));
3210+
assert!(res.is_err());
3211+
assert_eq!(u.into_inner(), Box::new(AU64(124)));
3212+
}
3213+
31323214
#[test]
31333215
fn test_read_write() {
31343216
const VAL: u64 = 0x12345678;

0 commit comments

Comments
 (0)