|
| 1 | +use crate::Allowed; |
| 2 | +use core::marker::PhantomData; |
| 3 | +use core::ptr::NonNull; |
| 4 | + |
| 5 | +// How do we simulate accesses to the shared buffer by the kernel? |
| 6 | +// |
| 7 | +// Well, a naive way would be to mutate the `buffer` variable directly. Because |
| 8 | +// Allowed accesses the memory through a *mut pointer, such a test would compile |
| 9 | +// and run fine with the current Rust compiler. As far as I can tell, it would |
| 10 | +// not hit any behavior documented as undefined at |
| 11 | +// https://doc.rust-lang.org/stable/reference/behavior-considered-undefined.html, |
| 12 | +// nor would it cause rustc to generate LLVM bitcode that encounters undefined |
| 13 | +// behavior. |
| 14 | +// |
| 15 | +// However, the naive approach will throw an "undefined behavior" error when run |
| 16 | +// under Miri (e.g. with `cargo miri test`), which uses the stacked borrows |
| 17 | +// model [1]. In particular, accessing the `buffer` variable directly pops the |
| 18 | +// SharedRW off buffer's borrow stack, which prevents Allowed from using its |
| 19 | +// *mut pointer to access `buffer` afterwards. It is likely that Rust will adopt |
| 20 | +// the stacked borrows model as its formal model for borrow validity, and in |
| 21 | +// that case accessing `buffer` in that manner will become undefined behavior. |
| 22 | +// In addition, running these tests under Miri is highly valuable, as this is |
| 23 | +// tricky code to get correct and an unsound API may be hard to fix. |
| 24 | +// |
| 25 | +// Instead, we explicitly refer to buffer through use of a KernelPtr that |
| 26 | +// simulates the pointer that `allow()` would hand to the Tock kernel. As far as |
| 27 | +// the stacked borrows model is concerned, accesses through the KernelPtr |
| 28 | +// variable behave identically to mutations performed by the kernel. This |
| 29 | +// pattern allows us to use `cargo miri test` to not only execute the unit |
| 30 | +// tests, but to test whether Allowed would encounter undefined behavior when |
| 31 | +// interacting with a real Tock kernel. |
| 32 | +// |
| 33 | +// [1] https://plv.mpi-sws.org/rustbelt/stacked-borrows/paper.pdf |
| 34 | +struct KernelPtr<'b, T: Copy + 'b> { |
| 35 | + ptr: NonNull<T>, |
| 36 | + |
| 37 | + // We need to consume the 'b lifetime. This is very similar to Allowed's |
| 38 | + // implementation. |
| 39 | + _phantom: PhantomData<&'b mut T>, |
| 40 | +} |
| 41 | + |
| 42 | +impl<'b, T: Copy + 'b> KernelPtr<'b, T> { |
| 43 | + // The constructor for KernelPtr; simulates allow(). Returns both the |
| 44 | + // Allowed instance the Platform would return and a KernelPtr the test can |
| 45 | + // use to simulate a kernel. |
| 46 | + pub fn allow(buffer: &'b mut T) -> (Allowed<'b, T>, KernelPtr<'b, T>) { |
| 47 | + let ptr = NonNull::new(buffer).unwrap(); |
| 48 | + // Discard buffer *without* creating a reference to it, as would be done |
| 49 | + // if we called drop(). |
| 50 | + let _ = buffer; |
| 51 | + // All 3 preconditions of Allowed::new are satisfied by the fact that |
| 52 | + // `buffer` is directly derived from a &'b mut T. |
| 53 | + let allowed = unsafe { Allowed::new(ptr) }; |
| 54 | + let kernel_ptr = KernelPtr { |
| 55 | + ptr, |
| 56 | + _phantom: PhantomData, |
| 57 | + }; |
| 58 | + (allowed, kernel_ptr) |
| 59 | + } |
| 60 | + |
| 61 | + // Replaces the value in the buffer with a new one. |
| 62 | + pub fn set(&self, value: T) { |
| 63 | + unsafe { |
| 64 | + core::ptr::write(self.ptr.as_ptr(), value); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // Copies the contained value out of the buffer. |
| 69 | + pub fn get(&self) -> T { |
| 70 | + unsafe { core::ptr::read(self.ptr.as_ptr()) } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +#[test] |
| 75 | +fn set() { |
| 76 | + let mut buffer = 1; |
| 77 | + let (allowed, kernel_ptr) = KernelPtr::allow(&mut buffer); |
| 78 | + assert_eq!(kernel_ptr.get(), 1); |
| 79 | + |
| 80 | + // Simulate the kernel replacing the value in buffer. |
| 81 | + kernel_ptr.set(2); |
| 82 | + allowed.set(3); |
| 83 | + assert_eq!(kernel_ptr.get(), 3); |
| 84 | +} |
| 85 | + |
| 86 | +#[test] |
| 87 | +fn replace() { |
| 88 | + let mut buffer = 1; |
| 89 | + let (allowed, kernel_ptr) = KernelPtr::allow(&mut buffer); |
| 90 | + assert_eq!(kernel_ptr.get(), 1); |
| 91 | + |
| 92 | + // Simulate the kernel replacing the value in buffer. |
| 93 | + kernel_ptr.set(2); |
| 94 | + let returned = allowed.replace(3); |
| 95 | + assert_eq!(returned, 2); |
| 96 | + assert_eq!(kernel_ptr.get(), 3); |
| 97 | +} |
| 98 | + |
| 99 | +#[test] |
| 100 | +fn get() { |
| 101 | + let mut buffer = 1; |
| 102 | + let (allowed, kernel_ptr) = KernelPtr::allow(&mut buffer); |
| 103 | + assert_eq!(kernel_ptr.get(), 1); |
| 104 | + |
| 105 | + assert_eq!(allowed.get(), 1); |
| 106 | + assert_eq!(kernel_ptr.get(), 1); |
| 107 | + |
| 108 | + kernel_ptr.set(2); |
| 109 | + assert_eq!(allowed.get(), 2); |
| 110 | + assert_eq!(kernel_ptr.get(), 2); |
| 111 | +} |
| 112 | + |
| 113 | +#[test] |
| 114 | +fn take() { |
| 115 | + let mut buffer = 1; |
| 116 | + let (allowed, kernel_ptr) = KernelPtr::allow(&mut buffer); |
| 117 | + assert_eq!(kernel_ptr.get(), 1); |
| 118 | + |
| 119 | + // Simulate the kernel replacing the value in buffer. |
| 120 | + kernel_ptr.set(2); |
| 121 | + let returned = allowed.take(); |
| 122 | + assert_eq!(returned, 2); |
| 123 | + assert_eq!(kernel_ptr.get(), 0); |
| 124 | +} |
0 commit comments