]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
Read f->func.cxa under the lock.
authorVitaly Buka <vitalybuka@google.com>
Thu, 15 Jul 2021 21:39:33 +0000 (14:39 -0700)
committerFangrui Song <i@maskray.me>
Sat, 28 Aug 2021 00:26:07 +0000 (17:26 -0700)
stdlib/Makefile
stdlib/exit.c
stdlib/test-cxa_atexit-race2.c [new file with mode: 0644]

index a9ad849531d6b46d5783ed96e4f7bc10d1b3fe53..c11a794d2acf92ee85c3346528c566a30e855450 100644 (file)
@@ -82,7 +82,8 @@ tests         := tst-strtol tst-strtod testmb testrand testsort testdiv   \
                   tst-width-stdint tst-strfrom tst-strfrom-locale          \
                   tst-getrandom tst-atexit tst-at_quick_exit               \
                   tst-cxa_atexit tst-on_exit test-atexit-race              \
-                  test-at_quick_exit-race test-cxa_atexit-race             \
+                  test-at_quick_exit-race test-cxa_atexit-race             \
+                  test-cxa_atexit-race2                                    \
                   test-on_exit-race test-dlclose-exit-race                 \
                   tst-makecontext-align test-bz22786
 
@@ -97,6 +98,7 @@ endif
 LDLIBS-test-atexit-race = $(shared-thread-library)
 LDLIBS-test-at_quick_exit-race = $(shared-thread-library)
 LDLIBS-test-cxa_atexit-race = $(shared-thread-library)
+LDLIBS-test-cxa_atexit-race2 = $(shared-thread-library)
 LDLIBS-test-on_exit-race = $(shared-thread-library)
 
 LDLIBS-test-dlclose-exit-race = $(shared-thread-library) $(libdl)
index 0a4944fa051e70049a568924a4dd03afa6f7a8f1..9c669fd1a847a0417520b0004c10f32404284841 100644 (file)
@@ -45,6 +45,8 @@ __run_exit_handlers (int status, struct exit_function_list **listp,
     if (run_dtors)
       __call_tls_dtors ();
 
+  __libc_lock_lock (__exit_funcs_lock);
+
   /* We do it this way to handle recursive calls to exit () made by
      the functions registered with `atexit' and `on_exit'. We call
      everyone on the list and use the status value in the last
@@ -53,8 +55,6 @@ __run_exit_handlers (int status, struct exit_function_list **listp,
     {
       struct exit_function_list *cur;
 
-      __libc_lock_lock (__exit_funcs_lock);
-
     restart:
       cur = *listp;
 
@@ -63,7 +63,6 @@ __run_exit_handlers (int status, struct exit_function_list **listp,
          /* Exit processing complete.  We will not allow any more
             atexit/on_exit registrations.  */
          __exit_funcs_done = true;
-         __libc_lock_unlock (__exit_funcs_lock);
          break;
        }
 
@@ -72,44 +71,52 @@ __run_exit_handlers (int status, struct exit_function_list **listp,
          struct exit_function *const f = &cur->fns[--cur->idx];
          const uint64_t new_exitfn_called = __new_exitfn_called;
 
-         /* Unlock the list while we call a foreign function.  */
-         __libc_lock_unlock (__exit_funcs_lock);
          switch (f->flavor)
            {
              void (*atfct) (void);
              void (*onfct) (int status, void *arg);
              void (*cxafct) (void *arg, int status);
+             void *arg;
 
            case ef_free:
            case ef_us:
              break;
            case ef_on:
              onfct = f->func.on.fn;
+             arg = f->func.on.arg;
 #ifdef PTR_DEMANGLE
              PTR_DEMANGLE (onfct);
 #endif
-             onfct (status, f->func.on.arg);
+             /* Unlock the list while we call a foreign function.  */
+             __libc_lock_unlock (__exit_funcs_lock);
+             onfct (status, arg);
+             __libc_lock_lock (__exit_funcs_lock);
              break;
            case ef_at:
              atfct = f->func.at;
 #ifdef PTR_DEMANGLE
              PTR_DEMANGLE (atfct);
 #endif
+             /* Unlock the list while we call a foreign function.  */
+             __libc_lock_unlock (__exit_funcs_lock);
              atfct ();
+             __libc_lock_lock (__exit_funcs_lock);
              break;
            case ef_cxa:
              /* To avoid dlclose/exit race calling cxafct twice (BZ 22180),
                 we must mark this function as ef_free.  */
              f->flavor = ef_free;
              cxafct = f->func.cxa.fn;
+             arg = f->func.cxa.arg;
 #ifdef PTR_DEMANGLE
              PTR_DEMANGLE (cxafct);
 #endif
-             cxafct (f->func.cxa.arg, status);
+             /* Unlock the list while we call a foreign function.  */
+             __libc_lock_unlock (__exit_funcs_lock);
+             cxafct (arg, status);
+             __libc_lock_lock (__exit_funcs_lock);
              break;
            }
-         /* Re-lock again before looking at global state.  */
-         __libc_lock_lock (__exit_funcs_lock);
 
          if (__glibc_unlikely (new_exitfn_called != __new_exitfn_called))
            /* The last exit function, or another thread, has registered
@@ -123,9 +130,10 @@ __run_exit_handlers (int status, struct exit_function_list **listp,
           allocate element.  */
        free (cur);
 
-      __libc_lock_unlock (__exit_funcs_lock);
     }
 
+  __libc_lock_unlock (__exit_funcs_lock);
+
   if (run_list_atexit)
     RUN_HOOK (__libc_atexit, ());
 
diff --git a/stdlib/test-cxa_atexit-race2.c b/stdlib/test-cxa_atexit-race2.c
new file mode 100644 (file)
index 0000000..27f142c
--- /dev/null
@@ -0,0 +1,114 @@
+/* Support file for atexit/exit, etc. race tests.
+   Copyright (C) 2017-2021 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+/* This file must be run from within a directory called "stdlib".  */
+
+/* The atexit/exit, at_quick_exit/quick_exit, __cxa_atexit/exit, etc. exhibited
+   data race while calling destructors.
+
+   This test registers destructors from the background thread, and checks that
+   the same destructor is not called more than once.  */
+
+#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)
+{
+  atomic_fetch_sub (&registered, 1);
+  static void *prev;
+  if (arg == prev)
+    {
+      printf ("%p\n", arg);
+      abort ();
+    }
+  prev = arg;
+
+  while (atomic_load (&todo) > 0 && atomic_load (&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);
+  atomic_fetch_add (&registered, n);
+  atomic_fetch_sub (&todo, n);
+}
+
+static void *
+thread_func (void *arg)
+{
+  while (atomic_load (&todo) > 0)
+    if (atomic_load (&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 (!atomic_load (&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>