|
| 1 | +use ref_mut_stack::{ParkableRefMut, RefMutStack}; |
| 2 | + |
| 3 | +// We want to provide a way to the user to use the references in destructors. |
| 4 | +// |
| 5 | +// But because of the existence of such a destructor, the reference needs to be detached from |
| 6 | +// `self` while unparking. This is why it is wrapped with an `Option`. |
| 7 | + |
| 8 | +struct Builder<'a>(Option<ParkableRefMut<'a, usize, Self>>, usize); |
| 9 | + |
| 10 | +impl<'a> Builder<'a> { |
| 11 | + fn build(mut self) -> Option<Self> { |
| 12 | + let r = self.0.take().unwrap(); |
| 13 | + r.unpark() |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +impl<'a> Drop for Builder<'a> { |
| 18 | + fn drop(&mut self) { |
| 19 | + if let Some(r) = self.0.as_mut() { |
| 20 | + **r = self.1 |
| 21 | + }; |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +#[test] |
| 26 | +pub fn test_sound_incomplete_unstacking() { |
| 27 | + let mut root = 12; |
| 28 | + |
| 29 | + { |
| 30 | + let mut stack = RefMutStack::<usize, Builder>::new(&mut root); |
| 31 | + let mut b1 = Builder(Some(stack.borrow_mut()), 100); |
| 32 | + let mut b2 = Builder(Some(b1.0.as_mut().unwrap().parker().park(b1, |r| r)), 200); |
| 33 | + let mut b3 = Builder(Some(b2.0.as_mut().unwrap().parker().park(b2, |r| r)), 300); |
| 34 | + let b4 = Builder(Some(b3.0.as_mut().unwrap().parker().park(b3, |r| r)), 400); |
| 35 | + |
| 36 | + let Some(_b3) = b4.build() else { |
| 37 | + panic!("b4 build should return b3") |
| 38 | + }; |
| 39 | + // `b3`, `b2` and `b1` are not built. |
| 40 | + // |
| 41 | + // The last 2 are still in the stack and need to be dropped in order for the process to be |
| 42 | + // sound. |
| 43 | + // |
| 44 | + // Everything is dropped here. |
| 45 | + } |
| 46 | + |
| 47 | + // If not 100 that means the destructors are not called in the right order, which indicates |
| 48 | + // unsound behaviour (detected by Miri). |
| 49 | + assert_eq!(root, 100); |
| 50 | +} |
| 51 | + |
| 52 | +#[test] |
| 53 | +pub fn test_sound_unwinding() { |
| 54 | + // Need a lock to cross the catch_unwind scope safely |
| 55 | + let root = std::sync::Mutex::new(12); |
| 56 | + |
| 57 | + std::panic::catch_unwind(|| { |
| 58 | + let mut root = root.lock().unwrap(); |
| 59 | + |
| 60 | + let mut stack = RefMutStack::<usize, Builder>::new(&mut root); |
| 61 | + let mut b1 = Builder(Some(stack.borrow_mut()), 100); |
| 62 | + let mut b2 = Builder(Some(b1.0.as_mut().unwrap().parker().park(b1, |r| r)), 200); |
| 63 | + let mut b3 = Builder(Some(b2.0.as_mut().unwrap().parker().park(b2, |r| r)), 300); |
| 64 | + let b4 = Builder(Some(b3.0.as_mut().unwrap().parker().park(b3, |r| r)), 400); |
| 65 | + |
| 66 | + // Drop root to make sure the lock is not poisoned |
| 67 | + drop(b4); |
| 68 | + drop(root); |
| 69 | + |
| 70 | + // 3 builders are still in the stack and need to be dropped in order for the process to be |
| 71 | + // sound. |
| 72 | + |
| 73 | + panic!("intentional panic"); |
| 74 | + }) |
| 75 | + .unwrap_err(); |
| 76 | + |
| 77 | + // If not 100 that means the destructors are not called in the right order, which indicates |
| 78 | + // unsound behaviour (detected by Miri). |
| 79 | + assert_eq!(*root.lock().unwrap(), 100); |
| 80 | +} |
0 commit comments