Skip to content

Commit f940617

Browse files
Rollup merge of rust-lang#62103 - RalfJung:debug-assert, r=alexcrichton
Add debug assertions to write_bytes and copy* Looks like @nitnelave went MIA in rust-lang#58783, so I am re-submitting their PR, tweaked just a bit. I took care to preserve commit authorship. Cc rust-lang#53871
2 parents 4b65a86 + 85d76a1 commit f940617

File tree

6 files changed

+57
-23
lines changed

6 files changed

+57
-23
lines changed

Cargo.lock

+19-19
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ dependencies = [
2525
name = "alloc"
2626
version = "0.0.0"
2727
dependencies = [
28-
"compiler_builtins 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
28+
"compiler_builtins 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)",
2929
"core 0.0.0",
3030
"rand 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
3131
"rand_xorshift 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -131,7 +131,7 @@ dependencies = [
131131
"autocfg 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
132132
"backtrace-sys 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)",
133133
"cfg-if 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
134-
"compiler_builtins 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
134+
"compiler_builtins 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)",
135135
"libc 0.2.54 (registry+https://github.com/rust-lang/crates.io-index)",
136136
"rustc-demangle 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)",
137137
"rustc-std-workspace-core 1.0.0",
@@ -143,7 +143,7 @@ version = "0.1.27"
143143
source = "registry+https://github.com/rust-lang/crates.io-index"
144144
dependencies = [
145145
"cc 1.0.35 (registry+https://github.com/rust-lang/crates.io-index)",
146-
"compiler_builtins 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
146+
"compiler_builtins 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)",
147147
"libc 0.2.54 (registry+https://github.com/rust-lang/crates.io-index)",
148148
"rustc-std-workspace-core 1.0.0",
149149
]
@@ -373,7 +373,7 @@ name = "cfg-if"
373373
version = "0.1.8"
374374
source = "registry+https://github.com/rust-lang/crates.io-index"
375375
dependencies = [
376-
"compiler_builtins 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
376+
"compiler_builtins 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)",
377377
"rustc-std-workspace-core 1.0.0",
378378
]
379379

@@ -502,7 +502,7 @@ dependencies = [
502502

503503
[[package]]
504504
name = "compiler_builtins"
505-
version = "0.1.16"
505+
version = "0.1.17"
506506
source = "registry+https://github.com/rust-lang/crates.io-index"
507507
dependencies = [
508508
"cc 1.0.35 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -834,7 +834,7 @@ name = "dlmalloc"
834834
version = "0.1.3"
835835
source = "registry+https://github.com/rust-lang/crates.io-index"
836836
dependencies = [
837-
"compiler_builtins 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
837+
"compiler_builtins 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)",
838838
"libc 0.2.54 (registry+https://github.com/rust-lang/crates.io-index)",
839839
"rustc-std-workspace-core 1.0.0",
840840
]
@@ -1008,7 +1008,7 @@ name = "fortanix-sgx-abi"
10081008
version = "0.3.2"
10091009
source = "registry+https://github.com/rust-lang/crates.io-index"
10101010
dependencies = [
1011-
"compiler_builtins 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
1011+
"compiler_builtins 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)",
10121012
"rustc-std-workspace-core 1.0.0",
10131013
]
10141014

@@ -1199,7 +1199,7 @@ name = "hashbrown"
11991199
version = "0.4.0"
12001200
source = "registry+https://github.com/rust-lang/crates.io-index"
12011201
dependencies = [
1202-
"compiler_builtins 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
1202+
"compiler_builtins 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)",
12031203
"rustc-std-workspace-alloc 1.0.0",
12041204
"rustc-std-workspace-core 1.0.0",
12051205
]
@@ -2066,7 +2066,7 @@ dependencies = [
20662066
name = "panic_abort"
20672067
version = "0.0.0"
20682068
dependencies = [
2069-
"compiler_builtins 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
2069+
"compiler_builtins 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)",
20702070
"core 0.0.0",
20712071
"libc 0.2.54 (registry+https://github.com/rust-lang/crates.io-index)",
20722072
]
@@ -2077,7 +2077,7 @@ version = "0.0.0"
20772077
dependencies = [
20782078
"alloc 0.0.0",
20792079
"cfg-if 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
2080-
"compiler_builtins 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
2080+
"compiler_builtins 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)",
20812081
"core 0.0.0",
20822082
"libc 0.2.54 (registry+https://github.com/rust-lang/crates.io-index)",
20832083
"unwind 0.0.0",
@@ -2263,7 +2263,7 @@ name = "profiler_builtins"
22632263
version = "0.0.0"
22642264
dependencies = [
22652265
"cc 1.0.35 (registry+https://github.com/rust-lang/crates.io-index)",
2266-
"compiler_builtins 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
2266+
"compiler_builtins 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)",
22672267
"core 0.0.0",
22682268
]
22692269

@@ -2835,7 +2835,7 @@ name = "rustc-demangle"
28352835
version = "0.1.15"
28362836
source = "registry+https://github.com/rust-lang/crates.io-index"
28372837
dependencies = [
2838-
"compiler_builtins 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
2838+
"compiler_builtins 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)",
28392839
"rustc-std-workspace-core 1.0.0",
28402840
]
28412841

@@ -2941,7 +2941,7 @@ dependencies = [
29412941
"alloc 0.0.0",
29422942
"build_helper 0.1.0",
29432943
"cmake 0.1.38 (registry+https://github.com/rust-lang/crates.io-index)",
2944-
"compiler_builtins 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
2944+
"compiler_builtins 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)",
29452945
"core 0.0.0",
29462946
]
29472947

@@ -3160,7 +3160,7 @@ dependencies = [
31603160
"alloc 0.0.0",
31613161
"build_helper 0.1.0",
31623162
"cmake 0.1.38 (registry+https://github.com/rust-lang/crates.io-index)",
3163-
"compiler_builtins 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
3163+
"compiler_builtins 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)",
31643164
"core 0.0.0",
31653165
]
31663166

@@ -3223,7 +3223,7 @@ dependencies = [
32233223
"alloc 0.0.0",
32243224
"build_helper 0.1.0",
32253225
"cmake 0.1.38 (registry+https://github.com/rust-lang/crates.io-index)",
3226-
"compiler_builtins 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
3226+
"compiler_builtins 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)",
32273227
"core 0.0.0",
32283228
]
32293229

@@ -3341,7 +3341,7 @@ dependencies = [
33413341
"alloc 0.0.0",
33423342
"build_helper 0.1.0",
33433343
"cmake 0.1.38 (registry+https://github.com/rust-lang/crates.io-index)",
3344-
"compiler_builtins 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
3344+
"compiler_builtins 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)",
33453345
"core 0.0.0",
33463346
]
33473347

@@ -3648,7 +3648,7 @@ dependencies = [
36483648
"backtrace 0.3.29 (registry+https://github.com/rust-lang/crates.io-index)",
36493649
"cc 1.0.35 (registry+https://github.com/rust-lang/crates.io-index)",
36503650
"cfg-if 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
3651-
"compiler_builtins 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
3651+
"compiler_builtins 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)",
36523652
"core 0.0.0",
36533653
"dlmalloc 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
36543654
"fortanix-sgx-abi 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -4309,7 +4309,7 @@ version = "0.0.0"
43094309
dependencies = [
43104310
"cc 1.0.35 (registry+https://github.com/rust-lang/crates.io-index)",
43114311
"cfg-if 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
4312-
"compiler_builtins 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
4312+
"compiler_builtins 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)",
43134313
"core 0.0.0",
43144314
"libc 0.2.54 (registry+https://github.com/rust-lang/crates.io-index)",
43154315
]
@@ -4527,7 +4527,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
45274527
"checksum colored 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b0aa3473e85a3161b59845d6096b289bb577874cafeaf75ea1b1beaa6572c7fc"
45284528
"checksum commoncrypto 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d056a8586ba25a1e4d61cb090900e495952c7886786fc55f909ab2f819b69007"
45294529
"checksum commoncrypto-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1fed34f46747aa73dfaa578069fd8279d2818ade2b55f38f22a9401c7f4083e2"
4530-
"checksum compiler_builtins 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "e79ed19793c99771b386d76e08c3419409bb3d418b81a8b8afc73524247461cf"
4530+
"checksum compiler_builtins 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)" = "ad9b4731b9e701aefe9e6bd1e9173f30526661508f9aaadaa5caec25ddf95585"
45314531
"checksum compiletest_rs 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)" = "f40ecc9332b68270998995c00f8051ee856121764a0d3230e64c9efd059d27b6"
45324532
"checksum constant_time_eq 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8ff012e225ce166d4422e0e78419d901719760f62ae2b7969ca6b564d1b54a9e"
45334533
"checksum core-foundation 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "4e2640d6d0bf22e82bed1b73c6aef8d5dd31e5abe6666c57e6d45e2649f4f887"

src/libcore/intrinsics.rs

+32-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
issue = "0")]
3737
#![allow(missing_docs)]
3838

39+
use crate::mem;
40+
3941
#[stable(feature = "drop_in_place", since = "1.8.0")]
4042
#[rustc_deprecated(reason = "no longer an intrinsic - use `ptr::drop_in_place` directly",
4143
since = "1.18.0")]
@@ -1323,6 +1325,26 @@ extern "rust-intrinsic" {
13231325
// (`transmute` also falls into this category, but it cannot be wrapped due to the
13241326
// check that `T` and `U` have the same size.)
13251327

1328+
/// Checks whether `ptr` is properly aligned with respect to
1329+
/// `align_of::<T>()`.
1330+
pub(crate) fn is_aligned_and_not_null<T>(ptr: *const T) -> bool {
1331+
!ptr.is_null() && ptr as usize % mem::align_of::<T>() == 0
1332+
}
1333+
1334+
/// Checks whether the regions of memory starting at `src` and `dst` of size
1335+
/// `count * size_of::<T>()` overlap.
1336+
fn overlaps<T>(src: *const T, dst: *const T, count: usize) -> bool {
1337+
let src_usize = src as usize;
1338+
let dst_usize = dst as usize;
1339+
let size = mem::size_of::<T>().checked_mul(count).unwrap();
1340+
let diff = if src_usize > dst_usize {
1341+
src_usize - dst_usize
1342+
} else {
1343+
dst_usize - src_usize
1344+
};
1345+
size > diff
1346+
}
1347+
13261348
/// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
13271349
/// and destination must *not* overlap.
13281350
///
@@ -1412,7 +1434,11 @@ pub unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize) {
14121434
extern "rust-intrinsic" {
14131435
fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
14141436
}
1415-
copy_nonoverlapping(src, dst, count);
1437+
1438+
debug_assert!(is_aligned_and_not_null(src), "attempt to copy from unaligned or null pointer");
1439+
debug_assert!(is_aligned_and_not_null(dst), "attempt to copy to unaligned or null pointer");
1440+
debug_assert!(!overlaps(src, dst, count), "attempt to copy to overlapping memory");
1441+
copy_nonoverlapping(src, dst, count)
14161442
}
14171443

14181444
/// Copies `count * size_of::<T>()` bytes from `src` to `dst`. The source
@@ -1472,6 +1498,9 @@ pub unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
14721498
extern "rust-intrinsic" {
14731499
fn copy<T>(src: *const T, dst: *mut T, count: usize);
14741500
}
1501+
1502+
debug_assert!(is_aligned_and_not_null(src), "attempt to copy from unaligned or null pointer");
1503+
debug_assert!(is_aligned_and_not_null(dst), "attempt to copy to unaligned or null pointer");
14751504
copy(src, dst, count)
14761505
}
14771506

@@ -1553,5 +1582,7 @@ pub unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
15531582
extern "rust-intrinsic" {
15541583
fn write_bytes<T>(dst: *mut T, val: u8, count: usize);
15551584
}
1585+
1586+
debug_assert!(is_aligned_and_not_null(dst), "attempt to write to unaligned or null pointer");
15561587
write_bytes(dst, val, count)
15571588
}

src/libcore/slice/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
use crate::cmp::Ordering::{self, Less, Equal, Greater};
2626
use crate::cmp;
2727
use crate::fmt;
28-
use crate::intrinsics::{assume, exact_div, unchecked_sub};
28+
use crate::intrinsics::{assume, exact_div, unchecked_sub, is_aligned_and_not_null};
2929
use crate::isize;
3030
use crate::iter::*;
3131
use crate::ops::{FnMut, Try, self};
@@ -5228,7 +5228,7 @@ unsafe impl<'a, T> TrustedRandomAccess for RChunksExactMut<'a, T> {
52285228
#[inline]
52295229
#[stable(feature = "rust1", since = "1.0.0")]
52305230
pub unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] {
5231-
debug_assert!(data as usize % mem::align_of::<T>() == 0, "attempt to create unaligned slice");
5231+
debug_assert!(is_aligned_and_not_null(data), "attempt to create unaligned or null slice");
52325232
debug_assert!(mem::size_of::<T>().saturating_mul(len) <= isize::MAX as usize,
52335233
"attempt to create slice covering half the address space");
52345234
&*ptr::slice_from_raw_parts(data, len)
@@ -5249,7 +5249,7 @@ pub unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] {
52495249
#[inline]
52505250
#[stable(feature = "rust1", since = "1.0.0")]
52515251
pub unsafe fn from_raw_parts_mut<'a, T>(data: *mut T, len: usize) -> &'a mut [T] {
5252-
debug_assert!(data as usize % mem::align_of::<T>() == 0, "attempt to create unaligned slice");
5252+
debug_assert!(is_aligned_and_not_null(data), "attempt to create unaligned or null slice");
52535253
debug_assert!(mem::size_of::<T>().saturating_mul(len) <= isize::MAX as usize,
52545254
"attempt to create slice covering half the address space");
52555255
&mut *ptr::slice_from_raw_parts_mut(data, len)

src/test/codegen/issue-45222.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// compile-flags: -O
2+
// ignore-debug: the debug assertions get in the way
23

34
#![crate_type = "lib"]
45

src/test/codegen/issue-45466.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// compile-flags: -O
2+
// ignore-debug: the debug assertions get in the way
23

34
#![crate_type="rlib"]
45

src/test/codegen/swap-small-types.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// compile-flags: -O
22
// only-x86_64
3+
// ignore-debug: the debug assertions get in the way
34

45
#![crate_type = "lib"]
56

0 commit comments

Comments
 (0)