inspector: more conservative minimum stack size

PTHREAD_STACK_MIN is 2 KB with musl, which is too small to safely
receive signals. PTHREAD_STACK_MIN + MINSIGSTKSZ is 8 KB on arm64,
which is the musl architecture with the biggest MINSIGSTKSZ so let's
use that as a lower bound and let's quadruple it just in case.

PR-URL: https://github.com/nodejs/node/pull/27855
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Eugene Ostroukhov <eostroukhov@google.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
Ben Noordhuis 2019-05-24 16:56:19 +02:00 committed by Rich Trott
parent c6f545a74a
commit 5539d7e360

View File

@ -25,6 +25,7 @@
#include <climits> // PTHREAD_STACK_MIN #include <climits> // PTHREAD_STACK_MIN
#endif // __POSIX__ #endif // __POSIX__
#include <algorithm>
#include <cstring> #include <cstring>
#include <sstream> #include <sstream>
#include <unordered_map> #include <unordered_map>
@ -111,12 +112,18 @@ static int StartDebugSignalHandler() {
CHECK_EQ(0, uv_sem_init(&start_io_thread_semaphore, 0)); CHECK_EQ(0, uv_sem_init(&start_io_thread_semaphore, 0));
pthread_attr_t attr; pthread_attr_t attr;
CHECK_EQ(0, pthread_attr_init(&attr)); CHECK_EQ(0, pthread_attr_init(&attr));
// Don't shrink the thread's stack on FreeBSD. Said platform decided to #if defined(PTHREAD_STACK_MIN) && !defined(__FreeBSD__)
// follow the pthreads specification to the letter rather than in spirit: // PTHREAD_STACK_MIN is 2 KB with musl libc, which is too small to safely
// https://lists.freebsd.org/pipermail/freebsd-current/2014-March/048885.html // receive signals. PTHREAD_STACK_MIN + MINSIGSTKSZ is 8 KB on arm64, which
#ifndef __FreeBSD__ // is the musl architecture with the biggest MINSIGSTKSZ so let's use that
CHECK_EQ(0, pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN)); // as a lower bound and let's quadruple it just in case. The goal is to avoid
#endif // __FreeBSD__ // creating a big 2 or 4 MB address space gap (problematic on 32 bits
// because of fragmentation), not squeeze out every last byte.
// Omitted on FreeBSD because it doesn't seem to like small stacks.
const size_t stack_size = std::max(static_cast<size_t>(4 * 8192),
static_cast<size_t>(PTHREAD_STACK_MIN));
CHECK_EQ(0, pthread_attr_setstacksize(&attr, stack_size));
#endif // defined(PTHREAD_STACK_MIN) && !defined(__FreeBSD__)
CHECK_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)); CHECK_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED));
sigset_t sigmask; sigset_t sigmask;
// Mask all signals. // Mask all signals.