-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
fix vec::IntoIter::drop on high-alignment ZST #106084
Merged
Merged
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 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 |
---|---|---|
|
@@ -40,7 +40,9 @@ pub struct IntoIter< | |
// to avoid dropping the allocator twice we need to wrap it into ManuallyDrop | ||
pub(super) alloc: ManuallyDrop<A>, | ||
pub(super) ptr: *const T, | ||
pub(super) end: *const T, | ||
pub(super) end: *const T, // If T is a ZST, this is actually ptr+len. This encoding is picked so that | ||
// ptr == end is a quick test for the Iterator being empty, that works | ||
// for both ZST and non-ZST. | ||
} | ||
|
||
#[stable(feature = "vec_intoiter_debug", since = "1.13.0")] | ||
|
@@ -132,7 +134,9 @@ impl<T, A: Allocator> IntoIter<T, A> { | |
|
||
/// Forgets to Drop the remaining elements while still allowing the backing allocation to be freed. | ||
pub(crate) fn forget_remaining_elements(&mut self) { | ||
self.ptr = self.end; | ||
// For th ZST case, it is crucial that we mutate `end` here, not `ptr`. | ||
// `ptr` must stay aligned, while `end` may be unaligned. | ||
self.end = self.ptr; | ||
} | ||
|
||
#[cfg(not(no_global_oom_handling))] | ||
|
@@ -184,10 +188,9 @@ impl<T, A: Allocator> Iterator for IntoIter<T, A> { | |
if self.ptr == self.end { | ||
None | ||
} else if T::IS_ZST { | ||
// purposefully don't use 'ptr.offset' because for | ||
// vectors with 0-size elements this would return the | ||
// same pointer. | ||
self.ptr = self.ptr.wrapping_byte_add(1); | ||
// `ptr` has to stay where it is to remain aligned, so we reduce the length by 1 by | ||
// reducing the `end`. | ||
self.end = self.end.wrapping_byte_sub(1); | ||
|
||
// Make up a value of this ZST. | ||
Some(unsafe { mem::zeroed() }) | ||
|
@@ -214,10 +217,8 @@ impl<T, A: Allocator> Iterator for IntoIter<T, A> { | |
let step_size = self.len().min(n); | ||
let to_drop = ptr::slice_from_raw_parts_mut(self.ptr as *mut T, step_size); | ||
if T::IS_ZST { | ||
// SAFETY: due to unchecked casts of unsigned amounts to signed offsets the wraparound | ||
// effectively results in unsigned pointers representing positions 0..usize::MAX, | ||
// which is valid for ZSTs. | ||
self.ptr = self.ptr.wrapping_byte_add(step_size); | ||
// See `next` for why we sub `end` here. | ||
self.end = self.end.wrapping_byte_sub(step_size); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FWIW I don't think we have a test for this (with a ZST that needs alignment) |
||
} else { | ||
// SAFETY: the min() above ensures that step_size is in bounds | ||
self.ptr = unsafe { self.ptr.add(step_size) }; | ||
|
@@ -250,7 +251,7 @@ impl<T, A: Allocator> Iterator for IntoIter<T, A> { | |
return Err(unsafe { array::IntoIter::new_unchecked(raw_ary, 0..len) }); | ||
} | ||
|
||
self.ptr = self.ptr.wrapping_byte_add(N); | ||
self.end = self.end.wrapping_byte_sub(N); | ||
// Safety: ditto | ||
return Ok(unsafe { raw_ary.transpose().assume_init() }); | ||
} | ||
|
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
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.
FWIW I don't think we have a test for this (with a ZST that needs alignment)
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.
Wouldn't it be better to add the tests to std instead of the miri testsuite then?
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.
I'm not sure how this test would be written in std in a way that would actually test the behavior except under miri.
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.
Maybe by adding an unsafe precondition debug assert to drop_in_place?
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.
I would not be opposed to that, but I don't think it needs to be done in this PR.