Skip to content

Commit ad5ee2d

Browse files
committed
miri: Fix unsoundness in doctests for VolatileArrayRef
The tests in question took a mutable reference to the first element of a vector, converted it to a `*mut u8` and constructed a `VolatileArrayRef` from it. However, the pointer only has ownership of the first element of the Vec, not the entire vector (since it originated from a reference to the first element). By using `Vec::as_mut_ptr()` instead, we get a pointer that takes ownership of the entire backing memory, avoiding unsoundness. Signed-off-by: Patrick Roy <[email protected]>
1 parent 146a8ea commit ad5ee2d

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/volatile_memory.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,7 +1155,7 @@ where
11551155
/// # use vm_memory::VolatileArrayRef;
11561156
/// #
11571157
/// let mut v = [0u8; 32];
1158-
/// let v_ref = unsafe { VolatileArrayRef::new(&mut v[0] as *mut u8, v.len()) };
1158+
/// let v_ref = unsafe { VolatileArrayRef::new(v.as_mut_ptr(), v.len()) };
11591159
///
11601160
/// let mut buf = [5u8; 16];
11611161
/// v_ref.copy_to(&mut buf[..]);
@@ -1205,9 +1205,9 @@ where
12051205
/// # use vm_memory::VolatileArrayRef;
12061206
/// #
12071207
/// let mut v = [0u8; 32];
1208-
/// let v_ref = unsafe { VolatileArrayRef::<u8>::new(&mut v[0] as *mut u8, v.len()) };
1208+
/// let v_ref = unsafe { VolatileArrayRef::<u8>::new(v.as_mut_ptr(), v.len()) };
12091209
/// let mut buf = [5u8; 16];
1210-
/// let v_ref2 = unsafe { VolatileArrayRef::<u8>::new(&mut buf[0] as *mut u8, buf.len()) };
1210+
/// let v_ref2 = unsafe { VolatileArrayRef::<u8>::new(buf.as_mut_ptr(), buf.len()) };
12111211
///
12121212
/// v_ref.copy_to_volatile_slice(v_ref2.to_slice());
12131213
/// for &v in &buf[..] {
@@ -1238,7 +1238,7 @@ where
12381238
/// # use vm_memory::VolatileArrayRef;
12391239
/// #
12401240
/// let mut v = [0u8; 32];
1241-
/// let v_ref = unsafe { VolatileArrayRef::<u8>::new(&mut v[0] as *mut u8, v.len()) };
1241+
/// let v_ref = unsafe { VolatileArrayRef::<u8>::new(v.as_mut_ptr(), v.len()) };
12421242
///
12431243
/// let buf = [5u8; 64];
12441244
/// v_ref.copy_from(&buf[..]);

0 commit comments

Comments
 (0)