Skip to content

Commit 74d2427

Browse files
authored
Allow to set native stack boundary to exec_env (#3862)
Add runtime API wasm_runtime_set_native_stack_boundary. p.s. #3816
1 parent 7e625a0 commit 74d2427

File tree

5 files changed

+43
-2
lines changed

5 files changed

+43
-2
lines changed

core/iwasm/common/wasm_exec_env.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,13 @@ wasm_exec_env_set_thread_info(WASMExecEnv *exec_env)
282282
os_mutex_lock(&exec_env->wait_lock);
283283
#endif
284284
exec_env->handle = os_self_thread();
285-
exec_env->native_stack_boundary =
286-
stack_boundary ? stack_boundary + WASM_STACK_GUARD_SIZE : NULL;
285+
if (exec_env->user_native_stack_boundary)
286+
/* WASM_STACK_GUARD_SIZE isn't added for flexibility to developer,
287+
he must ensure that enough guard bytes are kept. */
288+
exec_env->native_stack_boundary = exec_env->user_native_stack_boundary;
289+
else
290+
exec_env->native_stack_boundary =
291+
stack_boundary ? stack_boundary + WASM_STACK_GUARD_SIZE : NULL;
287292
exec_env->native_stack_top_min = (void *)UINTPTR_MAX;
288293
#if WASM_ENABLE_THREAD_MGR != 0
289294
os_mutex_unlock(&exec_env->wait_lock);

core/iwasm/common/wasm_exec_env.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ typedef struct WASMExecEnv {
136136

137137
void *user_data;
138138

139+
/* The boundary of native stack set by host embedder. It is used
140+
if it is not NULL when calling wasm functions. */
141+
uint8 *user_native_stack_boundary;
142+
139143
/* The native thread handle of current thread */
140144
korp_tid handle;
141145

core/iwasm/common/wasm_runtime_common.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2225,6 +2225,13 @@ wasm_runtime_get_user_data(WASMExecEnv *exec_env)
22252225
return exec_env->user_data;
22262226
}
22272227

2228+
void
2229+
wasm_runtime_set_native_stack_boundary(WASMExecEnv *exec_env,
2230+
uint8 *native_stack_boundary)
2231+
{
2232+
exec_env->user_native_stack_boundary = native_stack_boundary;
2233+
}
2234+
22282235
#ifdef OS_ENABLE_HW_BOUND_CHECK
22292236
void
22302237
wasm_runtime_access_exce_check_guard_page()

core/iwasm/common/wasm_runtime_common.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,11 @@ wasm_runtime_set_user_data(WASMExecEnv *exec_env, void *user_data);
673673
WASM_RUNTIME_API_EXTERN void *
674674
wasm_runtime_get_user_data(WASMExecEnv *exec_env);
675675

676+
/* See wasm_export.h for description */
677+
WASM_RUNTIME_API_EXTERN void
678+
wasm_runtime_set_native_stack_boundary(WASMExecEnv *exec_env,
679+
uint8 *native_stack_boundary);
680+
676681
#if WASM_CONFIGURABLE_BOUNDS_CHECKS != 0
677682
/* See wasm_export.h for description */
678683
WASM_RUNTIME_API_EXTERN void

core/iwasm/include/wasm_export.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1755,6 +1755,26 @@ wasm_runtime_set_user_data(wasm_exec_env_t exec_env, void *user_data);
17551755
WASM_RUNTIME_API_EXTERN void *
17561756
wasm_runtime_get_user_data(wasm_exec_env_t exec_env);
17571757

1758+
/**
1759+
* Set native stack boundary to execution environment, if it is set,
1760+
* it will be used instead of getting the boundary with the platform
1761+
* layer API when calling wasm functions. This is useful for some
1762+
* fiber cases.
1763+
*
1764+
* Note: unlike setting the boundary by runtime, this API doesn't add
1765+
* the WASM_STACK_GUARD_SIZE(see comments in core/config.h) to the
1766+
* exec_env's native_stack_boundary to reserve bytes to the native
1767+
* thread stack boundary, which is used to throw native stack overflow
1768+
* exception if the guard boundary is reached. Developer should ensure
1769+
* that enough guard bytes are kept.
1770+
*
1771+
* @param exec_env the execution environment
1772+
* @param native_stack_boundary the user data to be set
1773+
*/
1774+
WASM_RUNTIME_API_EXTERN void
1775+
wasm_runtime_set_native_stack_boundary(wasm_exec_env_t exec_env,
1776+
uint8_t *native_stack_boundary);
1777+
17581778
/**
17591779
* Dump runtime memory consumption, including:
17601780
* Exec env memory consumption

0 commit comments

Comments
 (0)