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:
@ -72,6 +72,11 @@ if (CMAKE_C_COMPILER_ID MATCHES "Clang" AND CMAKE_C_COMPILER_VERSION VERSION_GRE
|
||||
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fstack-usage")
|
||||
endif ()
|
||||
|
||||
# GCC doesn't have disable_tail_calls attribute
|
||||
if (CMAKE_C_COMPILER_ID MATCHES "GNU")
|
||||
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-optimize-sibling-calls")
|
||||
endif ()
|
||||
|
||||
# build out vmlib
|
||||
set (WAMR_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../..)
|
||||
include (${WAMR_ROOT_DIR}/build-scripts/runtime_lib.cmake)
|
||||
|
||||
@ -69,7 +69,7 @@ echo "#################### build wasm apps done"
|
||||
|
||||
echo "#################### aot-compile"
|
||||
WAMRC=${WAMR_DIR}/wamr-compiler/build/wamrc
|
||||
${WAMRC} -o ${OUT_DIR}/wasm-apps/${OUT_FILE}.aot ${OUT_DIR}/wasm-apps/${OUT_FILE}
|
||||
${WAMRC} -o ${OUT_DIR}/wasm-apps/${OUT_FILE}.aot --size-level=0 ${OUT_DIR}/wasm-apps/${OUT_FILE}
|
||||
|
||||
echo "#################### aot-compile (--bounds-checks=1)"
|
||||
${WAMRC} -o ${OUT_DIR}/wasm-apps/${OUT_FILE}.aot.bounds-checks --bounds-checks=1 ${OUT_DIR}/wasm-apps/${OUT_FILE}
|
||||
${WAMRC} -o ${OUT_DIR}/wasm-apps/${OUT_FILE}.aot.bounds-checks --size-level=0 --bounds-checks=1 ${OUT_DIR}/wasm-apps/${OUT_FILE}
|
||||
|
||||
@ -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