From 59282f7ddb5347beff34b941c42e42bb72bb7a0f Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Fri, 11 Feb 2022 11:43:03 +0800 Subject: [PATCH] Fix native stack overflow check failed in interpreter (#992) Increase default/min native stack size when UVWASI is enabled as UVWASI requires larger native stack size. Increase the reserved bytes to the native thread stack boundary to better detect the native stack overflow. Set WASM_DISABLE_HW_BOUND_CHECK to 0 when interpreter is enabled and AOT is disabled, as memory access boundary check with hardware trap is only enabled in AOT/JIT mode. --- build-scripts/config_common.cmake | 4 ++++ core/config.h | 23 +++++++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/build-scripts/config_common.cmake b/build-scripts/config_common.cmake index 19d12b6a..46c5981e 100644 --- a/build-scripts/config_common.cmake +++ b/build-scripts/config_common.cmake @@ -198,6 +198,10 @@ endif () if (WAMR_DISABLE_HW_BOUND_CHECK EQUAL 1) add_definitions (-DWASM_DISABLE_HW_BOUND_CHECK=1) message (" Hardware boundary check disabled") +elseif (NOT WAMR_BUILD_AOT EQUAL 1) + # Enable memory access boundary check with hardware trap + # only when AOT/JIT is enabled + add_definitions (-DWASM_DISABLE_HW_BOUND_CHECK=1) else () add_definitions (-DWASM_DISABLE_HW_BOUND_CHECK=0) endif () diff --git a/core/config.h b/core/config.h index cd3a0eef..9017ba7c 100644 --- a/core/config.h +++ b/core/config.h @@ -285,7 +285,9 @@ /* Min auxilliary stack size of each wasm thread */ #define WASM_THREAD_AUX_STACK_SIZE_MIN (256) -/* Default/min/max stack size of each app thread */ +/* Default/min native stack size of each app thread */ +#if !(defined(APP_THREAD_STACK_SIZE_DEFAULT) \ + && defined(APP_THREAD_STACK_SIZE_MIN)) #if defined(BH_PLATFORM_ZEPHYR) || defined(BH_PLATFORM_ALIOS_THINGS) \ || defined(BH_PLATFORM_ESP_IDF) || defined(BH_PLATFORM_OPENRTOS) #define APP_THREAD_STACK_SIZE_DEFAULT (6 * 1024) @@ -293,20 +295,37 @@ #elif defined(PTHREAD_STACK_DEFAULT) && defined(PTHREAD_STACK_MIN) #define APP_THREAD_STACK_SIZE_DEFAULT PTHREAD_STACK_DEFAULT #define APP_THREAD_STACK_SIZE_MIN PTHREAD_STACK_MIN +#elif WASM_ENABLE_UVWASI != 0 +/* UVWASI requires larger native stack */ +#define APP_THREAD_STACK_SIZE_DEFAULT (64 * 1024) +#define APP_THREAD_STACK_SIZE_MIN (48 * 1024) #else #define APP_THREAD_STACK_SIZE_DEFAULT (32 * 1024) #define APP_THREAD_STACK_SIZE_MIN (24 * 1024) #endif +#endif /* end of !(defined(APP_THREAD_STACK_SIZE_DEFAULT) \ + && defined(APP_THREAD_STACK_SIZE_MIN)) */ + +/* Max native stack size of each app thread */ #if !defined(APP_THREAD_STACK_SIZE_MAX) #define APP_THREAD_STACK_SIZE_MAX (8 * 1024 * 1024) #endif /* Reserved bytes to the native thread stack boundary, throw native stack overflow exception if the guard boudary is reached */ -#define RESERVED_BYTES_TO_NATIVE_STACK_BOUNDARY (512) +#ifndef RESERVED_BYTES_TO_NATIVE_STACK_BOUNDARY +#if WASM_ENABLE_UVWASI != 0 +/* UVWASI requires larger native stack */ +#define RESERVED_BYTES_TO_NATIVE_STACK_BOUNDARY (4096 * 6) +#else +#define RESERVED_BYTES_TO_NATIVE_STACK_BOUNDARY (1024) +#endif +#endif /* Guard page count for stack overflow check with hardware trap */ +#ifndef STACK_OVERFLOW_CHECK_GUARD_PAGE_COUNT #define STACK_OVERFLOW_CHECK_GUARD_PAGE_COUNT 3 +#endif /* Default wasm block address cache size and conflict list size */ #ifndef BLOCK_ADDR_CACHE_SIZE