mailing list of musl libc
 help / color / mirror / code / Atom feed
* [PATCH] Wasm support patch 1 (support systems without mmap)
@ 2017-11-28 11:50 Nicholas Wilson
  2017-11-28 15:46 ` Rich Felker
  0 siblings, 1 reply; 4+ messages in thread
From: Nicholas Wilson @ 2017-11-28 11:50 UTC (permalink / raw)
  To: musl

I'm hoping the first patch is uncontroversial.

WebAssembly has a linear/flat memory model, whereby it's simply impossible for the addressable memory to contain "holes". Therefore, mmap can't really be emulated, and all memory has to be allocated via brk.

I've done this by allowing malloc to fall back to brk (even for allocations above MMAP_THRESHOLD) if mmap returns ENOSYS.

It's one line of code that will do harm in "normal" systems, and allows platforms that are emulating syscalls to choose not to support mmap, and still get a working malloc if brk is working.

All the best,
Nick


diff --git a/src/malloc/malloc.c b/src/malloc/malloc.c
index 9e05e1d6..572232e1 100644
--- a/src/malloc/malloc.c
+++ b/src/malloc/malloc.c
@@ -328,13 +328,17 @@ void *malloc(size_t n)
                size_t len = n + OVERHEAD + PAGE_SIZE - 1 & -PAGE_SIZE;
                char *base = __mmap(0, len, PROT_READ|PROT_WRITE,
                        MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
-               if (base == (void *)-1) return 0;
+               if (base == MAP_FAILED) {
+                       if (errno == ENOSYS) goto nommap;
+                       return 0;
+               }
                c = (void *)(base + SIZE_ALIGN - OVERHEAD);
                c->csize = len - (SIZE_ALIGN - OVERHEAD);
                c->psize = SIZE_ALIGN - OVERHEAD;
                return CHUNK_TO_MEM(c);
        }
 
+nommap:
        i = bin_index_up(n);
        for (;;) {
                uint64_t mask = mal.binmap & -(1ULL<<i);
@@ -405,7 +409,7 @@ void *realloc(void *p, size_t n)
                newlen = (newlen + PAGE_SIZE-1) & -PAGE_SIZE;
                if (oldlen == newlen) return p;
                base = __mremap(base, oldlen, newlen, MREMAP_MAYMOVE);
-               if (base == (void *)-1)
+               if (base == MAP_FAILED)
                        goto copy_realloc;
                self = (void *)(base + extra);
                self->csize = newlen - extra;

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2017-11-28 19:20 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-11-28 11:50 [PATCH] Wasm support patch 1 (support systems without mmap) Nicholas Wilson
2017-11-28 15:46 ` Rich Felker
2017-11-28 16:34   ` Nicholas Wilson
2017-11-28 19:20     ` Rich Felker

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).