Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 4K wasm page with gocr example #96

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions PATCH-README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@


To run gocr with 4k pages

1. ./patch-wasi-libc.sh
2. make -C applications dist/gocr.awsm
3. ./applications/dist/gocr.awsm <applications/wasm_apps/gocr/examples/5x8.pnm

6 changes: 3 additions & 3 deletions applications/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ AWSMCC=../target/release/awsm
CC=clang

# Used by aWsm when compiling the *.wasm to *.bc
AWSMFLAGS= --inline-constant-globals
AWSMFLAGS=

RUNTIME_PATH=../runtime
RUNTIME_INCLUDES=-I${RUNTIME_PATH}/libc/wasi/include -I${RUNTIME_PATH}/thirdparty/dist/include
Expand All @@ -12,7 +12,7 @@ WASI_CPATH+=${RUNTIME_PATH}/libc/wasi/wasi_main.c
WASI_CPATH+=${RUNTIME_PATH}/libc/wasi/wasi_backing.c
WASI_CPATH+=${RUNTIME_PATH}/libc/wasi/wasi_impl_uvwasi.c
WASI_CPATH+=${RUNTIME_PATH}/libc/env.c
WASI_CPATH+=${RUNTIME_PATH}/memory/64bit_nix.c
WASI_CPATH+=${RUNTIME_PATH}/memory/no_protection.c
WASI_CPATH+=${RUNTIME_PATH}/thirdparty/dist/lib/libuv_a.a
WASI_CPATH+=${RUNTIME_PATH}/thirdparty/dist/lib/libuvwasi_a.a

Expand Down Expand Up @@ -70,7 +70,7 @@ dist/unreachable.awsm: dist/unreachable.bc unreachable/main.c ${RUNTIME_PATH}/ru
${CC} -lm -O3 -flto $^ -o $@

dist/%.awsm: dist/%.bc ${WASI_CPATH}
${CC} -pthread -ldl -lm -O3 -flto -g ${RUNTIME_INCLUDES} $^ -o $@
${CC} -pthread -ldl -lm -O0 -flto -g3 ${RUNTIME_INCLUDES} $^ -o $@

.PHONY: all.awsm
all.awsm: \
Expand Down
2 changes: 1 addition & 1 deletion applications/wasm_apps
Submodule wasm_apps updated 5 files
+1 −0 Makefile
+7 −1 fibonacci/main.c
+1 −1 gocr
+6 −8 html/Makefile
+38 −11 html/main.c
27 changes: 27 additions & 0 deletions patch-wasi-libc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

git clone https://github.com/WebAssembly/wasi-libc.git
pushd wasi-libc
git reset --hard
git clean -f
git checkout 9886d3d6200fcc3726329966860fc058707406cd
popd

# Resize Wasm pages from 64k to 4k
mv wasi-libc/libc-bottom-half/headers/public/__macro_PAGESIZE.h wasi-libc/libc-bottom-half/headers/public/__macro_PAGESIZE.h.old
mv wasi-libc/expected/wasm32-wasi/predefined-macros.txt wasi-libc/expected/wasm32-wasi/predefined-macros.txt.old

sed "s/#define PAGESIZE (0x10000)/#define PAGESIZE (0x400)/" \
wasi-libc/libc-bottom-half/headers/public/__macro_PAGESIZE.h.old \
>wasi-libc/libc-bottom-half/headers/public/__macro_PAGESIZE.h

sed "s/#define PAGESIZE (0x10000)/#define PAGESIZE (0x400)/" \
wasi-libc/expected/wasm32-wasi/predefined-macros.txt.old \
>wasi-libc/expected/wasm32-wasi/predefined-macros.txt

rm wasi-libc/libc-bottom-half/headers/public/__macro_PAGESIZE.h.old
rm wasi-libc/expected/wasm32-wasi/predefined-macros.txt.old

make -C wasi-libc CC=/usr/bin/clang-13 \
AR=/usr/bin/llvm-ar-13 \
NM=/usr/bin/llvm-nm-13
17 changes: 2 additions & 15 deletions runtime/memory/no_protection.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,14 @@ void alloc_linear_memory() {
memory_size = starting_pages * WASM_PAGE_SIZE;
}

void expand_memory() {
awsm_assert(memory_size / WASM_PAGE_SIZE < max_pages);

memory = realloc(memory, memory_size + WASM_PAGE_SIZE);
awsm_assert(memory);

char* mem_as_chars = memory;
memset(&mem_as_chars[memory_size], 0, WASM_PAGE_SIZE);
memory_size += WASM_PAGE_SIZE;
}

i32 instruction_memory_size() {
return memory_size / WASM_PAGE_SIZE;
}

i32 instruction_memory_grow(i32 count) {
i32 prev_size = instruction_memory_size();
for (int i = 0; i < count; i++) {
expand_memory();
}

memory = realloc(memory, memory_size + (WASM_PAGE_SIZE * count));
memory_size += (WASM_PAGE_SIZE * count);
return prev_size;
}

Expand Down
23 changes: 3 additions & 20 deletions runtime/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ void env___cxa_pure_virtual() {

// Region initialization helper function
EXPORT void initialize_region(u32 offset, u32 data_count, char* data) {
awsm_assert(memory_size >= data_count);
awsm_assert(offset < memory_size - data_count);
// awsm_assert(offset <= memory_size - data_count);
if (offset + data_count >= memory_size) {
instruction_memory_grow(offset + data_count - memory_size);
}

// FIXME: Hack around segmented and unsegmented access
memcpy(get_memory_ptr_for_runtime(offset, data_count), data, data_count);
Expand Down Expand Up @@ -289,23 +289,6 @@ __attribute__((noreturn)) void awsm_abi__trap_unreachable() {
WEAK u32 wasmg___heap_base = 0;
u32 runtime_heap_base;

u32 allocate_n_bytes(u32 n) {
awsm_assert(memory_size > 0);
u32 res = runtime_heap_base;
runtime_heap_base += n;
while (memory_size < runtime_heap_base) {
expand_memory();
}
printf("rhb %d\n", runtime_heap_base);
return res;
}

void* allocate_n_bytes_ptr(u32 n) {
awsm_assert(memory_size > 0);
u32 addr = allocate_n_bytes(n);
return get_memory_ptr_for_runtime(addr, n);
}

// If we are using runtime globals, we need to populate them
WEAK void populate_globals() {}

Expand Down
4 changes: 2 additions & 2 deletions runtime/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ float truncf(float x);
#define UINT64_MAX (0xffffffffffffffff)
#endif

#define WASM_PAGE_SIZE (1024 * 64)
#define WASM_PAGE_SIZE (0x1000)

// The code generator compiles in the starting number of wasm pages, and the maximum number of pages
// If we try and allocate more than max_pages, we should fault
Expand Down Expand Up @@ -128,7 +128,7 @@ INLINE void set_f32(u32 offset, float);
INLINE void set_f64(u32 offset, double);

i32 instruction_memory_size();
i32 instruction_memory_grow();
i32 instruction_memory_grow(i32);

static inline void* get_memory_ptr_void(u32 offset, u32 bounds_check) {
return (void*)get_memory_ptr_for_runtime(offset, bounds_check);
Expand Down