Disable aux stack allocations for threads spawned by wasi_thread_start#1867
Conversation
This syscall doesn't need allocating stack or TLS and it's expected from the application to do that instead.
daf6367 to
dfa58f8
Compare
For pthreads this is configured in wasi-libc, but since we don't use pthreads here, we have to do it explicitly. The wasi_thread_start code is implemented in wasm to make sure there's no stack operations before the stack pointer is set to a correct value.
dfa58f8 to
cace517
Compare
| wasm_exec_env_is_aux_stack_managed_by_runtime(WASMExecEnv *exec_env) | ||
| { | ||
| return exec_env->aux_stack_boundary.boundary != 0 | ||
| || exec_env->aux_stack_bottom.bottom != 0; |
There was a problem hiding this comment.
i feel it's simpler to use a dedicated flag as 0 is a valid address in wasm. how do you think?
also, i guess aot needs an equivalent around create_aux_stack_info.
There was a problem hiding this comment.
i feel it's simpler to use a dedicated flag as 0 is a valid address in wasm. how do you think?
Yeah, I did think about it. But I noticed that WAMR actually doesn't always respect 0 as a valid address; for example, it considers wasm_runtime_malloc() returning 0 as a failure. Flag was another option but didn't want to increase the size of the object unnecessarily if there was a way to achieve the same without it (if my assumption about WAMR not respecting 0 as a correct value holds).
also, i guess aot needs an equivalent around create_aux_stack_info.
That's correct. At the moment we're not testing AOT though so I was planning to do that (and other necessary things) in the follow-up PR. Does it make sense?
There was a problem hiding this comment.
i feel it's simpler to use a dedicated flag as 0 is a valid address in wasm. how do you think?
Yeah, I did think about it. But I noticed that WAMR actually doesn't always respect 0 as a valid address; for example, it considers
wasm_runtime_malloc()returning0as a failure. Flag was another option but didn't want to increase the size of the object unnecessarily if there was a way to achieve the same without it (if my assumption about WAMR not respecting 0 as a correct value holds).
ok.
also, i guess aot needs an equivalent around create_aux_stack_info.
That's correct. At the moment we're not testing AOT though so I was planning to do that (and other necessary things) in the follow-up PR. Does it make sense?
ok.
| wasm_set_exception(module, | ||
| "wasm auxiliary stack underflow"); | ||
| goto got_exception; | ||
| if (wasm_exec_env_is_aux_stack_managed_by_runtime(exec_env)) { |
There was a problem hiding this comment.
Do we need to apply similar changes for LLVM AOT/JIT and Fast JIT, refer to:
There was a problem hiding this comment.
Yes we do; however, for now we focus on the interpreter and we'll work on JIT/AOT next.
There was a problem hiding this comment.
OK, so let's merge this PR first?
There was a problem hiding this comment.
Yes, we can merge this one and next we'll continue working on AOT and JIT
There was a problem hiding this comment.
There was a problem hiding this comment.
i guess it can be a workaround.
but i think it's better to avoid making the compiler depend on build-time configurations like this.
There was a problem hiding this comment.
at this point, should we just specify in the documentation to compile with --disable-aux-stack-check when using WASI threads with AOT?
for fast JIT we could instead put an #if WASM_ENABLE_LIB_WASI_THREADS == 0 around here
wasm-micro-runtime/core/iwasm/fast-jit/fe/jit_emit_variable.c
Lines 264 to 280 in 622cdbe
Describe how to use WASI threads in AOT mode, following the discussion below: #1867 (comment) 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
bytecodealliance#1867) This syscall doesn't need allocating stack or TLS and it's expected from the application to do that instead. E.g. WASI-libc already does this for `pthread_create`. Also fix some of the examples to allocate memory for stack and not use stack before the stack pointer is set to a correct value.
Describe how to use WASI threads in AOT mode, following the discussion below: bytecodealliance#1867 (comment) 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
Describe how to use WASI threads in AOT mode, following the discussion below: bytecodealliance/wasm-micro-runtime#1867 (comment) 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
This syscall doesn't need allocating stack or TLS and it's expected from the application to do that instead. E.g. WASI-libc already does this for
pthread_create.I also fixed some of the examples to allocate memory for stack and not use stack before the stack pointer is set to a correct value.