-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
size_of_val_raw: for length 0 this is safe to call #126152
Merged
Merged
Changes from all commits
Commits
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 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 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
RalfJung marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains 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,18 @@ | ||
//@ build-fail | ||
//@ compile-flags: --crate-type lib | ||
//@ only-32bit Layout computation rejects this layout for different reasons on 64-bit. | ||
//@ error-pattern: too big for the current architecture | ||
#![feature(core_intrinsics)] | ||
#![allow(internal_features)] | ||
|
||
// isize::MAX is fine, but with the padding for the unsized tail it is too big. | ||
#[repr(C)] | ||
pub struct Example([u8; isize::MAX as usize], [u16]); | ||
|
||
// We guarantee that with length 0, `size_of_val_raw` (which calls the `size_of_val` intrinsic) | ||
// is safe to call. The compiler aborts compilation if a length of 0 would overflow. | ||
// So let's construct a case where length 0 just barely overflows, and ensure that | ||
// does abort compilation. | ||
pub fn check(x: *const Example) -> usize { | ||
unsafe { std::intrinsics::size_of_val(x) } | ||
} |
This file contains 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,4 @@ | ||
error: values of the type `Example` are too big for the current architecture | ||
|
||
error: aborting due to 1 previous error | ||
|
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we missing tests exercising this specifically (in the "success" sense, perhaps miri tests or a run-pass test w/ an
assert_eq!
)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR adds the relevant test. I don't think there's a useful "success" test here, what do you have in mind?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like, when we're calling
size_of_val_raw
on a struct with a tail who is a slice of size zero or something and the pointer is invalid or something (is that what this is guaranteeing??).I just think it's weird that we're adding assumptions guaranteeing additional special cases for safety here, but adding tests that guarantee an error. I think that's because of the note you had above:
but I'm just confused why a "negative" test is guaranteed for a new "positive" property, if you see what I mean.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I can see how that's confusing. But a test that just checks that
size_of_val_raw
returns the right result for some small slice seems entirely pointless to me, the functionality of that intrinsic is already covered by existing tests. This includes all the Miri tests that usesize_of_val
/size_of_val_raw
. The key point is that compilation is guaranteed to fail if an overflow were to happen, and that's what the new test ensures.