From 54bf1cda4fd0124ff0bcf5e384f9718d9ded1954 Mon Sep 17 00:00:00 2001 From: YAMAMOTO Takashi Date: Mon, 19 Dec 2022 21:16:54 +0900 Subject: [PATCH] Reduce over-allocation of stack (#365) * Disable stack guard * Stop rounding up stack size to PAGE_SIZE --- libc-top-half/musl/src/internal/pthread_impl.h | 5 +++++ libc-top-half/musl/src/thread/pthread_create.c | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/libc-top-half/musl/src/internal/pthread_impl.h b/libc-top-half/musl/src/internal/pthread_impl.h index a6d188b..1e7b974 100644 --- a/libc-top-half/musl/src/internal/pthread_impl.h +++ b/libc-top-half/musl/src/internal/pthread_impl.h @@ -216,7 +216,12 @@ extern hidden unsigned __default_stacksize; extern hidden unsigned __default_guardsize; #define DEFAULT_STACK_SIZE 131072 +#ifdef __wasilibc_unmodified_upstream #define DEFAULT_GUARD_SIZE 8192 +#else +/* guard doesn't make much sense without mprotect. */ +#define DEFAULT_GUARD_SIZE 0 +#endif #define DEFAULT_STACK_MAX (8<<20) #define DEFAULT_GUARD_MAX (1<<20) diff --git a/libc-top-half/musl/src/thread/pthread_create.c b/libc-top-half/musl/src/thread/pthread_create.c index 3b3a264..1ac920f 100644 --- a/libc-top-half/musl/src/thread/pthread_create.c +++ b/libc-top-half/musl/src/thread/pthread_create.c @@ -295,7 +295,17 @@ _Noreturn void wasi_thread_start(int tid, void *p) } #endif +#ifdef __wasilibc_unmodified_upstream #define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE) +#else +/* + * As we allocate stack with malloc() instead of mmap/mprotect, + * there is no point to round it up to PAGE_SIZE. + * Instead, round up to a sane alignment. + * Note: PAGE_SIZE is rather big on WASM. (65536) + */ +#define ROUND(x) (((x)+16-1)&-16) +#endif /* pthread_key_create.c overrides this */ static volatile size_t dummy = 0; -- 2.39.2