Auto merge of #115254 - cuviper:aligned_alloc-size, r=thomcc

wasi: round up the size for `aligned_alloc`

C11 `aligned_alloc` requires that the size be a multiple of the
alignment. This is enforced in the wasi-libc emmalloc implementation,
which always returns NULL if the size is not a multiple.
(The default `MALLOC_IMPL=dlmalloc` does not currently check this.)
This commit is contained in:
bors 2023-08-28 00:02:42 +00:00
commit 1baf77aad0

View File

@ -86,7 +86,11 @@ unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 {
} else if #[cfg(target_os = "wasi")] { } else if #[cfg(target_os = "wasi")] {
#[inline] #[inline]
unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 { unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 {
libc::aligned_alloc(layout.align(), layout.size()) as *mut u8 // C11 aligned_alloc requires that the size be a multiple of the alignment.
// Layout already checks that the size rounded up doesn't overflow isize::MAX.
let align = layout.align();
let size = layout.size().next_multiple_of(align);
libc::aligned_alloc(align, size) as *mut u8
} }
} else { } else {
#[inline] #[inline]