Have pthread_attr_getstack for the main thread report RLIMIT_STACK...
...rather than just what's already mapped in. This seems somewhat
contrary to POSIX's "All pages within the stack described by stackaddr
and stacksize shall be both readable and writable by the thread", but
it's what glibc does.
Bug: 17111575
Change-Id: If9e2dfad9a603c0d0615a8123aacda4946e95b2c
diff --git a/libc/bionic/pthread_attr.cpp b/libc/bionic/pthread_attr.cpp
index 8df3bff..c93970a 100644
--- a/libc/bionic/pthread_attr.cpp
+++ b/libc/bionic/pthread_attr.cpp
@@ -116,6 +116,16 @@
static int __pthread_attr_getstack_main_thread(void** stack_base, size_t* stack_size) {
ErrnoRestorer errno_restorer;
+ rlimit stack_limit;
+ if (getrlimit(RLIMIT_STACK, &stack_limit) == -1) {
+ return errno;
+ }
+
+ // If the current RLIMIT_STACK is RLIM_INFINITY, only admit to an 8MiB stack for sanity's sake.
+ if (stack_limit.rlim_cur == RLIM_INFINITY) {
+ stack_limit.rlim_cur = 8 * 1024 * 1024;
+ }
+
// It doesn't matter which thread we are; we're just looking for "[stack]".
FILE* fp = fopen("/proc/self/maps", "re");
if (fp == NULL) {
@@ -126,17 +136,8 @@
if (ends_with(line, " [stack]\n")) {
uintptr_t lo, hi;
if (sscanf(line, "%" SCNxPTR "-%" SCNxPTR, &lo, &hi) == 2) {
- *stack_base = reinterpret_cast<void*>(lo);
- *stack_size = hi - lo;
-
- // Does our current RLIMIT_STACK mean we won't actually get everything /proc/maps promises?
- rlimit stack_limit;
- if (getrlimit(RLIMIT_STACK, &stack_limit) != -1) {
- if (*stack_size > stack_limit.rlim_cur) {
- *stack_size = stack_limit.rlim_cur;
- }
- }
-
+ *stack_size = stack_limit.rlim_cur;
+ *stack_base = reinterpret_cast<void*>(hi - *stack_size);
fclose(fp);
return 0;
}