public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "vitalybuka at google dot com" <sourceware-bugzilla@sourceware.org>
To: glibc-bugs@sourceware.org
Subject: [Bug libc/27749] New: Data race __run_exit_handlers
Date: Sat, 17 Apr 2021 16:15:15 +0000	[thread overview]
Message-ID: <bug-27749-131@http.sourceware.org/bugzilla/> (raw)

https://sourceware.org/bugzilla/show_bug.cgi?id=27749

            Bug ID: 27749
           Summary: Data race __run_exit_handlers
           Product: glibc
           Version: unspecified
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: libc
          Assignee: unassigned at sourceware dot org
          Reporter: vitalybuka at google dot com
                CC: drepper.fsp at gmail dot com
  Target Milestone: ---

In our fleet it mostly manifested with tsan builds, which is very sensitive to
callback called twice. But I managed to reproduce with code like below of
regular builds.

With asserts it triggers internal accerts, without them it calls atexit
callback twice for the same argument.

It very similar to bug 14333, but the fix for that bug just significantly
reduced probability of the data race but not eliminated it completely.

I reproduced it on 2.27 and head, but I should be reproducible for earlier
versions as well.

#include <stdatomic.h>
#include <stdio.h>
#include <stdlib.h>
#include <support/xthread.h>
#include <sys/wait.h>
#include <unistd.h>

static atomic_int registered;
static atomic_int todo = 100000;

static void
atexit_cb (void *arg)
{
  --registered;
  static void *prev;
  if (arg == prev)
    {
      printf ("%p\n", arg);
      abort ();
    }
  prev = arg;

  while (todo > 0 && registered < 100)
    ;
}

int __cxa_atexit (void (*func) (void *), void *arg, void *d);

static void *cb_arg = NULL;
static void
add_handlers (void)
{
  int n = 10;
  for (int i = 0; i < n; ++i)
    __cxa_atexit (&atexit_cb, ++cb_arg, 0);
  registered += n;
  todo -= n;
}

static void *
thread_func (void *arg)
{
  while (todo > 0)
    if (registered < 10000)
      add_handlers ();
  return 0;
}

static void
test_and_exit (void)
{
  pthread_attr_t attr;

  xpthread_attr_init (&attr);
  xpthread_attr_setdetachstate (&attr, 1);

  xpthread_create (&attr, thread_func, NULL);
  xpthread_attr_destroy (&attr);
  while (!registered)
    ;
  exit (0);
}

static int
do_test (void)
{
  for (int i = 0; i < 20; ++i)
    {
      for (int i = 0; i < 10; ++i)
        if (fork () == 0)
          test_and_exit ();

      int status;
      while (wait (&status) > 0)
        {
          if (!WIFEXITED (status))
            {
              printf ("Failed interation %d\n", i);
              abort ();
            }
        }
    }

  exit (0);
}

#define TEST_FUNCTION do_test
#include <support/test-driver.c>

-- 
You are receiving this mail because:
You are on the CC list for the bug.

             reply	other threads:[~2021-04-17 16:15 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-17 16:15 vitalybuka at google dot com [this message]
2021-04-17 17:16 ` [Bug libc/27749] " vitalybuka at google dot com
2021-04-17 17:24 ` vitalybuka at google dot com
2021-05-14 14:38 ` adhemerval.zanella at linaro dot org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-27749-131@http.sourceware.org/bugzilla/ \
    --to=sourceware-bugzilla@sourceware.org \
    --cc=glibc-bugs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).