Skip to content

Commit 5539d7e

Browse files
bnoordhuisTrott
authored andcommitted
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: nodejs#27855 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Eugene Ostroukhov <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent c6f545a commit 5539d7e

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/inspector_agent.cc

+13-6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <climits> // PTHREAD_STACK_MIN
2626
#endif // __POSIX__
2727

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

0 commit comments

Comments
 (0)