Skip to content

Commit 125c196

Browse files
authored
Rollup merge of #73054 - RalfJung:dont-panic, r=Mark-Simulacrum
memory access sanity checks: abort instead of panic Suggested by @Mark-Simulacrum, this should help reduce the performance impact of these checks.
2 parents 0851036 + 81c7ebd commit 125c196

File tree

4 files changed

+33
-14
lines changed

4 files changed

+33
-14
lines changed

src/libcore/intrinsics.rs

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

2067-
debug_assert!(is_aligned_and_not_null(src), "attempt to copy from unaligned or null pointer");
2068-
debug_assert!(is_aligned_and_not_null(dst), "attempt to copy to unaligned or null pointer");
2069-
debug_assert!(is_nonoverlapping(src, dst, count), "attempt to copy to overlapping memory");
2067+
if cfg!(debug_assertions)
2068+
&& !(is_aligned_and_not_null(src)
2069+
&& is_aligned_and_not_null(dst)
2070+
&& is_nonoverlapping(src, dst, count))
2071+
{
2072+
// Not panicking to keep codegen impact smaller.
2073+
abort();
2074+
}
20702075
copy_nonoverlapping(src, dst, count)
20712076
}
20722077

@@ -2129,8 +2134,10 @@ pub unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
21292134
fn copy<T>(src: *const T, dst: *mut T, count: usize);
21302135
}
21312136

2132-
debug_assert!(is_aligned_and_not_null(src), "attempt to copy from unaligned or null pointer");
2133-
debug_assert!(is_aligned_and_not_null(dst), "attempt to copy to unaligned or null pointer");
2137+
if cfg!(debug_assertions) && !(is_aligned_and_not_null(src) && is_aligned_and_not_null(dst)) {
2138+
// Not panicking to keep codegen impact smaller.
2139+
abort();
2140+
}
21342141
copy(src, dst, count)
21352142
}
21362143

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

src/test/codegen/vec-clear.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// ignore-debug: the debug assertions get in the way
21
// compile-flags: -O
32

43
#![crate_type = "lib"]

src/test/codegen/vec-optimizes-away.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
//
21
// ignore-debug: the debug assertions get in the way
32
// no-system-llvm
43
// compile-flags: -O

0 commit comments

Comments
 (0)