Skip to content
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

Fix Rc/Arc allocation layout #55764

Merged
merged 1 commit into from
Nov 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/liballoc/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,14 +672,16 @@ impl<T: ?Sized> Rc<T> {
// Previously, layout was calculated on the expression
// `&*(ptr as *const RcBox<T>)`, but this created a misaligned
// reference (see #54908).
let (layout, _) = Layout::new::<RcBox<()>>()
.extend(Layout::for_value(&*ptr)).unwrap();
let layout = Layout::new::<RcBox<()>>()
.extend(Layout::for_value(&*ptr)).unwrap().0
.pad_to_align().unwrap();

let mem = Global.alloc(layout)
.unwrap_or_else(|_| handle_alloc_error(layout));

// Initialize the RcBox
let inner = set_data_ptr(ptr as *mut T, mem.as_ptr() as *mut u8) as *mut RcBox<T>;
debug_assert_eq!(Layout::for_value(&*inner), layout);

ptr::write(&mut (*inner).strong, Cell::new(1));
ptr::write(&mut (*inner).weak, Cell::new(1));
Expand Down
6 changes: 4 additions & 2 deletions src/liballoc/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,14 +575,16 @@ impl<T: ?Sized> Arc<T> {
// Previously, layout was calculated on the expression
// `&*(ptr as *const ArcInner<T>)`, but this created a misaligned
// reference (see #54908).
let (layout, _) = Layout::new::<ArcInner<()>>()
.extend(Layout::for_value(&*ptr)).unwrap();
let layout = Layout::new::<ArcInner<()>>()
.extend(Layout::for_value(&*ptr)).unwrap().0
.pad_to_align().unwrap();

let mem = Global.alloc(layout)
.unwrap_or_else(|_| handle_alloc_error(layout));

// Initialize the ArcInner
let inner = set_data_ptr(ptr as *mut T, mem.as_ptr() as *mut u8) as *mut ArcInner<T>;
debug_assert_eq!(Layout::for_value(&*inner), layout);

ptr::write(&mut (*inner).strong, atomic::AtomicUsize::new(1));
ptr::write(&mut (*inner).weak, atomic::AtomicUsize::new(1));
Expand Down
17 changes: 17 additions & 0 deletions src/libcore/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,23 @@ impl Layout {
len_rounded_up.wrapping_sub(len)
}

/// Creates a layout by rounding the size of this layout up to a multiple
/// of the layout's alignment.
///
/// Returns `Err` if the padded size would overflow.
///
/// This is equivalent to adding the result of `padding_needed_for`
/// to the layout's current size.
#[unstable(feature = "alloc_layout_extra", issue = "55724")]
#[inline]
pub fn pad_to_align(&self) -> Result<Layout, LayoutErr> {
let pad = self.padding_needed_for(self.align());
let new_size = self.size().checked_add(pad)
.ok_or(LayoutErr { private: () })?;

Layout::from_size_align(new_size, self.align())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be from_size_align_unchecked, right?

}

/// Creates a layout describing the record for `n` instances of
/// `self`, with a suitable amount of padding between each to
/// ensure that each instance is given its requested size and
Expand Down