Skip to content

Commit f1bad78

Browse files
yamtvictoryang00
authored andcommitted
Add wasm_runtime_detect_native_stack_overflow_size (#3355)
- Add a few API (#3325) ```c wasm_runtime_detect_native_stack_overflow_size wasm_runtime_detect_native_stack_overflow ``` - Adapt the runtime to use them - Adapt samples/native-stack-overflow to use them - Add a few missing overflow checks in the interpreters - Build and run the sample on the CI Signed-off-by: victoryang00 <[email protected]>
1 parent 55097ce commit f1bad78

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

core/iwasm/include/wasm_export.h

+51
Original file line numberDiff line numberDiff line change
@@ -1998,6 +1998,57 @@ wasm_runtime_set_module_name(wasm_module_t module, const char *name,
19981998
WASM_RUNTIME_API_EXTERN const char *
19991999
wasm_runtime_get_module_name(wasm_module_t module);
20002000

2001+
/*
2002+
* wasm_runtime_detect_native_stack_overflow
2003+
*
2004+
* Detect native stack shortage.
2005+
* Ensure that the calling thread still has a reasonable amount of
2006+
* native stack (WASM_STACK_GUARD_SIZE bytes) available.
2007+
*
2008+
* If enough stack is left, this function returns true.
2009+
* Otherwise, this function raises a "native stack overflow" trap and
2010+
* returns false.
2011+
*
2012+
* Note: please do not expect a very strict detection. it's a good idea
2013+
* to give some margins. wasm_runtime_detect_native_stack_overflow itself
2014+
* requires a small amount of stack to run.
2015+
*/
2016+
WASM_RUNTIME_API_EXTERN bool
2017+
wasm_runtime_detect_native_stack_overflow(wasm_exec_env_t exec_env);
2018+
2019+
/*
2020+
* wasm_runtime_detect_native_stack_overflow_size
2021+
*
2022+
* Similar to wasm_runtime_detect_native_stack_overflow,
2023+
* but use the caller-specified size instead of WASM_STACK_GUARD_SIZE.
2024+
*
2025+
* An expected usage:
2026+
* ```c
2027+
* __attribute__((noinline)) // inlining can break the stack check
2028+
* void stack_hog(void)
2029+
* {
2030+
* // consume a lot of stack here
2031+
* }
2032+
*
2033+
* void
2034+
* stack_hog_wrapper(exec_env) {
2035+
* // the amount of stack stack_hog would consume,
2036+
* // plus a small margin
2037+
* uint32_t size = 10000000;
2038+
*
2039+
* if (!wasm_runtime_detect_native_stack_overflow_size(exec_env, size)) {
2040+
* // wasm_runtime_detect_native_stack_overflow_size has raised
2041+
* // a trap.
2042+
* return;
2043+
* }
2044+
* stack_hog();
2045+
* }
2046+
* ```
2047+
*/
2048+
WASM_RUNTIME_API_EXTERN bool
2049+
wasm_runtime_detect_native_stack_overflow_size(wasm_exec_env_t exec_env,
2050+
uint32_t required_size);
2051+
20012052
#ifdef __cplusplus
20022053
}
20032054
#endif

0 commit comments

Comments
 (0)