From b6765226b36e198eb774b38173440726da13cb90 Mon Sep 17 00:00:00 2001 From: Marcin Kolny Date: Fri, 31 Jan 2025 18:37:02 +0000 Subject: [PATCH] Add API for disabling native stack boundary checks This API can be helpful if e.g. module is working in a reactor mode, and the module is being used from multiple threads (where the caller guarantees that no two threads execute code on the same execution environment). Currently this usecase fails because the native stack boundaries for each thread are different. Alternative to this solution would be to update the boundary on every thread switch but that might not be desired due to performance. --- core/iwasm/common/wasm_exec_env.c | 4 +++- core/iwasm/common/wasm_runtime_common.c | 6 ++++++ core/iwasm/common/wasm_runtime_common.h | 4 ++++ core/iwasm/include/wasm_export.h | 8 ++++++++ 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/core/iwasm/common/wasm_exec_env.c b/core/iwasm/common/wasm_exec_env.c index 48465fc94..d00a3f181 100644 --- a/core/iwasm/common/wasm_exec_env.c +++ b/core/iwasm/common/wasm_exec_env.c @@ -282,7 +282,9 @@ wasm_exec_env_set_thread_info(WASMExecEnv *exec_env) os_mutex_lock(&exec_env->wait_lock); #endif exec_env->handle = os_self_thread(); - if (exec_env->user_native_stack_boundary) + if (exec_env->user_native_stack_boundary == (void *)UINTPTR_MAX) + exec_env->native_stack_boundary = NULL; + else if (exec_env->user_native_stack_boundary) /* WASM_STACK_GUARD_SIZE isn't added for flexibility to developer, he must ensure that enough guard bytes are kept. */ exec_env->native_stack_boundary = exec_env->user_native_stack_boundary; diff --git a/core/iwasm/common/wasm_runtime_common.c b/core/iwasm/common/wasm_runtime_common.c index cc6badd9e..df6ae52c3 100644 --- a/core/iwasm/common/wasm_runtime_common.c +++ b/core/iwasm/common/wasm_runtime_common.c @@ -2232,6 +2232,12 @@ wasm_runtime_set_native_stack_boundary(WASMExecEnv *exec_env, exec_env->user_native_stack_boundary = native_stack_boundary; } +void +wasm_runtime_disable_native_stack_boundary_check(WASMExecEnv *exec_env) +{ + wasm_runtime_set_native_stack_boundary(exec_env, (void *)UINTPTR_MAX); +} + #ifdef OS_ENABLE_HW_BOUND_CHECK void wasm_runtime_access_exce_check_guard_page() diff --git a/core/iwasm/common/wasm_runtime_common.h b/core/iwasm/common/wasm_runtime_common.h index 4c7dfed4f..c97eae1f0 100644 --- a/core/iwasm/common/wasm_runtime_common.h +++ b/core/iwasm/common/wasm_runtime_common.h @@ -678,6 +678,10 @@ WASM_RUNTIME_API_EXTERN void wasm_runtime_set_native_stack_boundary(WASMExecEnv *exec_env, uint8 *native_stack_boundary); +/* See wasm_export.h for description */ +WASM_RUNTIME_API_EXTERN void +wasm_runtime_disable_native_stack_boundary_check(WASMExecEnv *exec_env); + #if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0 /* See wasm_export.h for description */ WASM_RUNTIME_API_EXTERN void diff --git a/core/iwasm/include/wasm_export.h b/core/iwasm/include/wasm_export.h index 273657246..9807546da 100644 --- a/core/iwasm/include/wasm_export.h +++ b/core/iwasm/include/wasm_export.h @@ -1777,6 +1777,14 @@ WASM_RUNTIME_API_EXTERN void wasm_runtime_set_native_stack_boundary(wasm_exec_env_t exec_env, uint8_t *native_stack_boundary); +/** + * Disable native stack boundary check. + * + * @param exec_env the execution environment + */ +WASM_RUNTIME_API_EXTERN void +wasm_runtime_disable_native_stack_boundary_check(wasm_exec_env_t exec_env); + /** * Dump runtime memory consumption, including: * Exec env memory consumption