Skip to content

Commit 72c63de

Browse files
committed
Auto merge of #84052 - RalfJung:libcore-miri, r=Mark-Simulacrum
fix Miri errors in libcore doctests Now that Miri can run doctests, it found some issues in the libcore doctests: * The `AtomicPtr` tests accessed dangling memory! `AtomicPtr::new(&mut 10);` makes the `10` a temporary that is deallocated after the end of this expression. * The tests for `set_ptr_value` used `&array[0] as *const _` to get a pointer to the array; this needs to be `array.as_ptr()` instead (Cc rust-lang/unsafe-code-guidelines#134). * I reduced a buffer size in a `MaybeUninit` test to make it less slow in Miri, and added a spin loop hint to fix a diverging loop in Miri.
2 parents 4029d4d + b35ac69 commit 72c63de

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-15
lines changed

library/core/src/mem/maybe_uninit.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -736,22 +736,22 @@ impl<T> MaybeUninit<T> {
736736
/// #![feature(maybe_uninit_ref)]
737737
/// use std::mem::MaybeUninit;
738738
///
739-
/// # unsafe extern "C" fn initialize_buffer(buf: *mut [u8; 2048]) { *buf = [0; 2048] }
739+
/// # unsafe extern "C" fn initialize_buffer(buf: *mut [u8; 1024]) { *buf = [0; 1024] }
740740
/// # #[cfg(FALSE)]
741741
/// extern "C" {
742742
/// /// Initializes *all* the bytes of the input buffer.
743-
/// fn initialize_buffer(buf: *mut [u8; 2048]);
743+
/// fn initialize_buffer(buf: *mut [u8; 1024]);
744744
/// }
745745
///
746-
/// let mut buf = MaybeUninit::<[u8; 2048]>::uninit();
746+
/// let mut buf = MaybeUninit::<[u8; 1024]>::uninit();
747747
///
748748
/// // Initialize `buf`:
749749
/// unsafe { initialize_buffer(buf.as_mut_ptr()); }
750750
/// // Now we know that `buf` has been initialized, so we could `.assume_init()` it.
751-
/// // However, using `.assume_init()` may trigger a `memcpy` of the 2048 bytes.
751+
/// // However, using `.assume_init()` may trigger a `memcpy` of the 1024 bytes.
752752
/// // To assert our buffer has been initialized without copying it, we upgrade
753-
/// // the `&mut MaybeUninit<[u8; 2048]>` to a `&mut [u8; 2048]`:
754-
/// let buf: &mut [u8; 2048] = unsafe {
753+
/// // the `&mut MaybeUninit<[u8; 1024]>` to a `&mut [u8; 1024]`:
754+
/// let buf: &mut [u8; 1024] = unsafe {
755755
/// // SAFETY: `buf` has been initialized.
756756
/// buf.assume_init_mut()
757757
/// };

library/core/src/ptr/const_ptr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ impl<T: ?Sized> *const T {
724724
/// #![feature(set_ptr_value)]
725725
/// # use core::fmt::Debug;
726726
/// let arr: [i32; 3] = [1, 2, 3];
727-
/// let mut ptr = &arr[0] as *const dyn Debug;
727+
/// let mut ptr = arr.as_ptr() as *const dyn Debug;
728728
/// let thin = ptr as *const u8;
729729
/// unsafe {
730730
/// ptr = ptr.set_ptr_value(thin.add(8));

library/core/src/ptr/mut_ptr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ impl<T: ?Sized> *mut T {
830830
/// #![feature(set_ptr_value)]
831831
/// # use core::fmt::Debug;
832832
/// let mut arr: [i32; 3] = [1, 2, 3];
833-
/// let mut ptr = &mut arr[0] as *mut dyn Debug;
833+
/// let mut ptr = arr.as_mut_ptr() as *mut dyn Debug;
834834
/// let thin = ptr as *mut u8;
835835
/// unsafe {
836836
/// ptr = ptr.set_ptr_value(thin.add(8));

library/core/src/sync/atomic.rs

+14-7
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
//! ```
7979
//! use std::sync::Arc;
8080
//! use std::sync::atomic::{AtomicUsize, Ordering};
81-
//! use std::thread;
81+
//! use std::{hint, thread};
8282
//!
8383
//! fn main() {
8484
//! let spinlock = Arc::new(AtomicUsize::new(1));
@@ -89,7 +89,9 @@
8989
//! });
9090
//!
9191
//! // Wait for the other thread to release the lock
92-
//! while spinlock.load(Ordering::SeqCst) != 0 {}
92+
//! while spinlock.load(Ordering::SeqCst) != 0 {
93+
//! hint::spin_loop();
94+
//! }
9395
//!
9496
//! if let Err(panic) = thread.join() {
9597
//! println!("Thread had an error: {:?}", panic);
@@ -898,8 +900,10 @@ impl<T> AtomicPtr<T> {
898900
/// ```
899901
/// use std::sync::atomic::{AtomicPtr, Ordering};
900902
///
901-
/// let mut atomic_ptr = AtomicPtr::new(&mut 10);
902-
/// *atomic_ptr.get_mut() = &mut 5;
903+
/// let mut data = 10;
904+
/// let mut atomic_ptr = AtomicPtr::new(&mut data);
905+
/// let mut other_data = 5;
906+
/// *atomic_ptr.get_mut() = &mut other_data;
903907
/// assert_eq!(unsafe { *atomic_ptr.load(Ordering::SeqCst) }, 5);
904908
/// ```
905909
#[inline]
@@ -916,9 +920,11 @@ impl<T> AtomicPtr<T> {
916920
/// #![feature(atomic_from_mut)]
917921
/// use std::sync::atomic::{AtomicPtr, Ordering};
918922
///
919-
/// let mut some_ptr = &mut 123 as *mut i32;
923+
/// let mut data = 123;
924+
/// let mut some_ptr = &mut data as *mut i32;
920925
/// let a = AtomicPtr::from_mut(&mut some_ptr);
921-
/// a.store(&mut 456, Ordering::Relaxed);
926+
/// let mut other_data = 456;
927+
/// a.store(&mut other_data, Ordering::Relaxed);
922928
/// assert_eq!(unsafe { *some_ptr }, 456);
923929
/// ```
924930
#[inline]
@@ -944,7 +950,8 @@ impl<T> AtomicPtr<T> {
944950
/// ```
945951
/// use std::sync::atomic::AtomicPtr;
946952
///
947-
/// let atomic_ptr = AtomicPtr::new(&mut 5);
953+
/// let mut data = 5;
954+
/// let atomic_ptr = AtomicPtr::new(&mut data);
948955
/// assert_eq!(unsafe { *atomic_ptr.into_inner() }, 5);
949956
/// ```
950957
#[inline]

0 commit comments

Comments
 (0)