Skip to content

Commit 2a72651

Browse files
authored
[wasi] Implement TODO in mono-threads-wasi.S (#101337)
1 parent 2e585aa commit 2a72651

File tree

4 files changed

+43
-68
lines changed

4 files changed

+43
-68
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_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_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

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.globl get_wasm_stack_low
2+
.globl get_wasm_stack_high
3+
4+
get_wasm_stack_low:
5+
.functype get_wasm_stack_low () -> (i32)
6+
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
12+
end_function
13+
14+
get_wasm_stack_high:
15+
.functype get_wasm_stack_high () -> (i32)
16+
global.get __stack_high@GOT
17+
end_function

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

Lines changed: 23 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -20,74 +20,46 @@
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

28+
#endif
2929

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 */
49-
50-
// TODO after https://github.com/llvm/llvm-project/commit/1532be98f99384990544bd5289ba339bca61e15b
51-
// use __stack_low && __stack_high
52-
// see mono-threads-wasi.S
53-
uintptr_t get_wasm_heap_base(void);
54-
uintptr_t get_wasm_data_end(void);
30+
uintptr_t get_wasm_stack_high(void);
31+
uintptr_t get_wasm_stack_low(void);
5532

5633
static int
5734
wasm_get_stack_size (void)
5835
{
36+
#if defined(HOST_WASI) && !defined(DISABLE_THREADS)
37+
// TODO: this will need changes for WASI multithreading as the stack will be allocated per thread at different addresses
38+
g_assert_not_reached ();
39+
#else
5940
/*
6041
* | -- increasing address ---> |
61-
* | data (data_end)| stack |(heap_base) heap |
42+
* | data |(stack low) stack (stack high)| heap |
6243
*/
63-
size_t heap_base = get_wasm_heap_base();
64-
size_t data_end = get_wasm_data_end();
65-
size_t max_stack_size = heap_base - data_end;
66-
67-
g_assert (data_end > 0);
68-
g_assert (heap_base > data_end);
44+
size_t stack_high = get_wasm_stack_high();
45+
size_t stack_low = get_wasm_stack_low();
46+
size_t max_stack_size = stack_high - stack_low;
6947

70-
// this is the max available stack size size,
71-
// return a 16-byte aligned smaller size
72-
return max_stack_size & ~0xF;
73-
}
48+
g_assert (stack_low >= 0);
49+
g_assert (stack_high > stack_low);
50+
g_assert (max_stack_size >= 64 * 1024);
7451

75-
static int
76-
wasm_get_stack_base (void)
77-
{
78-
return get_wasm_data_end();
79-
// this will need further change for multithreading as the stack will allocated be per thread at different addresses
52+
// this is the max available stack size size
53+
return max_stack_size;
54+
#endif
8055
}
8156

82-
#endif /* HOST_BROWSER */
83-
8457
int
8558
mono_thread_info_get_system_max_stack_size (void)
8659
{
8760
return wasm_get_stack_size ();
8861
}
8962

90-
9163
void
9264
mono_threads_suspend_init_signals (void)
9365
{
@@ -226,12 +198,13 @@ mono_threads_platform_get_stack_bounds (guint8 **staddr, size_t *stsize)
226198
if (G_UNLIKELY (res != 0))
227199
g_error ("%s: pthread_attr_destroy failed with \"%s\" (%d)", __func__, g_strerror (res), res);
228200

229-
if (*staddr == NULL) {
230-
*staddr = (guint8*)wasm_get_stack_base ();
231-
*stsize = wasm_get_stack_size ();
232-
}
201+
g_assert (*staddr != NULL);
202+
g_assert (*stsize != (size_t)-1);
203+
#elif defined(HOST_WASI) && !defined(DISABLE_THREADS)
204+
// TODO: this will need changes for WASI multithreading as the stack will be allocated per thread at different addresses
205+
g_assert_not_reached ();
233206
#else
234-
*staddr = (guint8*)wasm_get_stack_base ();
207+
*staddr = (guint8*)get_wasm_stack_low ();
235208
*stsize = wasm_get_stack_size ();
236209
#endif
237210

0 commit comments

Comments
 (0)