From ff9eb6083322ac3ab5a633c3ca497c590c4b60db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Ibarra=20Corretg=C3=A9?= Date: Fri, 7 Mar 2025 09:55:16 +0100 Subject: [PATCH 1/2] Remove explicit stack size from api-test Let it overflow. --- api-test.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/api-test.c b/api-test.c index 1c604c7b2..84b275815 100644 --- a/api-test.c +++ b/api-test.c @@ -8,7 +8,7 @@ #define MAX_TIME 10 -static int timeout_interrupt_handler(JSRuntime *rt, void *opaque) +static int timeout_interrupt_handler(JSRuntime *rt, void *opaque) { int *time = (int *)opaque; if (*time <= MAX_TIME) @@ -18,7 +18,7 @@ static int timeout_interrupt_handler(JSRuntime *rt, void *opaque) static void sync_call(void) { - const char *code = + const char *code = "(function() { \ try { \ while (true) {} \ @@ -43,7 +43,7 @@ static void sync_call(void) static void async_call(void) { - const char *code = + const char *code = "(async function() { \ const loop = async () => { \ await Promise.resolve(); \ @@ -97,8 +97,6 @@ static void async_call_stack_overflow(void) JSRuntime *rt = JS_NewRuntime(); JSContext *ctx = JS_NewContext(rt); - JS_SetMaxStackSize(rt, 128 * 1024); - JS_UpdateStackTop(rt); JSValue value = JS_UNDEFINED; JS_SetContextOpaque(ctx, &value); JSValue global = JS_GetGlobalObject(ctx); From ebaca1a09e754292b80ac85332a50244f8ac64ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Ibarra=20Corretg=C3=A9?= Date: Fri, 7 Mar 2025 10:00:08 +0100 Subject: [PATCH 2/2] Set default stack size on Windows When compiling with MSVC (that includes ClangCL) it defaults to 1MB, which means our default of 1MB in the interpreter is too large to detect actual overflow. When compiling with MinGW GCC/Clang it defaults to 2MB. Set it to 8MB like Linux for some consistency. --- CMakeLists.txt | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index d445fca60..133de63f5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -70,6 +70,18 @@ if(MSVC) xcheck_add_c_compiler_flag(/wd5045) # Compiler will insert Spectre mitigation for memory load if /Qspectre switch specified endif() +# Set a 8MB default stack size on Windows. +# It defaults to 1MB on MSVC, which is the same as our current JS stack size, +# so it will overflow and crash otherwise. +# On MinGW it defaults to 2MB. +if(WIN32) + if(MSVC) + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /STACK:8388608") + else() + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--stack,8388608") + endif() +endif() + # MacOS and GCC 11 or later need -Wno-maybe-uninitialized # https://github.com/quickjs-ng/quickjs/issues/453 if(APPLE AND CMAKE_C_COMPILER_ID STREQUAL "GNU" AND CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 11)