From 11458f0d91654feed9b4e225c4e6ac8528057355 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 10 Nov 2024 13:24:21 +0100 Subject: [PATCH] AK: Use getrlimit() to find the correct main thread stack size on macOS This is what JavaScriptCore does as well. --- AK/StackInfo.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/AK/StackInfo.cpp b/AK/StackInfo.cpp index 4777c48375d..b4324cb7d60 100644 --- a/AK/StackInfo.cpp +++ b/AK/StackInfo.cpp @@ -65,11 +65,16 @@ StackInfo::StackInfo() // MacOS seems inconsistent on what stack size is given for the main thread. // According to the Apple docs, default for main thread is 8MB, and default for // other threads is 512KB - constexpr size_t eight_megabytes = 0x800000; - if (pthread_main_np() == 1 && m_size < eight_megabytes) { - // Assume no one messed with stack size linker options for the main thread, - // and just set it to 8MB. - m_size = eight_megabytes; + if (pthread_main_np() == 1) { + // Apparently the main thread's stack size is not reported correctly on macOS + // but we can use getrlimit to get the correct value. + rlimit limit {}; + getrlimit(RLIMIT_STACK, &limit); + if (limit.rlim_cur == RLIM_INFINITY) { + m_size = 8 * MiB; + } else { + m_size = limit.rlim_cur; + } } m_base = top_of_stack - m_size; #elif defined(AK_OS_OPENBSD)