Skip to content

Commit aaf2f82

Browse files
bnoordhuisrichardlau
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. Backport-PR-URL: #33720 PR-URL: #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 3acc89f commit aaf2f82

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
@@ -14,6 +14,7 @@
1414

1515
#include "libplatform/libplatform.h"
1616

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

0 commit comments

Comments
 (0)