diff --git a/src/mono/mono/utils/CMakeLists.txt b/src/mono/mono/utils/CMakeLists.txt index e5bd089b8203c..ef271dcb830a9 100644 --- a/src/mono/mono/utils/CMakeLists.txt +++ b/src/mono/mono/utils/CMakeLists.txt @@ -17,7 +17,7 @@ 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) +elseif(HOST_WASM) set (CMAKE_ASM_COMPILER_VERSION "${CMAKE_C_COMPILER_VERSION}") set (CMAKE_ASM_COMPILER_TARGET "${CMAKE_C_COMPILER_TARGET}") enable_language(ASM) @@ -30,8 +30,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) +elseif(HOST_WASM) + set(utils_platform_sources ${utils_unix_sources} mono-threads-wasm.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 deleted file mode 100644 index 6de6429445618..0000000000000 --- a/src/mono/mono/utils/mono-threads-wasi.S +++ /dev/null @@ -1,15 +0,0 @@ -// TODO after https://github.com/llvm/llvm-project/commit/1532be98f99384990544bd5289ba339bca61e15b -// use __stack_low && __stack_high - -.globl get_wasm_data_end -.globl get_wasm_heap_base - -get_wasm_data_end: - .functype get_wasm_data_end () -> (i32) - global.get __data_end@GOT - end_function - -get_wasm_heap_base: - .functype get_wasm_heap_base () -> (i32) - global.get __heap_base@GOT - end_function diff --git a/src/mono/mono/utils/mono-threads-wasm.S b/src/mono/mono/utils/mono-threads-wasm.S new file mode 100644 index 0000000000000..8752b03f124b7 --- /dev/null +++ b/src/mono/mono/utils/mono-threads-wasm.S @@ -0,0 +1,17 @@ +.globl get_wasm_stack_low +.globl get_wasm_stack_high + +get_wasm_stack_low: + .functype get_wasm_stack_low () -> (i32) + global.get __stack_low@GOT + // align up to 16 bytes + i32.const 0xF + i32.add + i32.const -0x10 + i32.and + end_function + +get_wasm_stack_high: + .functype get_wasm_stack_high () -> (i32) + global.get __stack_high@GOT + end_function diff --git a/src/mono/mono/utils/mono-threads-wasm.c b/src/mono/mono/utils/mono-threads-wasm.c index 597592a7966c6..c1e6d9144e7b3 100644 --- a/src/mono/mono/utils/mono-threads-wasm.c +++ b/src/mono/mono/utils/mono-threads-wasm.c @@ -20,74 +20,46 @@ #include #include -#include #ifndef DISABLE_THREADS #include #include #endif +#endif -#define round_down(addr, val) ((void*)((addr) & ~((val) - 1))) - -EMSCRIPTEN_KEEPALIVE -static int -wasm_get_stack_base (void) -{ - // wasm-mt: add MONO_ENTER_GC_UNSAFE / MONO_EXIT_GC_UNSAFE if this function becomes more complex - return emscripten_stack_get_end (); -} - -EMSCRIPTEN_KEEPALIVE -static int -wasm_get_stack_size (void) -{ - // wasm-mt: add MONO_ENTER_GC_UNSAFE / MONO_EXIT_GC_UNSAFE if this function becomes more complex - return (guint8*)emscripten_stack_get_base () - (guint8*)emscripten_stack_get_end (); -} - -#else /* HOST_BROWSER -> WASI */ - -// TODO after https://github.com/llvm/llvm-project/commit/1532be98f99384990544bd5289ba339bca61e15b -// use __stack_low && __stack_high -// see mono-threads-wasi.S -uintptr_t get_wasm_heap_base(void); -uintptr_t get_wasm_data_end(void); +uintptr_t get_wasm_stack_high(void); +uintptr_t get_wasm_stack_low(void); static int wasm_get_stack_size (void) { +#if defined(HOST_WASI) && !defined(DISABLE_THREADS) + // TODO: this will need changes for WASI multithreading as the stack will be allocated per thread at different addresses + g_assert_not_reached (); +#else /* * | -- increasing address ---> | - * | data (data_end)| stack |(heap_base) heap | + * | data |(stack low) stack (stack high)| heap | */ - size_t heap_base = get_wasm_heap_base(); - size_t data_end = get_wasm_data_end(); - size_t max_stack_size = heap_base - data_end; - - g_assert (data_end > 0); - g_assert (heap_base > data_end); + size_t stack_high = get_wasm_stack_high(); + size_t stack_low = get_wasm_stack_low(); + size_t max_stack_size = stack_high - stack_low; - // this is the max available stack size size, - // return a 16-byte aligned smaller size - return max_stack_size & ~0xF; -} + g_assert (stack_low >= 0); + g_assert (stack_high > stack_low); + g_assert (max_stack_size >= 64 * 1024); -static int -wasm_get_stack_base (void) -{ - return get_wasm_data_end(); - // this will need further change for multithreading as the stack will allocated be per thread at different addresses + // this is the max available stack size size + return max_stack_size; +#endif } -#endif /* HOST_BROWSER */ - int mono_thread_info_get_system_max_stack_size (void) { return wasm_get_stack_size (); } - void mono_threads_suspend_init_signals (void) { @@ -226,12 +198,13 @@ mono_threads_platform_get_stack_bounds (guint8 **staddr, size_t *stsize) if (G_UNLIKELY (res != 0)) g_error ("%s: pthread_attr_destroy failed with \"%s\" (%d)", __func__, g_strerror (res), res); - if (*staddr == NULL) { - *staddr = (guint8*)wasm_get_stack_base (); - *stsize = wasm_get_stack_size (); - } + g_assert (*staddr != NULL); + g_assert (*stsize != (size_t)-1); +#elif defined(HOST_WASI) && !defined(DISABLE_THREADS) + // TODO: this will need changes for WASI multithreading as the stack will be allocated per thread at different addresses + g_assert_not_reached (); #else - *staddr = (guint8*)wasm_get_stack_base (); + *staddr = (guint8*)get_wasm_stack_low (); *stsize = wasm_get_stack_size (); #endif