|
20 | 20 | #include <mono/utils/mono-threads-wasm.h>
|
21 | 21 |
|
22 | 22 | #include <emscripten.h>
|
23 |
| -#include <emscripten/stack.h> |
24 | 23 | #ifndef DISABLE_THREADS
|
25 | 24 | #include <emscripten/threading.h>
|
26 | 25 | #include <mono/metadata/threads-types.h>
|
27 | 26 | #endif
|
28 | 27 |
|
| 28 | +#endif |
29 | 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 */ |
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); |
55 | 32 |
|
56 | 33 | static int
|
57 | 34 | wasm_get_stack_size (void)
|
58 | 35 | {
|
| 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 |
59 | 40 | /*
|
60 | 41 | * | -- increasing address ---> |
|
61 |
| - * | data (data_end)| stack |(heap_base) heap | |
| 42 | + * | data |(stack low) stack (stack high)| heap | |
62 | 43 | */
|
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; |
69 | 47 |
|
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); |
74 | 51 |
|
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 |
80 | 55 | }
|
81 | 56 |
|
82 |
| -#endif /* HOST_BROWSER */ |
83 |
| - |
84 | 57 | int
|
85 | 58 | mono_thread_info_get_system_max_stack_size (void)
|
86 | 59 | {
|
87 | 60 | return wasm_get_stack_size ();
|
88 | 61 | }
|
89 | 62 |
|
90 |
| - |
91 | 63 | void
|
92 | 64 | mono_threads_suspend_init_signals (void)
|
93 | 65 | {
|
@@ -226,12 +198,13 @@ mono_threads_platform_get_stack_bounds (guint8 **staddr, size_t *stsize)
|
226 | 198 | if (G_UNLIKELY (res != 0))
|
227 | 199 | g_error ("%s: pthread_attr_destroy failed with \"%s\" (%d)", __func__, g_strerror (res), res);
|
228 | 200 |
|
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 (); |
233 | 206 | #else
|
234 |
| - *staddr = (guint8*)wasm_get_stack_base (); |
| 207 | + *staddr = (guint8*)get_wasm_stack_low (); |
235 | 208 | *stsize = wasm_get_stack_size ();
|
236 | 209 | #endif
|
237 | 210 |
|
|
0 commit comments