From eac35edfd1101e8f7c34dbdd7b595fdac8f0ad4c Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Tue, 18 Feb 2025 16:06:36 +0000 Subject: [PATCH] [wasm] Stop using mprotect(PROT_NONE) on WASI we had been using a stub weak definition of `mprotect` in wasm/missing.c so far, but wasi-sdk 23 added mprotect emulation to wasi-libc[^1], so the emulation is now linked instead. However, the emulation doesn't support PROT_NONE and fails with ENOSYS, so we need to avoid calling mprotect completely on WASI. [^1]: https://github.com/WebAssembly/wasi-libc/commit/7528b13170462c82e367d91ae0ecead84e470ceb --- cont.c | 3 +++ gc/default/default.c | 4 ++++ wasm/missing.c | 7 ------- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/cont.c b/cont.c index 929bbd06c1..072ae4562f 100644 --- a/cont.c +++ b/cont.c @@ -551,6 +551,9 @@ fiber_pool_expand(struct fiber_pool * fiber_pool, size_t count) VirtualFree(allocation->base, 0, MEM_RELEASE); rb_raise(rb_eFiberError, "can't set a guard page: %s", ERRNOMSG); } +#elif defined(__wasi__) + // wasi-libc's mprotect emulation doesn't support PROT_NONE. + (void)page; #else if (mprotect(page, RB_PAGE_SIZE, PROT_NONE) < 0) { munmap(allocation->base, count*stride); diff --git a/gc/default/default.c b/gc/default/default.c index 7e0049bb4f..4cf39a0331 100644 --- a/gc/default/default.c +++ b/gc/default/default.c @@ -3199,6 +3199,10 @@ protect_page_body(struct heap_page_body *body, DWORD protect) DWORD old_protect; return VirtualProtect(body, HEAP_PAGE_SIZE, protect, &old_protect) != 0; } +#elif defined(__wasi__) +// wasi-libc's mprotect emulation does not support PROT_NONE +enum {HEAP_PAGE_LOCK, HEAP_PAGE_UNLOCK}; +#define protect_page_body(body, protect) 1 #else enum {HEAP_PAGE_LOCK = PROT_NONE, HEAP_PAGE_UNLOCK = PROT_READ | PROT_WRITE}; #define protect_page_body(body, protect) !mprotect((body), HEAP_PAGE_SIZE, (protect)) diff --git a/wasm/missing.c b/wasm/missing.c index 5bbf988642..b9ecf520fd 100644 --- a/wasm/missing.c +++ b/wasm/missing.c @@ -119,13 +119,6 @@ umask(rb_mode_t mask) return 0; } -WASM_MISSING_LIBC_FUNC -int -mprotect(const void *addr, size_t len, int prot) -{ - return 0; -} - WASM_MISSING_LIBC_FUNC int pclose(FILE *stream)