Skip to content

Commit f0a4233

Browse files
RalfJungMark-Simulacrum
authored andcommitted
memory access sanity checks: abort instead of panic
1 parent c8a9c34 commit f0a4233

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

src/libcore/intrinsics.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -2057,9 +2057,14 @@ pub unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize) {
20572057
fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
20582058
}
20592059

2060-
debug_assert!(is_aligned_and_not_null(src), "attempt to copy from unaligned or null pointer");
2061-
debug_assert!(is_aligned_and_not_null(dst), "attempt to copy to unaligned or null pointer");
2062-
debug_assert!(is_nonoverlapping(src, dst, count), "attempt to copy to overlapping memory");
2060+
if cfg!(debug_assertions)
2061+
&& !(is_aligned_and_not_null(src)
2062+
&& is_aligned_and_not_null(dst)
2063+
&& is_nonoverlapping(src, dst, count))
2064+
{
2065+
// Not panicking to keep codegen impact smaller.
2066+
abort();
2067+
}
20632068
copy_nonoverlapping(src, dst, count)
20642069
}
20652070

@@ -2122,8 +2127,10 @@ pub unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
21222127
fn copy<T>(src: *const T, dst: *mut T, count: usize);
21232128
}
21242129

2125-
debug_assert!(is_aligned_and_not_null(src), "attempt to copy from unaligned or null pointer");
2126-
debug_assert!(is_aligned_and_not_null(dst), "attempt to copy to unaligned or null pointer");
2130+
if cfg!(debug_assertions) && !(is_aligned_and_not_null(src) && is_aligned_and_not_null(dst)) {
2131+
// Not panicking to keep codegen impact smaller.
2132+
abort();
2133+
}
21272134
copy(src, dst, count)
21282135
}
21292136

src/libcore/ptr/mod.rs

+21-7
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
use crate::cmp::Ordering;
7171
use crate::fmt;
7272
use crate::hash;
73-
use crate::intrinsics::{self, is_aligned_and_not_null, is_nonoverlapping};
73+
use crate::intrinsics::{self, abort, is_aligned_and_not_null, is_nonoverlapping};
7474
use crate::mem::{self, MaybeUninit};
7575

7676
#[stable(feature = "rust1", since = "1.0.0")]
@@ -420,9 +420,14 @@ pub unsafe fn swap<T>(x: *mut T, y: *mut T) {
420420
#[inline]
421421
#[stable(feature = "swap_nonoverlapping", since = "1.27.0")]
422422
pub unsafe fn swap_nonoverlapping<T>(x: *mut T, y: *mut T, count: usize) {
423-
debug_assert!(is_aligned_and_not_null(x), "attempt to swap unaligned or null pointer");
424-
debug_assert!(is_aligned_and_not_null(y), "attempt to swap unaligned or null pointer");
425-
debug_assert!(is_nonoverlapping(x, y, count), "attempt to swap overlapping memory");
423+
if cfg!(debug_assertions)
424+
&& !(is_aligned_and_not_null(x)
425+
&& is_aligned_and_not_null(y)
426+
&& is_nonoverlapping(x, y, count))
427+
{
428+
// Not panicking to keep codegen impact smaller.
429+
abort();
430+
}
426431

427432
let x = x as *mut u8;
428433
let y = y as *mut u8;
@@ -838,7 +843,10 @@ pub unsafe fn read_unaligned<T>(src: *const T) -> T {
838843
#[inline]
839844
#[stable(feature = "rust1", since = "1.0.0")]
840845
pub unsafe fn write<T>(dst: *mut T, src: T) {
841-
debug_assert!(is_aligned_and_not_null(dst), "attempt to write to unaligned or null pointer");
846+
if cfg!(debug_assertions) && !is_aligned_and_not_null(dst) {
847+
// Not panicking to keep codegen impact smaller.
848+
abort();
849+
}
842850
intrinsics::move_val_init(&mut *dst, src)
843851
}
844852

@@ -1003,7 +1011,10 @@ pub unsafe fn write_unaligned<T>(dst: *mut T, src: T) {
10031011
#[inline]
10041012
#[stable(feature = "volatile", since = "1.9.0")]
10051013
pub unsafe fn read_volatile<T>(src: *const T) -> T {
1006-
debug_assert!(is_aligned_and_not_null(src), "attempt to read from unaligned or null pointer");
1014+
if cfg!(debug_assertions) && !is_aligned_and_not_null(src) {
1015+
// Not panicking to keep codegen impact smaller.
1016+
abort();
1017+
}
10071018
intrinsics::volatile_load(src)
10081019
}
10091020

@@ -1072,7 +1083,10 @@ pub unsafe fn read_volatile<T>(src: *const T) -> T {
10721083
#[inline]
10731084
#[stable(feature = "volatile", since = "1.9.0")]
10741085
pub unsafe fn write_volatile<T>(dst: *mut T, src: T) {
1075-
debug_assert!(is_aligned_and_not_null(dst), "attempt to write to unaligned or null pointer");
1086+
if cfg!(debug_assertions) && !is_aligned_and_not_null(dst) {
1087+
// Not panicking to keep codegen impact smaller.
1088+
abort();
1089+
}
10761090
intrinsics::volatile_store(dst, src);
10771091
}
10781092

0 commit comments

Comments
 (0)