Skip to content

Commit

Permalink
wasi: round up the size for aligned_alloc
Browse files Browse the repository at this point in the history
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.)
  • Loading branch information
cuviper committed Aug 26, 2023
1 parent 22d41ae commit 1c6d867
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion library/std/src/sys/unix/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ cfg_if::cfg_if! {
} else if #[cfg(target_os = "wasi")] {
#[inline]
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 {
#[inline]
Expand Down

0 comments on commit 1c6d867

Please sign in to comment.