From beb1b15f7fc4ab4527667cabd02047d4ed5afef9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sa=C3=BAl=20Ibarra=20Corretg=C3=A9?= Date: Mon, 16 Dec 2024 09:25:53 +0100 Subject: [PATCH] Use 1 MB as the default stack size In addition: - Move the WASI override to quickjs.c - Allow it to be user defined Ref: https://github.com/quickjs-ng/quickjs/issues/749#issuecomment-2540167690 --- quickjs-libc.c | 2 +- quickjs.c | 4 ++++ quickjs.h | 6 ++---- run-test262.c | 2 +- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/quickjs-libc.c b/quickjs-libc.c index 24e7a843..6dbe501e 100644 --- a/quickjs-libc.c +++ b/quickjs-libc.c @@ -3636,7 +3636,7 @@ static JSValue js_worker_ctor(JSContext *ctx, JSValue new_target, /* no join at the end */ pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); // musl libc gives threads 80 kb stacks, much smaller than - // JS_DEFAULT_STACK_SIZE (256 kb) + // JS_DEFAULT_STACK_SIZE (1 MB) pthread_attr_setstacksize(&attr, 2 << 20); // 2 MB, glibc default ret = pthread_create(&tid, &attr, worker_func, args); pthread_attr_destroy(&attr); diff --git a/quickjs.c b/quickjs.c index cc630ab9..0c168265 100644 --- a/quickjs.c +++ b/quickjs.c @@ -1846,6 +1846,10 @@ JSRuntime *JS_NewRuntime2(const JSMallocFunctions *mf, void *opaque) #ifdef __ASAN__ rt->stack_size *= 2; // stack frames are bigger under AddressSanitizer #endif +#ifdef __wasi__ + rt->stack_size = 0; +#endif + JS_UpdateStackTop(rt); rt->current_exception = JS_UNINITIALIZED; diff --git a/quickjs.h b/quickjs.h index 9e8b5113..f74d238a 100644 --- a/quickjs.h +++ b/quickjs.h @@ -280,10 +280,8 @@ static inline JS_BOOL JS_VALUE_IS_NAN(JSValue v) #define JS_PROP_DEFINE_PROPERTY (1 << 18) /* internal use */ #define JS_PROP_REFLECT_DEFINE_PROPERTY (1 << 19) /* internal use */ -#if defined(__wasi__) -#define JS_DEFAULT_STACK_SIZE 0 -#else -#define JS_DEFAULT_STACK_SIZE (256 * 1024) +#ifndef JS_DEFAULT_STACK_SIZE +#define JS_DEFAULT_STACK_SIZE (1024 * 1024) #endif /* JS_Eval() flags */ diff --git a/run-test262.c b/run-test262.c index 1b629e79..8261abbf 100644 --- a/run-test262.c +++ b/run-test262.c @@ -515,7 +515,7 @@ static JSValue js_evalScript_262(JSContext *ctx, JSValue this_val, static void start_thread(js_thread_t *thrd, void *(*start)(void *), void *arg) { // musl libc gives threads 80 kb stacks, much smaller than - // JS_DEFAULT_STACK_SIZE (256 kb) + // JS_DEFAULT_STACK_SIZE (1 MB) static const unsigned stacksize = 2 << 20; // 2 MB, glibc default #ifdef _WIN32 HANDLE h, cp;