From 88cf1e36c139e6df88475bc0c4c85d8458d597b1 Mon Sep 17 00:00:00 2001 From: Huang Qi Date: Fri, 12 Aug 2022 10:17:11 +0800 Subject: [PATCH] Support custom stack guard size (#1368) Add a new option WAMR_BUILD_STACK_GUARD_SIZE to set the custom stack guard size. For most RTOS systems, we use the native stack base address as the check boundary which may be not safe as POSIX based systems (like Linux). --- build-scripts/config_common.cmake | 4 ++++ core/config.h | 6 +++--- core/iwasm/common/wasm_exec_env.c | 5 +++-- doc/build_wamr.md | 4 ++++ product-mini/platforms/nuttx/wamr.mk | 2 ++ 5 files changed, 16 insertions(+), 5 deletions(-) diff --git a/build-scripts/config_common.cmake b/build-scripts/config_common.cmake index dba0188f..593954c9 100644 --- a/build-scripts/config_common.cmake +++ b/build-scripts/config_common.cmake @@ -262,3 +262,7 @@ if (WAMR_BUILD_LOAD_CUSTOM_SECTION EQUAL 1) add_definitions (-DWASM_ENABLE_LOAD_CUSTOM_SECTION=1) message (" Load custom section enabled") endif () +if (WAMR_BUILD_STACK_GUARD_SIZE GREATER 0) + add_definitions (-DWASM_STACK_GUARD_SIZE=${WAMR_BUILD_STACK_GUARD_SIZE}) + message (" Custom stack guard size: " ${WAMR_BUILD_STACK_GUARD_SIZE}) +endif () diff --git a/core/config.h b/core/config.h index 82b8c5cf..6b4392b7 100644 --- a/core/config.h +++ b/core/config.h @@ -343,12 +343,12 @@ /* 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 +#ifndef WASM_STACK_GUARD_SIZE #if WASM_ENABLE_UVWASI != 0 /* UVWASI requires larger native stack */ -#define RESERVED_BYTES_TO_NATIVE_STACK_BOUNDARY (4096 * 6) +#define WASM_STACK_GUARD_SIZE (4096 * 6) #else -#define RESERVED_BYTES_TO_NATIVE_STACK_BOUNDARY (1024) +#define WASM_STACK_GUARD_SIZE (1024) #endif #endif diff --git a/core/iwasm/common/wasm_exec_env.c b/core/iwasm/common/wasm_exec_env.c index abc69fdc..ae05cce7 100644 --- a/core/iwasm/common/wasm_exec_env.c +++ b/core/iwasm/common/wasm_exec_env.c @@ -189,9 +189,10 @@ wasm_exec_env_set_module_inst(WASMExecEnv *exec_env, void wasm_exec_env_set_thread_info(WASMExecEnv *exec_env) { + uint8 *stack_boundary = os_thread_get_stack_boundary(); exec_env->handle = os_self_thread(); - exec_env->native_stack_boundary = os_thread_get_stack_boundary() - + RESERVED_BYTES_TO_NATIVE_STACK_BOUNDARY; + exec_env->native_stack_boundary = + stack_boundary ? stack_boundary + WASM_STACK_GUARD_SIZE : NULL; } #if WASM_ENABLE_THREAD_MGR != 0 diff --git a/doc/build_wamr.md b/doc/build_wamr.md index 4a7f26e3..555ca2ac 100644 --- a/doc/build_wamr.md +++ b/doc/build_wamr.md @@ -158,6 +158,10 @@ Currently we only profile the memory consumption of module, module_instance and > For AoT file, must use `--emit-custom-sections` to specify which sections need to be emitted into AoT file, otherwise all custom sections (except custom name section) will be ignored. +### **Stack guard size** +- **WAMR_BUILD_STACK_GUARD_SIZE**=n, default to N/A if not set. +> Note: By default, the stack guard size is 1K (1024) or 24K (if uvwasi enabled). + **Combination of configurations:** We can combine the configurations. For example, if we want to disable interpreter, enable AOT and WASI, we can run command: diff --git a/product-mini/platforms/nuttx/wamr.mk b/product-mini/platforms/nuttx/wamr.mk index 4ea3a6be..fc4cbfef 100644 --- a/product-mini/platforms/nuttx/wamr.mk +++ b/product-mini/platforms/nuttx/wamr.mk @@ -147,6 +147,8 @@ CSRCS += wasm_loader.c endif endif +CFLAGS += -DWASM_STACK_GUARD_SIZE=CONFIG_INTERPRETERS_WAMR_STACK_GUARD_SIZE + ifeq ($(CONFIG_INTERPRETERS_WAMR_SHARED_MEMORY),y) CFLAGS += -DWASM_ENABLE_SHARED_MEMORY=1 CSRCS += wasm_shared_memory.c