From patchwork Tue May 31 20:28:10 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: 54594 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 C1652396E858 for ; Tue, 31 May 2022 20:28:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C1652396E858 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1654028917; bh=u9IsIuzaOyyYPLZVfA6YkRiGHin4WFJFKGJQSWufHG0=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=lelv038QzxxzEKP0G23LbXEcMtHd0bQ8n83Wl7owb4rSHWUEsaS6yTPwwx7LWtNZM GCT8l9AWRFL6EBpyxmkGXec5VMUQh46KOdaJ0HJIHl2mNpIBmQMACNT4hPU01rUxAp EbtcJbeTYh6SfhX52/s1BIyvuGi2aYHXXTQB+FJs= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-ot1-x334.google.com (mail-ot1-x334.google.com [IPv6:2607:f8b0:4864:20::334]) by sourceware.org (Postfix) with ESMTPS id 9F2FA3857C48 for ; Tue, 31 May 2022 20:28:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 9F2FA3857C48 Received: by mail-ot1-x334.google.com with SMTP id e11-20020a9d6e0b000000b0060afcbafa80so10383256otr.3 for ; Tue, 31 May 2022 13:28:15 -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=u9IsIuzaOyyYPLZVfA6YkRiGHin4WFJFKGJQSWufHG0=; b=4+B7bWt6XAwEnqt9zxAh63ZAOC3pl/cTxuomym7qNKoNwOyGADnowtF46UUVdRE8eH E7DuklCPJDqU16TOFg1dO42w545YDBYIszNwMDN3bdde0sDJQM/1rpONP4N/7FGpJsyG LV9bSXDgeTensk0rgpkBuOBBz7SBK9ReaLYDkK640wjcui1dvpctF633hw49G9vI+qsV 8/jLM7vbYOOboeZO3soTR9n4BtZfgv5tHz/L1pXmcx+Ero/Bw5URHjZ27aOswJe2y/2p cBNjLFRSgtrmbwETm3KHYb2Rsju8Pj8PFSE710zC0KEX5gJ/XIqh3vtlbfIXqVsquHdZ te4A== X-Gm-Message-State: AOAM531aPTETo/73cMp/y9VvMYBciCueiUP830YuHEh5RbBksyojatwb 4v81+Gb72R6Wp5p3XtF9lLQBsb4B7WrRfA== X-Google-Smtp-Source: ABdhPJwgvP76f7QfzPX7WQzMRfVwygTP5ETiD+Hciw2hGB4plYlJgaDOVOd8GYJk4hRDk+GEKYijWw== X-Received: by 2002:a05:6830:1dd3:b0:60b:1e56:f1f5 with SMTP id a19-20020a0568301dd300b0060b1e56f1f5mr15446200otj.280.1654028894628; Tue, 31 May 2022 13:28:14 -0700 (PDT) Received: from birita.. ([2804:431:c7ca:e39c:980f:eaf1:7a30:10ef]) by smtp.gmail.com with ESMTPSA id s143-20020acaa995000000b00325cda1ffb4sm6098452oie.51.2022.05.31.13.28.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 May 2022 13:28:14 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH] nptl: Fix __libc_cleanup_pop_restore asynchronous restore (BZ#29214) Date: Tue, 31 May 2022 17:28:10 -0300 Message-Id: <20220531202810.3864051-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-12.7 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. --- nptl/libc-cleanup.c | 3 +- sysdeps/pthread/Makefile | 1 + sysdeps/pthread/tst-cancel30.c | 75 ++++++++++++++++++++++++++++++++++ 3 files changed, 78 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..ef3f187e71 --- /dev/null +++ b/sysdeps/pthread/tst-cancel30.c @@ -0,0 +1,75 @@ +/* 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 + +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_futex + syscall (SYS_futex, &(int){0}, FUTEX_WAIT_PRIVATE, 0, NULL, NULL, 0); +#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