Skip to content

Commit 3f1e290

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 d05156e commit 3f1e290

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

lib/std/c.zig

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1830,7 +1830,16 @@ pub const POLL = switch (native_os) {
18301830
/// Basic memory protection flags
18311831
pub const PROT = switch (native_os) {
18321832
.linux => linux.PROT,
1833-
.emscripten => emscripten.PROT,
1833+
// https://github.com/emscripten-core/emscripten/blob/08e2de1031913e4ba7963b1c56f35f036a7d4d56/system/lib/libc/musl/include/sys/mman.h#L57-L62
1834+
// lib/libc/include/wasm-wasi-musl/sys/mman.h
1835+
.emscripten, .wasi => struct {
1836+
pub const NONE = 0;
1837+
pub const READ = 1;
1838+
pub const WRITE = 2;
1839+
pub const EXEC = 4;
1840+
pub const GROWSDOWN = 0x01000000;
1841+
pub const GROWSUP = 0x02000000;
1842+
},
18341843
// https://github.com/SerenityOS/serenity/blob/6d59d4d3d9e76e39112842ec487840828f1c9bfe/Kernel/API/POSIX/sys/mman.h#L28-L31
18351844
.openbsd, .haiku, .dragonfly, .netbsd, .illumos, .freebsd, .windows, .serenity => struct {
18361845
/// page can not be accessed
@@ -8692,7 +8701,9 @@ pub const O = switch (native_os) {
86928701

86938702
pub const MAP = switch (native_os) {
86948703
.linux => linux.MAP,
8695-
.emscripten => packed struct(u32) {
8704+
// https://github.com/emscripten-core/emscripten/blob/08e2de1031913e4ba7963b1c56f35f036a7d4d56/system/lib/libc/musl/include/sys/mman.h#L21-L39
8705+
// lib/libc/include/wasm-wasi-musl/sys/mman.h
8706+
.emscripten, .wasi => packed struct(u32) {
86968707
TYPE: enum(u4) {
86978708
SHARED = 0x01,
86988709
PRIVATE = 0x02,

lib/std/heap.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ pub const page_allocator: Allocator = if (@hasDecl(root, "os") and
366366
@hasDecl(root.os, "heap") and
367367
@hasDecl(root.os.heap, "page_allocator"))
368368
root.os.heap.page_allocator
369-
else if (builtin.target.cpu.arch.isWasm()) .{
369+
else if (builtin.target.cpu.arch.isWasm() and !builtin.link_libc) .{
370370
.ptr = undefined,
371371
.vtable = &WasmAllocator.vtable,
372372
} else if (builtin.target.os.tag == .plan9) .{

lib/std/os/emscripten.zig

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -331,15 +331,6 @@ pub const POLL = struct {
331331
pub const RDBAND = 0x080;
332332
};
333333

334-
pub const PROT = struct {
335-
pub const NONE = 0x0;
336-
pub const READ = 0x1;
337-
pub const WRITE = 0x2;
338-
pub const EXEC = 0x4;
339-
pub const GROWSDOWN = 0x01000000;
340-
pub const GROWSUP = 0x02000000;
341-
};
342-
343334
pub const rlim_t = u64;
344335

345336
pub const RLIM = struct {

0 commit comments

Comments
 (0)