-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Use zeroed allocations in the mir interpreter instead eagerly touching the memory #87777
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
Changes from all commits
de91157
83b01b9
f408d4b
6ed2d87
55def12
1c21373
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -157,7 +157,6 @@ use crate::alloc::{handle_alloc_error, WriteCloneIntoRaw}; | |
use crate::alloc::{AllocError, Allocator, Global, Layout}; | ||
#[cfg(not(no_global_oom_handling))] | ||
use crate::borrow::Cow; | ||
#[cfg(not(no_global_oom_handling))] | ||
use crate::raw_vec::RawVec; | ||
#[cfg(not(no_global_oom_handling))] | ||
use crate::str::from_boxed_utf8_unchecked; | ||
|
@@ -589,6 +588,71 @@ impl<T> Box<[T]> { | |
pub fn new_zeroed_slice(len: usize) -> Box<[mem::MaybeUninit<T>]> { | ||
unsafe { RawVec::with_capacity_zeroed(len).into_box(len) } | ||
} | ||
|
||
/// Constructs a new boxed slice with uninitialized contents. Returns an error if | ||
/// the allocation fails | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// #![feature(allocator_api, new_uninit)] | ||
/// | ||
/// let mut values = Box::<[u32]>::try_new_uninit_slice(3)?; | ||
/// let values = unsafe { | ||
/// // Deferred initialization: | ||
/// values[0].as_mut_ptr().write(1); | ||
/// values[1].as_mut_ptr().write(2); | ||
/// values[2].as_mut_ptr().write(3); | ||
/// values.assume_init() | ||
/// }; | ||
/// | ||
/// assert_eq!(*values, [1, 2, 3]); | ||
/// # Ok::<(), std::alloc::AllocError>(()) | ||
/// ``` | ||
#[unstable(feature = "allocator_api", issue = "32838")] | ||
#[inline] | ||
pub fn try_new_uninit_slice(len: usize) -> Result<Box<[mem::MaybeUninit<T>]>, AllocError> { | ||
unsafe { | ||
let layout = match Layout::array::<mem::MaybeUninit<T>>(len) { | ||
Ok(l) => l, | ||
Err(_) => return Err(AllocError), | ||
}; | ||
let ptr = Global.allocate(layout)?; | ||
Ok(RawVec::from_raw_parts_in(ptr.as_mut_ptr() as *mut _, len, Global).into_box(len)) | ||
} | ||
} | ||
|
||
/// Constructs a new boxed slice with uninitialized contents, with the memory | ||
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. Saying 'uninitialized contents' sounds confusing when we also state that the memory is zeroed. I think it would be better to write out 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. I copied that from another |
||
/// being filled with `0` bytes. Returns an error if the allocation fails | ||
/// | ||
/// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and incorrect usage | ||
/// of this method. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// #![feature(allocator_api, new_uninit)] | ||
/// | ||
/// let values = Box::<[u32]>::try_new_zeroed_slice(3)?; | ||
/// let values = unsafe { values.assume_init() }; | ||
/// | ||
/// assert_eq!(*values, [0, 0, 0]); | ||
/// # Ok::<(), std::alloc::AllocError>(()) | ||
/// ``` | ||
/// | ||
/// [zeroed]: mem::MaybeUninit::zeroed | ||
#[unstable(feature = "allocator_api", issue = "32838")] | ||
#[inline] | ||
pub fn try_new_zeroed_slice(len: usize) -> Result<Box<[mem::MaybeUninit<T>]>, AllocError> { | ||
the8472 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
unsafe { | ||
let layout = match Layout::array::<mem::MaybeUninit<T>>(len) { | ||
Ok(l) => l, | ||
Err(_) => return Err(AllocError), | ||
}; | ||
let ptr = Global.allocate_zeroed(layout)?; | ||
Ok(RawVec::from_raw_parts_in(ptr.as_mut_ptr() as *mut _, len, Global).into_box(len)) | ||
} | ||
} | ||
} | ||
|
||
impl<T, A: Allocator> Box<[T], A> { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Turns out changing this from
Vec
toBox
introduced UB into the interpreter itself... also see Zulip