From patchwork Sat Apr 17 17:11:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vitaly Buka X-Patchwork-Id: 43027 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 188153896815; Sat, 17 Apr 2021 17:11:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 188153896815 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1618679469; bh=RJUOFatsf9I3gDPmCMBToR8NcldVI0yQStEkop8/PM0=; h=Date:In-Reply-To:References:Subject:To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=BFNmvnPhDARbO21bxmfI8fdl7kwHypAaNcUwhRF32+6ExldDA+zWEbjxd7awiKJS/ Idr2nqcbs7u5twR7pM6Sy5lNaVgFL1HbtZ2T5dYEZr2VET5gHgQiYDwUOvh5E02w7/ yW3T4/YfSWBzaAzRSgotW+4pZM7BkfkuErjFDc7c= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by sourceware.org (Postfix) with ESMTPS id 22BB73896815 for ; Sat, 17 Apr 2021 17:11:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 22BB73896815 Received: by mail-pj1-x1049.google.com with SMTP id mw17-20020a17090b4d11b029014cf3b640f4so16352593pjb.6 for ; Sat, 17 Apr 2021 10:11:05 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=RJUOFatsf9I3gDPmCMBToR8NcldVI0yQStEkop8/PM0=; b=Nk8RIR0oXRpUeWLytdhbUUYFSWg5lDCwcIiO3OAA9ktuHyBqSxuALVUVHL+y0mES7o R+GPAP3A23uQ9hZN4X8F5JMErc534KC5pnsHTU6FYLJvRnXrfTWx7pAIYSiawL3HjJdX UGhz0Im/M7aDU4ec6suHl1+q9C/7O6OQxrDqepKgmJ5WTdD6lOyHeriStKUVAYTGz78i 6jaZLM30btEPLWi4uX3bJuWdxCsK715W5Kg+SjsdfgC2Rwellpw+KYBixO3R4UiQt5yi OaWtUHcuVI78HA80jhkmySAasdgn36p0pFoVB+qSY9uR859BMHtVVa62Kb2/gEOchvIY qwiA== X-Gm-Message-State: AOAM532vnuS6LgujNO0u0AfsyMui5WGprslvINXTJJhO9O5V+iY+jqBG vax2UJ16M4MYozXBLZX+DrEAwqVPD5GoYai2HglhVyMbR+6N669fLt7O65K3le5jRSYZkIuQHlq 2ropFCARTSaXa3ZhHoe5oEXTCqS6axS2o8r26ak6yX+jncgzNaG8jZJEfL4LVCaDAk9ekVbHB6n 93 X-Google-Smtp-Source: ABdhPJy9EGr2PUP6+XvvNvflBgy6h/BhVFFfD6tO6Mh+ZIDwrzkFK+rZyuo6CiNd8BuuuzFr6fNhuVuuYQ6ssFL+ X-Received: from vbl.svl.corp.google.com ([2620:15c:2ce:200:29c7:1d41:7951:c5f2]) (user=vitalybuka job=sendgmr) by 2002:a17:902:c407:b029:e7:2272:d164 with SMTP id k7-20020a170902c407b02900e72272d164mr15098432plk.84.1618679464152; Sat, 17 Apr 2021 10:11:04 -0700 (PDT) Date: Sat, 17 Apr 2021 10:11:00 -0700 In-Reply-To: <20210417161644.2211140-1-vitalybuka@google.com> Message-Id: <20210417171100.2698832-1-vitalybuka@google.com> Mime-Version: 1.0 References: <20210417161644.2211140-1-vitalybuka@google.com> X-Mailer: git-send-email 2.31.1.368.gbe11c130af-goog Subject: [PATCH] stdlib: Fix data race in __run_exit_handlers To: libc-alpha@sourceware.org X-Spam-Status: No, score=-20.4 required=5.0 tests=BAYES_00, DKIMWL_WL_MED, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Vitaly Buka via Libc-alpha From: Vitaly Buka Reply-To: Vitaly Buka Cc: Vitaly Buka Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" Keep __exit_funcs_lock almost all the time and unlock it only to execute callbacks. This fixed two issues. 1. f->func.cxa was modified outside the lock with rare data race like: 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 2. Don't unlock in main while loop after *listp = cur->next. If *listp is NULL and __exit_funcs_done is false another thread may fail in __new_exitfn on assert (l != NULL). The test needs multiple iterations to consistently fail without the fix. --- stdlib/Makefile | 4 +- stdlib/exit.c | 28 +++++--- stdlib/test-cxa_atexit-race2.c | 114 +++++++++++++++++++++++++++++++++ 3 files changed, 135 insertions(+), 11 deletions(-) create mode 100644 stdlib/test-cxa_atexit-race2.c diff --git a/stdlib/Makefile b/stdlib/Makefile index b3b30ab73e..f5755a1654 100644 --- a/stdlib/Makefile +++ b/stdlib/Makefile @@ -81,7 +81,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 \ @@ -100,6 +101,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-tst-canon-bz26341 = $(shared-thread-library) diff --git a/stdlib/exit.c b/stdlib/exit.c index bed82733ad..f095b38ab3 100644 --- a/stdlib/exit.c +++ b/stdlib/exit.c @@ -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 index 0000000000..04656d261c --- /dev/null +++ b/stdlib/test-cxa_atexit-race2.c @@ -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 + . */ + +/* 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 +#include +#include +#include +#include +#include + +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