-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Make too-large byte sizes a validity error in Layout
#99134
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
Closed
scottmcm
wants to merge
2
commits into
rust-lang:master
from
scottmcm:bytes-validity-invariant-for-layout
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use crate::convert::TryFrom; | ||
use crate::intrinsics::assert_unsafe_precondition; | ||
use crate::{fmt, num}; | ||
|
||
/// A type storing a possible object size (in bytes) in the rust abstract machine. | ||
/// | ||
/// This can be thought of as a positive `isize`, or `usize` without the high bit | ||
/// set. This is important because [`pointer::offset`] is UB for *byte* sizes | ||
/// too large for an `isize`, and there's a corresponding language limit on the | ||
/// size of any allocated object. | ||
/// | ||
/// Note that particularly large sizes, while representable in this type, are | ||
/// likely not to be supported by actual allocators and machines. | ||
#[derive(Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)] | ||
#[repr(transparent)] | ||
#[cfg_attr(target_pointer_width = "16", rustc_layout_scalar_valid_range_end(0x7FFF))] | ||
#[cfg_attr(target_pointer_width = "32", rustc_layout_scalar_valid_range_end(0x7FFF_FFFF))] | ||
#[cfg_attr(target_pointer_width = "64", rustc_layout_scalar_valid_range_end(0x7FFF_FFFF_FFFF_FFFF))] | ||
pub(crate) struct ValidSize(usize); | ||
|
||
const MAX_SIZE: usize = isize::MAX as usize; | ||
|
||
impl ValidSize { | ||
/// Creates a `ValidSize` from a `usize` that fits in an `isize`. | ||
/// | ||
/// # Safety | ||
/// | ||
/// `size` must be less than or equal to `isize::MAX`. | ||
/// | ||
/// Equivalently, it must not have its high bit set. | ||
#[inline] | ||
pub(crate) const unsafe fn new_unchecked(size: usize) -> Self { | ||
// SAFETY: By precondition, this must be within our validity invariant. | ||
unsafe { | ||
assert_unsafe_precondition!(size <= MAX_SIZE); | ||
ValidSize(size) | ||
} | ||
} | ||
|
||
#[inline] | ||
pub(crate) const fn as_usize(self) -> usize { | ||
self.0 | ||
} | ||
} | ||
|
||
impl TryFrom<usize> for ValidSize { | ||
type Error = num::TryFromIntError; | ||
|
||
#[inline] | ||
fn try_from(size: usize) -> Result<ValidSize, Self::Error> { | ||
if size <= MAX_SIZE { | ||
// SAFETY: Just checked it's within our validity invariant. | ||
unsafe { Ok(ValidSize(size)) } | ||
scottmcm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
Err(num::TryFromIntError(())) | ||
} | ||
} | ||
} | ||
|
||
impl fmt::Debug for ValidSize { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
self.as_usize().fmt(f) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.