Skip to content

Commit

Permalink
Fix logic
Browse files Browse the repository at this point in the history
  • Loading branch information
akoeplinger committed Jun 17, 2024
1 parent 8db5530 commit 0875d5c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 45 deletions.
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)
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
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
55 changes: 13 additions & 42 deletions src/mono/mono/utils/mono-threads-wasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,71 +20,42 @@
#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


#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);

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)
{
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 0875d5c

Please sign in to comment.