diff --git a/src/mono/mono/utils/CMakeLists.txt b/src/mono/mono/utils/CMakeLists.txt index 4d552b6e033dd9..92f59c08a75916 100644 --- a/src/mono/mono/utils/CMakeLists.txt +++ b/src/mono/mono/utils/CMakeLists.txt @@ -14,6 +14,9 @@ if(HOST_WIN32 AND HOST_AMD64) set(CMAKE_ASM_MASM_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreadedDebugDLL "") list(APPEND utils_win32_sources win64.asm) +elseif(HOST_WASI) + set (CMAKE_ASM_COMPILER_VERSION "${CMAKE_C_COMPILER_VERSION}") + enable_language(ASM) endif() set(utils_unix_sources @@ -23,6 +26,8 @@ set(utils_unix_sources if(HOST_WIN32) set(utils_platform_sources ${utils_win32_sources}) +elseif(HOST_WASI) + set(utils_platform_sources ${utils_unix_sources} mono-threads-wasi.S) else() set(utils_platform_sources ${utils_unix_sources}) endif() diff --git a/src/mono/mono/utils/mono-threads-wasi.S b/src/mono/mono/utils/mono-threads-wasi.S new file mode 100644 index 00000000000000..f2669f19d5e9b6 --- /dev/null +++ b/src/mono/mono/utils/mono-threads-wasi.S @@ -0,0 +1,16 @@ +.globl get_wasm_stack_high +.globl get_wasm_stack_low + +get_wasm_stack_high: + .functype get_wasm_stack_high () -> (i32) + global.get __data_end@GOT + // TODO after https://github.com/llvm/llvm-project/commit/1532be98f99384990544bd5289ba339bca61e15b + // global.get __stack_high@GOT + end_function + +get_wasm_stack_low: + .functype get_wasm_stack_low () -> (i32) + global.get __heap_base@GOT + // TODO after https://github.com/llvm/llvm-project/commit/1532be98f99384990544bd5289ba339bca61e15b + // global.get __stack_low@GOT + end_function diff --git a/src/mono/mono/utils/mono-threads-wasm.c b/src/mono/mono/utils/mono-threads-wasm.c index 436974f947db38..29d09c794457d7 100644 --- a/src/mono/mono/utils/mono-threads-wasm.c +++ b/src/mono/mono/utils/mono-threads-wasm.c @@ -43,22 +43,24 @@ wasm_get_stack_size (void) #else /* WASI */ +// see mono-threads-wasi.S +uintptr_t get_wasm_stack_high(void); +uintptr_t get_wasm_stack_low(void); + static int wasm_get_stack_base (void) { - // TODO: For WASI, we need to ensure the stack location makes sense and won't interfere with the heap. - // Currently these hardcoded values are sufficient for a working prototype. It's an arbitrary nonzero - // value that aligns to 32 bits. - return 4; + return get_wasm_stack_high(); + // this will need change for multithreading as the stack will allocated be per thread at different addresses } static int wasm_get_stack_size (void) { - // TODO: For WASI, we need to ensure the stack location makes sense and won't interfere with the heap. - // Currently these hardcoded values are sufficient for a working prototype. It's an arbitrary nonzero - // value that aligns to 32 bits. - return 4; + // keep in sync with src\mono\wasi\wasi.proj stack-size + return 8388608; + // TODO after https://github.com/llvm/llvm-project/commit/1532be98f99384990544bd5289ba339bca61e15b + // return (guint8*)get_wasm_stack_high () - (guint8*)get_wasm_stack_low (); } #endif @@ -180,9 +182,7 @@ mono_threads_platform_yield (void) void mono_threads_platform_get_stack_bounds (guint8 **staddr, size_t *stsize) { -#ifndef HOST_WASI int tmp; -#endif #ifdef __EMSCRIPTEN_PTHREADS__ pthread_attr_t attr; gint res; @@ -215,13 +215,8 @@ mono_threads_platform_get_stack_bounds (guint8 **staddr, size_t *stsize) *stsize = wasm_get_stack_size (); #endif -#ifdef HOST_WASI - // TODO: For WASI, we need to ensure the stack is positioned correctly and reintroduce these assertions. - // Currently it works anyway in prototypes (except these checks would fail) -#else g_assert ((guint8*)&tmp > *staddr); g_assert ((guint8*)&tmp < (guint8*)*staddr + *stsize); -#endif } gboolean diff --git a/src/mono/mono/utils/mono-threads.c b/src/mono/mono/utils/mono-threads.c index ac6d2078bb63b8..dc188e4bbde863 100644 --- a/src/mono/mono/utils/mono-threads.c +++ b/src/mono/mono/utils/mono-threads.c @@ -1615,21 +1615,14 @@ mono_thread_info_is_async_context (void) */ void mono_thread_info_get_stack_bounds (guint8 **staddr, size_t *stsize) -{ -#ifndef HOST_WASI +{ guint8 *current = (guint8 *)&stsize; -#endif mono_threads_platform_get_stack_bounds (staddr, stsize); if (!*staddr) return; -#ifdef HOST_WASI - // TODO: Fix the stack positioning on WASI and re-enable the following check. - // Currently it works as a prototype anyway. -#else /* Sanity check the result */ g_assert ((current > *staddr) && (current < *staddr + *stsize)); -#endif #ifndef TARGET_WASM /* When running under emacs, sometimes staddr is not aligned to a page size */ diff --git a/src/mono/wasi/wasi.proj b/src/mono/wasi/wasi.proj index 1cdad54d21740f..22b4196089687e 100644 --- a/src/mono/wasi/wasi.proj +++ b/src/mono/wasi/wasi.proj @@ -102,7 +102,8 @@ <_WasiCompileFlags Include="-D_WASI_EMULATED_PROCESS_CLOCKS"/> <_WasiCompileFlags Include="-D_WASI_EMULATED_SIGNAL"/> <_WasiCompileFlags Include="-D_WASI_EMULATED_MMAN"/> - <_WasiLinkFlags Include="-Wl,-z,stack-size=1048576,--initial-memory=5242880,--max-memory=52428800,-lwasi-emulated-process-clocks,-lwasi-emulated-signal,-lwasi-emulated-mman"/> + + <_WasiLinkFlags Include="-Wl,-z,stack-size=8388608,--initial-memory=52428800,--max-memory=671088640,-lwasi-emulated-process-clocks,-lwasi-emulated-signal,-lwasi-emulated-mman"/>