-
Notifications
You must be signed in to change notification settings - Fork 13.4k
fix VecDeque::iter_mut aliasing issues #76911
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f251dc4
VecDeque: fix incorrect &mut aliasing in IterMut::next/next_back
RalfJung e4c1a38
VecDeque: avoid more aliasing issues by working with raw pointers ins…
RalfJung 69669cb
make IterMut Send/Sync again
RalfJung fa6a4f7
avoid unnecessary intermediate reference and improve safety comments
RalfJung 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ use core::cmp::{self, Ordering}; | |
use core::fmt; | ||
use core::hash::{Hash, Hasher}; | ||
use core::iter::{repeat_with, FromIterator, FusedIterator}; | ||
use core::marker::PhantomData; | ||
use core::mem::{self, replace, ManuallyDrop}; | ||
use core::ops::{Index, IndexMut, Range, RangeBounds, Try}; | ||
use core::ptr::{self, NonNull}; | ||
|
@@ -982,7 +983,12 @@ impl<T> VecDeque<T> { | |
/// ``` | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub fn iter_mut(&mut self) -> IterMut<'_, T> { | ||
IterMut { tail: self.tail, head: self.head, ring: unsafe { self.buffer_as_mut_slice() } } | ||
IterMut { | ||
tail: self.tail, | ||
head: self.head, | ||
ring: unsafe { self.buffer_as_mut_slice() }, | ||
phantom: PhantomData, | ||
} | ||
} | ||
|
||
/// Returns a pair of slices which contain, in order, the contents of the | ||
|
@@ -1175,6 +1181,7 @@ impl<T> VecDeque<T> { | |
head, | ||
// The shared reference we have in &mut self is maintained in the '_ of IterMut. | ||
ring: unsafe { self.buffer_as_mut_slice() }, | ||
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. The same "no need for unsafe or reference" change applies here, too |
||
phantom: PhantomData, | ||
} | ||
} | ||
|
||
|
@@ -2493,6 +2500,25 @@ impl<T> RingSlices for &mut [T] { | |
} | ||
} | ||
|
||
impl<T> RingSlices for *mut [T] { | ||
fn slice(self, from: usize, to: usize) -> Self { | ||
assert!(from <= to && to < self.len()); | ||
// Not using `get_unchecked_mut` to keep this a safe operation. | ||
let len = to - from; | ||
ptr::slice_from_raw_parts_mut(self.as_mut_ptr().wrapping_add(from), len) | ||
} | ||
|
||
fn split_at(self, mid: usize) -> (Self, Self) { | ||
let len = self.len(); | ||
let ptr = self.as_mut_ptr(); | ||
assert!(mid <= len); | ||
( | ||
ptr::slice_from_raw_parts_mut(ptr, mid), | ||
ptr::slice_from_raw_parts_mut(ptr.wrapping_add(mid), len - mid), | ||
) | ||
} | ||
} | ||
|
||
/// Calculate the number of elements left to be read in the buffer | ||
#[inline] | ||
fn count(tail: usize, head: usize, size: usize) -> usize { | ||
|
@@ -2662,15 +2688,26 @@ impl<T> FusedIterator for Iter<'_, T> {} | |
/// [`iter_mut`]: VecDeque::iter_mut | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub struct IterMut<'a, T: 'a> { | ||
ring: &'a mut [T], | ||
ring: *mut [T], | ||
tail: usize, | ||
head: usize, | ||
phantom: PhantomData<&'a mut [T]>, | ||
} | ||
|
||
// SAFETY: we do nothing thread-local and there is no interior mutability, | ||
// so the usual structural `Send`/`Sync` apply. | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
unsafe impl<T: Send> Send for IterMut<'_, T> {} | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
unsafe impl<T: Sync> Sync for IterMut<'_, T> {} | ||
|
||
#[stable(feature = "collection_debug", since = "1.17.0")] | ||
impl<T: fmt::Debug> fmt::Debug for IterMut<'_, T> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
let (front, back) = RingSlices::ring_slices(&*self.ring, self.head, self.tail); | ||
let (front, back) = RingSlices::ring_slices(self.ring, self.head, self.tail); | ||
// SAFETY: these are the elements we have not handed out yet, so aliasing is fine. | ||
// We also ensure everything is dereferencable and in-bounds. | ||
RalfJung marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let (front, back) = unsafe { (&*front, &*back) }; | ||
f.debug_tuple("IterMut").field(&front).field(&back).finish() | ||
} | ||
} | ||
|
@@ -2689,7 +2726,7 @@ impl<'a, T> Iterator for IterMut<'a, T> { | |
|
||
unsafe { | ||
let elem = self.ring.get_unchecked_mut(tail); | ||
Some(&mut *(elem as *mut _)) | ||
Some(&mut *elem) | ||
} | ||
} | ||
|
||
|
@@ -2704,6 +2741,9 @@ impl<'a, T> Iterator for IterMut<'a, T> { | |
F: FnMut(Acc, Self::Item) -> Acc, | ||
{ | ||
let (front, back) = RingSlices::ring_slices(self.ring, self.head, self.tail); | ||
// SAFETY: these are the elements we have not handed out yet, so aliasing is fine. | ||
// We also ensure everything is dereferencable and in-bounds. | ||
let (front, back) = unsafe { (&mut *front, &mut *back) }; | ||
accum = front.iter_mut().fold(accum, &mut f); | ||
back.iter_mut().fold(accum, &mut f) | ||
} | ||
|
@@ -2735,7 +2775,7 @@ impl<'a, T> DoubleEndedIterator for IterMut<'a, T> { | |
|
||
unsafe { | ||
let elem = self.ring.get_unchecked_mut(self.head); | ||
Some(&mut *(elem as *mut _)) | ||
Some(&mut *elem) | ||
} | ||
} | ||
|
||
|
@@ -2744,6 +2784,9 @@ impl<'a, T> DoubleEndedIterator for IterMut<'a, T> { | |
F: FnMut(Acc, Self::Item) -> Acc, | ||
{ | ||
let (front, back) = RingSlices::ring_slices(self.ring, self.head, self.tail); | ||
// SAFETY: these are the elements we have not handed out yet, so aliasing is fine. | ||
// We also ensure everything is dereferencable and in-bounds. | ||
let (front, back) = unsafe { (&mut *front, &mut *back) }; | ||
accum = back.iter_mut().rfold(accum, &mut f); | ||
front.iter_mut().rfold(accum, &mut f) | ||
} | ||
|
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.