Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some targeted assume(bytes <= isize::MAX) around slices #123575

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions library/core/src/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1235,6 +1235,17 @@ pub trait SizedTypeProperties: Sized {
#[doc(hidden)]
#[unstable(feature = "sized_type_properties", issue = "none")]
const IS_ZST: bool = size_of::<Self>() == 0;

/// The largest safe length for a `[Self]`.
///
/// Anything larger than this would make `size_of_val` overflow `isize::MAX`,
/// which is never allowed for a single object.
#[doc(hidden)]
#[unstable(feature = "sized_type_properties", issue = "none")]
const MAX_SLICE_LEN: usize = match size_of::<Self>() {
0 => usize::MAX,
n => (isize::MAX as usize) / n,
};
}
#[doc(hidden)]
#[unstable(feature = "sized_type_properties", issue = "none")]
Expand Down
13 changes: 8 additions & 5 deletions library/core/src/slice/raw.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! Free functions to create `&[T]` and `&mut [T]`.

use crate::array;
use crate::mem::{align_of, size_of};
use crate::intrinsics;
use crate::mem::{align_of, SizedTypeProperties};
use crate::ops::Range;
use crate::ptr;
use crate::ub_checks;
Expand Down Expand Up @@ -98,13 +99,14 @@ pub const unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T]
"slice::from_raw_parts requires the pointer to be aligned and non-null, and the total size of the slice not to exceed `isize::MAX`",
(
data: *mut () = data as *mut (),
size: usize = size_of::<T>(),
align: usize = align_of::<T>(),
len: usize = len,
max_len: usize = T::MAX_SLICE_LEN,
) =>
ub_checks::is_aligned_and_not_null(data, align)
&& ub_checks::is_valid_allocation_size(size, len)
&& len <= max_len
);
intrinsics::assume(len <= T::MAX_SLICE_LEN);
&*ptr::slice_from_raw_parts(data, len)
}
}
Expand Down Expand Up @@ -152,13 +154,14 @@ pub const unsafe fn from_raw_parts_mut<'a, T>(data: *mut T, len: usize) -> &'a m
"slice::from_raw_parts_mut requires the pointer to be aligned and non-null, and the total size of the slice not to exceed `isize::MAX`",
(
data: *mut () = data as *mut (),
size: usize = size_of::<T>(),
align: usize = align_of::<T>(),
len: usize = len,
max_len: usize = T::MAX_SLICE_LEN,
) =>
ub_checks::is_aligned_and_not_null(data, align)
&& ub_checks::is_valid_allocation_size(size, len)
&& len <= max_len
);
intrinsics::assume(len <= T::MAX_SLICE_LEN);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given this was already assert_unsafe_preconditioned, might as well assume it too.

(Not using assert_unchecked because it doesn't need a second UbCheck.)

&mut *ptr::slice_from_raw_parts_mut(data, len)
}
}
Expand Down
6 changes: 0 additions & 6 deletions library/core/src/ub_checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,6 @@ pub(crate) const fn is_aligned_and_not_null(ptr: *const (), align: usize) -> boo
!ptr.is_null() && ptr.is_aligned_to(align)
}

#[inline]
pub(crate) const fn is_valid_allocation_size(size: usize, len: usize) -> bool {
let max_len = if size == 0 { usize::MAX } else { isize::MAX as usize / size };
len <= max_len
}

/// Checks whether the regions of memory starting at `src` and `dst` of size
/// `count * size` do *not* overlap.
///
Expand Down
Loading