From d89bd156cfc9f66003a430c31149c4b94e18b904 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Fri, 7 Jun 2019 09:58:49 +0200 Subject: [PATCH] darwin,linux: more conservative minimum stack size MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit uv_thread_create() inspects min(PTHREAD_STACK_MIN, RLIMIT_STACK) to find out the minimum allowed stack size. After spelunking through kernel and libc code, I came to the conclusion that allowing such small stacks does not mix well with signals. Musl's PTHREAD_STACK_MIN is 2 KB but signal handlers on many architectures need at least 1 KB to store the signal context, making it almost impossible to do anything on the thread without signal delivery overflowing the stack. Therefore, increase the lower bound to 8 KB. That corresponds to PTHREAD_STACK_MIN + MINSIGSTKSZ on arm64, which has the largest MINSIGSTKSZ of the architectures that musl supports. PR-URL: https://github.com/libuv/libuv/pull/2310 Reviewed-By: Colin Ihrig Reviewed-By: Saúl Ibarra Corretgé --- src/unix/thread.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/unix/thread.c b/src/unix/thread.c index 9a50448e..cd0b7aa6 100644 --- a/src/unix/thread.c +++ b/src/unix/thread.c @@ -178,8 +178,21 @@ static size_t thread_stack_size(void) { if (lim.rlim_cur != RLIM_INFINITY) { /* pthread_attr_setstacksize() expects page-aligned values. */ lim.rlim_cur -= lim.rlim_cur % (rlim_t) getpagesize(); - if (lim.rlim_cur >= PTHREAD_STACK_MIN) - return lim.rlim_cur; + + /* Musl's PTHREAD_STACK_MIN is 2 KB on all architectures, which is + * too small to safely receive signals on. + * + * Musl's PTHREAD_STACK_MIN + MINSIGSTKSZ == 8192 on arm64 (which has + * the largest MINSIGSTKSZ of the architectures that musl supports) so + * let's use that as a lower bound. + * + * We use a hardcoded value because PTHREAD_STACK_MIN + MINSIGSTKSZ + * is between 28 and 133 KB when compiling against glibc, depending + * on the architecture. + */ + if (lim.rlim_cur >= 8192) + if (lim.rlim_cur >= PTHREAD_STACK_MIN) + return lim.rlim_cur; } #endif