From patchwork Sun Sep 20 12:09:57 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vitaly Buka X-Patchwork-Id: 40459 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 B024D3971C54; Sun, 20 Sep 2020 12:10:04 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B024D3971C54 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1600603804; bh=n3XkSogXYcNYKYV1tt3WcMKvO5kd6QBEAKG93zGQpjI=; h=Date:In-Reply-To:References:Subject:To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=GfJarAFEiJkAHffHutsN/S9QksnWY2V04Nsr0yIdTEtFCpqUqclCzGJHYFJZM8p27 nV/hkvqeRe7VFM7IyZ5zvMAEUE7r17I4YsH1/r5V6BZckiINU245fL7mY23vLOzwod nGnpBojM8jbdSbllKnP203NlduYWyWZGBuRmmz6I= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qt1-x849.google.com (mail-qt1-x849.google.com [IPv6:2607:f8b0:4864:20::849]) by sourceware.org (Postfix) with ESMTPS id 370213857C5C for ; Sun, 20 Sep 2020 12:10:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 370213857C5C Received: by mail-qt1-x849.google.com with SMTP id f12so10118080qtq.5 for ; Sun, 20 Sep 2020 05:10:02 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=n3XkSogXYcNYKYV1tt3WcMKvO5kd6QBEAKG93zGQpjI=; b=P+FmfXEZHW80EGU9JdLObIuU0d2oCiL6v/68HsVY/Tq61twAnubtRUXPr5qrb5qJZC 8DPnpiWprJy5jjMffxZR0xpXie1M6inHUKb7T6AFxLchOQ4rDGN5nvkYv0XysDGXJqlw lCsgfmng/f5IhqBtnMNPgXXyZlS8uQWiyhTuecFCN3cZabE/KJQ0cHW8+L7zVuyJx0s1 Z7madTp1Djh9ojo7J4OxYXU9osPDjp1n6ZPydEc/bq5KCIkCuKh9V/KL6EAna1FGun/j rBBzvgNtyKaU+B7/buKV8v9AbJfZNISGsZx7aQq9ciguD/758OUk9II1wdB/qko69k9J 7YiA== X-Gm-Message-State: AOAM531+WRHaghy59WgDsMjIE2zFinnoYzEBUexN6JMgEu/EwC0cqX5A u+T65RFAqVTKWy3Zr0y469Znw3bliifiuZLyZX9nHUY1JhnZJ4oLmCKGZLj8Jffvuj+1tBS3m3C sozpYbB3n6QI4zXdhgmXCVzVWcNwQpqtx6iGlt16ns9SAsgAveaAJPp39wo6EOrkxEh5f1jBrRC nF X-Google-Smtp-Source: ABdhPJwnDIfVR4eDJH0S7apCfe50svyr/qxfWCU9P73tImu96KMW4smhbjlAz59uV0FuMgcltLEN7ZxcKAf1NBSn X-Received: from vbl.svl.corp.google.com ([2620:15c:2ce:0:a28c:fdff:fee0:ac24]) (user=vitalybuka job=sendgmr) by 2002:ad4:5745:: with SMTP id q5mr41997213qvx.29.1600603801413; Sun, 20 Sep 2020 05:10:01 -0700 (PDT) Date: Sun, 20 Sep 2020 05:09:57 -0700 In-Reply-To: <20200920095750.396908-1-vitalybuka@google.com> Message-Id: <20200920120957.852821-1-vitalybuka@google.com> Mime-Version: 1.0 References: <20200920095750.396908-1-vitalybuka@google.com> X-Mailer: git-send-email 2.28.0.681.g6f77f65b4e-goog Subject: [PATCH] stdlib: Fix data race in __run_exit_handlers To: libc-alpha@sourceware.org X-Spam-Status: No, score=-19.8 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 Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" 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 It's incredibly rare so the test without patch fails only about 10%. --- stdlib/Makefile | 4 +- stdlib/exit.c | 20 +++++--- stdlib/test-cxa_atexit-race2.c | 94 ++++++++++++++++++++++++++++++++++ 3 files changed, 111 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..93cdec82f0 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; + /* Unlock the list while we call a foreign function. */ + __libc_lock_unlock (__exit_funcs_lock); #ifdef PTR_DEMANGLE PTR_DEMANGLE (onfct); #endif - onfct (status, f->func.on.arg); + onfct (status, arg); + __libc_lock_lock (__exit_funcs_lock); break; case ef_at: atfct = f->func.at; + /* Unlock the list while we call a foreign function. */ + __libc_lock_unlock (__exit_funcs_lock); #ifdef PTR_DEMANGLE PTR_DEMANGLE (atfct); #endif 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; + /* Unlock the list while we call a foreign function. */ + __libc_lock_unlock (__exit_funcs_lock); #ifdef PTR_DEMANGLE PTR_DEMANGLE (cxafct); #endif - cxafct (f->func.cxa.arg, status); + 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..08acafe19f --- /dev/null +++ b/stdlib/test-cxa_atexit-race2.c @@ -0,0 +1,94 @@ +/* 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 + . */ + +/* 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 accessing destructor function list (Bug 14333). + + This test spawns large number of threads, and check the same + desctructor is not called more then once. */ + +#include +#include +#include +#include +#include + +extern void *__dso_handle; + +const size_t kNumThreads = 50; +const size_t kStacksize = +#ifdef PTHREAD_STACK_MIN + 0x20000 < PTHREAD_STACK_MIN ? PTHREAD_STACK_MIN : +#endif + 0x20000; + +static atomic_intptr_t added; +static atomic_intptr_t done; +static void *prev; + +static void +atexitcb (void *arg) +{ + if (arg == prev) + abort (); + prev = arg; + ++done; +} + +int __cxa_atexit (void (*func) (void *), void *arg, void *d); + +static void * +threadfunc (void *unused) +{ + for (; done < 1e6;) + { + if (added < done + 100) + { + __cxa_atexit (&atexitcb, (void *)(++added), __dso_handle); + } + } + return 0; +} + +static int +do_test (void) +{ + size_t i; + pthread_attr_t attr; + + xpthread_attr_init (&attr); + xpthread_attr_setdetachstate (&attr, 1); + + /* With default 8MiB Linux stack size, creating 1024 threads can cause + VM exhausiton on 32-bit machines. Reduce stack size of each thread to + 128KiB for a maximum required VM size of 128MiB. */ + xpthread_attr_setstacksize (&attr, kStacksize); + + for (i = 0; i < kNumThreads; ++i) + { + xpthread_create (&attr, threadfunc, NULL); + } + xpthread_attr_destroy (&attr); + + exit (0); +} + +#define TEST_FUNCTION do_test +#include