From 2e26221d8990db5d47248e4e4b0ce1b37a54c474 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20K=C3=B6plinger?= Date: Sat, 20 Apr 2024 19:14:53 +0200 Subject: [PATCH 1/6] [wasi] Implement TODO in mono-threads-wasi.S Noticed this TODO and since we're using an LLVM version that contains this change we can fix it. --- src/mono/mono/utils/mono-threads-wasi.S | 19 ++++++++----------- src/mono/mono/utils/mono-threads-wasm.c | 13 +++++-------- 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/src/mono/mono/utils/mono-threads-wasi.S b/src/mono/mono/utils/mono-threads-wasi.S index 6de64294456183..41cd94aab2dd17 100644 --- a/src/mono/mono/utils/mono-threads-wasi.S +++ b/src/mono/mono/utils/mono-threads-wasi.S @@ -1,15 +1,12 @@ -// TODO after https://github.com/llvm/llvm-project/commit/1532be98f99384990544bd5289ba339bca61e15b -// use __stack_low && __stack_high +.globl get_wasm_stack_low +.globl get_wasm_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 +get_wasm_stack_low: + .functype get_wasm_stack_low () -> (i32) + global.get __stack_low@GOT end_function -get_wasm_heap_base: - .functype get_wasm_heap_base () -> (i32) - global.get __heap_base@GOT +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 597592a7966c66..66a0704b648759 100644 --- a/src/mono/mono/utils/mono-threads-wasm.c +++ b/src/mono/mono/utils/mono-threads-wasm.c @@ -47,11 +47,8 @@ wasm_get_stack_size (void) #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) @@ -60,8 +57,8 @@ wasm_get_stack_size (void) * | -- increasing address ---> | * | data (data_end)| stack |(heap_base) heap | */ - size_t heap_base = get_wasm_heap_base(); - size_t data_end = get_wasm_data_end(); + 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; g_assert (data_end > 0); @@ -75,7 +72,7 @@ wasm_get_stack_size (void) static int wasm_get_stack_base (void) { - return get_wasm_data_end(); + return get_wasm_stack_high(); // this will need further change for multithreading as the stack will allocated be per thread at different addresses } 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 2/6] 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 e5bd089b8203c7..7078b5d548b663 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 41cd94aab2dd17..8752b03f124b78 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 66a0704b648759..5e3fd185589bd3 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 From 9e795d161be5faede77d15c69b82853b18ef34c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20K=C3=B6plinger?= Date: Mon, 17 Jun 2024 20:17:13 +0200 Subject: [PATCH 3/6] Add assert --- src/mono/mono/utils/mono-threads-wasm.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/mono/mono/utils/mono-threads-wasm.c b/src/mono/mono/utils/mono-threads-wasm.c index 5e3fd185589bd3..7649a9c4c1e0ad 100644 --- a/src/mono/mono/utils/mono-threads-wasm.c +++ b/src/mono/mono/utils/mono-threads-wasm.c @@ -33,8 +33,7 @@ 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 - +#ifdef __EMSCRIPTEN_PTHREADS__ /* * | -- increasing address ---> | * | data |(stack low) stack (stack high)| heap | @@ -48,6 +47,10 @@ wasm_get_stack_size (void) // this is the max available stack size size return max_stack_size; +#else + // TODO: this will need changes for WASI multithreading as the stack will be allocated per thread at different addresses + g_assert_not_reached (); +#endif } int @@ -199,8 +202,8 @@ mono_threads_platform_get_stack_bounds (guint8 **staddr, size_t *stsize) *stsize = wasm_get_stack_size (); } #else - *staddr = (guint8*)get_wasm_stack_low (); - *stsize = wasm_get_stack_size (); + // TODO: this will need changes for WASI multithreading as the stack will be allocated per thread at different addresses + g_assert_not_reached (); #endif g_assert ((guint8*)&tmp > *staddr); From f48a66050bd20c89c85931adaf855f93ba3dde02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20K=C3=B6plinger?= Date: Mon, 17 Jun 2024 21:49:41 +0200 Subject: [PATCH 4/6] Fix assert --- src/mono/mono/utils/mono-threads-wasm.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/mono/mono/utils/mono-threads-wasm.c b/src/mono/mono/utils/mono-threads-wasm.c index 7649a9c4c1e0ad..a09841cd64c575 100644 --- a/src/mono/mono/utils/mono-threads-wasm.c +++ b/src/mono/mono/utils/mono-threads-wasm.c @@ -33,7 +33,10 @@ uintptr_t get_wasm_stack_low(void); static int wasm_get_stack_size (void) { -#ifdef __EMSCRIPTEN_PTHREADS__ +#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 |(stack low) stack (stack high)| heap | @@ -47,9 +50,6 @@ wasm_get_stack_size (void) // this is the max available stack size size return max_stack_size; -#else - // TODO: this will need changes for WASI multithreading as the stack will be allocated per thread at different addresses - g_assert_not_reached (); #endif } @@ -201,9 +201,12 @@ mono_threads_platform_get_stack_bounds (guint8 **staddr, size_t *stsize) *staddr = (guint8*)get_wasm_stack_low (); *stsize = wasm_get_stack_size (); } -#else +#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*)get_wasm_stack_low (); + *stsize = wasm_get_stack_size (); #endif g_assert ((guint8*)&tmp > *staddr); From 227ac5dc3f97e38669fca5cf7805a77724fa5461 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20K=C3=B6plinger?= Date: Tue, 18 Jun 2024 13:52:25 +0200 Subject: [PATCH 5/6] Improve assert --- src/mono/mono/utils/mono-threads-wasm.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/mono/mono/utils/mono-threads-wasm.c b/src/mono/mono/utils/mono-threads-wasm.c index a09841cd64c575..c1e6d9144e7b30 100644 --- a/src/mono/mono/utils/mono-threads-wasm.c +++ b/src/mono/mono/utils/mono-threads-wasm.c @@ -45,8 +45,9 @@ wasm_get_stack_size (void) size_t stack_low = get_wasm_stack_low(); size_t max_stack_size = stack_high - stack_low; - g_assert (stack_low > 0); + g_assert (stack_low >= 0); g_assert (stack_high > stack_low); + g_assert (max_stack_size >= 64 * 1024); // this is the max available stack size size return max_stack_size; @@ -197,10 +198,8 @@ 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*)get_wasm_stack_low (); - *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 (); From c0eed7fc6bf248c98bbf17e2663365311fa80192 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20K=C3=B6plinger?= Date: Thu, 20 Jun 2024 17:09:00 +0200 Subject: [PATCH 6/6] PR feedback --- src/mono/mono/utils/CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mono/mono/utils/CMakeLists.txt b/src/mono/mono/utils/CMakeLists.txt index 7078b5d548b663..ef271dcb830a9d 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 OR HOST_WASM) +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,7 +30,7 @@ set(utils_unix_sources if(HOST_WIN32) set(utils_platform_sources ${utils_win32_sources}) -elseif(HOST_WASI OR HOST_WASM) +elseif(HOST_WASM) set(utils_platform_sources ${utils_unix_sources} mono-threads-wasm.S) else() set(utils_platform_sources ${utils_unix_sources})