From patchwork Thu Aug 1 16:56:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 1967932 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=Wisaxy5a; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WZZsL2Tvzz1yZv for ; Fri, 2 Aug 2024 02:57:58 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8B0DB3858D3C for ; Thu, 1 Aug 2024 16:57:56 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x62b.google.com (mail-pl1-x62b.google.com [IPv6:2607:f8b0:4864:20::62b]) by sourceware.org (Postfix) with ESMTPS id C3D37385E83D for ; Thu, 1 Aug 2024 16:57:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C3D37385E83D Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C3D37385E83D Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::62b ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722531438; cv=none; b=KL7SngOvHUkV0EZuzCjaO0EU4RQ5JAux+itjIZwbCNHadE9+0G346EE9FUVNwnw8uPEQpYY3XvzihzfT3RnMnCge/RTZp6oZIed2bWVtQNRcYS10NufYGNtyoCLpRjDoTU5D9wyTrgZcsBOVJyd/jQjWTu7mzRPmmg76nAmkCdQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722531438; c=relaxed/simple; bh=yl/tF31/Z7luGGi7sZSr4ntg1apZ5x8ADI25GScdg+M=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=B+LeiXHofmcXGmOcLWoTQ09PgRUs7Fut6J7xxslbD8ZUYD6X2ijq20i2AuYnDXGLoLGSFUpaGQgsch/oHpim5f2jHdZfx9SjGOPsFRd/hnBuW9tCcJoXb1XIPQqQ+hSZ3p7G14/MbVCBzhCBckGVJhbsEQz55dJAB3oJPXT7Qdo= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-1fc66fc35f2so17178305ad.0 for ; Thu, 01 Aug 2024 09:57:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1722531432; x=1723136232; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=IrrMSlADfpucaYnjKACZeUrzWMcxd4V4mVFi/Ycg1y0=; b=Wisaxy5aKa9jlPOXioe11s+aJr4/rFcW8EnWm6bv5pdPP29bAZnIHBoH1TqBtiFTqD tyQPVQr96oGo5P75pqVa8wsEFTP1nmy6S5SZxF/rMawfcApCdRen/5vhvBSwrIVjlsHu 6foJVaxClhI+byASZdr5UHt9chVxgtQjqsIFZcOQkvmRgQCoG3mSfIDmJzcV6jElQLkM tbNxEQ2FY7kZoEdtkgDeL3OV7SXIvdwqb+fv/jbUnzpzzgfxa+Ii3fEizZJIkCGR1N7s /wNInpyNaiWreyerV/Dj4Qak9Kcf1eoTcXjwvYhS3wvp8yt8h5c+rpdJESK+8qlg6snX bWeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722531432; x=1723136232; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=IrrMSlADfpucaYnjKACZeUrzWMcxd4V4mVFi/Ycg1y0=; b=H/Dp/TlrqDKCx+dQDJRpkFLBnAgQx7u7q9y29qW7+fyCWE7Atp7S0cht+RlzjhW/QJ hLW14DmVfXA3sp47lNNKbB1DykIJDouOUHwXhA+6fEqCr0CNR4WX8/nHMwkCG0pCgfDw AHm7WDRTIds8DJN1pcGhAt4SwO1id1XdSyQLlH0kqy6BRoqgF/K5tSDfpSQcy9/lbsmB XWAZCLE/nY+fZmtMEYb90iKfUnwtyK0di2ZYSNPdXMdHuZqrTmi8CiO2AstZzTy4yyTs EefcYBlsc+BPvSbRaqkPkrx+dC52yK7PLeWkUvl2F4X6qFxvOKPghlmDvN0abZ1QoT7C nyNg== X-Gm-Message-State: AOJu0Yy00h+hkHZFuXbsA2IlY4Bzp8q5PeTQw5wHJ99mWhXYP10dwW+1 a0mpAVTU3FjmRpgbFFzww+D+s5/FWw1tKfNAgpq3qEhpFCFbaXyjza8/RF3h+Q+q53kbou78UKO f X-Google-Smtp-Source: AGHT+IGG1BnxU1MJCD15LSBZ5Vtfa158PWL7NY3BonL4rLGkqjP9GKVsNjBYY7bgQEmj3flvsVzQ3A== X-Received: by 2002:a17:903:41c8:b0:1fd:8be5:eb9a with SMTP id d9443c01a7336-1ff5242880dmr28957585ad.13.1722531431983; Thu, 01 Aug 2024 09:57:11 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c1:a5f6:ee4a:e4b3:8292:e6ee]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff59059f78sm981575ad.163.2024.08.01.09.57.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Aug 2024 09:57:11 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Carlos O'Donell Subject: [PATCH] stdlib: Allow concurrent quick_exit (BZ 31997) Date: Thu, 1 Aug 2024 13:56:49 -0300 Message-ID: <20240801165707.3977885-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Spam-Status: No, score=-12.8 required=5.0 tests=BAYES_00, 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 autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org As for exit, also allows concurrent quick_exit to avoid race conditions when it is called concurrently. Since it uses the same internal function as exit, the __exit_lock lock is moved to __run_exit_handlers. It also solved a potential concurrent when calling exit and quick_exit concurrently. The test case 'expected' is expanded to a value larger than the minimum required by C/POSIX (32 entries) so at_quick_exit() will require libc to allocate a new block. This makes the test mre likely to trigger concurrent issues (through free() at __run_exit_handlers) if quick_exit() interacts with the at_quick_exit list concurrently. This is also the latest interpretation of the Austin Ticket [1]. Checked on x86_64-linux-gnu. [1] https://austingroupbugs.net/view.php?id=1845 --- stdlib/Makefile | 1 + stdlib/exit.c | 18 +-- stdlib/tst-concurrent-exit-skeleton.c | 160 ++++++++++++++++++++++++++ stdlib/tst-concurrent-exit.c | 141 +---------------------- stdlib/tst-concurrent-quick_exit.c | 22 ++++ 5 files changed, 196 insertions(+), 146 deletions(-) create mode 100644 stdlib/tst-concurrent-exit-skeleton.c create mode 100644 stdlib/tst-concurrent-quick_exit.c diff --git a/stdlib/Makefile b/stdlib/Makefile index 7eec68b73a..a1be390e72 100644 --- a/stdlib/Makefile +++ b/stdlib/Makefile @@ -273,6 +273,7 @@ tests := \ tst-bsearch \ tst-bz20544 \ tst-canon-bz26341 \ + tst-concurrent-quick_exit \ tst-concurrent-exit \ tst-cxa_atexit \ tst-environ \ diff --git a/stdlib/exit.c b/stdlib/exit.c index bbaf138806..8d7e2e53d0 100644 --- a/stdlib/exit.c +++ b/stdlib/exit.c @@ -28,6 +28,13 @@ __exit_funcs_lock is declared. */ bool __exit_funcs_done = false; +/* The lock handles concurrent exit() and quick_exit(), even though the + C/POSIX standard states that calling exit() more than once is UB. The + recursive lock allows atexit() handlers or destructors to call exit() + itself. In this case, the handler list execution will resume at the + point of the current handler. */ +__libc_lock_define_initialized_recursive (static, __exit_lock) + /* Call all functions registered with `atexit' and `on_exit', in the reverse of the order in which they were registered perform stdio cleanup, and terminate program execution with STATUS. */ @@ -36,6 +43,9 @@ attribute_hidden __run_exit_handlers (int status, struct exit_function_list **listp, bool run_list_atexit, bool run_dtors) { + /* The exit should never return, so there is no need to unlock it. */ + __libc_lock_lock_recursive (__exit_lock); + /* First, call the TLS destructors. */ if (run_dtors) call_function_static_weak (__call_tls_dtors); @@ -132,17 +142,9 @@ __run_exit_handlers (int status, struct exit_function_list **listp, } -/* The lock handles concurrent exit(), even though the C/POSIX standard states - that calling exit() more than once is UB. The recursive lock allows - atexit() handlers or destructors to call exit() itself. In this case, the - handler list execution will resume at the point of the current handler. */ -__libc_lock_define_initialized_recursive (static, __exit_lock) - void exit (int status) { - /* The exit should never return, so there is no need to unlock it. */ - __libc_lock_lock_recursive (__exit_lock); __run_exit_handlers (status, &__exit_funcs, true, true); } libc_hidden_def (exit) diff --git a/stdlib/tst-concurrent-exit-skeleton.c b/stdlib/tst-concurrent-exit-skeleton.c new file mode 100644 index 0000000000..cfd5140466 --- /dev/null +++ b/stdlib/tst-concurrent-exit-skeleton.c @@ -0,0 +1,160 @@ +/* Check if exit/quick_exit can be called concurrently by multiple threads. + Copyright (C) 2024 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 + . */ + +#include +#include +#include +#include +#include +#include +#include + +/* A value larger than the minimum required by C/POSIX (32), to trigger a + new block memory allocation. */ +#define MAX_atexit 64 + +static pthread_barrier_t barrier; + +static void * +tf (void *closure) +{ + xpthread_barrier_wait (&barrier); + EXIT (0); + + return NULL; +} + +static const char expected[] = "00000000000000000000000000000000000" + "00000000000000000000003021121130211"; +static char crumbs[sizeof (expected)]; +static int next_slot = 0; + +static void +exit_with_flush (int code) +{ + fflush (stdout); + /* glibc allows recursive EXIT, the ATEXIT handlers execution will be + resumed from the where the previous EXIT was interrupted. */ + EXIT (code); +} + +/* Take some time, so another thread potentially issue EXIT. */ +#define SETUP_NANOSLEEP \ + if (nanosleep (&(struct timespec) { .tv_sec = 0, .tv_nsec = 1000L }, \ + NULL) != 0) \ + FAIL_EXIT1 ("nanosleep: %m") + +static void +fn0 (void) +{ + crumbs[next_slot++] = '0'; + SETUP_NANOSLEEP; +} + +static void +fn1 (void) +{ + crumbs[next_slot++] = '1'; + SETUP_NANOSLEEP; +} + +static void +fn2 (void) +{ + crumbs[next_slot++] = '2'; + ATEXIT (fn1); + SETUP_NANOSLEEP; +} + +static void +fn3 (void) +{ + crumbs[next_slot++] = '3'; + ATEXIT (fn2); + ATEXIT (fn0); + SETUP_NANOSLEEP; +} + +static void +fn_final (void) +{ + TEST_COMPARE_STRING (crumbs, expected); + exit_with_flush (0); +} + +_Noreturn static void +child (void) +{ + enum { nthreads = 8 }; + + xpthread_barrier_init (&barrier, NULL, nthreads + 1); + + pthread_t thr[nthreads]; + for (int i = 0; i < nthreads; i++) + thr[i] = xpthread_create (NULL, tf, NULL); + + xpthread_barrier_wait (&barrier); + + for (int i = 0; i < nthreads; i++) + { + pthread_join (thr[i], NULL); + /* It should not be reached, it means that thread did not exit for + some reason. */ + support_record_failure (); + } + + EXIT (2); +} + +static int +do_test (void) +{ + /* Register a large number of handler that will trigger a heap allocation + for the handle state. On EXIT, each block will be freed after the + handle is processed. */ + int slots_remaining = MAX_atexit; + + /* Register this first so it can verify expected order of the rest. */ + ATEXIT (fn_final); --slots_remaining; + + TEST_VERIFY_EXIT (ATEXIT (fn1) == 0); --slots_remaining; + TEST_VERIFY_EXIT (ATEXIT (fn3) == 0); --slots_remaining; + TEST_VERIFY_EXIT (ATEXIT (fn1) == 0); --slots_remaining; + TEST_VERIFY_EXIT (ATEXIT (fn2) == 0); --slots_remaining; + TEST_VERIFY_EXIT (ATEXIT (fn1) == 0); --slots_remaining; + TEST_VERIFY_EXIT (ATEXIT (fn3) == 0); --slots_remaining; + + while (slots_remaining > 0) + { + TEST_VERIFY_EXIT (ATEXIT (fn0) == 0); --slots_remaining; + } + + pid_t pid = xfork (); + if (pid != 0) + { + int status; + xwaitpid (pid, &status, 0); + TEST_VERIFY (WIFEXITED (status)); + } + else + child (); + + return 0; +} + +#include diff --git a/stdlib/tst-concurrent-exit.c b/stdlib/tst-concurrent-exit.c index 1141130f87..421c39d631 100644 --- a/stdlib/tst-concurrent-exit.c +++ b/stdlib/tst-concurrent-exit.c @@ -16,142 +16,7 @@ License along with the GNU C Library; if not, see . */ -#include -#include -#include -#include -#include -#include -#include +#define EXIT(__r) exit (__r) +#define ATEXIT(__f) atexit (__f) -#define MAX_atexit 32 - -static pthread_barrier_t barrier; - -static void * -tf (void *closure) -{ - xpthread_barrier_wait (&barrier); - exit (0); - - return NULL; -} - -static const char expected[] = "00000000000000000000000003021121130211"; -static char crumbs[sizeof (expected)]; -static int next_slot = 0; - -static void -exit_with_flush (int code) -{ - fflush (stdout); - /* glibc allows recursive exit, the atexit handlers execution will be - resumed from the where the previous exit was interrupted. */ - exit (code); -} - -/* Take some time, so another thread potentially issue exit. */ -#define SETUP_NANOSLEEP \ - if (nanosleep (&(struct timespec) { .tv_sec = 0, .tv_nsec = 1000L }, \ - NULL) != 0) \ - FAIL_EXIT1 ("nanosleep: %m") - -static void -fn0 (void) -{ - crumbs[next_slot++] = '0'; - SETUP_NANOSLEEP; -} - -static void -fn1 (void) -{ - crumbs[next_slot++] = '1'; - SETUP_NANOSLEEP; -} - -static void -fn2 (void) -{ - crumbs[next_slot++] = '2'; - atexit (fn1); - SETUP_NANOSLEEP; -} - -static void -fn3 (void) -{ - crumbs[next_slot++] = '3'; - atexit (fn2); - atexit (fn0); - SETUP_NANOSLEEP; -} - -static void -fn_final (void) -{ - TEST_COMPARE_STRING (crumbs, expected); - exit_with_flush (0); -} - -_Noreturn static void -child (void) -{ - enum { nthreads = 8 }; - - xpthread_barrier_init (&barrier, NULL, nthreads + 1); - - pthread_t thr[nthreads]; - for (int i = 0; i < nthreads; i++) - thr[i] = xpthread_create (NULL, tf, NULL); - - xpthread_barrier_wait (&barrier); - - for (int i = 0; i < nthreads; i++) - { - pthread_join (thr[i], NULL); - /* It should not be reached, it means that thread did not exit for - some reason. */ - support_record_failure (); - } - - exit (2); -} - -static int -do_test (void) -{ - /* Register a large number of handler that will trigger a heap allocation - for the handle state. On exit, each block will be freed after the - handle is processed. */ - int slots_remaining = MAX_atexit; - - /* Register this first so it can verify expected order of the rest. */ - atexit (fn_final); --slots_remaining; - - TEST_VERIFY_EXIT (atexit (fn1) == 0); --slots_remaining; - TEST_VERIFY_EXIT (atexit (fn3) == 0); --slots_remaining; - TEST_VERIFY_EXIT (atexit (fn1) == 0); --slots_remaining; - TEST_VERIFY_EXIT (atexit (fn2) == 0); --slots_remaining; - TEST_VERIFY_EXIT (atexit (fn1) == 0); --slots_remaining; - TEST_VERIFY_EXIT (atexit (fn3) == 0); --slots_remaining; - - while (slots_remaining > 0) - { - TEST_VERIFY_EXIT (atexit (fn0) == 0); --slots_remaining; - } - - pid_t pid = xfork (); - if (pid != 0) - { - int status; - xwaitpid (pid, &status, 0); - TEST_VERIFY (WIFEXITED (status)); - } - else - child (); - - return 0; -} - -#include +#include "tst-concurrent-exit-skeleton.c" diff --git a/stdlib/tst-concurrent-quick_exit.c b/stdlib/tst-concurrent-quick_exit.c new file mode 100644 index 0000000000..3f321668d6 --- /dev/null +++ b/stdlib/tst-concurrent-quick_exit.c @@ -0,0 +1,22 @@ +/* Check if quick_exit can be called concurrently by multiple threads. + Copyright (C) 2024 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 + . */ + +#define EXIT(__r) quick_exit (__r) +#define ATEXIT(__f) at_quick_exit (__f) + +#include "tst-concurrent-exit-skeleton.c"