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

[wasi] Implement TODO in mono-threads-wasi.S #101337

Merged
merged 8 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
6 changes: 3 additions & 3 deletions src/mono/mono/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
pavelsavara marked this conversation as resolved.
Show resolved Hide resolved
set (CMAKE_ASM_COMPILER_VERSION "${CMAKE_C_COMPILER_VERSION}")
set (CMAKE_ASM_COMPILER_TARGET "${CMAKE_C_COMPILER_TARGET}")
enable_language(ASM)
Expand All @@ -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()
Expand Down
15 changes: 0 additions & 15 deletions src/mono/mono/utils/mono-threads-wasi.S

This file was deleted.

17 changes: 17 additions & 0 deletions src/mono/mono/utils/mono-threads-wasm.S
Original file line number Diff line number Diff line change
@@ -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
73 changes: 23 additions & 50 deletions src/mono/mono/utils/mono-threads-wasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,74 +20,46 @@
#include <mono/utils/mono-threads-wasm.h>

#include <emscripten.h>
#include <emscripten/stack.h>
#ifndef DISABLE_THREADS
#include <emscripten/threading.h>
#include <mono/metadata/threads-types.h>
#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)
{
Expand Down Expand Up @@ -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 ();
pavelsavara marked this conversation as resolved.
Show resolved Hide resolved
*stsize = wasm_get_stack_size ();
#endif

Expand Down
Loading