Add wasm_runtime_detect_native_stack_overflow_size (#3355)
- Add a few API (https://github.com/bytecodealliance/wasm-micro-runtime/issues/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
This commit is contained in:
@ -114,7 +114,7 @@ main(int argc, char **argv)
|
||||
"--------\n");
|
||||
|
||||
unsigned int stack;
|
||||
unsigned int prevstack;
|
||||
unsigned int prevstack = 0; /* appease GCC -Wmaybe-uninitialized */
|
||||
unsigned int stack_range_start = 0;
|
||||
unsigned int stack_range_end = 4096 * 6;
|
||||
unsigned int step = 16;
|
||||
|
||||
@ -9,10 +9,6 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#if defined(__APPLE__)
|
||||
#include <Availability.h>
|
||||
#endif
|
||||
|
||||
#include "wasm_export.h"
|
||||
#include "bh_platform.h"
|
||||
|
||||
@ -45,9 +41,12 @@ host_consume_stack_and_call_indirect(wasm_exec_env_t exec_env, uint32_t funcidx,
|
||||
void *boundary = os_thread_get_stack_boundary();
|
||||
void *fp = __builtin_frame_address(0);
|
||||
ptrdiff_t diff = fp - boundary;
|
||||
if ((unsigned char *)fp < (unsigned char *)boundary + 1024 * 5) {
|
||||
wasm_runtime_set_exception(wasm_runtime_get_module_inst(exec_env),
|
||||
"native stack overflow 2");
|
||||
/*
|
||||
* because this function performs recursive calls depending on
|
||||
* the user input, we don't have an apriori knowledge how much stack
|
||||
* we need. perform the overflow check on each iteration.
|
||||
*/
|
||||
if (!wasm_runtime_detect_native_stack_overflow(exec_env)) {
|
||||
return 0;
|
||||
}
|
||||
if (diff > stack) {
|
||||
@ -63,27 +62,13 @@ host_consume_stack_and_call_indirect(wasm_exec_env_t exec_env, uint32_t funcidx,
|
||||
|
||||
__attribute__((noinline)) static uint32_t
|
||||
consume_stack1(wasm_exec_env_t exec_env, void *base, uint32_t stack)
|
||||
#if defined(__clang__)
|
||||
__attribute__((disable_tail_calls))
|
||||
#endif
|
||||
{
|
||||
void *fp = __builtin_frame_address(0);
|
||||
ptrdiff_t diff = (unsigned char *)base - (unsigned char *)fp;
|
||||
assert(diff > 0);
|
||||
char buf[16];
|
||||
/*
|
||||
* note: we prefer to use memset_s here because, unlike memset,
|
||||
* memset_s is not allowed to be optimized away.
|
||||
*
|
||||
* memset_s is available for macOS 10.13+ according to:
|
||||
* https://developer.apple.com/documentation/kernel/2876438-memset_s
|
||||
*/
|
||||
#if defined(__STDC_LIB_EXT1__) \
|
||||
|| (defined(__MAC_OS_X_VERSION_MAX_ALLOWED) \
|
||||
&& __MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3)
|
||||
memset_s(buf, sizeof(buf), 0, sizeof(buf));
|
||||
#else
|
||||
#warning memset_s is not available
|
||||
memset(buf, 0, sizeof(buf));
|
||||
#endif
|
||||
if (diff > stack) {
|
||||
return diff;
|
||||
}
|
||||
@ -93,6 +78,12 @@ consume_stack1(wasm_exec_env_t exec_env, void *base, uint32_t stack)
|
||||
uint32_t
|
||||
host_consume_stack(wasm_exec_env_t exec_env, uint32_t stack)
|
||||
{
|
||||
/*
|
||||
* this function consumes a bit more than "stack" bytes.
|
||||
*/
|
||||
if (!wasm_runtime_detect_native_stack_overflow_size(exec_env, 64 + stack)) {
|
||||
return 0;
|
||||
}
|
||||
void *base = __builtin_frame_address(0);
|
||||
return consume_stack1(exec_env, base, stack);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user