Skip to content

Commit 0875d5c

Browse files
committed
Fix logic
1 parent 8db5530 commit 0875d5c

File tree

3 files changed

+21
-45
lines changed

3 files changed

+21
-45
lines changed

src/mono/mono/utils/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ if(HOST_WIN32 AND HOST_AMD64)
1717
set(CMAKE_ASM_MASM_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreadedDebugDLL "")
1818

1919
list(APPEND utils_win32_sources win64.asm)
20-
elseif(HOST_WASI)
20+
elseif(HOST_WASI OR HOST_WASM)
2121
set (CMAKE_ASM_COMPILER_VERSION "${CMAKE_C_COMPILER_VERSION}")
2222
set (CMAKE_ASM_COMPILER_TARGET "${CMAKE_C_COMPILER_TARGET}")
2323
enable_language(ASM)
@@ -30,8 +30,8 @@ set(utils_unix_sources
3030

3131
if(HOST_WIN32)
3232
set(utils_platform_sources ${utils_win32_sources})
33-
elseif(HOST_WASI)
34-
set(utils_platform_sources ${utils_unix_sources} mono-threads-wasi.S)
33+
elseif(HOST_WASI OR HOST_WASM)
34+
set(utils_platform_sources ${utils_unix_sources} mono-threads-wasm.S)
3535
else()
3636
set(utils_platform_sources ${utils_unix_sources})
3737
endif()

src/mono/mono/utils/mono-threads-wasi.S renamed to src/mono/mono/utils/mono-threads-wasm.S

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
get_wasm_stack_low:
55
.functype get_wasm_stack_low () -> (i32)
66
global.get __stack_low@GOT
7+
// align up to 16 bytes
8+
i32.const 0xF
9+
i32.add
10+
i32.const -0x10
11+
i32.and
712
end_function
813

914
get_wasm_stack_high:

src/mono/mono/utils/mono-threads-wasm.c

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,71 +20,42 @@
2020
#include <mono/utils/mono-threads-wasm.h>
2121

2222
#include <emscripten.h>
23-
#include <emscripten/stack.h>
2423
#ifndef DISABLE_THREADS
2524
#include <emscripten/threading.h>
2625
#include <mono/metadata/threads-types.h>
2726
#endif
2827

29-
30-
#define round_down(addr, val) ((void*)((addr) & ~((val) - 1)))
31-
32-
EMSCRIPTEN_KEEPALIVE
33-
static int
34-
wasm_get_stack_base (void)
35-
{
36-
// wasm-mt: add MONO_ENTER_GC_UNSAFE / MONO_EXIT_GC_UNSAFE if this function becomes more complex
37-
return emscripten_stack_get_end ();
38-
}
39-
40-
EMSCRIPTEN_KEEPALIVE
41-
static int
42-
wasm_get_stack_size (void)
43-
{
44-
// wasm-mt: add MONO_ENTER_GC_UNSAFE / MONO_EXIT_GC_UNSAFE if this function becomes more complex
45-
return (guint8*)emscripten_stack_get_base () - (guint8*)emscripten_stack_get_end ();
46-
}
47-
48-
#else /* HOST_BROWSER -> WASI */
28+
#endif
4929

5030
uintptr_t get_wasm_stack_high(void);
5131
uintptr_t get_wasm_stack_low(void);
5232

5333
static int
5434
wasm_get_stack_size (void)
5535
{
36+
// this will need further change for multithreading as the stack will allocated be per thread at different addresses
37+
5638
/*
5739
* | -- increasing address ---> |
58-
* | data (data_end)| stack |(heap_base) heap |
40+
* | data |(stack low) stack (stack high)| heap |
5941
*/
60-
size_t heap_base = get_wasm_stack_high();
61-
size_t data_end = get_wasm_stack_low();
62-
size_t max_stack_size = heap_base - data_end;
42+
size_t stack_high = get_wasm_stack_high();
43+
size_t stack_low = get_wasm_stack_low();
44+
size_t max_stack_size = stack_high - stack_low;
6345

64-
g_assert (data_end > 0);
65-
g_assert (heap_base > data_end);
46+
g_assert (stack_low > 0);
47+
g_assert (stack_high > stack_low);
6648

67-
// this is the max available stack size size,
68-
// return a 16-byte aligned smaller size
69-
return max_stack_size & ~0xF;
49+
// this is the max available stack size size
50+
return max_stack_size;
7051
}
7152

72-
static int
73-
wasm_get_stack_base (void)
74-
{
75-
return get_wasm_stack_high();
76-
// this will need further change for multithreading as the stack will allocated be per thread at different addresses
77-
}
78-
79-
#endif /* HOST_BROWSER */
80-
8153
int
8254
mono_thread_info_get_system_max_stack_size (void)
8355
{
8456
return wasm_get_stack_size ();
8557
}
8658

87-
8859
void
8960
mono_threads_suspend_init_signals (void)
9061
{
@@ -224,11 +195,11 @@ mono_threads_platform_get_stack_bounds (guint8 **staddr, size_t *stsize)
224195
g_error ("%s: pthread_attr_destroy failed with \"%s\" (%d)", __func__, g_strerror (res), res);
225196

226197
if (*staddr == NULL) {
227-
*staddr = (guint8*)wasm_get_stack_base ();
198+
*staddr = (guint8*)get_wasm_stack_low ();
228199
*stsize = wasm_get_stack_size ();
229200
}
230201
#else
231-
*staddr = (guint8*)wasm_get_stack_base ();
202+
*staddr = (guint8*)get_wasm_stack_low ();
232203
*stsize = wasm_get_stack_size ();
233204
#endif
234205

0 commit comments

Comments
 (0)