Skip to content

Commit 2efcdf8

Browse files
conradludgatesaethlin
authored andcommitted
Replace RawVec<T> with Box<[MaybeUninit<T>]>
Fix soundness problems/make miri-test-libstd pass Co-authored-by: Conrad Ludgate <[email protected]>
1 parent babff22 commit 2efcdf8

22 files changed

+640
-696
lines changed

library/alloc/src/box_storage.rs

+399
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,399 @@
1+
#![unstable(feature = "raw_vec_internals", reason = "unstable const warnings", issue = "none")]
2+
3+
use core::alloc::LayoutError;
4+
use core::cmp;
5+
use core::intrinsics;
6+
use core::mem;
7+
use core::mem::MaybeUninit;
8+
use core::ptr::NonNull;
9+
10+
#[cfg(not(no_global_oom_handling))]
11+
use crate::alloc::handle_alloc_error;
12+
use crate::alloc::{Allocator, Layout};
13+
use crate::boxed::Box;
14+
use crate::collections::TryReserveError;
15+
use crate::collections::TryReserveErrorKind::*;
16+
17+
#[cfg(test)]
18+
mod tests;
19+
20+
#[cfg(not(no_global_oom_handling))]
21+
pub(crate) enum AllocInit {
22+
/// The contents of the new memory are uninitialized.
23+
Uninitialized,
24+
/// The new memory is guaranteed to be zeroed.
25+
Zeroed,
26+
}
27+
28+
pub(crate) trait BoxStorage: Sized {
29+
// Tiny Vecs are dumb. Skip to:
30+
// - 8 if the element size is 1, because any heap allocators is likely
31+
// to round up a request of less than 8 bytes to at least 8 bytes.
32+
// - 4 if elements are moderate-sized (<= 1 KiB).
33+
// - 1 otherwise, to avoid wasting too much space for very short Vecs.
34+
const MIN_NON_ZERO_CAP: usize;
35+
36+
/// Gets the capacity of the allocation.
37+
///
38+
/// This will always be `usize::MAX` if `T` is zero-sized.
39+
fn capacity(&self) -> usize;
40+
41+
/// Ensures that the buffer contains at least enough space to hold `len +
42+
/// additional` elements. If it doesn't already have enough capacity, will
43+
/// reallocate enough space plus comfortable slack space to get amortized
44+
/// *O*(1) behavior. Will limit this behavior if it would needlessly cause
45+
/// itself to panic.
46+
///
47+
/// If `len` exceeds `self.capacity()`, this may fail to actually allocate
48+
/// the requested space. This is not really unsafe, but the unsafe
49+
/// code *you* write that relies on the behavior of this function may break.
50+
///
51+
/// This is ideal for implementing a bulk-push operation like `extend`.
52+
///
53+
/// # Panics
54+
///
55+
/// Panics if the new capacity exceeds `isize::MAX` bytes.
56+
///
57+
/// # Aborts
58+
///
59+
/// Aborts on OOM.
60+
#[cfg(not(no_global_oom_handling))]
61+
#[inline]
62+
fn reserve(&mut self, len: usize, additional: usize) {
63+
// Callers expect this function to be very cheap when there is already sufficient capacity.
64+
// Therefore, we move all the resizing and error-handling logic from grow_amortized and
65+
// handle_reserve behind a call, while making sure that this function is likely to be
66+
// inlined as just a comparison and a call if the comparison fails.
67+
#[cold]
68+
fn do_reserve_and_handle<T: BoxStorage>(slf: &mut T, len: usize, additional: usize) {
69+
handle_reserve(slf.grow_amortized(len, additional));
70+
}
71+
72+
if self.needs_to_grow(len, additional) {
73+
do_reserve_and_handle(self, len, additional);
74+
}
75+
}
76+
77+
/// Returns if the buffer needs to grow to fulfill the needed extra capacity.
78+
/// Mainly used to make inlining reserve-calls possible without inlining `grow`.
79+
fn needs_to_grow(&self, len: usize, additional: usize) -> bool {
80+
additional > self.capacity().wrapping_sub(len)
81+
}
82+
83+
/// A specialized version of `reserve()` used only by the hot and
84+
/// oft-instantiated `Vec::push()`, which does its own capacity check.
85+
#[cfg(not(no_global_oom_handling))]
86+
#[inline(never)]
87+
fn reserve_for_push(&mut self, len: usize) {
88+
handle_reserve(self.grow_amortized(len, 1));
89+
}
90+
91+
/// Shrinks the buffer down to the specified capacity. If the given amount
92+
/// is 0, actually completely deallocates.
93+
///
94+
/// # Panics
95+
///
96+
/// Panics if the given amount is *larger* than the current capacity.
97+
///
98+
/// # Aborts
99+
///
100+
/// Aborts on OOM.
101+
#[cfg(not(no_global_oom_handling))]
102+
fn shrink_to_fit(&mut self, cap: usize) {
103+
handle_reserve(self.shrink(cap));
104+
}
105+
106+
/// The same as `reserve`, but returns on errors instead of panicking or aborting.
107+
fn try_reserve(&mut self, len: usize, additional: usize) -> Result<(), TryReserveError> {
108+
if self.needs_to_grow(len, additional) {
109+
self.grow_amortized(len, additional)
110+
} else {
111+
Ok(())
112+
}
113+
}
114+
115+
/// Ensures that the buffer contains at least enough space to hold `len +
116+
/// additional` elements. If it doesn't already, will reallocate the
117+
/// minimum possible amount of memory necessary. Generally this will be
118+
/// exactly the amount of memory necessary, but in principle the allocator
119+
/// is free to give back more than we asked for.
120+
///
121+
/// If `len` exceeds `self.capacity()`, this may fail to actually allocate
122+
/// the requested space. This is not really unsafe, but the unsafe code
123+
/// *you* write that relies on the behavior of this function may break.
124+
///
125+
/// # Panics
126+
///
127+
/// Panics if the new capacity exceeds `isize::MAX` bytes.
128+
///
129+
/// # Aborts
130+
///
131+
/// Aborts on OOM.
132+
#[cfg(not(no_global_oom_handling))]
133+
fn reserve_exact(&mut self, len: usize, additional: usize) {
134+
handle_reserve(self.try_reserve_exact(len, additional));
135+
}
136+
137+
/// The same as `reserve_exact`, but returns on errors instead of panicking or aborting.
138+
fn try_reserve_exact(&mut self, len: usize, additional: usize) -> Result<(), TryReserveError> {
139+
if self.needs_to_grow(len, additional) { self.grow_exact(len, additional) } else { Ok(()) }
140+
}
141+
142+
fn grow_amortized(&mut self, len: usize, additional: usize) -> Result<(), TryReserveError>;
143+
fn grow_exact(&mut self, len: usize, additional: usize) -> Result<(), TryReserveError>;
144+
fn shrink(&mut self, cap: usize) -> Result<(), TryReserveError>;
145+
}
146+
147+
impl<T, A: Allocator> BoxStorage for Box<[mem::MaybeUninit<T>], A> {
148+
const MIN_NON_ZERO_CAP: usize = if mem::size_of::<T>() == 1 {
149+
8
150+
} else if mem::size_of::<T>() <= 1024 {
151+
4
152+
} else {
153+
1
154+
};
155+
156+
#[inline(always)]
157+
fn capacity(&self) -> usize {
158+
if mem::size_of::<T>() == 0 {
159+
usize::MAX
160+
} else {
161+
unsafe {
162+
let ptr: *const usize = core::mem::transmute(self);
163+
*ptr.add(1)
164+
}
165+
}
166+
}
167+
168+
// This method is usually instantiated many times. So we want it to be as
169+
// small as possible, to improve compile times. But we also want as much of
170+
// its contents to be statically computable as possible, to make the
171+
// generated code run faster. Therefore, this method is carefully written
172+
// so that all of the code that depends on `T` is within it, while as much
173+
// of the code that doesn't depend on `T` as possible is in functions that
174+
// are non-generic over `T`.
175+
fn grow_amortized(&mut self, len: usize, additional: usize) -> Result<(), TryReserveError> {
176+
// This is ensured by the calling contexts.
177+
debug_assert!(additional > 0);
178+
179+
if mem::size_of::<T>() == 0 {
180+
// Since we return a capacity of `usize::MAX` when `elem_size` is
181+
// 0, getting to here necessarily means the boxed-slice is overfull.
182+
return Err(CapacityOverflow.into());
183+
}
184+
185+
// Nothing we can really do about these checks, sadly.
186+
let required_cap = len.checked_add(additional).ok_or(CapacityOverflow)?;
187+
188+
// This guarantees exponential growth. The doubling cannot overflow
189+
// because `cap <= isize::MAX` and the type of `cap` is `usize`.
190+
let cap = self.len();
191+
let cap = cmp::max(cap * 2, required_cap);
192+
let cap = cmp::max(Self::MIN_NON_ZERO_CAP, cap);
193+
194+
replace(self, |current_memory, alloc| {
195+
let new_layout = Layout::array::<T>(cap);
196+
// `finish_grow` is non-generic over `T`.
197+
let ptr = finish_grow(new_layout, current_memory, alloc)?;
198+
Ok(Some((ptr, cap)))
199+
})
200+
}
201+
202+
// The constraints on this method are much the same as those on
203+
// `grow_amortized`, but this method is usually instantiated less often so
204+
// it's less critical.
205+
fn grow_exact(&mut self, len: usize, additional: usize) -> Result<(), TryReserveError> {
206+
if mem::size_of::<T>() == 0 {
207+
// Since we return a capacity of `usize::MAX` when the type size is
208+
// 0, getting to here necessarily means the boxed-slice is overfull.
209+
return Err(CapacityOverflow.into());
210+
}
211+
let cap = len.checked_add(additional).ok_or(CapacityOverflow)?;
212+
213+
replace(self, |current_memory, alloc| {
214+
let new_layout = Layout::array::<T>(cap);
215+
// `finish_grow` is non-generic over `T`.
216+
let ptr = finish_grow(new_layout, current_memory, alloc)?;
217+
Ok(Some((ptr, cap)))
218+
})
219+
}
220+
221+
fn shrink(&mut self, cap: usize) -> Result<(), TryReserveError> {
222+
assert!(cap <= self.capacity(), "Tried to shrink to a larger capacity");
223+
replace(self, |current_memory, alloc| {
224+
let (ptr, layout) = if let Some(mem) = current_memory { mem } else { return Ok(None) };
225+
226+
let ptr = unsafe {
227+
// `Layout::array` cannot overflow here because it would have
228+
// overflowed earlier when capacity was larger.
229+
let new_layout = Layout::array::<T>(cap).unwrap_unchecked();
230+
alloc
231+
.shrink(ptr, layout, new_layout)
232+
.map_err(|_| AllocError { layout: new_layout, non_exhaustive: () })?
233+
};
234+
Ok(Some((ptr, cap)))
235+
})
236+
}
237+
}
238+
239+
pub(crate) unsafe fn storage_from_raw_parts_in<T, A: Allocator>(
240+
ptr: *mut T,
241+
len: usize,
242+
alloc: A,
243+
) -> Box<[MaybeUninit<T>], A> {
244+
unsafe {
245+
let raw = core::slice::from_raw_parts_mut(ptr.cast(), len);
246+
Box::from_raw_in(raw, alloc)
247+
}
248+
}
249+
250+
fn replace<T, A: Allocator>(
251+
dst: &mut Box<[mem::MaybeUninit<T>], A>,
252+
f: impl FnOnce(
253+
Option<(NonNull<u8>, Layout)>,
254+
&A,
255+
) -> Result<Option<(NonNull<[u8]>, usize)>, TryReserveError>,
256+
) -> Result<(), TryReserveError> {
257+
unsafe {
258+
let (old, alloc) = Box::into_raw_with_allocator(core::ptr::read(dst));
259+
let current_memory = slice_layout(&mut *old);
260+
match f(current_memory, &alloc) {
261+
Ok(None) => Ok(()),
262+
Ok(Some((ptr, len))) => {
263+
// hack because we don't have access to box here :()
264+
265+
// Create a raw pointer slice to the new allocation
266+
let raw =
267+
core::ptr::slice_from_raw_parts_mut(ptr.as_ptr().cast::<MaybeUninit<T>>(), len);
268+
269+
// Create a new Box from our new allocation
270+
let this = Box::from_raw_in(raw, alloc);
271+
core::ptr::write(dst, this);
272+
Ok(())
273+
}
274+
Err(err) => Err(err),
275+
}
276+
}
277+
}
278+
279+
fn slice_layout<T>(slice: &mut [MaybeUninit<T>]) -> Option<(NonNull<u8>, Layout)> {
280+
if mem::size_of::<T>() == 0 || slice.len() == 0 {
281+
None
282+
} else {
283+
// We have an allocated chunk of memory, so we can bypass runtime
284+
// checks to get our current layout.
285+
unsafe {
286+
let layout = Layout::array::<T>(slice.len()).unwrap_unchecked();
287+
Some((NonNull::new_unchecked(slice.as_mut_ptr().cast()), layout))
288+
}
289+
}
290+
}
291+
292+
// This function is outside `RawVec` to minimize compile times. See the comment
293+
// above `RawVec::grow_amortized` for details. (The `A` parameter isn't
294+
// significant, because the number of different `A` types seen in practice is
295+
// much smaller than the number of `T` types.)
296+
#[inline(never)]
297+
fn finish_grow<A>(
298+
new_layout: Result<Layout, LayoutError>,
299+
current_memory: Option<(NonNull<u8>, Layout)>,
300+
alloc: &A,
301+
) -> Result<NonNull<[u8]>, TryReserveError>
302+
where
303+
A: Allocator,
304+
{
305+
// Check for the error here to minimize the size of `RawVec::grow_*`.
306+
let new_layout = new_layout.map_err(|_| CapacityOverflow)?;
307+
308+
alloc_guard(new_layout.size())?;
309+
310+
let memory = if let Some((ptr, old_layout)) = current_memory {
311+
debug_assert_eq!(old_layout.align(), new_layout.align());
312+
unsafe {
313+
// The allocator checks for alignment equality
314+
intrinsics::assume(old_layout.align() == new_layout.align());
315+
alloc.grow(ptr, old_layout, new_layout)
316+
}
317+
} else {
318+
alloc.allocate(new_layout)
319+
};
320+
321+
memory.map_err(|_| AllocError { layout: new_layout, non_exhaustive: () }.into())
322+
}
323+
324+
// Central function for reserve error handling.
325+
#[cfg(not(no_global_oom_handling))]
326+
#[inline]
327+
fn handle_reserve(result: Result<(), TryReserveError>) {
328+
match result.map_err(|e| e.kind()) {
329+
Err(CapacityOverflow) => capacity_overflow(),
330+
Err(AllocError { layout, .. }) => handle_alloc_error(layout),
331+
Ok(()) => { /* yay */ }
332+
}
333+
}
334+
335+
// We need to guarantee the following:
336+
// * We don't ever allocate `> isize::MAX` byte-size objects.
337+
// * We don't overflow `usize::MAX` and actually allocate too little.
338+
//
339+
// On 64-bit we just need to check for overflow since trying to allocate
340+
// `> isize::MAX` bytes will surely fail. On 32-bit and 16-bit we need to add
341+
// an extra guard for this in case we're running on a platform which can use
342+
// all 4GB in user-space, e.g., PAE or x32.
343+
344+
#[inline]
345+
pub(crate) fn alloc_guard(alloc_size: usize) -> Result<(), TryReserveError> {
346+
if usize::BITS < 64 && alloc_size > isize::MAX as usize {
347+
Err(CapacityOverflow.into())
348+
} else {
349+
Ok(())
350+
}
351+
}
352+
353+
// One central function responsible for reporting capacity overflows. This'll
354+
// ensure that the code generation related to these panics is minimal as there's
355+
// only one location which panics rather than a bunch throughout the module.
356+
#[cfg(not(no_global_oom_handling))]
357+
pub(crate) fn capacity_overflow() -> ! {
358+
panic!("capacity overflow");
359+
}
360+
361+
#[cfg(not(no_global_oom_handling))]
362+
pub(crate) fn allocate_in<T, A: Allocator>(
363+
capacity: usize,
364+
init: AllocInit,
365+
alloc: A,
366+
) -> Box<[mem::MaybeUninit<T>], A> {
367+
// Don't allocate here because `Drop` will not deallocate when `capacity` is 0.
368+
if capacity == 0 {
369+
Box::empty_in(alloc)
370+
} else if mem::size_of::<T>() == 0 {
371+
unsafe {
372+
storage_from_raw_parts_in(core::ptr::Unique::dangling().as_ptr(), capacity, alloc)
373+
}
374+
} else {
375+
// We avoid `unwrap_or_else` here because it bloats the amount of
376+
// LLVM IR generated.
377+
let layout = match Layout::array::<T>(capacity) {
378+
Ok(layout) => layout,
379+
Err(_) => capacity_overflow(),
380+
};
381+
match alloc_guard(layout.size()) {
382+
Ok(_) => {}
383+
Err(_) => capacity_overflow(),
384+
}
385+
let result = match init {
386+
AllocInit::Uninitialized => alloc.allocate(layout),
387+
AllocInit::Zeroed => alloc.allocate_zeroed(layout),
388+
};
389+
let ptr = match result {
390+
Ok(ptr) => ptr,
391+
Err(_) => handle_alloc_error(layout),
392+
};
393+
394+
// Allocators currently return a `NonNull<[u8]>` whose length
395+
// matches the size requested. If that ever changes, the capacity
396+
// here should change to `ptr.len() / mem::size_of::<T>()`.
397+
unsafe { storage_from_raw_parts_in(ptr.as_ptr().cast(), capacity, alloc) }
398+
}
399+
}

0 commit comments

Comments
 (0)