From patchwork Tue Jun 7 13:44:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 54888 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 0F2D338133E6 for ; Tue, 7 Jun 2022 13:44:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0F2D338133E6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1654609478; bh=Gnrd9Ftlag05CXkRUMNzZZbqwqUu1wGMt5WzOsJ6Tz8=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=uy3OeFoGnwp35W6kPccICd2yekKlmH4IIVeqG/sGjeY8xAmw8uJSWOlVdvGXy/+fj LOndyfrvVVObnbNuMHKXyO8OE9ZGNOlD6hihzJLOncwlO62MfdpcNe/yf/VEfxRC+T ffpxKCxZowFBT65ILFqrltNaMUYZwoGj1beZdpPI= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oi1-x229.google.com (mail-oi1-x229.google.com [IPv6:2607:f8b0:4864:20::229]) by sourceware.org (Postfix) with ESMTPS id 4EDCB3857425 for ; Tue, 7 Jun 2022 13:44:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 4EDCB3857425 Received: by mail-oi1-x229.google.com with SMTP id h187so10812821oif.4 for ; Tue, 07 Jun 2022 06:44:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=Gnrd9Ftlag05CXkRUMNzZZbqwqUu1wGMt5WzOsJ6Tz8=; b=hKHapR5IE1N0w+aPu1WNPP9YwBUmqbp3Jle6VKADFy5yCNju0ZnBqSPn55VvBMfrEF X/imwA0WPIVlrDvRNmDGPNf9GlObai/jykd8LAHgthTTT0vMSvO7Ph4/ZFLD6r8vxnyg hIAKjoNWMWbyrfMfvfcxSq/L0hUlAWnBCr2QS/c9N8W++I74gnrYjZFakCixfb5x+HQR 1MuzN6jC/3U6F4eR0yaJUWvBZF2EZKipgLRiFPo/LLpCZn9ktDHQuhdXqMtY45PrqWEE avYxB6uWihKt9rIsigCd0C8syyPitiR4nu/PGFP8P5kYzvxLaf1ifH1SfqCVOOs6lQxb 8rzQ== X-Gm-Message-State: AOAM533X008l4lLYD/0RuSnjbvs7pflpba3cgXBxwj14OpG1l+Ffd5qn e9AuntrBn7rFsW51xoQaIPCZKGlOSYtsnQ== X-Google-Smtp-Source: ABdhPJwBjrFTATQgDuZiYX3YPpED1OPLOMXud1X1M3mHBSsQ0JUvshBT0IyI11iUkjr7zF5pHqBLZQ== X-Received: by 2002:a05:6808:221a:b0:32b:9564:c949 with SMTP id bd26-20020a056808221a00b0032b9564c949mr32294087oib.149.1654609455026; Tue, 07 Jun 2022 06:44:15 -0700 (PDT) Received: from birita.. ([2804:431:c7cb:a613:be74:464d:5224:bb97]) by smtp.gmail.com with ESMTPSA id j1-20020a056830240100b0060b0c4704fasm9400028ots.4.2022.06.07.06.44.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jun 2022 06:44:14 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH v2] nptl: Fix __libc_cleanup_pop_restore asynchronous restore (BZ#29214) Date: Tue, 7 Jun 2022 10:44:11 -0300 Message-Id: <20220607134411.3206119-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-12.3 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, T_SCC_BODY_TEXT_LINE 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.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" This was due a wrong revert done on 404656009b459658. Checked on x86_64-linux-gnu. --- v2: Fixed sleep syscall handling. --- nptl/libc-cleanup.c | 3 +- sysdeps/pthread/Makefile | 1 + sysdeps/pthread/tst-cancel30.c | 82 ++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 sysdeps/pthread/tst-cancel30.c diff --git a/nptl/libc-cleanup.c b/nptl/libc-cleanup.c index c4a83591bf..2ce59388d4 100644 --- a/nptl/libc-cleanup.c +++ b/nptl/libc-cleanup.c @@ -57,7 +57,8 @@ __libc_cleanup_pop_restore (struct _pthread_cleanup_buffer *buffer) THREAD_SETMEM (self, cleanup, buffer->__prev); int cancelhandling = atomic_load_relaxed (&self->cancelhandling); - if (cancelhandling & CANCELTYPE_BITMASK) + if (buffer->__canceltype != PTHREAD_CANCEL_DEFERRED + && (cancelhandling & CANCELTYPE_BITMASK) == 0) { int newval; do diff --git a/sysdeps/pthread/Makefile b/sysdeps/pthread/Makefile index 8cebe7a784..7d1670da87 100644 --- a/sysdeps/pthread/Makefile +++ b/sysdeps/pthread/Makefile @@ -126,6 +126,7 @@ tests += tst-cnd-basic tst-mtx-trylock tst-cnd-broadcast \ tst-pthread-raise-blocked-self \ tst-pthread_kill-exited \ tst-pthread_kill-exiting \ + tst-cancel30 \ # tests tests-time64 += \ diff --git a/sysdeps/pthread/tst-cancel30.c b/sysdeps/pthread/tst-cancel30.c new file mode 100644 index 0000000000..f7327c8786 --- /dev/null +++ b/sysdeps/pthread/tst-cancel30.c @@ -0,0 +1,82 @@ +/* Check if printf like functions does not disable asynchronous cancellation + mode (BZ#29214). + + Copyright (C) 2022 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 + +static pthread_barrier_t b; + +static void * +tf (void *arg) +{ + int old; + + TEST_COMPARE (pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL), 0); + + TEST_COMPARE (pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, &old), 0); + TEST_COMPARE (old, PTHREAD_CANCEL_ASYNCHRONOUS); + + /* Check if internal lock cleanup routines restore the cancellation type + correctly. */ + printf ("...\n"); + TEST_COMPARE (pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, &old), 0); + TEST_COMPARE (old, PTHREAD_CANCEL_ASYNCHRONOUS); + + xpthread_barrier_wait (&b); + + /* Wait indefinitely for cancellation, which only works if asynchronous + cancellation is enabled. */ +#ifdef SYS_pause + syscall (__NR_pause); +#elif defined SYS_ppoll || defined SYS_ppoll_time64 +# ifndef SYS_ppoll_time64 +# define SYS_ppoll_time64 SYS_ppoll +# endif + syscall (SYS_ppoll_time64, NULL, 0, NULL, NULL); +#else + for (;;); +#endif + + return 0; +} + +static int +do_test (void) +{ + xpthread_barrier_init (&b, NULL, 2); + + pthread_t th = xpthread_create (NULL, tf, NULL); + + xpthread_barrier_wait (&b); + + xpthread_cancel (th); + + void *status = xpthread_join (th); + TEST_VERIFY (status == PTHREAD_CANCELED); + + return 0; +} + +/* There is no need to wait full TIMEOUT if asynchronous is not working. */ +#define TIMEOUT 3 +#include