Bug 248225 - Linuxulator: pthread_join crash
Summary: Linuxulator: pthread_join crash
Status: In Progress
Alias: None
Product: Base System
Classification: Unclassified
Component: kern (show other bugs)
Version: 12.1-RELEASE
Hardware: amd64 Any
: --- Affects Only Me
Assignee: Edward Tomasz Napierala
Depends on:
Blocks: 247219
  Show dependency treegraph
Reported: 2020-07-23 19:23 UTC by Alex S
Modified: 2020-10-27 20:06 UTC (History)
2 users (show)

See Also:


Note You need to log in before you can comment on or make changes to this bug.
Description Alex S 2020-07-23 19:23:40 UTC
% cat pthread-crash.c
#define _GNU_SOURCE

#include <pthread.h>
#include <stdio.h>

static void* test(void* arg) {
  printf("%s started\n", __func__);
  return NULL;

int main() {

  pthread_t t;
  pthread_create(&t, NULL, test, NULL);

  void* val;

  pthread_join(t, &val);
  pthread_join(t, &val);

  return 0;
% /compat/linux/bin/gcc -Wall pthread-crash.c -pthread -o pthread-crash
% ./pthread-crash
test started
Segmentation fault (core dumped)

It should be noted, while calling pthread_join in that manner is explicitly discouraged in both FreeBSD and Linux man pages, some programs (Steam) still occasionally do this and this does not lead to crash on Linux.
Comment 1 Alex S 2020-07-25 15:30:48 UTC
Turns out pthread_join in glibc calls this bit of code [1]:

  if (__builtin_expect (stack_cache_actsize > stack_cache_maxsize, 0))
    __free_stacks (stack_cache_maxsize);

Consequently, `limit stacksize 8192` (the default value on Linux) gets rid of crash.

[1] https://sourceware.org/git/?p=glibc.git;a=blob;f=nptl/allocatestack.c;h=4ae4b5a9862f35f7f42ceb775094c85320203cce;hb=7ebd114211dcd290efd54e610bbde0765bd7764c#l312
Comment 2 Edward Tomasz Napierala freebsd_committer 2020-07-25 16:27:01 UTC
Thanks for tracking this down!  What a happy coincidence; I've been submitted a patch that adjusts Linux stack sizes to get the core dump size down to reasonable levels; seems like we'll be able to kill two birds with one stone.
Comment 3 Edward Tomasz Napierala freebsd_committer 2020-10-14 13:46:49 UTC
Comment 4 commit-hook freebsd_committer 2020-10-16 11:24:25 UTC
A commit references this bug:

Author: trasz
Date: Fri Oct 16 11:23:31 UTC 2020
New revision: 366756
URL: https://svnweb.freebsd.org/changeset/base/366756

  Set default stack size for Linux apps to 8MB.  This matches Linux'
  defaults, makes core files smaller, and fixes applications which use
  pthread_join(3) in a wrong way, namely Steam.

  This is based on a patch submitted by Jason Yang, which I've reworked
  to set the limit instead of only changing the value reported (which
  is enough to fix the bug for Linux pthreads, but could be confusing).

  PR:		248225
  Submitted by:	Jason_YH_Yang at wistron.com (earlier version)
  Analyzed by:	Alex S <iwtcex@gmail.com>
  Reviewed by:	emaste
  MFC after:	2 weeks
  Sponsored by:	The FreeBSD Foundation
  Differential Revision:	https://reviews.freebsd.org/D26778