Skip to content

Commit c2b89eb

Browse files
authored
Rollup merge of rust-lang#62296 - RalfJung:memalign, r=alexcrichton
request at least ptr-size alignment from posix_memalign Fixes rust-lang#62251
2 parents 7a595ee + 2e47fc3 commit c2b89eb

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

src/liballoc/tests/heap.rs

+17-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::alloc::{Global, Alloc, Layout, System};
22

3-
/// Issue #45955.
3+
/// Issue #45955 and #62251.
44
#[test]
55
fn alloc_system_overaligned_request() {
66
check_overalign_requests(System)
@@ -12,21 +12,23 @@ fn std_heap_overaligned_request() {
1212
}
1313

1414
fn check_overalign_requests<T: Alloc>(mut allocator: T) {
15-
let size = 8;
16-
let align = 16; // greater than size
17-
let iterations = 100;
18-
unsafe {
19-
let pointers: Vec<_> = (0..iterations).map(|_| {
20-
allocator.alloc(Layout::from_size_align(size, align).unwrap()).unwrap()
21-
}).collect();
22-
for &ptr in &pointers {
23-
assert_eq!((ptr.as_ptr() as usize) % align, 0,
24-
"Got a pointer less aligned than requested")
25-
}
15+
for &align in &[4, 8, 16, 32] { // less than and bigger than `MIN_ALIGN`
16+
for &size in &[align/2, align-1] { // size less than alignment
17+
let iterations = 128;
18+
unsafe {
19+
let pointers: Vec<_> = (0..iterations).map(|_| {
20+
allocator.alloc(Layout::from_size_align(size, align).unwrap()).unwrap()
21+
}).collect();
22+
for &ptr in &pointers {
23+
assert_eq!((ptr.as_ptr() as usize) % align, 0,
24+
"Got a pointer less aligned than requested")
25+
}
2626

27-
// Clean up
28-
for &ptr in &pointers {
29-
allocator.dealloc(ptr, Layout::from_size_align(size, align).unwrap())
27+
// Clean up
28+
for &ptr in &pointers {
29+
allocator.dealloc(ptr, Layout::from_size_align(size, align).unwrap())
30+
}
31+
}
3032
}
3133
}
3234
}

src/libstd/sys/unix/alloc.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ use crate::alloc::{GlobalAlloc, Layout, System};
66
unsafe impl GlobalAlloc for System {
77
#[inline]
88
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
9+
// jemalloc provides alignment less than MIN_ALIGN for small allocations.
10+
// So only rely on MIN_ALIGN if size >= align.
11+
// Also see <https://github.com/rust-lang/rust/issues/45955> and
12+
// <https://github.com/rust-lang/rust/issues/62251#issuecomment-507580914>.
913
if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
1014
libc::malloc(layout.size()) as *mut u8
1115
} else {
@@ -21,6 +25,7 @@ unsafe impl GlobalAlloc for System {
2125

2226
#[inline]
2327
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
28+
// See the comment above in `alloc` for why this check looks the way it does.
2429
if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
2530
libc::calloc(layout.size(), 1) as *mut u8
2631
} else {
@@ -80,7 +85,10 @@ unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 {
8085
#[inline]
8186
unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 {
8287
let mut out = ptr::null_mut();
83-
let ret = libc::posix_memalign(&mut out, layout.align(), layout.size());
88+
// posix_memalign requires that the alignment be a multiple of `sizeof(void*)`.
89+
// Since these are all powers of 2, we can just use max.
90+
let align = layout.align().max(crate::mem::size_of::<usize>());
91+
let ret = libc::posix_memalign(&mut out, align, layout.size());
8492
if ret != 0 {
8593
ptr::null_mut()
8694
} else {

0 commit comments

Comments
 (0)