Skip to content

Commit 20338a5

Browse files
committed
Auto merge of #49573 - glandium:huge-align, r=SimonSapin
Reject huge alignments on macos with system allocator only ef8804b addressed #30170 by rejecting huge alignments at the allocator API level, transforming a specific platform bug/limitation into an enforced API limitation on all platforms. This change essentially reverts that commit, and instead makes alloc() itself return AllocErr::Unsupported when receiving huge alignments. This was discussed in #32838 (comment) and following.
2 parents 199b7e2 + 98175a8 commit 20338a5

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

src/liballoc_system/lib.rs

+8
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ mod platform {
131131
let ptr = if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
132132
libc::malloc(layout.size()) as *mut u8
133133
} else {
134+
#[cfg(target_os = "macos")]
135+
{
136+
if layout.align() > (1 << 31) {
137+
return Err(AllocErr::Unsupported {
138+
details: "requested alignment too large"
139+
})
140+
}
141+
}
134142
aligned_malloc(&layout)
135143
};
136144
if !ptr.is_null() {

src/libcore/heap.rs

+7-14
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,11 @@ pub struct Layout {
6565

6666
impl Layout {
6767
/// Constructs a `Layout` from a given `size` and `align`,
68-
/// or returns `None` if any of the following conditions
68+
/// or returns `None` if either of the following conditions
6969
/// are not met:
7070
///
7171
/// * `align` must be a power of two,
7272
///
73-
/// * `align` must not exceed 2<sup>31</sup> (i.e. `1 << 31`),
74-
///
7573
/// * `size`, when rounded up to the nearest multiple of `align`,
7674
/// must not overflow (i.e. the rounded value must be less than
7775
/// `usize::MAX`).
@@ -81,10 +79,6 @@ impl Layout {
8179
return None;
8280
}
8381

84-
if align > (1 << 31) {
85-
return None;
86-
}
87-
8882
// (power-of-two implies align != 0.)
8983

9084
// Rounded up size is:
@@ -113,9 +107,8 @@ impl Layout {
113107
/// # Safety
114108
///
115109
/// This function is unsafe as it does not verify that `align` is
116-
/// a power-of-two that is also less than or equal to 2<sup>31</sup>, nor
117-
/// that `size` aligned to `align` fits within the address space
118-
/// (i.e. the `Layout::from_size_align` preconditions).
110+
/// a power-of-two nor `size` aligned to `align` fits within the
111+
/// address space (i.e. the `Layout::from_size_align` preconditions).
119112
#[inline]
120113
pub unsafe fn from_size_align_unchecked(size: usize, align: usize) -> Layout {
121114
Layout { size: size, align: align }
@@ -220,10 +213,10 @@ impl Layout {
220213
let padded_size = self.size.checked_add(self.padding_needed_for(self.align))?;
221214
let alloc_size = padded_size.checked_mul(n)?;
222215

223-
// We can assume that `self.align` is a power-of-two that does
224-
// not exceed 2<sup>31</sup>. Furthermore, `alloc_size` has already been
225-
// rounded up to a multiple of `self.align`; therefore, the
226-
// call to `Layout::from_size_align` below should never panic.
216+
// We can assume that `self.align` is a power-of-two.
217+
// Furthermore, `alloc_size` has already been rounded up
218+
// to a multiple of `self.align`; therefore, the call to
219+
// `Layout::from_size_align` below should never panic.
227220
Some((Layout::from_size_align(alloc_size, self.align).unwrap(), padded_size))
228221
}
229222

0 commit comments

Comments
 (0)