commit 18445d661e14752d320f3afa16dcb6e880451439
parent 45c7dd1b7a0bc76b71801257538c4586c56e1e08
Author: Yersa Nordman <finwo@pm.me>
Date: Wed, 18 Aug 2021 18:07:27 +0200
sbrk now calls upon __builtin_wasm_memory_grow for wasm32
Diffstat:
5 files changed, 25 insertions(+), 2 deletions(-)
diff --git a/Makefile b/Makefile
@@ -11,7 +11,7 @@ INCLUDES+=-Iarch/$(TARGET)/include
INCLUDES+=-Iinclude
CFLAGS?=
-CFLAGS+=-fno-builtin -nostdinc
+CFLAGS+=-nostdinc -Wl,--allow-undefined
libmatter.a: $(OBJ)
rm -f $@
diff --git a/arch/wasm32/config.mk b/arch/wasm32/config.mk
@@ -1,4 +1,4 @@
-CC=clang --target=wasm32
+CC=clang --target=wasm32-unknown-unknown-wasm
LLE=$(CC) -emit-llvm
LLC=llc -march=wasm32
AR=llvm-ar
diff --git a/arch/wasm32/include/matter/page_size.h b/arch/wasm32/include/matter/page_size.h
@@ -0,0 +1,6 @@
+#ifndef _MATTER_PAGE_SIZE_H_
+#define _MATTER_PAGE_SIZE_H_
+
+#define PAGE_SIZE (64*1024)
+
+#endif
diff --git a/arch/wasm32/src/unistd/brk.c b/arch/wasm32/src/unistd/brk.c
@@ -1,8 +1,11 @@
#ifndef _UNISTD_BRK_C_
#define _UNISTD_BRK_C_
+#include <stddef.h>
#include <unistd.h>
+#include <matter/page_size.h>
+
extern unsigned char __heap_base;
void *break_pointer = &__heap_base;
@@ -12,8 +15,16 @@ int brk(void *addr) {
}
void * sbrk(ssize_t increment) {
+ size_t current_pages = __builtin_wasm_memory_size(0);
+ void *heap_limit = current_pages * PAGE_SIZE;
+
void *ret = break_pointer;
break_pointer += increment;
+
+ if (break_pointer > heap_limit) {
+ __builtin_wasm_memory_grow(0, ((break_pointer - heap_limit) / PAGE_SIZE) + 1);
+ }
+
return ret;
}
diff --git a/include/matter/page_size.h b/include/matter/page_size.h
@@ -0,0 +1,6 @@
+#ifndef _MATTER_PAGE_SIZE_H_
+#define _MATTER_PAGE_SIZE_H_
+
+#define PAGE_SIZE (4*1024)
+
+#endif