Skip to content

Commit

Permalink
Don't use WASI emulated mmap
Browse files Browse the repository at this point in the history
It does not return aligned memory (WebAssembly/wasi-libc#207) and we don't need it for anything fancy.

Instead, just use posix_memalign() and free() for the GC functions.
  • Loading branch information
agoode committed Feb 25, 2024
1 parent 62c1ffb commit 9eb4b36
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 22 deletions.
2 changes: 0 additions & 2 deletions bin/mlton-script
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ for arg in "$@"; do
-target-cc-opt wasi '-D_WASI_EMULATED_SIGNAL' \
-target-cc-opt wasi '-D_WASI_EMULATED_PROCESS_CLOCKS' \
-target-cc-opt wasi '-D_WASI_EMULATED_GETPID' \
-target-cc-opt wasi '-D_WASI_EMULATED_MMAN' \
-target-cc-opt x86 '-m32' \
-target-link-opt aix '-maix64' \
-target-link-opt alpha \
Expand All @@ -150,7 +149,6 @@ for arg in "$@"; do
-target-link-opt wasi '-lwasi-emulated-signal' \
-target-link-opt wasi '-lwasi-emulated-process-clocks' \
-target-link-opt wasi '-lwasi-emulated-getpid' \
-target-link-opt wasi '-lwasi-emulated-mman' \
-target-link-opt x86 '-m32' \
-profile-exclude '\$\(SML_LIB\)'
fi
Expand Down
6 changes: 2 additions & 4 deletions runtime/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,10 @@ ifeq ($(TARGET_OS), wasi)
WASMTIME := wasmtime
XCPPFLAGS += -D_WASI_EMULATED_SIGNAL \
-D_WASI_EMULATED_PROCESS_CLOCKS \
-D_WASI_EMULATED_GETPID \
-D_WASI_EMULATED_MMAN
-D_WASI_EMULATED_GETPID
XLDFLAGS += -lwasi-emulated-signal \
-lwasi-emulated-process-clocks \
-lwasi-emulated-getpid \
-lwasi-emulated-mman
-lwasi-emulated-getpid
endif

ifeq ($(TARGET_OS), mingw)
Expand Down
28 changes: 14 additions & 14 deletions runtime/platform/wasi.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#include "platform.h"

#include "platform/mremap.c"
#include "platform/use-mmap.c"

/* WASI only implements a subset of POSIX, and given how it works it doesn't
* make too much sense to try too hard to emulate missing functionality.
*
Expand All @@ -18,13 +15,7 @@
*/

size_t GC_pageSize (void) {
long int pageSize;

pageSize = sysconf (_SC_PAGESIZE);
if (pageSize < 0)
diee ("GC_pageSize error: sysconf (_SC_PAGESIZE) failed");

return (size_t)pageSize;
return PAGESIZE;
}

uintmax_t GC_physMem (void) {
Expand All @@ -34,12 +25,21 @@ uintmax_t GC_physMem (void) {
return 1 << 30; /* 1 GiB */
}

void* GC_extendHead (void *base, size_t length) {
return mmapAnon (base, length);
void *GC_mmapAnon (__attribute__ ((unused)) void *start, size_t length) {
void *mem;
int err = posix_memalign (&mem, PAGESIZE, length);
if (err) {
return (void *) -1;
}
return memset(mem, 0, length);
}

void *GC_mmapAnonFlags (void *start, size_t length, __attribute__ ((unused)) int flags) {
return GC_mmapAnon(start, length);
}

void* GC_extendTail (void *base, size_t length) {
return mmapAnon (base, length);
void GC_release (void *base, __attribute__ ((unused)) size_t length) {
free (base);
}

void GC_displayMem (void) {
Expand Down
3 changes: 1 addition & 2 deletions runtime/platform/wasi.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <netinet/tcp.h>
#include <poll.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <sys/stat.h>
Expand All @@ -20,7 +19,7 @@

#define HAS_FEROUND TRUE
#define HAS_MSG_DONTWAIT TRUE
#define HAS_REMAP TRUE
#define HAS_REMAP FALSE
#define HAS_SIGALTSTACK FALSE
#define NEEDS_SIGALTSTACK_EXEC FALSE
#define HAS_SPAWN FALSE
Expand Down

0 comments on commit 9eb4b36

Please sign in to comment.