Skip to content

Commit

Permalink
Unrolled build for rust-lang#122298
Browse files Browse the repository at this point in the history
Rollup merge of rust-lang#122298 - RalfJung:raw-vec-into-box, r=cuviper

RawVec::into_box: avoid unnecessary intermediate reference

Fixes the problem described [here](rust-lang/miri#3341 (comment)).
  • Loading branch information
rust-timer authored Mar 11, 2024
2 parents 6639672 + a7443f5 commit f5e5be5
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
3 changes: 1 addition & 2 deletions library/alloc/src/raw_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -192,7 +191,7 @@ impl<T, A: Allocator> RawVec<T, A> {

let me = ManuallyDrop::new(self);
unsafe {
let slice = slice::from_raw_parts_mut(me.ptr() as *mut MaybeUninit<T>, len);
let slice = ptr::slice_from_raw_parts_mut(me.ptr() as *mut MaybeUninit<T>, len);
Box::from_raw_in(slice, ptr::read(&me.alloc))
}
}
Expand Down
18 changes: 14 additions & 4 deletions src/tools/miri/tests/pass/box-custom-alloc-aliasing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ unsafe impl Allocator for MyAllocator {
}

unsafe fn deallocate(&self, ptr: NonNull<u8>, _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`!
Expand All @@ -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, A: Allocator>(t: T, a: A) -> Vec<T, A> {
fn v1<T, A: Allocator>(t: T, a: A) -> Vec<T, A> {
(Box::new_in([t], a) as Box<[T], A>).into_vec()
}
fn v2<T, A: Allocator>(t: T, a: A) -> Vec<T, A> {
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::<MyBin>() <= 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));
Expand Down
1 change: 1 addition & 0 deletions tests/ui/hygiene/panic-location.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/hygiene/panic-location.run.stderr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f5e5be5

Please sign in to comment.