Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 08 Feb 2021 06:42:37 +0000
From:      bugzilla-noreply@freebsd.org
To:        bugs@FreeBSD.org
Subject:   [Bug 253337] Linuxulator: glibc's pthread_getattr_np reports stack size as 124K
Message-ID:  <bug-253337-227@https.bugs.freebsd.org/bugzilla/>

next in thread | raw e-mail | index | archive | help
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=3D253337

            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@FreeBSD.org
          Reporter: iwtcex@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 =3D 0;
  void*  addr =3D NULL;

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

  fprintf(stderr, "stack size =3D %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=20=20=20=20
/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 =3D 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 =3D (size_t) iattr->stackaddr - last_to;

(https://sourceware.org/git/?p=3Dglibc.git;a=3Dblob;f=3Dnptl/pthread_getatt=
r_np.c;h=3D25807cb529880d67a6561b6ebcd45042e89dea3e;hb=3DHEAD#l144)

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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?bug-253337-227>