From patchwork Thu Sep 30 20:00:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 45623 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 7C15D3858001 for ; Thu, 30 Sep 2021 20:01:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7C15D3858001 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1633032119; bh=bTUgubhVK24kZGDDY9aqih+rj2HsuYn1T7kCpFIWOWw=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=sn/mLXM4LVMvjSQ/lzRJeyjNUV6PZ4Nqdv24Y9Okz+ma/CDhG0Qnh/fEh7KtM+++J GLlF9OtZzjK8B30g0b2pRzbY1oHKN/kVehFcPf6IslkIcbqo6ccil84vLljz4X91bB 8qPjwfpmrMk0YWrw1SgzNGJus5NxLLY/Y9Ll5uaM= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qt1-x833.google.com (mail-qt1-x833.google.com [IPv6:2607:f8b0:4864:20::833]) by sourceware.org (Postfix) with ESMTPS id 2B1843858C2C for ; Thu, 30 Sep 2021 20:00:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2B1843858C2C Received: by mail-qt1-x833.google.com with SMTP id b16so6883052qtt.7 for ; Thu, 30 Sep 2021 13:00:58 -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:in-reply-to :references:mime-version:content-transfer-encoding; bh=bTUgubhVK24kZGDDY9aqih+rj2HsuYn1T7kCpFIWOWw=; b=HlkwFkIfyYLwSRejrVsC2r0IUlYrW4n/tHaU72U+TRHTyIDlmn6O64ZYDQ7EBG21XL rPEC7DGDsFevzxr255N7P33wd12HQiop1obOK1nGsSAFtsGVaZVk3u5+BrQzMvRsablJ pd8ZfPU89PT121BtXjPxSyBDcbUJ+2sa4kPsYgtp1lVGBt/7iZcZrODqJoM1IZt0rdhU eO9jF1++8LHpXYtqe4GHw5A7jk4TB1sS7CzfPFLKNyOHwUUXRSjeUkQLfjPow8+GMT/H NlDpTcVzVlAFZ9r89LSGKeRt/+JYikCsb1Qq5pMqRWaaAaUyje1Srd+TJxpMiJeeIQnp nJWA== X-Gm-Message-State: AOAM53136l9HgNROMsY9306LF6wqX3GFnnZwI4Z1j/4gZf3wr/4naJ0m KpkmCn4Anq5GYliMquCoEzI7KoGwp0/qDw== X-Google-Smtp-Source: ABdhPJy8KmGRfPLp2+v2AhGE7pT6plzoiyZspvoKI/pNDo8yVZKREe2MB1v20CA4FWPU9kRiLeFA9w== X-Received: by 2002:ac8:538a:: with SMTP id x10mr8523009qtp.251.1633032057419; Thu, 30 Sep 2021 13:00:57 -0700 (PDT) Received: from birita.. ([2804:431:c7cb:b338:b20e:53f8:8b5:1bb2]) by smtp.gmail.com with ESMTPSA id r196sm1910203qka.119.2021.09.30.13.00.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Sep 2021 13:00:55 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [PATCH 01/15] nptl: Set cancellation type and state on pthread_exit (BZ #28267) Date: Thu, 30 Sep 2021 17:00:37 -0300 Message-Id: <20210930200051.1017457-2-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210930200051.1017457-1-adhemerval.zanella@linaro.org> References: <20210930200051.1017457-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.0 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.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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" It is required by POSIX XSH 2.9.5 Thread Cancellation under the heading Thread Cancellation Cleanup Handlers. Checked x86_64-linux-gnu. --- nptl/Makefile | 3 +- nptl/cancellation.c | 7 ++- nptl/pthread_cancel.c | 1 - nptl/pthread_exit.c | 4 +- nptl/pthread_testcancel.c | 1 - nptl/tst-cleanup5.c | 129 ++++++++++++++++++++++++++++++++++++++ sysdeps/nptl/pthreadP.h | 18 ++++-- 7 files changed, 152 insertions(+), 11 deletions(-) create mode 100644 nptl/tst-cleanup5.c diff --git a/nptl/Makefile b/nptl/Makefile index ff4d590f11..e1e195cc15 100644 --- a/nptl/Makefile +++ b/nptl/Makefile @@ -306,7 +306,8 @@ tests = tst-attr2 tst-attr3 tst-default-attr \ tst-pthread-gdb-attach tst-pthread-gdb-attach-static \ tst-pthread_exit-nothreads \ tst-pthread_exit-nothreads-static \ - tst-thread-setspecific + tst-thread-setspecific \ + tst-cleanup5 tests-nolibpthread = \ tst-pthread_exit-nothreads \ diff --git a/nptl/cancellation.c b/nptl/cancellation.c index 44f8417845..7cef9487bf 100644 --- a/nptl/cancellation.c +++ b/nptl/cancellation.c @@ -41,7 +41,6 @@ __pthread_enable_asynccancel (void) && !(ch & EXITING_BITMASK) && !(ch & TERMINATED_BITMASK)) { - THREAD_SETMEM (self, result, PTHREAD_CANCELED); __do_cancel (); } @@ -63,3 +62,9 @@ __pthread_disable_asynccancel (int oldtype) self->canceltype = PTHREAD_CANCEL_DEFERRED; } libc_hidden_def (__pthread_disable_asynccancel) + +void +__do_cancel (void) +{ + __exit_thread (PTHREAD_CANCELED); +} diff --git a/nptl/pthread_cancel.c b/nptl/pthread_cancel.c index a8aa3b3d15..868cf676a3 100644 --- a/nptl/pthread_cancel.c +++ b/nptl/pthread_cancel.c @@ -107,7 +107,6 @@ __pthread_cancel (pthread_t th) __libc_multiple_threads = 1; #endif - THREAD_SETMEM (pd, result, PTHREAD_CANCELED); if (pd->cancelstate == PTHREAD_CANCEL_ENABLE && pd->canceltype == PTHREAD_CANCEL_ASYNCHRONOUS) __do_cancel (); diff --git a/nptl/pthread_exit.c b/nptl/pthread_exit.c index f54388a8e2..14a73043bf 100644 --- a/nptl/pthread_exit.c +++ b/nptl/pthread_exit.c @@ -31,9 +31,7 @@ __pthread_exit (void *value) " must be installed for pthread_exit to work\n"); } - THREAD_SETMEM (THREAD_SELF, result, value); - - __do_cancel (); + __exit_thread (value); } libc_hidden_def (__pthread_exit) weak_alias (__pthread_exit, pthread_exit) diff --git a/nptl/pthread_testcancel.c b/nptl/pthread_testcancel.c index 7a7a199b6d..6c613462ab 100644 --- a/nptl/pthread_testcancel.c +++ b/nptl/pthread_testcancel.c @@ -29,7 +29,6 @@ ___pthread_testcancel (void) && !(cancelhandling & EXITING_BITMASK) && !(cancelhandling & TERMINATED_BITMASK)) { - THREAD_SETMEM (self, result, PTHREAD_CANCELED); __do_cancel (); } } diff --git a/nptl/tst-cleanup5.c b/nptl/tst-cleanup5.c new file mode 100644 index 0000000000..f1f81a40fe --- /dev/null +++ b/nptl/tst-cleanup5.c @@ -0,0 +1,129 @@ +/* Check if cancellation state and type is correctly set on thread exit. + Copyright (C) 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 + . */ + +#include +#include +#include + +static int pipefds[2]; +static pthread_barrier_t b; + +static void +clh (void *arg) +{ + /* Although POSIX state setting either the cancellation state or type is + undefined during cleanup handler execution, both calls should be safe + since none has any side-effect (they should not change current state + neither trigger a pending cancellation). */ + + int state; + TEST_VERIFY (pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &state) == 0); + TEST_COMPARE (state, PTHREAD_CANCEL_DISABLE); + + int type; + TEST_VERIFY (pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, &type) == 0); + TEST_COMPARE (type, PTHREAD_CANCEL_DEFERRED); +} + +/* Check if a thread with PTHREAD_CANCEL_DEFERRED cancellation on + pthread_cleanup_pop sets the correct state and type as pthread_exit. */ +static void * +tf_cancel_deferred (void *arg) +{ + xpthread_barrier_wait (&b); + + pthread_cleanup_push (clh, NULL); + + char c; + read (pipefds[0], &c, 1); + + pthread_cleanup_pop (1); + + return NULL; +} + +/* Check if a thread with PTHREAD_CANCEL_DEFERRED cancellation on + blocked read() sets the correct state and type as pthread_exit. */ +static void * +tf_testcancel (void *arg) +{ + xpthread_barrier_wait (&b); + + pthread_cleanup_push (clh, NULL); + + char c; + read (pipefds[0], &c, 1); + + pthread_testcancel (); + + pthread_cleanup_pop (1); + + return NULL; +} + +#define EXIT_EXPECTED_VALUE ((void *) 42) + +/* Check if a thread with PTHREAD_CANCEL_DEFERRED cancellation on + pthread_exit() sets the correct state and type. */ +static void * +tf_exit (void *arg) +{ + xpthread_barrier_wait (&b); + + pthread_cleanup_push (clh, NULL); + + pthread_exit (EXIT_EXPECTED_VALUE); + + pthread_cleanup_pop (1); + + return NULL; +} + +static int +do_test (void) +{ + xpipe (pipefds); + + xpthread_barrier_init (&b, NULL, 2); + { + pthread_t th = xpthread_create (NULL, tf_cancel_deferred, NULL); + xpthread_barrier_wait (&b); + xpthread_cancel (th); + void *r = xpthread_join (th); + TEST_VERIFY (r == PTHREAD_CANCELED); + } + + { + pthread_t th = xpthread_create (NULL, tf_testcancel, NULL); + xpthread_barrier_wait (&b); + xpthread_cancel (th); + void *r = xpthread_join (th); + TEST_VERIFY (r == PTHREAD_CANCELED); + } + + { + pthread_t th = xpthread_create (NULL, tf_exit, NULL); + xpthread_barrier_wait (&b); + void *r = xpthread_join (th); + TEST_VERIFY (r == EXIT_EXPECTED_VALUE); + } + + return 0; +} + +#include diff --git a/sysdeps/nptl/pthreadP.h b/sysdeps/nptl/pthreadP.h index 0e39afecc6..aebddb361b 100644 --- a/sysdeps/nptl/pthreadP.h +++ b/sysdeps/nptl/pthreadP.h @@ -267,20 +267,30 @@ extern void __pthread_unregister_cancel (__pthread_unwind_buf_t *__buf) __cleanup_fct_attribute; libc_hidden_proto (__pthread_unregister_cancel) -/* Called when a thread reacts on a cancellation request. */ -static inline void -__attribute ((noreturn, always_inline)) -__do_cancel (void) +static _Noreturn inline void +__exit_thread (void *value) { struct pthread *self = THREAD_SELF; /* Make sure we get no more cancellations. */ THREAD_ATOMIC_BIT_SET (self, cancelhandling, EXITING_BIT); + THREAD_SETMEM (self, result, value); + + /* It is required by POSIX XSH 2.9.5 Thread Cancellation under the heading + Thread Cancellation Cleanup Handlers and also avoid further cancellation + wrapper to act on cancellation. */ + THREAD_SETMEM (self, cancelstate, PTHREAD_CANCEL_DISABLE); + THREAD_SETMEM (self, canceltype, PTHREAD_CANCEL_DEFERRED); + __pthread_unwind ((__pthread_unwind_buf_t *) THREAD_GETMEM (self, cleanup_jmp_buf)); } +/* It is a wrapper over __exit_thread (PTHREAD_CANCELED). It is has its own + implementation because it might be called by arch-specific asm code. */ +_Noreturn void __do_cancel (void) attribute_hidden; + /* Internal prototypes. */