From 81ebaf27cb4301a833f5a4ba1f4a6e63cfcc32b3 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 10 Mar 2024 17:12:27 +0100 Subject: [PATCH 1/2] RawVec::into_box: avoid unnecessary intermediate reference --- library/alloc/src/raw_vec.rs | 3 +-- tests/ui/hygiene/panic-location.rs | 1 + tests/ui/hygiene/panic-location.run.stderr | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/library/alloc/src/raw_vec.rs b/library/alloc/src/raw_vec.rs index ace73c0fdaa5e..0ee293db73a93 100644 --- a/library/alloc/src/raw_vec.rs +++ b/library/alloc/src/raw_vec.rs @@ -5,7 +5,6 @@ use core::cmp; use core::hint; use core::mem::{self, ManuallyDrop, MaybeUninit, SizedTypeProperties}; use core::ptr::{self, NonNull, Unique}; -use core::slice; #[cfg(not(no_global_oom_handling))] use crate::alloc::handle_alloc_error; @@ -192,7 +191,7 @@ impl RawVec { let me = ManuallyDrop::new(self); unsafe { - let slice = slice::from_raw_parts_mut(me.ptr() as *mut MaybeUninit, len); + let slice = ptr::slice_from_raw_parts_mut(me.ptr() as *mut MaybeUninit, len); Box::from_raw_in(slice, ptr::read(&me.alloc)) } } diff --git a/tests/ui/hygiene/panic-location.rs b/tests/ui/hygiene/panic-location.rs index a98960d74b019..b2f9bfe4f9a7a 100644 --- a/tests/ui/hygiene/panic-location.rs +++ b/tests/ui/hygiene/panic-location.rs @@ -1,6 +1,7 @@ //@ run-fail //@ check-run-results //@ exec-env:RUST_BACKTRACE=0 +//@ normalize-stderr-test ".rs:\d+:\d+" -> ".rs:LL:CC" // // Regression test for issue #70963 // The captured stderr from this test reports a location diff --git a/tests/ui/hygiene/panic-location.run.stderr b/tests/ui/hygiene/panic-location.run.stderr index ec0ce18c3dfa7..5c1778bc485f9 100644 --- a/tests/ui/hygiene/panic-location.run.stderr +++ b/tests/ui/hygiene/panic-location.run.stderr @@ -1,3 +1,3 @@ -thread 'main' panicked at library/alloc/src/raw_vec.rs:26:5: +thread 'main' panicked at library/alloc/src/raw_vec.rs:LL:CC: capacity overflow note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace From a7443f554215b76ef1f911b1d6d07b4b7ebbb23c Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 10 Mar 2024 18:04:03 +0100 Subject: [PATCH 2/2] test into_boxed_slice with custom allocator in Miri --- .../tests/pass/box-custom-alloc-aliasing.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/tools/miri/tests/pass/box-custom-alloc-aliasing.rs b/src/tools/miri/tests/pass/box-custom-alloc-aliasing.rs index 541e5f244db08..ada9cf4537547 100644 --- a/src/tools/miri/tests/pass/box-custom-alloc-aliasing.rs +++ b/src/tools/miri/tests/pass/box-custom-alloc-aliasing.rs @@ -92,6 +92,8 @@ unsafe impl Allocator for MyAllocator { } unsafe fn deallocate(&self, ptr: NonNull, _layout: Layout) { + // Make sure accesses via `self` don't disturb anything. + let _val = self.bins[0].top.get(); // Since manually finding the corresponding bin of `ptr` is very expensive, // doing pointer arithmetics is preferred. // But this means we access `top` via `ptr` rather than `self`! @@ -101,22 +103,30 @@ unsafe impl Allocator for MyAllocator { if self.thread_id == thread_id { unsafe { (*their_bin).push(ptr) }; } else { - todo!("Deallocating from another thread") + todo!("Deallocating from another thread"); } + // Make sure we can also still access this via `self` after the rest is done. + let _val = self.bins[0].top.get(); } } // Make sure to involve `Box` in allocating these, // as that's where `noalias` may come from. -fn v(t: T, a: A) -> Vec { +fn v1(t: T, a: A) -> Vec { (Box::new_in([t], a) as Box<[T], A>).into_vec() } +fn v2(t: T, a: A) -> Vec { + let v = v1(t, a); + // There was a bug in `into_boxed_slice` that caused aliasing issues, + // so round-trip through that as well. + v.into_boxed_slice().into_vec() +} fn main() { assert!(mem::size_of::() <= 128); // if it grows bigger, the trick to access the "header" no longer works let my_alloc = MyAllocator::new(); - let a = v(1usize, &my_alloc); - let b = v(2usize, &my_alloc); + let a = v1(1usize, &my_alloc); + let b = v2(2usize, &my_alloc); assert_eq!(a[0] + 1, b[0]); assert_eq!(addr_of!(a[0]).wrapping_add(1), addr_of!(b[0])); drop((a, b));