Skip to content

Commit 5703b7a

Browse files
committed
Auto merge of #69208 - RalfJung:debug-assert, r=Mark-Simulacrum
debug_assert a few more raw pointer methods Makes progress for #53871
2 parents 0eb878d + 8d3b306 commit 5703b7a

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/libcore/intrinsics.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1423,13 +1423,15 @@ pub(crate) fn is_aligned_and_not_null<T>(ptr: *const T) -> bool {
14231423
}
14241424

14251425
/// Checks whether the regions of memory starting at `src` and `dst` of size
1426-
/// `count * size_of::<T>()` overlap.
1427-
fn overlaps<T>(src: *const T, dst: *const T, count: usize) -> bool {
1426+
/// `count * size_of::<T>()` do *not* overlap.
1427+
pub(crate) fn is_nonoverlapping<T>(src: *const T, dst: *const T, count: usize) -> bool {
14281428
let src_usize = src as usize;
14291429
let dst_usize = dst as usize;
14301430
let size = mem::size_of::<T>().checked_mul(count).unwrap();
14311431
let diff = if src_usize > dst_usize { src_usize - dst_usize } else { dst_usize - src_usize };
1432-
size > diff
1432+
// If the absolute distance between the ptrs is at least as big as the size of the buffer,
1433+
// they do not overlap.
1434+
diff >= size
14331435
}
14341436

14351437
/// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
@@ -1525,7 +1527,7 @@ pub unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize) {
15251527

15261528
debug_assert!(is_aligned_and_not_null(src), "attempt to copy from unaligned or null pointer");
15271529
debug_assert!(is_aligned_and_not_null(dst), "attempt to copy to unaligned or null pointer");
1528-
debug_assert!(!overlaps(src, dst, count), "attempt to copy to overlapping memory");
1530+
debug_assert!(is_nonoverlapping(src, dst, count), "attempt to copy to overlapping memory");
15291531
copy_nonoverlapping(src, dst, count)
15301532
}
15311533

src/libcore/ptr/mod.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
use crate::cmp::Ordering;
7373
use crate::fmt;
7474
use crate::hash;
75-
use crate::intrinsics;
75+
use crate::intrinsics::{self, is_aligned_and_not_null, is_nonoverlapping};
7676
use crate::mem::{self, MaybeUninit};
7777

7878
#[stable(feature = "rust1", since = "1.0.0")]
@@ -392,6 +392,10 @@ pub unsafe fn swap<T>(x: *mut T, y: *mut T) {
392392
#[inline]
393393
#[stable(feature = "swap_nonoverlapping", since = "1.27.0")]
394394
pub unsafe fn swap_nonoverlapping<T>(x: *mut T, y: *mut T, count: usize) {
395+
debug_assert!(is_aligned_and_not_null(x), "attempt to swap unaligned or null pointer");
396+
debug_assert!(is_aligned_and_not_null(y), "attempt to swap unaligned or null pointer");
397+
debug_assert!(is_nonoverlapping(x, y, count), "attempt to swap overlapping memory");
398+
395399
let x = x as *mut u8;
396400
let y = y as *mut u8;
397401
let len = mem::size_of::<T>() * count;
@@ -619,6 +623,7 @@ pub unsafe fn replace<T>(dst: *mut T, mut src: T) -> T {
619623
#[inline]
620624
#[stable(feature = "rust1", since = "1.0.0")]
621625
pub unsafe fn read<T>(src: *const T) -> T {
626+
// `copy_nonoverlapping` takes care of debug_assert.
622627
let mut tmp = MaybeUninit::<T>::uninit();
623628
copy_nonoverlapping(src, tmp.as_mut_ptr(), 1);
624629
tmp.assume_init()
@@ -712,6 +717,7 @@ pub unsafe fn read<T>(src: *const T) -> T {
712717
#[inline]
713718
#[stable(feature = "ptr_unaligned", since = "1.17.0")]
714719
pub unsafe fn read_unaligned<T>(src: *const T) -> T {
720+
// `copy_nonoverlapping` takes care of debug_assert.
715721
let mut tmp = MaybeUninit::<T>::uninit();
716722
copy_nonoverlapping(src as *const u8, tmp.as_mut_ptr() as *mut u8, mem::size_of::<T>());
717723
tmp.assume_init()
@@ -804,6 +810,9 @@ pub unsafe fn read_unaligned<T>(src: *const T) -> T {
804810
#[inline]
805811
#[stable(feature = "rust1", since = "1.0.0")]
806812
pub unsafe fn write<T>(dst: *mut T, src: T) {
813+
// FIXME: the debug assertion here causes codegen test failures on some architectures.
814+
// See <https://github.com/rust-lang/rust/pull/69208#issuecomment-591326757>.
815+
// debug_assert!(is_aligned_and_not_null(dst), "attempt to write to unaligned or null pointer");
807816
intrinsics::move_val_init(&mut *dst, src)
808817
}
809818

@@ -896,6 +905,7 @@ pub unsafe fn write<T>(dst: *mut T, src: T) {
896905
#[inline]
897906
#[stable(feature = "ptr_unaligned", since = "1.17.0")]
898907
pub unsafe fn write_unaligned<T>(dst: *mut T, src: T) {
908+
// `copy_nonoverlapping` takes care of debug_assert.
899909
copy_nonoverlapping(&src as *const T as *const u8, dst as *mut u8, mem::size_of::<T>());
900910
mem::forget(src);
901911
}
@@ -967,6 +977,7 @@ pub unsafe fn write_unaligned<T>(dst: *mut T, src: T) {
967977
#[inline]
968978
#[stable(feature = "volatile", since = "1.9.0")]
969979
pub unsafe fn read_volatile<T>(src: *const T) -> T {
980+
debug_assert!(is_aligned_and_not_null(src), "attempt to read from unaligned or null pointer");
970981
intrinsics::volatile_load(src)
971982
}
972983

@@ -1035,6 +1046,7 @@ pub unsafe fn read_volatile<T>(src: *const T) -> T {
10351046
#[inline]
10361047
#[stable(feature = "volatile", since = "1.9.0")]
10371048
pub unsafe fn write_volatile<T>(dst: *mut T, src: T) {
1049+
debug_assert!(is_aligned_and_not_null(dst), "attempt to write to unaligned or null pointer");
10381050
intrinsics::volatile_store(dst, src);
10391051
}
10401052

0 commit comments

Comments
 (0)