From 4b0a483c920eae296bacfe32e03eb5fc834068b6 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Sat, 29 Jan 2022 18:23:58 +0800 Subject: [PATCH 1/3] Fix stack overflow of uvwasi --- core/config.h | 7 +++++++ core/shared/platform/android/platform_internal.h | 4 ++-- core/shared/platform/darwin/platform_internal.h | 4 ++-- core/shared/platform/linux/platform_internal.h | 4 ++-- core/shared/platform/vxworks/platform_internal.h | 2 +- core/shared/platform/windows/platform_internal.h | 4 ++-- 6 files changed, 16 insertions(+), 9 deletions(-) diff --git a/core/config.h b/core/config.h index cd3a0eef2d..d90ff24b24 100644 --- a/core/config.h +++ b/core/config.h @@ -286,6 +286,8 @@ #define WASM_THREAD_AUX_STACK_SIZE_MIN (256) /* Default/min/max 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) @@ -297,16 +299,21 @@ #define APP_THREAD_STACK_SIZE_DEFAULT (32 * 1024) #define APP_THREAD_STACK_SIZE_MIN (24 * 1024) #endif +#endif #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 */ +#ifndef RESERVED_BYTES_TO_NATIVE_STACK_BOUNDARY #define RESERVED_BYTES_TO_NATIVE_STACK_BOUNDARY (512) +#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 diff --git a/core/shared/platform/android/platform_internal.h b/core/shared/platform/android/platform_internal.h index f7dea7f2b2..601a6d81c8 100644 --- a/core/shared/platform/android/platform_internal.h +++ b/core/shared/platform/android/platform_internal.h @@ -60,7 +60,7 @@ typedef pthread_t korp_thread; #define bh_socket_t int -#if WASM_DISABLE_HW_BOUND_CHECK == 0 +#if WASM_DISABLE_HW_BOUND_CHECK == 0 && WASM_ENABLE_AOT != 0 #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \ || defined(BUILD_TARGET_AARCH64) || defined(BUILD_TARGET_RISCV64_LP64D) \ || defined(BUILD_TARGET_RISCV64_LP64) @@ -94,7 +94,7 @@ os_signal_unmask(); void os_sigreturn(); #endif /* end of BUILD_TARGET_X86_64/AMD_64/AARCH64/RISCV64 */ -#endif /* end of WASM_DISABLE_HW_BOUND_CHECK */ +#endif /* end of WASM_DISABLE_HW_BOUND_CHECK == 0 && WASM_ENABLE_AOT != 0 */ typedef long int __syscall_slong_t; diff --git a/core/shared/platform/darwin/platform_internal.h b/core/shared/platform/darwin/platform_internal.h index 0a647ab99f..891c878a29 100644 --- a/core/shared/platform/darwin/platform_internal.h +++ b/core/shared/platform/darwin/platform_internal.h @@ -61,7 +61,7 @@ typedef pthread_t korp_thread; #define bh_socket_t int -#if WASM_DISABLE_HW_BOUND_CHECK == 0 +#if WASM_DISABLE_HW_BOUND_CHECK == 0 && WASM_ENABLE_AOT != 0 #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \ || defined(BUILD_TARGET_AARCH64) || defined(BUILD_TARGET_RISCV64_LP64D) \ || defined(BUILD_TARGET_RISCV64_LP64) @@ -95,7 +95,7 @@ os_signal_unmask(); void os_sigreturn(); #endif /* end of BUILD_TARGET_X86_64/AMD_64/AARCH64/RISCV64 */ -#endif /* end of WASM_DISABLE_HW_BOUND_CHECK */ +#endif /* end of WASM_DISABLE_HW_BOUND_CHECK == 0 && WASM_ENABLE_AOT != 0 */ #ifdef __cplusplus } diff --git a/core/shared/platform/linux/platform_internal.h b/core/shared/platform/linux/platform_internal.h index 5552104265..445f6df849 100644 --- a/core/shared/platform/linux/platform_internal.h +++ b/core/shared/platform/linux/platform_internal.h @@ -60,7 +60,7 @@ typedef pthread_t korp_thread; #define bh_socket_t int -#if WASM_DISABLE_HW_BOUND_CHECK == 0 +#if WASM_DISABLE_HW_BOUND_CHECK == 0 && WASM_ENABLE_AOT != 0 #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \ || defined(BUILD_TARGET_AARCH64) || defined(BUILD_TARGET_RISCV64_LP64D) \ || defined(BUILD_TARGET_RISCV64_LP64) @@ -94,7 +94,7 @@ os_signal_unmask(); void os_sigreturn(); #endif /* end of BUILD_TARGET_X86_64/AMD_64/AARCH64/RISCV64 */ -#endif /* end of WASM_DISABLE_HW_BOUND_CHECK */ +#endif /* end of WASM_DISABLE_HW_BOUND_CHECK == 0 && WASM_ENABLE_AOT != 0 */ #ifdef __cplusplus } diff --git a/core/shared/platform/vxworks/platform_internal.h b/core/shared/platform/vxworks/platform_internal.h index 9fa30722c6..6b5736d843 100644 --- a/core/shared/platform/vxworks/platform_internal.h +++ b/core/shared/platform/vxworks/platform_internal.h @@ -57,7 +57,7 @@ typedef pthread_t korp_thread; #define os_thread_local_attribute __thread -#if WASM_DISABLE_HW_BOUND_CHECK == 0 +#if WASM_DISABLE_HW_BOUND_CHECK == 0 && WASM_ENABLE_AOT != 0 #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \ || defined(BUILD_TARGET_AARCH64) diff --git a/core/shared/platform/windows/platform_internal.h b/core/shared/platform/windows/platform_internal.h index 5c50f9e30e..d130eaa4c7 100644 --- a/core/shared/platform/windows/platform_internal.h +++ b/core/shared/platform/windows/platform_internal.h @@ -77,7 +77,7 @@ os_mem_decommit(void *ptr, size_t size); #define strncasecmp _strnicmp #define strcasecmp _stricmp -#if WASM_DISABLE_HW_BOUND_CHECK == 0 +#if WASM_DISABLE_HW_BOUND_CHECK == 0 && WASM_ENABLE_AOT != 0 #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) #include @@ -102,7 +102,7 @@ os_thread_signal_inited(); #define os_sigreturn() (void)0 #endif /* end of BUILD_TARGET_X86_64/AMD_64 */ -#endif /* end of WASM_DISABLE_HW_BOUND_CHECK */ +#endif /* end of WASM_DISABLE_HW_BOUND_CHECK == 0 && WASM_ENABLE_AOT != 0 */ #ifdef __cplusplus } From 1db86873785d0e42bf8b8c4561283c942c7246bb Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Sat, 29 Jan 2022 20:11:03 +0800 Subject: [PATCH 2/3] Increase stack size for uvwasi --- core/config.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/core/config.h b/core/config.h index d90ff24b24..e9d8754bf8 100644 --- a/core/config.h +++ b/core/config.h @@ -295,6 +295,9 @@ #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 +#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) @@ -307,7 +310,11 @@ /* Reserved bytes to the native thread stack boundary, throw native stack overflow exception if the guard boudary is reached */ #ifndef RESERVED_BYTES_TO_NATIVE_STACK_BOUNDARY -#define RESERVED_BYTES_TO_NATIVE_STACK_BOUNDARY (512) +#if WASM_ENABLE_UVWASI != 0 +#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 */ From ad8734ed5b1612c215392ef2467b4ab0a2e9b195 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Thu, 10 Feb 2022 11:16:23 +0800 Subject: [PATCH 3/3] Disable hw bound check when AOT isn't enabled --- build-scripts/config_common.cmake | 4 ++++ core/config.h | 9 +++++++-- core/shared/platform/android/platform_internal.h | 4 ++-- core/shared/platform/darwin/platform_internal.h | 4 ++-- core/shared/platform/linux/platform_internal.h | 4 ++-- core/shared/platform/vxworks/platform_internal.h | 2 +- core/shared/platform/windows/platform_internal.h | 4 ++-- 7 files changed, 20 insertions(+), 11 deletions(-) diff --git a/build-scripts/config_common.cmake b/build-scripts/config_common.cmake index 19d12b6a43..46c5981e48 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 e9d8754bf8..9017ba7c90 100644 --- a/core/config.h +++ b/core/config.h @@ -285,7 +285,7 @@ /* 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) \ @@ -296,13 +296,17 @@ #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 +#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 @@ -311,6 +315,7 @@ stack overflow exception if the guard boudary is reached */ #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) diff --git a/core/shared/platform/android/platform_internal.h b/core/shared/platform/android/platform_internal.h index 601a6d81c8..f7dea7f2b2 100644 --- a/core/shared/platform/android/platform_internal.h +++ b/core/shared/platform/android/platform_internal.h @@ -60,7 +60,7 @@ typedef pthread_t korp_thread; #define bh_socket_t int -#if WASM_DISABLE_HW_BOUND_CHECK == 0 && WASM_ENABLE_AOT != 0 +#if WASM_DISABLE_HW_BOUND_CHECK == 0 #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \ || defined(BUILD_TARGET_AARCH64) || defined(BUILD_TARGET_RISCV64_LP64D) \ || defined(BUILD_TARGET_RISCV64_LP64) @@ -94,7 +94,7 @@ os_signal_unmask(); void os_sigreturn(); #endif /* end of BUILD_TARGET_X86_64/AMD_64/AARCH64/RISCV64 */ -#endif /* end of WASM_DISABLE_HW_BOUND_CHECK == 0 && WASM_ENABLE_AOT != 0 */ +#endif /* end of WASM_DISABLE_HW_BOUND_CHECK */ typedef long int __syscall_slong_t; diff --git a/core/shared/platform/darwin/platform_internal.h b/core/shared/platform/darwin/platform_internal.h index 891c878a29..0a647ab99f 100644 --- a/core/shared/platform/darwin/platform_internal.h +++ b/core/shared/platform/darwin/platform_internal.h @@ -61,7 +61,7 @@ typedef pthread_t korp_thread; #define bh_socket_t int -#if WASM_DISABLE_HW_BOUND_CHECK == 0 && WASM_ENABLE_AOT != 0 +#if WASM_DISABLE_HW_BOUND_CHECK == 0 #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \ || defined(BUILD_TARGET_AARCH64) || defined(BUILD_TARGET_RISCV64_LP64D) \ || defined(BUILD_TARGET_RISCV64_LP64) @@ -95,7 +95,7 @@ os_signal_unmask(); void os_sigreturn(); #endif /* end of BUILD_TARGET_X86_64/AMD_64/AARCH64/RISCV64 */ -#endif /* end of WASM_DISABLE_HW_BOUND_CHECK == 0 && WASM_ENABLE_AOT != 0 */ +#endif /* end of WASM_DISABLE_HW_BOUND_CHECK */ #ifdef __cplusplus } diff --git a/core/shared/platform/linux/platform_internal.h b/core/shared/platform/linux/platform_internal.h index 445f6df849..5552104265 100644 --- a/core/shared/platform/linux/platform_internal.h +++ b/core/shared/platform/linux/platform_internal.h @@ -60,7 +60,7 @@ typedef pthread_t korp_thread; #define bh_socket_t int -#if WASM_DISABLE_HW_BOUND_CHECK == 0 && WASM_ENABLE_AOT != 0 +#if WASM_DISABLE_HW_BOUND_CHECK == 0 #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \ || defined(BUILD_TARGET_AARCH64) || defined(BUILD_TARGET_RISCV64_LP64D) \ || defined(BUILD_TARGET_RISCV64_LP64) @@ -94,7 +94,7 @@ os_signal_unmask(); void os_sigreturn(); #endif /* end of BUILD_TARGET_X86_64/AMD_64/AARCH64/RISCV64 */ -#endif /* end of WASM_DISABLE_HW_BOUND_CHECK == 0 && WASM_ENABLE_AOT != 0 */ +#endif /* end of WASM_DISABLE_HW_BOUND_CHECK */ #ifdef __cplusplus } diff --git a/core/shared/platform/vxworks/platform_internal.h b/core/shared/platform/vxworks/platform_internal.h index 6b5736d843..9fa30722c6 100644 --- a/core/shared/platform/vxworks/platform_internal.h +++ b/core/shared/platform/vxworks/platform_internal.h @@ -57,7 +57,7 @@ typedef pthread_t korp_thread; #define os_thread_local_attribute __thread -#if WASM_DISABLE_HW_BOUND_CHECK == 0 && WASM_ENABLE_AOT != 0 +#if WASM_DISABLE_HW_BOUND_CHECK == 0 #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) \ || defined(BUILD_TARGET_AARCH64) diff --git a/core/shared/platform/windows/platform_internal.h b/core/shared/platform/windows/platform_internal.h index d130eaa4c7..5c50f9e30e 100644 --- a/core/shared/platform/windows/platform_internal.h +++ b/core/shared/platform/windows/platform_internal.h @@ -77,7 +77,7 @@ os_mem_decommit(void *ptr, size_t size); #define strncasecmp _strnicmp #define strcasecmp _stricmp -#if WASM_DISABLE_HW_BOUND_CHECK == 0 && WASM_ENABLE_AOT != 0 +#if WASM_DISABLE_HW_BOUND_CHECK == 0 #if defined(BUILD_TARGET_X86_64) || defined(BUILD_TARGET_AMD_64) #include @@ -102,7 +102,7 @@ os_thread_signal_inited(); #define os_sigreturn() (void)0 #endif /* end of BUILD_TARGET_X86_64/AMD_64 */ -#endif /* end of WASM_DISABLE_HW_BOUND_CHECK == 0 && WASM_ENABLE_AOT != 0 */ +#endif /* end of WASM_DISABLE_HW_BOUND_CHECK */ #ifdef __cplusplus }