-
Notifications
You must be signed in to change notification settings - Fork 646
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Disable aux stack allocations for threads spawned by wasi_thread_start #1867
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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()
returning0
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).
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to apply similar changes for LLVM AOT/JIT and Fast JIT, refer to:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, so let's merge this PR first?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we can merge this one and next we'll continue working on AOT and JIT
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wenyongh @yamt for AOT, should we disable the aux stack globally when WASI threads are enabled? by replacing this
comp_ctx->enable_aux_stack_check = true; |
with something like
#if WASM_ENABLE_LIB_WASI_THREADS != 0
comp_ctx->enable_aux_stack_check = false;
#else
comp_ctx->enable_aux_stack_check = true;
#endif
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
if (is_aux_stack) { | |
JitReg aux_stack_bound = get_aux_stack_bound_reg(cc->jit_frame); | |
JitReg aux_stack_bottom = | |
get_aux_stack_bottom_reg(cc->jit_frame); | |
GEN_INSN(CMP, cc->cmp_reg, value, aux_stack_bound); | |
if (!(jit_emit_exception(cc, EXCE_AUX_STACK_OVERFLOW, | |
JIT_OP_BLEU, cc->cmp_reg, NULL))) | |
goto fail; | |
GEN_INSN(CMP, cc->cmp_reg, value, aux_stack_bottom); | |
if (!(jit_emit_exception(cc, EXCE_AUX_STACK_UNDERFLOW, | |
JIT_OP_BGTU, cc->cmp_reg, NULL))) | |
goto fail; | |
} | |
GEN_INSN(STI32, value, get_module_inst_reg(cc->jit_frame), | |
NEW_CONST(I32, data_offset)); | |
break; | |
} |
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
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.