From a28deaad9bf218fb1470af023807a2fb755e7275 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Fri, 24 May 2019 16:56:19 +0200 Subject: [PATCH 1/2] 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. --- src/inspector_agent.cc | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/inspector_agent.cc b/src/inspector_agent.cc index 83f7140e93d7e7..753ef5f751bd7f 100644 --- a/src/inspector_agent.cc +++ b/src/inspector_agent.cc @@ -25,6 +25,7 @@ #include // PTHREAD_STACK_MIN #endif // __POSIX__ +#include #include #include #include @@ -111,12 +112,17 @@ static int StartDebugSignalHandler() { CHECK_EQ(0, uv_sem_init(&start_io_thread_semaphore, 0)); pthread_attr_t attr; CHECK_EQ(0, pthread_attr_init(&attr)); - // Don't shrink the thread's stack on FreeBSD. Said platform decided to - // follow the pthreads specification to the letter rather than in spirit: - // https://lists.freebsd.org/pipermail/freebsd-current/2014-March/048885.html #ifndef __FreeBSD__ - CHECK_EQ(0, pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN)); -#endif // __FreeBSD__ + // PTHREAD_STACK_MIN is 2 KB with musl libc, 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. The goal is to avoid + // creating a big 2 or 4 MB address space gap (problematic on 32 bits + // because of fragmentation), not squeeze out every last byte. + const size_t stack_size = std::max(static_cast(4 * 8192), + static_cast(PTHREAD_STACK_MIN)); + CHECK_EQ(0, pthread_attr_setstacksize(&attr, stack_size)); +#endif // !__FreeBSD__ CHECK_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)); sigset_t sigmask; // Mask all signals. From ee68f56f4ef344635da8da5234c980fe22f02816 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Sat, 1 Jun 2019 08:58:55 +0200 Subject: [PATCH 2/2] squash --- src/inspector_agent.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/inspector_agent.cc b/src/inspector_agent.cc index 753ef5f751bd7f..12e62d3e2b226e 100644 --- a/src/inspector_agent.cc +++ b/src/inspector_agent.cc @@ -112,17 +112,18 @@ static int StartDebugSignalHandler() { CHECK_EQ(0, uv_sem_init(&start_io_thread_semaphore, 0)); pthread_attr_t attr; CHECK_EQ(0, pthread_attr_init(&attr)); -#ifndef __FreeBSD__ +#if defined(PTHREAD_STACK_MIN) && !defined(__FreeBSD__) // PTHREAD_STACK_MIN is 2 KB with musl libc, 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. The goal is to avoid // 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(4 * 8192), static_cast(PTHREAD_STACK_MIN)); CHECK_EQ(0, pthread_attr_setstacksize(&attr, stack_size)); -#endif // !__FreeBSD__ +#endif // defined(PTHREAD_STACK_MIN) && !defined(__FreeBSD__) CHECK_EQ(0, pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)); sigset_t sigmask; // Mask all signals.