Skip to content

Commit d0f404d

Browse files
committed
fix IntoIter::drop on high-alignment ZST
1 parent 14e2fe4 commit d0f404d

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

library/alloc/src/vec/into_iter.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ pub struct IntoIter<
4040
// to avoid dropping the allocator twice we need to wrap it into ManuallyDrop
4141
pub(super) alloc: ManuallyDrop<A>,
4242
pub(super) ptr: *const T,
43-
pub(super) end: *const T,
43+
pub(super) end: *const T, // If T is a ZST, this is actually ptr+len. This encoding is picked so that
44+
// ptr == end is a quick test for the Iterator being empty, that works
45+
// for both ZST and non-ZST.
4446
}
4547

4648
#[stable(feature = "vec_intoiter_debug", since = "1.13.0")]
@@ -132,7 +134,9 @@ impl<T, A: Allocator> IntoIter<T, A> {
132134

133135
/// Forgets to Drop the remaining elements while still allowing the backing allocation to be freed.
134136
pub(crate) fn forget_remaining_elements(&mut self) {
135-
self.ptr = self.end;
137+
// For th ZST case, it is crucial that we mutate `end` here, not `ptr`.
138+
// `ptr` must stay aligned, while `end` may be unaligned.
139+
self.end = self.ptr;
136140
}
137141

138142
#[cfg(not(no_global_oom_handling))]
@@ -184,10 +188,9 @@ impl<T, A: Allocator> Iterator for IntoIter<T, A> {
184188
if self.ptr == self.end {
185189
None
186190
} else if T::IS_ZST {
187-
// purposefully don't use 'ptr.offset' because for
188-
// vectors with 0-size elements this would return the
189-
// same pointer.
190-
self.ptr = self.ptr.wrapping_byte_add(1);
191+
// `ptr` has to stay where it is to remain aligned, so we reduce the length by 1 by
192+
// reducing the `end`.
193+
self.end = self.end.wrapping_byte_sub(1);
191194

192195
// Make up a value of this ZST.
193196
Some(unsafe { mem::zeroed() })
@@ -214,10 +217,8 @@ impl<T, A: Allocator> Iterator for IntoIter<T, A> {
214217
let step_size = self.len().min(n);
215218
let to_drop = ptr::slice_from_raw_parts_mut(self.ptr as *mut T, step_size);
216219
if T::IS_ZST {
217-
// SAFETY: due to unchecked casts of unsigned amounts to signed offsets the wraparound
218-
// effectively results in unsigned pointers representing positions 0..usize::MAX,
219-
// which is valid for ZSTs.
220-
self.ptr = self.ptr.wrapping_byte_add(step_size);
220+
// See `next` for why we sub `end` here.
221+
self.end = self.end.wrapping_byte_sub(step_size);
221222
} else {
222223
// SAFETY: the min() above ensures that step_size is in bounds
223224
self.ptr = unsafe { self.ptr.add(step_size) };

src/tools/miri/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,7 @@ Definite bugs found:
639639
* [Data race in `thread::scope`](https://github.com/rust-lang/rust/issues/98498)
640640
* [`regex` incorrectly handling unaligned `Vec<u8>` buffers](https://www.reddit.com/r/rust/comments/vq3mmu/comment/ienc7t0?context=3)
641641
* [Incorrect use of `compare_exchange_weak` in `once_cell`](https://github.com/matklad/once_cell/issues/186)
642+
* [Dropping with unaligned pointers in `vec::IntoIter`](https://github.com/rust-lang/rust/pull/106084)
642643

643644
Violations of [Stacked Borrows] found that are likely bugs (but Stacked Borrows is currently just an experiment):
644645

src/tools/miri/tests/pass/vec.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,19 @@ fn vec_into_iter() -> u8 {
3737
}
3838

3939
fn vec_into_iter_rev() -> u8 {
40-
vec![1, 2, 3, 4].into_iter().map(|x| x * x).fold(0, |x, y| x + y)
40+
vec![1, 2, 3, 4].into_iter().rev().map(|x| x * x).fold(0, |x, y| x + y)
4141
}
4242

43-
fn vec_into_iter_zst() -> usize {
44-
vec![[0u64; 0], [0u64; 0]].into_iter().rev().map(|x| x.len()).sum()
43+
fn vec_into_iter_zst() {
44+
for _ in vec![[0u64; 0]].into_iter() {}
45+
let v = vec![[0u64; 0], [0u64; 0]].into_iter().map(|x| x.len()).sum::<usize>();
46+
assert_eq!(v, 0);
4547
}
4648

47-
fn vec_into_iter_rev_zst() -> usize {
48-
vec![[0u64; 0], [0u64; 0]].into_iter().rev().map(|x| x.len()).sum()
49+
fn vec_into_iter_rev_zst() {
50+
for _ in vec![[0u64; 0]; 5].into_iter().rev() {}
51+
let v = vec![[0u64; 0], [0u64; 0]].into_iter().rev().map(|x| x.len()).sum::<usize>();
52+
assert_eq!(v, 0);
4953
}
5054

5155
fn vec_iter_and_mut() {
@@ -150,8 +154,8 @@ fn main() {
150154
assert_eq!(vec_into_iter(), 30);
151155
assert_eq!(vec_into_iter_rev(), 30);
152156
vec_iter_and_mut();
153-
assert_eq!(vec_into_iter_zst(), 0);
154-
assert_eq!(vec_into_iter_rev_zst(), 0);
157+
vec_into_iter_zst();
158+
vec_into_iter_rev_zst();
155159
vec_iter_and_mut_rev();
156160

157161
assert_eq!(make_vec().capacity(), 4);

0 commit comments

Comments
 (0)