From 0875d5c8a0ec17a5b6637016ed4dcb59bd458080 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20K=C3=B6plinger?= Date: Mon, 17 Jun 2024 16:12:41 +0200 Subject: [PATCH] Fix logic --- src/mono/mono/utils/CMakeLists.txt | 6 +- ...ono-threads-wasi.S => mono-threads-wasm.S} | 5 ++ src/mono/mono/utils/mono-threads-wasm.c | 55 +++++-------------- 3 files changed, 21 insertions(+), 45 deletions(-) rename src/mono/mono/utils/{mono-threads-wasi.S => mono-threads-wasm.S} (77%) diff --git a/src/mono/mono/utils/CMakeLists.txt b/src/mono/mono/utils/CMakeLists.txt index e5bd089b8203c..7078b5d548b66 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_WASI OR 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_WASI OR 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-wasm.S similarity index 77% rename from src/mono/mono/utils/mono-threads-wasi.S rename to src/mono/mono/utils/mono-threads-wasm.S index 41cd94aab2dd1..8752b03f124b7 100644 --- a/src/mono/mono/utils/mono-threads-wasi.S +++ b/src/mono/mono/utils/mono-threads-wasm.S @@ -4,6 +4,11 @@ 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: diff --git a/src/mono/mono/utils/mono-threads-wasm.c b/src/mono/mono/utils/mono-threads-wasm.c index 66a0704b64875..5e3fd185589bd 100644 --- a/src/mono/mono/utils/mono-threads-wasm.c +++ b/src/mono/mono/utils/mono-threads-wasm.c @@ -20,32 +20,12 @@ #include #include -#include #ifndef DISABLE_THREADS #include #include #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 */ +#endif uintptr_t get_wasm_stack_high(void); uintptr_t get_wasm_stack_low(void); @@ -53,38 +33,29 @@ uintptr_t get_wasm_stack_low(void); static int wasm_get_stack_size (void) { + // this will need further change for multithreading as the stack will allocated be per thread at different addresses + /* * | -- increasing address ---> | - * | data (data_end)| stack |(heap_base) heap | + * | data |(stack low) stack (stack high)| heap | */ - size_t heap_base = get_wasm_stack_high(); - size_t data_end = get_wasm_stack_low(); - size_t max_stack_size = 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; - g_assert (data_end > 0); - g_assert (heap_base > data_end); + g_assert (stack_low > 0); + g_assert (stack_high > stack_low); - // this is the max available stack size size, - // return a 16-byte aligned smaller size - return max_stack_size & ~0xF; + // this is the max available stack size size + return max_stack_size; } -static int -wasm_get_stack_base (void) -{ - return get_wasm_stack_high(); - // this will need further change for multithreading as the stack will allocated be per thread at different addresses -} - -#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) { @@ -224,11 +195,11 @@ mono_threads_platform_get_stack_bounds (guint8 **staddr, size_t *stsize) g_error ("%s: pthread_attr_destroy failed with \"%s\" (%d)", __func__, g_strerror (res), res); if (*staddr == NULL) { - *staddr = (guint8*)wasm_get_stack_base (); + *staddr = (guint8*)get_wasm_stack_low (); *stsize = wasm_get_stack_size (); } #else - *staddr = (guint8*)wasm_get_stack_base (); + *staddr = (guint8*)get_wasm_stack_low (); *stsize = wasm_get_stack_size (); #endif