[libc-commits] [libc] [libc] Derive the default thread stack size from RLIMIT_STACK (PR #244571)
Pavel Labath via libc-commits
libc-commits at lists.llvm.org
Fri Nov 27 02:48:21 PST 2026
https://github.com/labath updated https://github.com/llvm/llvm-project/pull/244571
>From bda28ac9ac7255ee156d2fb9658e286f75b36867 Mon Sep 17 00:00:00 2001
From: Pavel Labath <pavel at labath.sk>
Date: Fri, 20 Nov 2026 08:04:41 +0000
Subject: [PATCH 1/5] [libc] Derive the default thread stack size from
RLIMIT_STACK
Our default thread stack size was a hard-coded 64KB, which is a lot
smaller than what most programs expect. As far as I can tell, there
was never a deliberate discussion on the default value. The
current value (first 32KB, then bumped to 64KB) was set back when
thrd_create was first implemented and has stuck around since.
This patch makes the default follow the soft RLIMIT_STACK limit:
- the limit is read once at startup (in do_start, before constructors
and main run), so later setrlimit calls don't change the default
- if the limit is RLIM_INFINITY (or too large to be page-aligned), we
use 2MB
- the value is raised to PTHREAD_STACK_MIN and rounded up to the page
size
POSIX leaves the default stack size unspecified, and this is the kind
of thing that has no one-size-fits-all solution. Using RLIMIT_STACK is
an idea I got from glibc. I'm implementing that here because it makes
sense to me, and I think it's the behavior most applications expect.
For contrast, musl uses a smaller default stack size (128KB -- still
larger than ours), but it offers a non-standard extension
(pthread_setattr_default_np) to change it.
This is the kind of thing that would make sense as a config option, but I
don't want to add it if there isn't a demand for it.
This default stack size applies to pthread_attr_init, pthread_create
with a null attr, and thrd_create. Since the default is no longer a
compile-time constant, DEFAULT_PTHREAD_ATTR becomes a
default_pthread_attr() function, and the stacksize argument of
Thread::run now defaults to 0. Thread::run already treated 0 as "use
the default" (support for this was added back during the review of the
patch which introduced Thread::DEFAULT_STACKSIZE,
https://reviews.llvm.org/D148290#4316958), so I'm reusing that instead
of e.g. switching to an optional.
The integration test re-execs itself with different RLIMIT_STACK values
to check the startup behavior, including that lowering the limit inside
main doesn't affect the default.
Assisted-by: Gemini
---
libc/src/__support/threads/CMakeLists.txt | 11 ++
.../__support/threads/linux/CMakeLists.txt | 17 ++
libc/src/__support/threads/linux/stack_size.h | 69 +++++++
libc/src/__support/threads/linux/thread.cpp | 5 +-
libc/src/__support/threads/stack_size.h | 25 +++
libc/src/__support/threads/thread.h | 11 +-
libc/src/pthread/CMakeLists.txt | 1 +
libc/src/pthread/pthread_attr.h | 21 ++-
libc/src/pthread/pthread_attr_init.cpp | 2 +-
libc/src/pthread/pthread_create.cpp | 7 +-
libc/startup/linux/CMakeLists.txt | 1 +
libc/startup/linux/do_start.cpp | 5 +
.../integration/src/pthread/CMakeLists.txt | 34 ++++
.../pthread_default_stacksize_test.cpp | 173 ++++++++++++++++++
libc/test/src/pthread/pthread_attr_test.cpp | 5 +
15 files changed, 365 insertions(+), 22 deletions(-)
create mode 100644 libc/src/__support/threads/linux/stack_size.h
create mode 100644 libc/src/__support/threads/stack_size.h
create mode 100644 libc/test/integration/src/pthread/pthread_default_stacksize_test.cpp
diff --git a/libc/src/__support/threads/CMakeLists.txt b/libc/src/__support/threads/CMakeLists.txt
index 81bcd610be143a..115d4b4d193274 100644
--- a/libc/src/__support/threads/CMakeLists.txt
+++ b/libc/src/__support/threads/CMakeLists.txt
@@ -58,6 +58,17 @@ if(TARGET libc.src.__support.threads.${LIBC_TARGET_OS}.tcb)
)
endif()
+if(TARGET libc.src.__support.threads.${LIBC_TARGET_OS}.stack_size)
+ add_header_library(
+ stack_size
+ HDRS
+ stack_size.h
+ DEPENDS
+ .${LIBC_TARGET_OS}.stack_size
+ libc.src.__support.macros.properties.os
+ )
+endif()
+
if(TARGET libc.src.__support.threads.${LIBC_TARGET_OS}.futex_utils)
add_header_library(
futex_utils
diff --git a/libc/src/__support/threads/linux/CMakeLists.txt b/libc/src/__support/threads/linux/CMakeLists.txt
index 42640b2da2b1ce..a23e5f269c54d3 100644
--- a/libc/src/__support/threads/linux/CMakeLists.txt
+++ b/libc/src/__support/threads/linux/CMakeLists.txt
@@ -26,6 +26,22 @@ if(NOT TARGET libc.src.__support.OSUtil.osutil)
return()
endif()
+add_header_library(
+ stack_size
+ HDRS
+ stack_size.h
+ DEPENDS
+ libc.hdr.limits_macros
+ libc.hdr.pthread_macros
+ libc.hdr.sys_resource_macros
+ libc.hdr.types.size_t
+ libc.hdr.types.struct_rlimit
+ libc.src.__support.CPP.algorithm
+ libc.src.__support.OSUtil.linux.syscall_wrappers.prlimit
+ libc.src.__support.macros.attributes
+ libc.src.__support.macros.config
+)
+
add_header_library(
futex_utils
HDRS
@@ -60,6 +76,7 @@ add_object_library(
thread.cpp
DEPENDS
.futex_utils
+ .stack_size
${thread_tls_dep}
libc.config.app_h
libc.include.sys_syscall
diff --git a/libc/src/__support/threads/linux/stack_size.h b/libc/src/__support/threads/linux/stack_size.h
new file mode 100644
index 00000000000000..0d4c31a3626737
--- /dev/null
+++ b/libc/src/__support/threads/linux/stack_size.h
@@ -0,0 +1,69 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Default thread stack size determination for Linux.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_THREADS_LINUX_STACK_SIZE_H
+#define LLVM_LIBC_SRC___SUPPORT_THREADS_LINUX_STACK_SIZE_H
+
+#include "hdr/limits_macros.h" // POSIX defines PTHREAD_STACK_MIN in limits.h.
+#include "hdr/pthread_macros.h"
+#include "hdr/sys_resource_macros.h"
+#include "hdr/types/size_t.h"
+#include "hdr/types/struct_rlimit.h"
+#include "src/__support/CPP/algorithm.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/prlimit.h"
+#include "src/__support/macros/attributes.h"
+#include "src/__support/macros/config.h"
+
+#include <linux/param.h> // For EXEC_PAGESIZE.
+
+namespace LIBC_NAMESPACE_DECL {
+namespace detail {
+
+LIBC_INLINE_VAR constexpr size_t FALLBACK_STACKSIZE = 2 * 1024 * 1024;
+
+// Derives the default thread stack size from the soft RLIMIT_STACK limit.
+// If the limit is RLIM_INFINITY or too large to page-align in size_t, falls
+// back to FALLBACK_STACKSIZE. Otherwise, clamps to at least PTHREAD_STACK_MIN
+// and rounds up to EXEC_PAGESIZE.
+LIBC_INLINE size_t stacksize_from_rlimit(rlim_t rlim_cur) {
+ constexpr size_t PAGE_MASK = static_cast<size_t>(EXEC_PAGESIZE) - 1;
+ constexpr size_t MAX_STACKSIZE = ~PAGE_MASK;
+ constexpr size_t MIN_STACKSIZE = static_cast<size_t>(PTHREAD_STACK_MIN);
+
+ if (rlim_cur == RLIM_INFINITY || rlim_cur > MAX_STACKSIZE)
+ return FALLBACK_STACKSIZE;
+
+ size_t stacksize = cpp::max(static_cast<size_t>(rlim_cur), MIN_STACKSIZE);
+ return (stacksize + PAGE_MASK) & ~PAGE_MASK;
+}
+
+// Written once by init_default_stacksize() during process startup, before any
+// other threads exist, and only read afterwards.
+LIBC_INLINE_VAR size_t default_stacksize_value = FALLBACK_STACKSIZE;
+
+} // namespace detail
+
+LIBC_INLINE void init_default_stacksize() {
+ struct rlimit limits;
+ if (linux_syscalls::prlimit(0, RLIMIT_STACK, nullptr, &limits).has_value())
+ detail::default_stacksize_value =
+ detail::stacksize_from_rlimit(limits.rlim_cur);
+}
+
+LIBC_INLINE size_t default_stacksize() {
+ return detail::default_stacksize_value;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_THREADS_LINUX_STACK_SIZE_H
diff --git a/libc/src/__support/threads/linux/thread.cpp b/libc/src/__support/threads/linux/thread.cpp
index a121cc0ecc709c..fdd3c29ea09158 100644
--- a/libc/src/__support/threads/linux/thread.cpp
+++ b/libc/src/__support/threads/linux/thread.cpp
@@ -38,6 +38,7 @@
#include "src/__support/macros/config.h"
#include "src/__support/threads/linux/futex_utils.h" // For FutexWordType
#include "src/__support/threads/linux/futex_word.h"
+#include "src/__support/threads/linux/stack_size.h"
#include "src/__support/threads/thread_attributes.h"
#include "src/__support/time/linux/cpu_clock.h"
@@ -191,10 +192,8 @@ int Thread::run(ThreadStyle style, ThreadRunner runner, void *arg, void *stack,
cpp::optional<SchedParameters> sched_params) {
bool owned_stack = false;
if (stack == nullptr) {
- // TODO: Should we return EINVAL here? Should we have a generic concept of a
- // minimum stacksize (like 16384 for pthread).
if (stacksize == 0)
- stacksize = DEFAULT_STACKSIZE;
+ stacksize = default_stacksize();
// Roundup stacksize/guardsize to page size.
// TODO: Should be also add sizeof(ThreadAttribute) and other internal
// meta data?
diff --git a/libc/src/__support/threads/stack_size.h b/libc/src/__support/threads/stack_size.h
new file mode 100644
index 00000000000000..bd224654a89dcb
--- /dev/null
+++ b/libc/src/__support/threads/stack_size.h
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// OS-level default thread stack size dispatcher.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_THREADS_STACK_SIZE_H
+#define LLVM_LIBC_SRC___SUPPORT_THREADS_STACK_SIZE_H
+
+#include "src/__support/macros/properties/os.h"
+
+#if defined(LIBC_TARGET_OS_IS_LINUX)
+#include "src/__support/threads/linux/stack_size.h"
+#else
+#error "Unsupported OS for thread stack size"
+#endif
+
+#endif // LLVM_LIBC_SRC___SUPPORT_THREADS_STACK_SIZE_H
diff --git a/libc/src/__support/threads/thread.h b/libc/src/__support/threads/thread.h
index cdd0cd9f64cbbc..48f1ce7527cc4d 100644
--- a/libc/src/__support/threads/thread.h
+++ b/libc/src/__support/threads/thread.h
@@ -63,9 +63,6 @@ bool set_tss_value(unsigned int key, void *value);
void *get_tss_value(unsigned int key);
struct Thread {
- // NB: Default stacksize of 64kb is exceedingly small compared to the 2mb norm
- // and will break many programs expecting the full 2mb.
- static constexpr size_t DEFAULT_STACKSIZE = 1 << 16;
static constexpr size_t DEFAULT_GUARDSIZE = EXEC_PAGESIZE;
static constexpr bool DEFAULT_DETACHED = false;
@@ -74,9 +71,10 @@ struct Thread {
constexpr Thread() : attrib(nullptr) {}
constexpr Thread(ThreadAttributes *attr) : attrib(attr) {}
+ // When stack is nullptr and stacksize is 0, the platform default stack size
+ // is used.
int run(ThreadRunnerPosix *func, void *arg, void *stack = nullptr,
- size_t stacksize = DEFAULT_STACKSIZE,
- size_t guardsize = DEFAULT_GUARDSIZE,
+ size_t stacksize = 0, size_t guardsize = DEFAULT_GUARDSIZE,
bool detached = DEFAULT_DETACHED,
cpp::optional<SchedParameters> sched_params = cpp::nullopt) {
ThreadRunner runner;
@@ -86,8 +84,7 @@ struct Thread {
}
int run(ThreadRunnerStdc *func, void *arg, void *stack = nullptr,
- size_t stacksize = DEFAULT_STACKSIZE,
- size_t guardsize = DEFAULT_GUARDSIZE,
+ size_t stacksize = 0, size_t guardsize = DEFAULT_GUARDSIZE,
bool detached = DEFAULT_DETACHED) {
ThreadRunner runner;
runner.stdc_runner = func;
diff --git a/libc/src/pthread/CMakeLists.txt b/libc/src/pthread/CMakeLists.txt
index 48ca5db6563b8e..f7d3ea5169d1cb 100644
--- a/libc/src/pthread/CMakeLists.txt
+++ b/libc/src/pthread/CMakeLists.txt
@@ -8,6 +8,7 @@ add_header_library(
libc.hdr.types.pthread_attr_t
libc.src.__support.macros.attributes
libc.src.__support.macros.config
+ libc.src.__support.threads.stack_size
libc.src.__support.threads.thread
)
diff --git a/libc/src/pthread/pthread_attr.h b/libc/src/pthread/pthread_attr.h
index 09eb61fc9fd6de..c5166b65830060 100644
--- a/libc/src/pthread/pthread_attr.h
+++ b/libc/src/pthread/pthread_attr.h
@@ -19,19 +19,22 @@
#include "hdr/types/pthread_attr_t.h"
#include "src/__support/macros/attributes.h"
#include "src/__support/macros/config.h"
+#include "src/__support/threads/stack_size.h"
#include "src/__support/threads/thread.h"
namespace LIBC_NAMESPACE_DECL {
-LIBC_INLINE_VAR constexpr pthread_attr_t DEFAULT_PTHREAD_ATTR = {
- PTHREAD_CREATE_JOINABLE, // Not detached
- PTHREAD_INHERIT_SCHED, // Default inherit scheduler
- SCHED_OTHER, // Default scheduling policy
- {}, // Default scheduling parameters
- nullptr, // Let the thread manage its stack
- Thread::DEFAULT_STACKSIZE, // stack size.
- Thread::DEFAULT_GUARDSIZE, // Default page size for the guard size.
-};
+LIBC_INLINE pthread_attr_t default_pthread_attr() {
+ return pthread_attr_t{
+ PTHREAD_CREATE_JOINABLE, // Not detached
+ PTHREAD_INHERIT_SCHED, // Default inherit scheduler
+ SCHED_OTHER, // Default scheduling policy
+ {}, // Default scheduling parameters
+ nullptr, // Let the thread manage its stack
+ default_stacksize(), // Default stack size
+ Thread::DEFAULT_GUARDSIZE, // Default page size for the guard size
+ };
+}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/pthread/pthread_attr_init.cpp b/libc/src/pthread/pthread_attr_init.cpp
index bb9676ce3941af..b2a1024e3bdf2a 100644
--- a/libc/src/pthread/pthread_attr_init.cpp
+++ b/libc/src/pthread/pthread_attr_init.cpp
@@ -22,7 +22,7 @@ namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(int, pthread_attr_init, (pthread_attr_t * attr)) {
LIBC_CRASH_ON_NULLPTR(attr);
- *attr = DEFAULT_PTHREAD_ATTR;
+ *attr = default_pthread_attr();
return 0;
}
diff --git a/libc/src/pthread/pthread_create.cpp b/libc/src/pthread/pthread_create.cpp
index 36fd02dd064cda..0369bb2494249b 100644
--- a/libc/src/pthread/pthread_create.cpp
+++ b/libc/src/pthread/pthread_create.cpp
@@ -32,8 +32,11 @@ LLVM_LIBC_FUNCTION(int, pthread_create,
(pthread_t *__restrict th,
const pthread_attr_t *__restrict attr,
__pthread_start_t func, void *arg)) {
- if (attr == nullptr)
- attr = &DEFAULT_PTHREAD_ATTR;
+ pthread_attr_t default_attr;
+ if (attr == nullptr) {
+ default_attr = default_pthread_attr();
+ attr = &default_attr;
+ }
void *stack = attr->__stack;
size_t stacksize = attr->__stacksize;
diff --git a/libc/startup/linux/CMakeLists.txt b/libc/startup/linux/CMakeLists.txt
index 18379a9b4e4489..8aa1baefe00af8 100644
--- a/libc/startup/linux/CMakeLists.txt
+++ b/libc/startup/linux/CMakeLists.txt
@@ -122,6 +122,7 @@ add_object_library(
libc.src.__support.OSUtil.linux.auxv
libc.src.__support.OSUtil.linux.syscall_wrappers.exit_group
libc.src.__support.OSUtil.osutil
+ libc.src.__support.threads.stack_size
libc.src.__support.threads.tcb
libc.src.__support.threads.thread
libc.src.__support.threads.linux.futex_utils
diff --git a/libc/startup/linux/do_start.cpp b/libc/startup/linux/do_start.cpp
index 504411971f43f1..18de8acdde22a7 100644
--- a/libc/startup/linux/do_start.cpp
+++ b/libc/startup/linux/do_start.cpp
@@ -20,6 +20,7 @@
#include "src/__support/OSUtil/syscall.h"
#include "src/__support/macros/config.h"
#include "src/__support/threads/linux/futex_utils.h"
+#include "src/__support/threads/stack_size.h"
#include "src/__support/threads/tcb.h"
#include "src/__support/threads/thread.h"
#include "src/errno/program_invocation_name.h"
@@ -207,6 +208,10 @@ static TLSDescriptor tls;
get_tcb(tls.tp)->attrib = &main_thread_attrib;
+ // This needs to happen before the init array callbacks, as global
+ // constructors can already create threads.
+ init_default_stacksize();
+
// We want the fini array callbacks to be run after other atexit
// callbacks are run. So, we register them before running the init
// array callbacks as they can potentially register their own atexit
diff --git a/libc/test/integration/src/pthread/CMakeLists.txt b/libc/test/integration/src/pthread/CMakeLists.txt
index 729a2f8769bb5f..a9e9f9cc624433 100644
--- a/libc/test/integration/src/pthread/CMakeLists.txt
+++ b/libc/test/integration/src/pthread/CMakeLists.txt
@@ -375,6 +375,40 @@ add_integration_test(
libc.src.unistd.sysconf
)
+add_integration_test(
+ pthread_default_stacksize_test
+ SUITE
+ libc-pthread-integration-tests
+ SRCS
+ pthread_default_stacksize_test.cpp
+ DEPENDS
+ libc.hdr.sys_resource_macros
+ libc.hdr.sys_wait_macros
+ libc.hdr.types.pid_t
+ libc.hdr.types.pthread_attr_t
+ libc.hdr.types.pthread_t
+ libc.hdr.types.struct_rlimit
+ libc.include.threads
+ libc.src.__support.CPP.string_view
+ libc.src.__support.threads.stack_size
+ libc.src.pthread.pthread_attr_destroy
+ libc.src.pthread.pthread_attr_getstacksize
+ libc.src.pthread.pthread_attr_init
+ libc.src.pthread.pthread_create
+ libc.src.pthread.pthread_getattr_np
+ libc.src.pthread.pthread_getstack_np
+ libc.src.pthread.pthread_join
+ libc.src.pthread.pthread_self
+ libc.src.stdlib._Exit
+ libc.src.sys.resource.getrlimit
+ libc.src.sys.resource.setrlimit
+ libc.src.sys.wait.waitpid
+ libc.src.threads.thrd_create
+ libc.src.threads.thrd_join
+ libc.src.unistd.execve
+ libc.src.unistd.fork
+)
+
add_integration_test(
pthread_setschedparam_test
SUITE
diff --git a/libc/test/integration/src/pthread/pthread_default_stacksize_test.cpp b/libc/test/integration/src/pthread/pthread_default_stacksize_test.cpp
new file mode 100644
index 00000000000000..6e40a710a408f0
--- /dev/null
+++ b/libc/test/integration/src/pthread/pthread_default_stacksize_test.cpp
@@ -0,0 +1,173 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Integration test for default thread stack size derived from startup
+/// RLIMIT_STACK.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/sys_resource_macros.h"
+#include "hdr/sys_wait_macros.h"
+#include "hdr/types/pid_t.h"
+#include "hdr/types/pthread_attr_t.h"
+#include "hdr/types/pthread_t.h"
+#include "hdr/types/struct_rlimit.h"
+#include "src/__support/CPP/string_view.h"
+#include "src/__support/threads/stack_size.h"
+#include "src/pthread/pthread_attr_destroy.h"
+#include "src/pthread/pthread_attr_getstacksize.h"
+#include "src/pthread/pthread_attr_init.h"
+#include "src/pthread/pthread_create.h"
+#include "src/pthread/pthread_getattr_np.h"
+#include "src/pthread/pthread_getstack_np.h"
+#include "src/pthread/pthread_join.h"
+#include "src/pthread/pthread_self.h"
+#include "src/stdlib/_Exit.h"
+#include "src/sys/resource/getrlimit.h"
+#include "src/sys/resource/setrlimit.h"
+#include "src/sys/wait/waitpid.h"
+#include "src/threads/thrd_create.h"
+#include "src/threads/thrd_join.h"
+#include "src/unistd/execve.h"
+#include "src/unistd/fork.h"
+#include "test/IntegrationTest/test.h"
+
+#include <linux/param.h> // For EXEC_PAGESIZE.
+#include <threads.h> // For thrd_t and thrd_success.
+
+static void *pthread_probe(void *arg) {
+ auto *out_size = static_cast<size_t *>(arg);
+ pthread_attr_t attr;
+ ASSERT_EQ(
+ LIBC_NAMESPACE::pthread_getattr_np(LIBC_NAMESPACE::pthread_self(), &attr),
+ 0);
+ size_t getattr_size = 0;
+ ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_getstacksize(&attr, &getattr_size), 0);
+ ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_destroy(&attr), 0);
+
+ void *stackaddr = nullptr;
+ size_t getstack_size = 0;
+ ASSERT_EQ(LIBC_NAMESPACE::pthread_getstack_np(LIBC_NAMESPACE::pthread_self(),
+ &stackaddr, &getstack_size),
+ 0);
+ ASSERT_EQ(getattr_size, getstack_size);
+ *out_size = getattr_size;
+ return nullptr;
+}
+
+static int c11_thread_probe(void *arg) {
+ auto *out_size = static_cast<size_t *>(arg);
+ void *stackaddr = nullptr;
+ size_t stacksize = 0;
+ ASSERT_EQ(LIBC_NAMESPACE::pthread_getstack_np(LIBC_NAMESPACE::pthread_self(),
+ &stackaddr, &stacksize),
+ 0);
+ *out_size = stacksize;
+ return 0;
+}
+
+static void verify_default_stacksize(size_t expected) {
+ pthread_attr_t attr;
+ ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_init(&attr), 0);
+ size_t attr_size = 0;
+ ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_getstacksize(&attr, &attr_size), 0);
+ ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_destroy(&attr), 0);
+ ASSERT_EQ(attr_size, expected);
+
+ size_t pt_size = 0;
+ pthread_t pt;
+ ASSERT_EQ(
+ LIBC_NAMESPACE::pthread_create(&pt, nullptr, pthread_probe, &pt_size), 0);
+ ASSERT_EQ(LIBC_NAMESPACE::pthread_join(pt, nullptr), 0);
+ ASSERT_EQ(pt_size, expected);
+
+ size_t ct_size = 0;
+ thrd_t ct;
+ ASSERT_EQ(LIBC_NAMESPACE::thrd_create(&ct, c11_thread_probe, &ct_size),
+ static_cast<int>(thrd_success));
+ ASSERT_EQ(LIBC_NAMESPACE::thrd_join(ct, nullptr),
+ static_cast<int>(thrd_success));
+ ASSERT_EQ(ct_size, expected);
+}
+
+static void spawn_child_with_rlimit(const char *self_path, char **envp,
+ rlim_t rlim_cur, const char *mode) {
+ pid_t pid = LIBC_NAMESPACE::fork();
+ ASSERT_TRUE(pid >= 0);
+ if (pid == 0) {
+ struct rlimit rl;
+ if (LIBC_NAMESPACE::getrlimit(RLIMIT_STACK, &rl) != 0)
+ LIBC_NAMESPACE::_Exit(1);
+ rl.rlim_cur = rlim_cur;
+ if (LIBC_NAMESPACE::setrlimit(RLIMIT_STACK, &rl) != 0)
+ LIBC_NAMESPACE::_Exit(2);
+
+ char *const child_argv[] = {
+ const_cast<char *>(self_path),
+ const_cast<char *>(mode),
+ nullptr,
+ };
+ LIBC_NAMESPACE::execve(self_path, child_argv, envp);
+ LIBC_NAMESPACE::_Exit(3);
+ }
+
+ int status = 0;
+ ASSERT_EQ(LIBC_NAMESPACE::waitpid(pid, &status, 0), pid);
+ ASSERT_TRUE(WIFEXITED(status));
+ ASSERT_EQ(WEXITSTATUS(status), 0);
+}
+
+TEST_MAIN(int argc, char **argv, char **envp) {
+ constexpr size_t FINITE_SIZE = 512 * 1024;
+ constexpr size_t RUNTIME_NEW_SIZE = 256 * 1024;
+ constexpr size_t UNALIGNED_SIZE = 256 * 1024 + 123;
+ constexpr size_t PAGE_MASK = static_cast<size_t>(EXEC_PAGESIZE) - 1;
+
+ if (argc == 2) {
+ LIBC_NAMESPACE::cpp::string_view mode(argv[1]);
+ if (mode == "unlimited") {
+ verify_default_stacksize(LIBC_NAMESPACE::detail::FALLBACK_STACKSIZE);
+ return 0;
+ }
+ if (mode == "finite_and_runtime_change") {
+ // Lower RLIMIT_STACK before making any thread or attribute calls in main;
+ // the default stack size must still reflect the startup RLIMIT_STACK.
+ struct rlimit rl;
+ ASSERT_EQ(LIBC_NAMESPACE::getrlimit(RLIMIT_STACK, &rl), 0);
+ ASSERT_EQ(rl.rlim_cur, static_cast<rlim_t>(FINITE_SIZE));
+ rl.rlim_cur = RUNTIME_NEW_SIZE;
+ ASSERT_EQ(LIBC_NAMESPACE::setrlimit(RLIMIT_STACK, &rl), 0);
+
+ verify_default_stacksize(FINITE_SIZE);
+ return 0;
+ }
+ if (mode == "unaligned") {
+ verify_default_stacksize((UNALIGNED_SIZE + PAGE_MASK) & ~PAGE_MASK);
+ return 0;
+ }
+ return 1;
+ }
+
+ struct rlimit rl;
+ ASSERT_EQ(LIBC_NAMESPACE::getrlimit(RLIMIT_STACK, &rl), 0);
+ verify_default_stacksize(
+ LIBC_NAMESPACE::detail::stacksize_from_rlimit(rl.rlim_cur));
+
+ if (rl.rlim_max == RLIM_INFINITY)
+ spawn_child_with_rlimit(argv[0], envp, RLIM_INFINITY, "unlimited");
+
+ if (rl.rlim_max >= FINITE_SIZE)
+ spawn_child_with_rlimit(argv[0], envp, FINITE_SIZE,
+ "finite_and_runtime_change");
+
+ if (rl.rlim_max >= UNALIGNED_SIZE)
+ spawn_child_with_rlimit(argv[0], envp, UNALIGNED_SIZE, "unaligned");
+
+ return 0;
+}
diff --git a/libc/test/src/pthread/pthread_attr_test.cpp b/libc/test/src/pthread/pthread_attr_test.cpp
index 8bd18e421351e5..50fc5213042ae0 100644
--- a/libc/test/src/pthread/pthread_attr_test.cpp
+++ b/libc/test/src/pthread/pthread_attr_test.cpp
@@ -52,6 +52,11 @@ TEST(LlvmLibcPThreadAttrTest, InitAndDestroy) {
ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_getguardsize(&attr, &guardsize), 0);
ASSERT_EQ(guardsize, size_t(EXEC_PAGESIZE));
+ size_t stacksize;
+ ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_getstacksize(&attr, &stacksize), 0);
+ ASSERT_GE(stacksize, size_t(PTHREAD_STACK_MIN));
+ ASSERT_EQ(stacksize % size_t(EXEC_PAGESIZE), size_t(0));
+
ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_destroy(&attr), 0);
}
>From 6ad6551bb14117e858d1f7b5630d08a052a84fe0 Mon Sep 17 00:00:00 2001
From: Pavel Labath <pavel at labath.sk>
Date: Thu, 26 Nov 2026 14:19:11 +0000
Subject: [PATCH 2/5] Reviewer feedback
---
libc/src/__support/threads/linux/stack_size.h | 3 +-
.../pthread_default_stacksize_test.cpp | 22 ++++++-
.../__support/threads/linux/CMakeLists.txt | 15 +++++
.../threads/linux/stack_size_test.cpp | 65 +++++++++++++++++++
4 files changed, 102 insertions(+), 3 deletions(-)
create mode 100644 libc/test/src/__support/threads/linux/stack_size_test.cpp
diff --git a/libc/src/__support/threads/linux/stack_size.h b/libc/src/__support/threads/linux/stack_size.h
index 0d4c31a3626737..4f35a4c02ad2d3 100644
--- a/libc/src/__support/threads/linux/stack_size.h
+++ b/libc/src/__support/threads/linux/stack_size.h
@@ -49,7 +49,8 @@ LIBC_INLINE size_t stacksize_from_rlimit(rlim_t rlim_cur) {
// Written once by init_default_stacksize() during process startup, before any
// other threads exist, and only read afterwards.
-LIBC_INLINE_VAR size_t default_stacksize_value = FALLBACK_STACKSIZE;
+LIBC_INLINE_VAR LIBC_CONSTINIT size_t default_stacksize_value =
+ FALLBACK_STACKSIZE;
} // namespace detail
diff --git a/libc/test/integration/src/pthread/pthread_default_stacksize_test.cpp b/libc/test/integration/src/pthread/pthread_default_stacksize_test.cpp
index 6e40a710a408f0..dfdeeae607d128 100644
--- a/libc/test/integration/src/pthread/pthread_default_stacksize_test.cpp
+++ b/libc/test/integration/src/pthread/pthread_default_stacksize_test.cpp
@@ -96,6 +96,19 @@ static void verify_default_stacksize(size_t expected) {
ASSERT_EQ(ct_size, expected);
}
+constexpr size_t FINITE_SIZE = 512 * 1024;
+
+static bool global_ctor_checked = false;
+
+__attribute__((constructor)) static void
+check_global_ctor(int argc, char **argv, char **) {
+ if (argc != 2 || LIBC_NAMESPACE::cpp::string_view(argv[1]) != "global_ctor")
+ return;
+
+ verify_default_stacksize(FINITE_SIZE);
+ global_ctor_checked = true;
+}
+
static void spawn_child_with_rlimit(const char *self_path, char **envp,
rlim_t rlim_cur, const char *mode) {
pid_t pid = LIBC_NAMESPACE::fork();
@@ -124,7 +137,6 @@ static void spawn_child_with_rlimit(const char *self_path, char **envp,
}
TEST_MAIN(int argc, char **argv, char **envp) {
- constexpr size_t FINITE_SIZE = 512 * 1024;
constexpr size_t RUNTIME_NEW_SIZE = 256 * 1024;
constexpr size_t UNALIGNED_SIZE = 256 * 1024 + 123;
constexpr size_t PAGE_MASK = static_cast<size_t>(EXEC_PAGESIZE) - 1;
@@ -147,6 +159,10 @@ TEST_MAIN(int argc, char **argv, char **envp) {
verify_default_stacksize(FINITE_SIZE);
return 0;
}
+ if (mode == "global_ctor") {
+ ASSERT_TRUE(global_ctor_checked);
+ return 0;
+ }
if (mode == "unaligned") {
verify_default_stacksize((UNALIGNED_SIZE + PAGE_MASK) & ~PAGE_MASK);
return 0;
@@ -162,9 +178,11 @@ TEST_MAIN(int argc, char **argv, char **envp) {
if (rl.rlim_max == RLIM_INFINITY)
spawn_child_with_rlimit(argv[0], envp, RLIM_INFINITY, "unlimited");
- if (rl.rlim_max >= FINITE_SIZE)
+ if (rl.rlim_max >= FINITE_SIZE) {
spawn_child_with_rlimit(argv[0], envp, FINITE_SIZE,
"finite_and_runtime_change");
+ spawn_child_with_rlimit(argv[0], envp, FINITE_SIZE, "global_ctor");
+ }
if (rl.rlim_max >= UNALIGNED_SIZE)
spawn_child_with_rlimit(argv[0], envp, UNALIGNED_SIZE, "unaligned");
diff --git a/libc/test/src/__support/threads/linux/CMakeLists.txt b/libc/test/src/__support/threads/linux/CMakeLists.txt
index dd0cc5c81d1765..e94edda41db825 100644
--- a/libc/test/src/__support/threads/linux/CMakeLists.txt
+++ b/libc/test/src/__support/threads/linux/CMakeLists.txt
@@ -22,3 +22,18 @@ add_libc_test(
DEPENDS
libc.src.__support.threads.raw_rwlock
)
+
+add_libc_test(
+ stack_size_test
+ SUITE
+ libc-support-threads-tests
+ SRCS
+ stack_size_test.cpp
+ DEPENDS
+ libc.hdr.limits_macros
+ libc.hdr.pthread_macros
+ libc.hdr.sys_resource_macros
+ libc.hdr.types.size_t
+ libc.hdr.types.struct_rlimit
+ libc.src.__support.threads.stack_size
+)
diff --git a/libc/test/src/__support/threads/linux/stack_size_test.cpp b/libc/test/src/__support/threads/linux/stack_size_test.cpp
new file mode 100644
index 00000000000000..161daa52427743
--- /dev/null
+++ b/libc/test/src/__support/threads/linux/stack_size_test.cpp
@@ -0,0 +1,65 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "hdr/limits_macros.h"
+#include "hdr/pthread_macros.h"
+#include "hdr/sys_resource_macros.h"
+#include "hdr/types/size_t.h"
+#include "hdr/types/struct_rlimit.h"
+#include "src/__support/threads/stack_size.h"
+#include "test/UnitTest/Test.h"
+
+#include <linux/param.h>
+
+namespace {
+
+using LIBC_NAMESPACE::detail::FALLBACK_STACKSIZE;
+using LIBC_NAMESPACE::detail::stacksize_from_rlimit;
+
+constexpr size_t PAGE_SIZE = static_cast<size_t>(EXEC_PAGESIZE);
+constexpr size_t PAGE_MASK = PAGE_SIZE - 1;
+constexpr size_t MAX_STACKSIZE = ~PAGE_MASK;
+constexpr size_t MIN_STACKSIZE = static_cast<size_t>(PTHREAD_STACK_MIN);
+constexpr size_t ALIGNED_MIN_STACKSIZE =
+ (MIN_STACKSIZE + PAGE_MASK) & ~PAGE_MASK;
+
+TEST(LlvmLibcSupportThreadsStackSizeTest, BelowAndAtMinimum) {
+ EXPECT_EQ(stacksize_from_rlimit(0), ALIGNED_MIN_STACKSIZE);
+ EXPECT_EQ(stacksize_from_rlimit(1), ALIGNED_MIN_STACKSIZE);
+ EXPECT_EQ(stacksize_from_rlimit(MIN_STACKSIZE - 1), ALIGNED_MIN_STACKSIZE);
+ EXPECT_EQ(stacksize_from_rlimit(MIN_STACKSIZE), ALIGNED_MIN_STACKSIZE);
+}
+
+TEST(LlvmLibcSupportThreadsStackSizeTest, PageAlignment) {
+ EXPECT_EQ(stacksize_from_rlimit(ALIGNED_MIN_STACKSIZE + 1),
+ ALIGNED_MIN_STACKSIZE + PAGE_SIZE);
+ EXPECT_EQ(stacksize_from_rlimit(ALIGNED_MIN_STACKSIZE + PAGE_MASK),
+ ALIGNED_MIN_STACKSIZE + PAGE_SIZE);
+ EXPECT_EQ(stacksize_from_rlimit(ALIGNED_MIN_STACKSIZE + PAGE_SIZE),
+ ALIGNED_MIN_STACKSIZE + PAGE_SIZE);
+}
+
+TEST(LlvmLibcSupportThreadsStackSizeTest, MaxStacksizeBoundaries) {
+ EXPECT_EQ(stacksize_from_rlimit(MAX_STACKSIZE - PAGE_SIZE),
+ MAX_STACKSIZE - PAGE_SIZE);
+ EXPECT_EQ(stacksize_from_rlimit(MAX_STACKSIZE - PAGE_MASK), MAX_STACKSIZE);
+ EXPECT_EQ(stacksize_from_rlimit(MAX_STACKSIZE - 1), MAX_STACKSIZE);
+ EXPECT_EQ(stacksize_from_rlimit(MAX_STACKSIZE), MAX_STACKSIZE);
+}
+
+TEST(LlvmLibcSupportThreadsStackSizeTest, AboveMaxStacksizeFallback) {
+ EXPECT_EQ(stacksize_from_rlimit(static_cast<rlim_t>(MAX_STACKSIZE) + 1),
+ FALLBACK_STACKSIZE);
+ EXPECT_EQ(stacksize_from_rlimit(RLIM_INFINITY - 1), FALLBACK_STACKSIZE);
+}
+
+TEST(LlvmLibcSupportThreadsStackSizeTest, RlimInfinity) {
+ EXPECT_EQ(stacksize_from_rlimit(RLIM_INFINITY), FALLBACK_STACKSIZE);
+}
+
+} // namespace
>From 0af9771c620e5c5fa2d3483e091beca205b0c69b Mon Sep 17 00:00:00 2001
From: Pavel Labath <pavel at labath.sk>
Date: Fri, 27 Nov 2026 09:13:34 +0000
Subject: [PATCH 3/5] review feedback
---
libc/src/__support/threads/linux/stack_size.h | 10 ++++++----
libc/test/integration/src/pthread/CMakeLists.txt | 1 +
.../src/pthread/pthread_default_stacksize_test.cpp | 1 +
.../src/__support/threads/linux/stack_size_test.cpp | 2 +-
4 files changed, 9 insertions(+), 5 deletions(-)
diff --git a/libc/src/__support/threads/linux/stack_size.h b/libc/src/__support/threads/linux/stack_size.h
index 4f35a4c02ad2d3..2ed0296b7b00a6 100644
--- a/libc/src/__support/threads/linux/stack_size.h
+++ b/libc/src/__support/threads/linux/stack_size.h
@@ -32,12 +32,14 @@ namespace detail {
LIBC_INLINE_VAR constexpr size_t FALLBACK_STACKSIZE = 2 * 1024 * 1024;
// Derives the default thread stack size from the soft RLIMIT_STACK limit.
-// If the limit is RLIM_INFINITY or too large to page-align in size_t, falls
-// back to FALLBACK_STACKSIZE. Otherwise, clamps to at least PTHREAD_STACK_MIN
-// and rounds up to EXEC_PAGESIZE.
+// If the limit is RLIM_INFINITY or too large to page-align in size_t (leaving
+// room for the default guard page), falls back to FALLBACK_STACKSIZE.
+// Otherwise, clamps to at least PTHREAD_STACK_MIN and rounds up to
+// EXEC_PAGESIZE.
LIBC_INLINE size_t stacksize_from_rlimit(rlim_t rlim_cur) {
constexpr size_t PAGE_MASK = static_cast<size_t>(EXEC_PAGESIZE) - 1;
- constexpr size_t MAX_STACKSIZE = ~PAGE_MASK;
+ constexpr size_t MAX_STACKSIZE =
+ ~PAGE_MASK - static_cast<size_t>(EXEC_PAGESIZE);
constexpr size_t MIN_STACKSIZE = static_cast<size_t>(PTHREAD_STACK_MIN);
if (rlim_cur == RLIM_INFINITY || rlim_cur > MAX_STACKSIZE)
diff --git a/libc/test/integration/src/pthread/CMakeLists.txt b/libc/test/integration/src/pthread/CMakeLists.txt
index a9e9f9cc624433..778ce6723af36f 100644
--- a/libc/test/integration/src/pthread/CMakeLists.txt
+++ b/libc/test/integration/src/pthread/CMakeLists.txt
@@ -387,6 +387,7 @@ add_integration_test(
libc.hdr.types.pid_t
libc.hdr.types.pthread_attr_t
libc.hdr.types.pthread_t
+ libc.hdr.types.size_t
libc.hdr.types.struct_rlimit
libc.include.threads
libc.src.__support.CPP.string_view
diff --git a/libc/test/integration/src/pthread/pthread_default_stacksize_test.cpp b/libc/test/integration/src/pthread/pthread_default_stacksize_test.cpp
index dfdeeae607d128..ec5318a2f0cf99 100644
--- a/libc/test/integration/src/pthread/pthread_default_stacksize_test.cpp
+++ b/libc/test/integration/src/pthread/pthread_default_stacksize_test.cpp
@@ -17,6 +17,7 @@
#include "hdr/types/pid_t.h"
#include "hdr/types/pthread_attr_t.h"
#include "hdr/types/pthread_t.h"
+#include "hdr/types/size_t.h"
#include "hdr/types/struct_rlimit.h"
#include "src/__support/CPP/string_view.h"
#include "src/__support/threads/stack_size.h"
diff --git a/libc/test/src/__support/threads/linux/stack_size_test.cpp b/libc/test/src/__support/threads/linux/stack_size_test.cpp
index 161daa52427743..f26135e8931169 100644
--- a/libc/test/src/__support/threads/linux/stack_size_test.cpp
+++ b/libc/test/src/__support/threads/linux/stack_size_test.cpp
@@ -23,7 +23,7 @@ using LIBC_NAMESPACE::detail::stacksize_from_rlimit;
constexpr size_t PAGE_SIZE = static_cast<size_t>(EXEC_PAGESIZE);
constexpr size_t PAGE_MASK = PAGE_SIZE - 1;
-constexpr size_t MAX_STACKSIZE = ~PAGE_MASK;
+constexpr size_t MAX_STACKSIZE = ~PAGE_MASK - PAGE_SIZE;
constexpr size_t MIN_STACKSIZE = static_cast<size_t>(PTHREAD_STACK_MIN);
constexpr size_t ALIGNED_MIN_STACKSIZE =
(MIN_STACKSIZE + PAGE_MASK) & ~PAGE_MASK;
>From 926f4572b4a7f00dc0abf1ea93db87f1a8d556aa Mon Sep 17 00:00:00 2001
From: Pavel Labath <pavel at labath.sk>
Date: Fri, 27 Nov 2026 10:27:04 +0000
Subject: [PATCH 4/5] fix overlay build (PTHREAD_STACK_MIN not constexpr)
---
libc/src/__support/threads/linux/stack_size.h | 4 +--
.../threads/linux/stack_size_test.cpp | 31 +++++++++++--------
2 files changed, 20 insertions(+), 15 deletions(-)
diff --git a/libc/src/__support/threads/linux/stack_size.h b/libc/src/__support/threads/linux/stack_size.h
index 2ed0296b7b00a6..000209b3eddd77 100644
--- a/libc/src/__support/threads/linux/stack_size.h
+++ b/libc/src/__support/threads/linux/stack_size.h
@@ -40,12 +40,12 @@ LIBC_INLINE size_t stacksize_from_rlimit(rlim_t rlim_cur) {
constexpr size_t PAGE_MASK = static_cast<size_t>(EXEC_PAGESIZE) - 1;
constexpr size_t MAX_STACKSIZE =
~PAGE_MASK - static_cast<size_t>(EXEC_PAGESIZE);
- constexpr size_t MIN_STACKSIZE = static_cast<size_t>(PTHREAD_STACK_MIN);
if (rlim_cur == RLIM_INFINITY || rlim_cur > MAX_STACKSIZE)
return FALLBACK_STACKSIZE;
- size_t stacksize = cpp::max(static_cast<size_t>(rlim_cur), MIN_STACKSIZE);
+ size_t stacksize = cpp::max(static_cast<size_t>(rlim_cur),
+ static_cast<size_t>(PTHREAD_STACK_MIN));
return (stacksize + PAGE_MASK) & ~PAGE_MASK;
}
diff --git a/libc/test/src/__support/threads/linux/stack_size_test.cpp b/libc/test/src/__support/threads/linux/stack_size_test.cpp
index f26135e8931169..3718508d77ead1 100644
--- a/libc/test/src/__support/threads/linux/stack_size_test.cpp
+++ b/libc/test/src/__support/threads/linux/stack_size_test.cpp
@@ -24,24 +24,29 @@ using LIBC_NAMESPACE::detail::stacksize_from_rlimit;
constexpr size_t PAGE_SIZE = static_cast<size_t>(EXEC_PAGESIZE);
constexpr size_t PAGE_MASK = PAGE_SIZE - 1;
constexpr size_t MAX_STACKSIZE = ~PAGE_MASK - PAGE_SIZE;
-constexpr size_t MIN_STACKSIZE = static_cast<size_t>(PTHREAD_STACK_MIN);
-constexpr size_t ALIGNED_MIN_STACKSIZE =
- (MIN_STACKSIZE + PAGE_MASK) & ~PAGE_MASK;
TEST(LlvmLibcSupportThreadsStackSizeTest, BelowAndAtMinimum) {
- EXPECT_EQ(stacksize_from_rlimit(0), ALIGNED_MIN_STACKSIZE);
- EXPECT_EQ(stacksize_from_rlimit(1), ALIGNED_MIN_STACKSIZE);
- EXPECT_EQ(stacksize_from_rlimit(MIN_STACKSIZE - 1), ALIGNED_MIN_STACKSIZE);
- EXPECT_EQ(stacksize_from_rlimit(MIN_STACKSIZE), ALIGNED_MIN_STACKSIZE);
+ const size_t min_stacksize = static_cast<size_t>(PTHREAD_STACK_MIN);
+ const size_t aligned_min_stacksize =
+ (min_stacksize + PAGE_MASK) & ~PAGE_MASK;
+
+ EXPECT_EQ(stacksize_from_rlimit(0), aligned_min_stacksize);
+ EXPECT_EQ(stacksize_from_rlimit(1), aligned_min_stacksize);
+ EXPECT_EQ(stacksize_from_rlimit(min_stacksize - 1), aligned_min_stacksize);
+ EXPECT_EQ(stacksize_from_rlimit(min_stacksize), aligned_min_stacksize);
}
TEST(LlvmLibcSupportThreadsStackSizeTest, PageAlignment) {
- EXPECT_EQ(stacksize_from_rlimit(ALIGNED_MIN_STACKSIZE + 1),
- ALIGNED_MIN_STACKSIZE + PAGE_SIZE);
- EXPECT_EQ(stacksize_from_rlimit(ALIGNED_MIN_STACKSIZE + PAGE_MASK),
- ALIGNED_MIN_STACKSIZE + PAGE_SIZE);
- EXPECT_EQ(stacksize_from_rlimit(ALIGNED_MIN_STACKSIZE + PAGE_SIZE),
- ALIGNED_MIN_STACKSIZE + PAGE_SIZE);
+ const size_t min_stacksize = static_cast<size_t>(PTHREAD_STACK_MIN);
+ const size_t aligned_min_stacksize =
+ (min_stacksize + PAGE_MASK) & ~PAGE_MASK;
+
+ EXPECT_EQ(stacksize_from_rlimit(aligned_min_stacksize + 1),
+ aligned_min_stacksize + PAGE_SIZE);
+ EXPECT_EQ(stacksize_from_rlimit(aligned_min_stacksize + PAGE_MASK),
+ aligned_min_stacksize + PAGE_SIZE);
+ EXPECT_EQ(stacksize_from_rlimit(aligned_min_stacksize + PAGE_SIZE),
+ aligned_min_stacksize + PAGE_SIZE);
}
TEST(LlvmLibcSupportThreadsStackSizeTest, MaxStacksizeBoundaries) {
>From 83944ded3d8cf4b8462d628686df2d58895cf1c8 Mon Sep 17 00:00:00 2001
From: Pavel Labath <pavel at labath.sk>
Date: Fri, 27 Nov 2026 10:47:54 +0000
Subject: [PATCH 5/5] format
---
libc/test/src/__support/threads/linux/stack_size_test.cpp | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/libc/test/src/__support/threads/linux/stack_size_test.cpp b/libc/test/src/__support/threads/linux/stack_size_test.cpp
index 3718508d77ead1..fe586d61e429ba 100644
--- a/libc/test/src/__support/threads/linux/stack_size_test.cpp
+++ b/libc/test/src/__support/threads/linux/stack_size_test.cpp
@@ -27,8 +27,7 @@ constexpr size_t MAX_STACKSIZE = ~PAGE_MASK - PAGE_SIZE;
TEST(LlvmLibcSupportThreadsStackSizeTest, BelowAndAtMinimum) {
const size_t min_stacksize = static_cast<size_t>(PTHREAD_STACK_MIN);
- const size_t aligned_min_stacksize =
- (min_stacksize + PAGE_MASK) & ~PAGE_MASK;
+ const size_t aligned_min_stacksize = (min_stacksize + PAGE_MASK) & ~PAGE_MASK;
EXPECT_EQ(stacksize_from_rlimit(0), aligned_min_stacksize);
EXPECT_EQ(stacksize_from_rlimit(1), aligned_min_stacksize);
@@ -38,8 +37,7 @@ TEST(LlvmLibcSupportThreadsStackSizeTest, BelowAndAtMinimum) {
TEST(LlvmLibcSupportThreadsStackSizeTest, PageAlignment) {
const size_t min_stacksize = static_cast<size_t>(PTHREAD_STACK_MIN);
- const size_t aligned_min_stacksize =
- (min_stacksize + PAGE_MASK) & ~PAGE_MASK;
+ const size_t aligned_min_stacksize = (min_stacksize + PAGE_MASK) & ~PAGE_MASK;
EXPECT_EQ(stacksize_from_rlimit(aligned_min_stacksize + 1),
aligned_min_stacksize + PAGE_SIZE);
More information about the libc-commits
mailing list