From 3f585c370431c5cb3d89b6b3267ad9ca9b6b0937 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Wed, 24 Apr 2024 17:18:58 +0900 Subject: [PATCH] Reduce WASM_STACK_GUARD_SIZE a bit for posix-like platforms (#3350) I found a few mistakes in my research on the stack consumption. Update the comment and tweak WASM_STACK_GUARD_SIZE accordingly. Signed-off-by: victoryang00 --- core/config.h | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/core/config.h b/core/config.h index e74306e15e..1f47923a67 100644 --- a/core/config.h +++ b/core/config.h @@ -468,9 +468,12 @@ * * - w/o hw bound check, the intepreter loop * - * the classic interpreter wasm_interp_call_func_bytecode alone - * seems to consume about 2600 bytes stack. - * (with the default configuration for macOS/amd64) + * the stack consumption heavily depends on compiler settings, + * especially for huge functions like the classic interpreter's + * wasm_interp_call_func_bytecode: + * + * 200 bytes (release build, macOS/amd64) + * 2600 bytes (debug build, macOS/amd64) * * libc snprintf (used by eg. wasm_runtime_set_exception) consumes about * 1600 bytes stack on macOS/amd64, about 2000 bytes on Ubuntu amd64 20.04. @@ -485,6 +488,10 @@ * Note: on platforms with lazy function binding, don't forget to consider * the symbol resolution overhead on the first call. For example, * on Ubuntu amd64 20.04, it seems to consume about 1500 bytes. + * For some reasons, macOS amd64 12.7.4 seems to resolve symbols eagerly. + * (Observed with a binary with traditional non-chained fixups.) + * The latest macOS seems to apply chained fixups in kernel on page-in time. + * (thus it wouldn't consume userland stack.) */ #ifndef WASM_STACK_GUARD_SIZE #if WASM_ENABLE_UVWASI != 0 @@ -494,15 +501,20 @@ /* * Use a larger default for platforms like macOS/Linux. * - * For example, wasm_interp_call_func_bytecode + wasm_runtime_set_exception - * would consume >4KB stack on x86-64 macOS. + * For example, the classic intepreter loop which ended up with a trap + * (wasm_runtime_set_exception) would consume about 2KB stack on x86-64 + * macOS. On Ubuntu amd64 20.04, it seems to consume a bit more. * * Although product-mini/platforms/nuttx always overrides * WASM_STACK_GUARD_SIZE, exclude NuttX here just in case. */ #if defined(__APPLE__) || (defined(__unix__) && !defined(__NuttX__)) +#if BH_DEBUG != 0 /* assumption: BH_DEBUG matches CMAKE_BUILD_TYPE=Debug */ #define WASM_STACK_GUARD_SIZE (1024 * 5) #else +#define WASM_STACK_GUARD_SIZE (1024 * 3) +#endif +#else /* * Otherwise, assume very small requirement for now. *