From 2eed50b3034b8eec531cce4a782b8c698fea00fd Mon Sep 17 00:00:00 2001 From: Enrico Loparco Date: Fri, 27 Jan 2023 11:32:33 +0100 Subject: [PATCH] Document how to use WASI threads in AOT mode (#1905) Describe how to use WASI threads in AOT mode, following the discussion below: https://github.com/bytecodealliance/wasm-micro-runtime/pull/1867#discussion_r1070268062 Make aux stack boundary checks of wasi-threads always successful by setting `exec_env->aux_stack_bottom` to UINT32_MAX and `exec_env->aux_stack_boundary` to 0 --- core/iwasm/interpreter/wasm_interp_classic.c | 20 ++++++++----------- core/iwasm/interpreter/wasm_interp_fast.c | 20 ++++++++----------- .../libraries/thread-mgr/thread_manager.c | 5 +++++ samples/wasi-threads/README.md | 9 ++++++++- 4 files changed, 29 insertions(+), 25 deletions(-) diff --git a/core/iwasm/interpreter/wasm_interp_classic.c b/core/iwasm/interpreter/wasm_interp_classic.c index 06d75a48..3e16b380 100644 --- a/core/iwasm/interpreter/wasm_interp_classic.c +++ b/core/iwasm/interpreter/wasm_interp_classic.c @@ -1780,18 +1780,14 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, global = globals + global_idx; global_addr = get_global_addr(global_data, global); aux_stack_top = *(uint32 *)(frame_sp - 1); - if (wasm_exec_env_is_aux_stack_managed_by_runtime(exec_env)) { - if (aux_stack_top - <= exec_env->aux_stack_boundary.boundary) { - wasm_set_exception(module, - "wasm auxiliary stack overflow"); - goto got_exception; - } - if (aux_stack_top > exec_env->aux_stack_bottom.bottom) { - wasm_set_exception(module, - "wasm auxiliary stack underflow"); - goto got_exception; - } + if (aux_stack_top <= exec_env->aux_stack_boundary.boundary) { + wasm_set_exception(module, "wasm auxiliary stack overflow"); + goto got_exception; + } + if (aux_stack_top > exec_env->aux_stack_bottom.bottom) { + wasm_set_exception(module, + "wasm auxiliary stack underflow"); + goto got_exception; } *(int32 *)global_addr = aux_stack_top; frame_sp--; diff --git a/core/iwasm/interpreter/wasm_interp_fast.c b/core/iwasm/interpreter/wasm_interp_fast.c index adcb18c2..0ea920f2 100644 --- a/core/iwasm/interpreter/wasm_interp_fast.c +++ b/core/iwasm/interpreter/wasm_interp_fast.c @@ -1576,18 +1576,14 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module, global = globals + global_idx; global_addr = get_global_addr(global_data, global); aux_stack_top = frame_lp[GET_OFFSET()]; - if (wasm_exec_env_is_aux_stack_managed_by_runtime(exec_env)) { - if (aux_stack_top - <= exec_env->aux_stack_boundary.boundary) { - wasm_set_exception(module, - "wasm auxiliary stack overflow"); - goto got_exception; - } - if (aux_stack_top > exec_env->aux_stack_bottom.bottom) { - wasm_set_exception(module, - "wasm auxiliary stack underflow"); - goto got_exception; - } + if (aux_stack_top <= exec_env->aux_stack_boundary.boundary) { + wasm_set_exception(module, "wasm auxiliary stack overflow"); + goto got_exception; + } + if (aux_stack_top > exec_env->aux_stack_bottom.bottom) { + wasm_set_exception(module, + "wasm auxiliary stack underflow"); + goto got_exception; } *(int32 *)global_addr = aux_stack_top; #if WASM_ENABLE_MEMORY_PROFILING != 0 diff --git a/core/iwasm/libraries/thread-mgr/thread_manager.c b/core/iwasm/libraries/thread-mgr/thread_manager.c index 492ed66b..8ec310d9 100644 --- a/core/iwasm/libraries/thread-mgr/thread_manager.c +++ b/core/iwasm/libraries/thread-mgr/thread_manager.c @@ -586,6 +586,11 @@ wasm_cluster_create_thread(WASMExecEnv *exec_env, goto fail3; } } + else { + /* Disable aux stack */ + new_exec_env->aux_stack_boundary.boundary = 0; + new_exec_env->aux_stack_bottom.bottom = UINT32_MAX; + } if (!wasm_cluster_add_exec_env(cluster, new_exec_env)) goto fail3; diff --git a/samples/wasi-threads/README.md b/samples/wasi-threads/README.md index 533f687e..499162b6 100644 --- a/samples/wasi-threads/README.md +++ b/samples/wasi-threads/README.md @@ -10,7 +10,7 @@ make \ THREAD_MODEL=posix ``` -Build and run the samples +## Build and run the samples ```shell $ mkdir build @@ -22,3 +22,10 @@ $ ./iwasm wasm-apps/no_pthread.wasm ... $ ./iwasm wasm-apps/exception_propagation.wasm ``` + +## Run samples in AOT mode +```shell +$ ../../../wamr-compiler/build/wamrc \ + -o wasm-apps/no_pthread.aot wasm-apps/no_pthread.wasm +$ ./iwasm wasm-apps/no_pthread.aot +```