diff options
| author | Dylan DPC <99973273+Dylan-DPC@users.noreply.github.com> | 2023-01-18 15:55:38 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-01-18 15:55:38 +0530 |
| commit | 1ff4a1259d0508bad3ff053f06ef3155f83588f9 (patch) | |
| tree | f890885fbfe8bcf43874942b07caa165d3ae9522 | |
| parent | 6f5c3c9cdb4c5a0e07d21c0aef9216a6f9ff3d5b (diff) | |
| parent | 2d54b7ceb2a5c078f019fd2eacf70d59ea37555b (diff) | |
Rollup merge of #106950 - the8472:fix-splice-miri, r=cuviper
Don't do pointer arithmetic on pointers to deallocated memory vec::Splice can invalidate the slice::Iter inside vec::Drain. So we replace them with dangling pointers which, unlike ones to deallocated memory, are allowed. Fixes miri test failures. Fixes https://github.com/rust-lang/miri/issues/2759
| -rw-r--r-- | library/alloc/src/vec/drain.rs | 6 | ||||
| -rw-r--r-- | library/alloc/src/vec/splice.rs | 6 | ||||
| -rw-r--r-- | src/tools/miri/tests/pass/vec.rs | 6 |
3 files changed, 15 insertions, 3 deletions
diff --git a/library/alloc/src/vec/drain.rs b/library/alloc/src/vec/drain.rs index 541f99bcfab..2b1a787cc54 100644 --- a/library/alloc/src/vec/drain.rs +++ b/library/alloc/src/vec/drain.rs @@ -223,9 +223,9 @@ impl<T, A: Allocator> Drop for Drain<'_, T, A> { } // as_slice() must only be called when iter.len() is > 0 because - // vec::Splice modifies vec::Drain fields and may grow the vec which would invalidate - // the iterator's internal pointers. Creating a reference to deallocated memory - // is invalid even when it is zero-length + // it also gets touched by vec::Splice which may turn it into a dangling pointer + // which would make it and the vec pointer point to different allocations which would + // lead to invalid pointer arithmetic below. let drop_ptr = iter.as_slice().as_ptr(); unsafe { diff --git a/library/alloc/src/vec/splice.rs b/library/alloc/src/vec/splice.rs index bad765c7f51..1861147fe72 100644 --- a/library/alloc/src/vec/splice.rs +++ b/library/alloc/src/vec/splice.rs @@ -54,6 +54,12 @@ impl<I: Iterator, A: Allocator> ExactSizeIterator for Splice<'_, I, A> {} impl<I: Iterator, A: Allocator> Drop for Splice<'_, I, A> { fn drop(&mut self) { self.drain.by_ref().for_each(drop); + // At this point draining is done and the only remaining tasks are splicing + // and moving things into the final place. + // Which means we can replace the slice::Iter with pointers that won't point to deallocated + // memory, so that Drain::drop is still allowed to call iter.len(), otherwise it would break + // the ptr.sub_ptr contract. + self.drain.iter = (&[]).iter(); unsafe { if self.drain.tail_len == 0 { diff --git a/src/tools/miri/tests/pass/vec.rs b/src/tools/miri/tests/pass/vec.rs index 3a6655e2ba6..30a28bc5803 100644 --- a/src/tools/miri/tests/pass/vec.rs +++ b/src/tools/miri/tests/pass/vec.rs @@ -162,6 +162,11 @@ fn reverse() { assert!(v[0].0 == 49); } +fn miri_issue_2759() { + let mut input = "1".to_string(); + input.replace_range(0..0, "0"); +} + fn main() { assert_eq!(vec_reallocate().len(), 5); @@ -191,4 +196,5 @@ fn main() { swap(); swap_remove(); reverse(); + miri_issue_2759(); } |
