diff --git a/build-scripts/config_common.cmake b/build-scripts/config_common.cmake index 3b7728f56..f22691dd6 100644 --- a/build-scripts/config_common.cmake +++ b/build-scripts/config_common.cmake @@ -162,4 +162,7 @@ if (WAMR_DISABLE_HW_BOUND_CHECK EQUAL 1) add_definitions (-DWASM_DISABLE_HW_BOUND_CHECK=1) message (" Hardware boundary check disabled") endif () +if (DEFINED WAMR_APP_THREAD_STACK_SIZE_MAX) + add_definitions (-DAPP_THREAD_STACK_SIZE_MAX=${WAMR_APP_THREAD_STACK_SIZE_MAX}) +endif () diff --git a/core/config.h b/core/config.h index ba9a1a2fd..414056b8a 100644 --- a/core/config.h +++ b/core/config.h @@ -216,11 +216,12 @@ enum { #if !defined(BH_PLATFORM_ZEPHYR) && !defined(BH_PLATFORM_ALIOS_THINGS) #define APP_THREAD_STACK_SIZE_DEFAULT (32 * 1024) #define APP_THREAD_STACK_SIZE_MIN (24 * 1024) -#define APP_THREAD_STACK_SIZE_MAX (256 * 1024) #else #define APP_THREAD_STACK_SIZE_DEFAULT (6 * 1024) #define APP_THREAD_STACK_SIZE_MIN (4 * 1024) -#define APP_THREAD_STACK_SIZE_MAX (256 * 1024) +#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 diff --git a/core/shared/platform/common/posix/posix_thread.c b/core/shared/platform/common/posix/posix_thread.c index 58d8fee7e..db9adbf61 100644 --- a/core/shared/platform/common/posix/posix_thread.c +++ b/core/shared/platform/common/posix/posix_thread.c @@ -242,12 +242,19 @@ uint8 *os_thread_get_stack_boundary() uint8 *addr = NULL; size_t stack_size, guard_size; int page_size = getpagesize(); + size_t max_stack_size = (APP_THREAD_STACK_SIZE_MAX + page_size - 1) + & ~(page_size - 1); + + if (max_stack_size < APP_THREAD_STACK_SIZE_DEFAULT) + max_stack_size = APP_THREAD_STACK_SIZE_DEFAULT; #ifdef __linux__ if (pthread_getattr_np(self, &attr) == 0) { pthread_attr_getstack(&attr, (void**)&addr, &stack_size); pthread_attr_getguardsize(&attr, &guard_size); pthread_attr_destroy(&attr); + if (stack_size > max_stack_size) + addr = addr + stack_size - max_stack_size; if (guard_size < (size_t)page_size) /* Reserved 1 guard page at least for safety */ guard_size = (size_t)page_size; @@ -257,7 +264,10 @@ uint8 *os_thread_get_stack_boundary() #elif defined(__APPLE__) if ((addr = (uint8*)pthread_get_stackaddr_np(self))) { stack_size = pthread_get_stacksize_np(self); - addr -= stack_size; + if (stack_size > max_stack_size) + addr -= max_stack_size; + else + addr -= stack_size; /* Reserved 1 guard page at least for safety */ addr += page_size; } diff --git a/doc/build_wamr.md b/doc/build_wamr.md index 378fb57a9..c0b36f29b 100644 --- a/doc/build_wamr.md +++ b/doc/build_wamr.md @@ -68,6 +68,10 @@ cmake -DWAMR_BUILD_PLATFORM=linux -DWAMR_BUILD_TARGET=ARM - **WAMR_DISABLE_HW_BOUND_CHECK**=1/0, default to enable if not set and supported by platform > Note: by default only platform linux/darwin/android/vxworks 64-bit will enable boundary check with hardware trap in AOT or JIT mode, and the wamrc tool will generate AOT code without boundary check instructions in all 64-bit targets except SGX to improve performance. +#### **Set maximum app thread stack size** +- **WAMR_APP_THREAD_STACK_SIZE_MAX**=n, default to 8 MB (8388608) if not set +> Note: the AOT boundary check with hardware trap mechanism might consume large stack since the OS may lazily grow the stack mapping as a guard page is hit, we may use this configuration to reduce the total stack usage, e.g. -DWAMR_APP_THREAD_STACK_SIZE_MAX=131072 (128 KB). + **Combination of configurations:** We can combine the configurations. For example, if we want to disable interpreter, enable AOT and WASI, we can run command: