From: Vitaly Buka <vitalybuka@google.com>
To: libc-alpha@sourceware.org
Subject: [PATCH] stdlib: Fix data race in __run_exit_handlers
Date: Mon, 21 Sep 2020 01:31:19 -0700 [thread overview]
Message-ID: <20200921083119.2748742-1-vitalybuka@google.com> (raw)
In-Reply-To: <20200920233756.3886676-1-vitalybuka@google.com>
Read f->func.cxa under the lock.
There is a clear data race:
thread 0: __run_exit_handlers unlock __exit_funcs_lock
thread 1: __internal_atexit locks __exit_funcs_lock
thread 0: f->flavor = ef_free;
thread 1: sees ef_free and use it as new
thread 1: new->func.cxa.fn = (void (*) (void *, int)) func;
thread 1: new->func.cxa.arg = arg;
thread 1: new->flavor = ef_cxa;
thread 0: cxafct = f->func.cxa.fn; // it's wrong fn!
thread 0: cxafct (f->func.cxa.arg, status); // it's wrong arg!
thread 0: goto restart;
thread 0: call the same exit_function again as it's ef_cxa
Without patch abd with NDEBUG the test fails about 30% of time.
But it fails very rarely with asserts enabled.
---
stdlib/Makefile | 4 +-
stdlib/exit.c | 20 ++++++---
stdlib/test-cxa_atexit-race2.c | 76 ++++++++++++++++++++++++++++++++++
3 files changed, 93 insertions(+), 7 deletions(-)
create mode 100644 stdlib/test-cxa_atexit-race2.c
diff --git a/stdlib/Makefile b/stdlib/Makefile
index 4615f6dfe7..f622fa9e0b 100644
--- a/stdlib/Makefile
+++ b/stdlib/Makefile
@@ -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 tst-strtod-nan-sign \
tst-swapcontext1 tst-setcontext4 tst-setcontext5 \
@@ -101,6 +102,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)
diff --git a/stdlib/exit.c b/stdlib/exit.c
index 7bca1cdc14..9c79aa4a62 100644
--- a/stdlib/exit.c
+++ b/stdlib/exit.c
@@ -72,44 +72,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
diff --git a/stdlib/test-cxa_atexit-race2.c b/stdlib/test-cxa_atexit-race2.c
new file mode 100644
index 0000000000..9eb72471da
--- /dev/null
+++ b/stdlib/test-cxa_atexit-race2.c
@@ -0,0 +1,76 @@
+/* Support file for atexit/exit, etc. race tests.
+ Copyright (C) 2017-2020 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 spawns multiple threads, and check the same desctructor is
+ not called more then once. */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <support/xthread.h>
+
+static const int thread_num = 4;
+
+static void
+atexitcb (void *arg)
+{
+ static void *prev;
+ if (arg == prev)
+ {
+ printf ("%p\n", arg);
+ abort ();
+ }
+ prev = arg;
+}
+
+int __cxa_atexit (void (*func) (void *), void *arg, void *d);
+
+static void *
+threadfunc (void *arg)
+{
+ for (int i = 0; i < 100; ++i)
+ {
+ for (int j = 0; j < 4000; ++j)
+ __cxa_atexit (&atexitcb, arg += thread_num, 0);
+ pthread_yield ();
+ }
+ return 0;
+}
+
+static int
+do_test (void)
+{
+ size_t i;
+ pthread_attr_t attr;
+
+ xpthread_attr_init (&attr);
+ xpthread_attr_setdetachstate (&attr, 1);
+
+ for (i = 0; i < thread_num; ++i)
+ xpthread_create (&attr, threadfunc, (void *)i);
+ xpthread_attr_destroy (&attr);
+
+ exit (0);
+}
+
+#define TEST_FUNCTION do_test
+#include <support/test-driver.c>
--
2.28.0.681.g6f77f65b4e-goog
next prev parent reply other threads:[~2020-09-21 8:31 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-09-20 9:57 Vitaly Buka
2020-09-20 12:09 ` Vitaly Buka
2020-09-20 20:41 ` Paul Pluzhnikov
2020-09-20 21:26 ` Vitaly Buka
2020-09-20 23:36 ` Vitaly Buka
2020-09-20 23:37 ` Vitaly Buka
2020-09-21 8:31 ` Vitaly Buka [this message]
2020-09-30 16:01 ` Joseph Myers
2021-04-17 16:16 ` Vitaly Buka
2021-04-17 17:11 ` Vitaly Buka
2021-04-17 17:13 ` Vitaly Buka
2021-04-17 17:22 ` Vitaly Buka
2021-04-17 18:01 ` Paul Pluzhnikov
2021-04-20 22:51 ` Vitaly Buka
2021-04-20 23:40 ` Paul Pluzhnikov
2021-04-26 19:20 ` Vitaly Buka
2021-04-26 19:23 ` Vitaly Buka
2021-04-26 19:27 ` Vitaly Buka
2021-05-13 13:15 ` Adhemerval Zanella
2021-05-14 6:50 ` Vitaly Buka
2021-04-17 17:36 ` Paul Pluzhnikov
2021-04-17 20:19 ` Florian Weimer
2021-04-19 2:48 ` Vitaly Buka
2021-04-19 2:57 ` Vitaly Buka
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=20200921083119.2748742-1-vitalybuka@google.com \
--to=vitalybuka@google.com \
--cc=libc-alpha@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).