Skip to content

Commit

Permalink
Add API for disabling native stack boundary checks
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
loganek committed Jan 31, 2025
1 parent b6dea22 commit e6c729b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
4 changes: 3 additions & 1 deletion core/iwasm/common/wasm_exec_env.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions core/iwasm/common/wasm_runtime_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
exec_env->user_native_stack_boundary = (void *)UINTPTR_MAX;
}

#ifdef OS_ENABLE_HW_BOUND_CHECK
void
wasm_runtime_access_exce_check_guard_page()
Expand Down
4 changes: 4 additions & 0 deletions core/iwasm/common/wasm_runtime_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions core/iwasm/include/wasm_export.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e6c729b

Please sign in to comment.