Skip to content

Commit 77bf60d

Browse files
committed
Use mmap std.heap.page_allocator impl when compiling for Wasm + libc
When linking libc, it should be the libc that manages the heap. The main Wasm memory might have been configured as non-growable, which makes `WasmAllocator` a poor default and causes the common `DebugAllocator` use case fail with OOM errors unless the user uses `std_options` to override the default page allocator. Additionally, on Emscripten, growing Wasm memory without notifying the JS glue code will cause array buffers to get detached and lead to spurious crashes.
1 parent 0aa71f3 commit 77bf60d

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

lib/std/c.zig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1812,6 +1812,15 @@ pub const POLL = switch (native_os) {
18121812
pub const PROT = switch (native_os) {
18131813
.linux => linux.PROT,
18141814
.emscripten => emscripten.PROT,
1815+
.wasi => struct {
1816+
// Match `PROT_*` constants from lib/libc/include/wasm-wasi-musl/sys/mman.h
1817+
pub const NONE = 0x0;
1818+
pub const READ = 0x1;
1819+
pub const WRITE = 0x2;
1820+
pub const EXEC = 0x4;
1821+
pub const GROWSDOWN = 0x01000000;
1822+
pub const GROWSUP = 0x02000000;
1823+
},
18151824
// https://github.com/SerenityOS/serenity/blob/6d59d4d3d9e76e39112842ec487840828f1c9bfe/Kernel/API/POSIX/sys/mman.h#L28-L31
18161825
.openbsd, .haiku, .dragonfly, .netbsd, .illumos, .freebsd, .windows, .serenity => struct {
18171826
/// page can not be accessed

lib/std/heap.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ pub const page_allocator: Allocator = if (@hasDecl(root, "os") and
356356
@hasDecl(root.os, "heap") and
357357
@hasDecl(root.os.heap, "page_allocator"))
358358
root.os.heap.page_allocator
359-
else if (builtin.target.cpu.arch.isWasm()) .{
359+
else if (builtin.target.cpu.arch.isWasm() and !builtin.link_libc) .{
360360
.ptr = undefined,
361361
.vtable = &WasmAllocator.vtable,
362362
} else if (builtin.target.os.tag == .plan9) .{

0 commit comments

Comments
 (0)