[Bug 253337] Linuxulator: glibc's pthread_getattr_np reports stack size as 124K

bugzilla-noreply at freebsd.org bugzilla-noreply at freebsd.org
Mon Feb 8 06:42:37 UTC 2021


https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=253337

            Bug ID: 253337
           Summary: Linuxulator: glibc's pthread_getattr_np reports stack
                    size as 124K
           Product: Base System
           Version: Unspecified
          Hardware: Any
                OS: Any
            Status: New
          Severity: Affects Only Me
          Priority: ---
         Component: kern
          Assignee: bugs at FreeBSD.org
          Reporter: iwtcex at gmail.com

I think it should return a value equal or slightly smaller than
RLIMIT_STACK instead. It does so on Ubuntu at least.

Apparently, Mono (most notably used in the popular Unity game engine)
relies on this for setting stack guards:
https://github.com/mono/mono/blob/da11592cbea4269971f4b1f9624769a85cc10660/mono/utils/mono-threads-linux.c#L13-L38,
https://github.com/mono/mono/blob/43190aeb5f7e4d7e0185d3b656054bf232219fe2/mono/mini/mini-exceptions.c#L3160-L3175.

Reproducer:
% uname -a
FreeBSD desktop 12.2-RELEASE-p1 FreeBSD 12.2-RELEASE-p1 GENERIC  amd64
% cat apparent_stack_size.c
#define _GNU_SOURCE

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>

int main() {
  char cmd[100];
  snprintf(cmd, sizeof(cmd), "cat /proc/%d/maps | tail -n 5", getpid());
  system(cmd);

  size_t size = 0;
  void*  addr = NULL;

  pthread_attr_t attr;
  assert(pthread_attr_init(&attr) == 0);
  assert(pthread_getattr_np(pthread_self(), &attr) == 0);
  assert(pthread_attr_getstack(&attr, &addr, &size) == 0);
  assert(pthread_attr_destroy(&attr) == 0);

  fprintf(stderr, "stack size = %zd\n", size);

  return 0;
}
% /compat/linux/bin/cc apparent_stack_size.c -pthread -o test
% ./test
00000008011c7000-00000008011c9000 rw-p 0038a000 00:00 391497    
/compat/linux/usr/lib64/libc-2.17.so
00000008011c9000-00000008011ce000 rw-p 00000000 00:00 0
00007fffdffff000-00007ffffffdf000 ---p 00000000 00:00 0
00007ffffffdf000-00007ffffffff000 rw-p 00000000 00:00 0           [stack]
00007ffffffff000-0000800000000000 r-xs 00000000 00:00 0           [vdso]
stack size = 126976

As it happens, glibc reads /proc/self/maps and compares the stack entry
to the preceding entry. You know, just in case:

  /* The limit might be too high.  */
  if ((size_t) iattr->stacksize
    > (size_t) iattr->stackaddr - last_to)
  iattr->stacksize = (size_t) iattr->stackaddr - last_to;

(https://sourceware.org/git/?p=glibc.git;a=blob;f=nptl/pthread_getattr_np.c;h=25807cb529880d67a6561b6ebcd45042e89dea3e;hb=HEAD#l144)

-- 
You are receiving this mail because:
You are the assignee for the bug.


More information about the freebsd-bugs mailing list