1515#include "aot_runtime.h"
1616#endif
1717
18+ typedef struct __wasi_thread_spawn_result_t {
19+ uint8 is_error ;
20+ union {
21+ uint8 error ;
22+ uint32 tid ;
23+ } u ;
24+ } __wasi_thread_spawn_result_t ;
25+
26+ bh_static_assert (sizeof (__wasi_thread_spawn_result_t ) == 8 );
27+ bh_static_assert (offsetof (__wasi_thread_spawn_result_t , u .error ) == 4 );
28+ bh_static_assert (offsetof (__wasi_thread_spawn_result_t , u .tid ) == 4 );
29+
30+ #define WASI_THREADS_EAGAIN 0
31+
1832static const char * THREAD_START_FUNCTION = "wasi_thread_start" ;
1933static korp_mutex thread_id_lock ;
2034static TidAllocator tid_allocator ;
@@ -71,8 +85,8 @@ thread_start(void *arg)
7185 return NULL ;
7286}
7387
74- static int32
75- thread_spawn_wrapper (wasm_exec_env_t exec_env , uint32 start_arg )
88+ static void
89+ thread_spawn_wrapper (wasm_exec_env_t exec_env , uint32 start_arg , void * retaddr )
7690{
7791 wasm_module_t module = wasm_exec_env_get_module (exec_env );
7892 wasm_module_inst_t module_inst = get_module_inst (exec_env );
@@ -85,15 +99,18 @@ thread_spawn_wrapper(wasm_exec_env_t exec_env, uint32 start_arg)
8599#if WASM_ENABLE_LIBC_WASI != 0
86100 WASIContext * wasi_ctx ;
87101#endif
102+ __wasi_thread_spawn_result_t result ;
103+ memset (& result , 0 , sizeof (result ));
88104
89105 bh_assert (module );
90106 bh_assert (module_inst );
91107
92108 stack_size = ((WASMModuleInstance * )module_inst )-> default_wasm_stack_size ;
93109
94110 if (!(new_module_inst = wasm_runtime_instantiate_internal (
95- module , true, stack_size , 0 , NULL , 0 )))
96- return -1 ;
111+ module , true, stack_size , 0 , NULL , 0 ))) {
112+ goto fail ;
113+ }
97114
98115 wasm_runtime_set_custom_data_internal (
99116 new_module_inst , wasm_runtime_get_custom_data (module_inst ));
@@ -134,7 +151,9 @@ thread_spawn_wrapper(wasm_exec_env_t exec_env, uint32 start_arg)
134151 }
135152 os_mutex_unlock (& exec_env -> wait_lock );
136153
137- return thread_id ;
154+ result .is_error = 0 ;
155+ result .u .tid = thread_id ;
156+ goto copyout ;
138157
139158thread_spawn_fail :
140159 os_mutex_unlock (& exec_env -> wait_lock );
@@ -146,16 +165,28 @@ thread_spawn_wrapper(wasm_exec_env_t exec_env, uint32 start_arg)
146165 if (thread_start_arg )
147166 wasm_runtime_free (thread_start_arg );
148167
149- return -1 ;
168+ fail :
169+ result .is_error = 1 ;
170+ result .u .error = WASI_THREADS_EAGAIN ;
171+
172+ copyout :
173+ if (!wasm_runtime_validate_native_addr (module_inst , retaddr ,
174+ sizeof (result ))) {
175+ wasm_runtime_set_exception (module_inst , "out of bounds memory access" );
176+ }
177+ else {
178+ memcpy (retaddr , & result , sizeof (result ));
179+ }
150180}
151181
152182/* clang-format off */
153- #define REG_NATIVE_FUNC (func_name , signature ) \
154- { #func_name , func_name##_wrapper, signature, NULL }
183+ #define REG_NATIVE_FUNC (symbol , func_name , signature ) \
184+ { symbol , func_name##_wrapper, signature, NULL }
155185/* clang-format on */
156186
157- static NativeSymbol native_symbols_lib_wasi_threads [] = { REG_NATIVE_FUNC (
158- thread_spawn , "(i)i" ) };
187+ static NativeSymbol native_symbols_lib_wasi_threads [] = {
188+ REG_NATIVE_FUNC ("thread-spawn" , thread_spawn , "(i*)" ),
189+ };
159190
160191uint32
161192get_lib_wasi_threads_export_apis (NativeSymbol * * p_lib_wasi_threads_apis )
0 commit comments