From patchwork Wed Mar 12 19:18:26 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 107797 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 86F343858429 for ; Wed, 12 Mar 2025 19:19:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 86F343858429 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=Q5r1l7fU X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by sourceware.org (Postfix) with ESMTPS id 4DD913857C6E for ; Wed, 12 Mar 2025 19:18:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4DD913857C6E 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 4DD913857C6E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::102e ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1741807115; cv=none; b=vRKwllT8yagRp8+qFqj0o1lAw7Zw5FXD+FQv9gZBi3RVUULIQ6BpUlYYArxbwQfQyQHEmLm2e7xMagZSzsEh+vmDntVcZyeBDRuwdpKcN7+M5TAZcIIBeiNVLSBsVNAyUzQ4eBMm3yyyiO0QCHbIy+WhxqxwEk49GFx3qoIhgZo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1741807115; c=relaxed/simple; bh=Y2DWQij3wdxIoDWGHauyKyoJV1+sJB+MuKFXrA9qC4c=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=IPF213F5ba54FU6G+oeZflZHcZwjsqew5IWSUY98AxVKLHma18pNwlafLcaLcX7oW2imDibDC5hAcqcwbzAqStymRas99Ai4H/ZgRmhxBU+Tlpt0j2P0Djf4B6xOMMCS+t8ZdB6Tsz6oli/0sNSt6m8CxjjMqNcIJNY7IZW9ZAY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4DD913857C6E Received: by mail-pj1-x102e.google.com with SMTP id 98e67ed59e1d1-2ff6ae7667dso572921a91.0 for ; Wed, 12 Mar 2025 12:18:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1741807114; x=1742411914; 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=ct4hG+IObtd09D0D2TBxOAThIm0AWOS9Sw8/xR3OH5g=; b=Q5r1l7fUKSIWuq7HtRx9dRhFmZ2HdfSg23HkoUwKJBb3oSvlCujahsMzezILWTicfR MbFwpuKcmgMRP9baplpL382DjtOgZ+abFKvYfFBYPCqe895UahGA4at09/0gkj/+qGzm kToBW7JasRav0IgAOX/bq2g/+kAp0VWHPOC6PO894sSv6RZ04oDSV6Xn+msnS2Jrw4u7 idE460BQKAS6pEpYu+kRyEiZzY4DJeSbGrj/ViT2SybEmq2n3KAsduBNAzjLEzeqE6QE w6etHk+d9nXV8HKVgP2sQKvuk4D8LIr0nPYSWdKpxQxHTaRTb2NEwKNZmpXD842H0AoC IBxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741807114; x=1742411914; 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=ct4hG+IObtd09D0D2TBxOAThIm0AWOS9Sw8/xR3OH5g=; b=Ox0lOmou2InWicOBXWdk6NmKmdcss8BoWaJokF5Y8Zx7JmeTEZieyO3Qt8MvEvixs2 /276jVAs8Ime6HBmd1qcPZQeV9oJoeGDj9N9m8Z1N9oLFNLDB8SYRDxG3LpDWx0iRU3m gol9+642LXillfax27UB1K7rPUVi3mvO8nC2F0r9U3khSzJf9bc1x15av92XM4b6quHo DrNF1xMM+kJ6IhfdZ2BzShxTznlJCdCXxdxG5VZ26nze1B7ovjQ8pCu01XfqfPyYEFkr f03WTWQtY5qFjubI/ZkCg/tC24iFWIqQdSQE2nuYn6B1teAKJBeIeCSRj2EBu/hP24F4 9ICA== X-Gm-Message-State: AOJu0Yyp8lerSMyoFjBHaTZbXOzf1Xf7ScgTHmPuCQ8Dbj4srnpZU/Ch P88bO9IhQZOqqR/1Ek7LIggSc+dM7NzGcCv2wxANnqnx1iGKfRJzMo5HmFP8chekvo0nI1q7NlP R X-Gm-Gg: ASbGncvBiZ4wprHLuoHkpiKAip72EjYFggwMW2MTjOzgvZGAwp0QUmrpYBW/HTM6GnO NiHeV5cghWVPvvBfTU00rGf4keQT/QlOnMf7p2JnuKWOeSRS237h/lJea6Sh/ITGAn2IVlGCUwI vYQ9U0x9/xK8K/Tt3QggO0sF0Wy3hsT69XMhw3bJ5cEcOoWICMV8ykG0lZOItEvILMg9l8qkM52 i8GkFANJqeFBVPe7pupPrWtqiLvcMBfha7czZ2YNsKERMkDaL/5HovpuVv83HLsYKjvGtX4fn/J qYmnaY8MSgt9CpJ1IGL/8+HWE+Xo1DgOtPxIITr4d5G2JYi8sON7OdA6TQ== X-Google-Smtp-Source: AGHT+IF2fnSLfq9EPwGwjLptRQjOIlyHA+VtX7K/qGZWP0qaBna7cauk8PYLLih+VR7hzD18NvLy1g== X-Received: by 2002:a05:6a21:50c:b0:1f5:6d00:ba03 with SMTP id adf61e73a8af0-1f56d00bc0amr30364736637.36.1741807112450; Wed, 12 Mar 2025 12:18:32 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c1:1ebf:78b3:c5aa:cdc8:55cd]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-af281093136sm11634200a12.18.2025.03.12.12.18.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Mar 2025 12:18:32 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Aurelien Jarno Subject: [PATCH v2] nptl: Check if thread is already terminated in sigcancel_handler (BZ 32782) Date: Wed, 12 Mar 2025 16:18:26 -0300 Message-ID: <20250312191828.173617-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 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 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~patchwork=sourceware.org@sourceware.org The SIGCANCEL signal handler should not issue __syscall_do_cancel, which calls __do_cancel and __pthread_unwind, if the cancellation is already in proces (and libgcc unwind is not reentrant). Any cancellation signal received after is ignored. Checked on x86_64-linux-gnu and aarch64-linux-gnu. Tested-by: Aurelien Jarno --- nptl/pthread_cancel.c | 14 ++++--- sysdeps/pthread/Makefile | 1 + sysdeps/pthread/tst-cancel32.c | 73 ++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 6 deletions(-) create mode 100644 sysdeps/pthread/tst-cancel32.c diff --git a/nptl/pthread_cancel.c b/nptl/pthread_cancel.c index f7ce3ec51b..79309163eb 100644 --- a/nptl/pthread_cancel.c +++ b/nptl/pthread_cancel.c @@ -41,15 +41,17 @@ sigcancel_handler (int sig, siginfo_t *si, void *ctx) || si->si_code != SI_TKILL) return; - /* Check if asynchronous cancellation mode is set or if interrupted - instruction pointer falls within the cancellable syscall bridge. For - interruptable syscalls with external side-effects (i.e. partial reads), - the kernel will set the IP to after __syscall_cancel_arch_end, thus - disabling the cancellation and allowing the process to handle such + /* Check if asynchronous cancellation mode is set and cancellation is not + already in progress, or if interrupted instruction pointer falls within + the cancellable syscall bridge. + Forinterruptable syscalls with external side-effects (i.e. partial + reads), the kernel will set the IP to after __syscall_cancel_arch_end, + thus disabling the cancellation and allowing the process to handle such conditions. */ struct pthread *self = THREAD_SELF; int oldval = atomic_load_relaxed (&self->cancelhandling); - if (cancel_async_enabled (oldval) || cancellation_pc_check (ctx)) + if (cancel_enabled_and_canceled_and_async (oldval) + || cancellation_pc_check (ctx)) __syscall_do_cancel (); } diff --git a/sysdeps/pthread/Makefile b/sysdeps/pthread/Makefile index 70e62b2e1b..d4869c624b 100644 --- a/sysdeps/pthread/Makefile +++ b/sysdeps/pthread/Makefile @@ -106,6 +106,7 @@ tests += \ tst-cancel28 \ tst-cancel29 \ tst-cancel30 \ + tst-cancel32 \ tst-cleanup0 \ tst-cleanup1 \ tst-cleanup2 \ diff --git a/sysdeps/pthread/tst-cancel32.c b/sysdeps/pthread/tst-cancel32.c new file mode 100644 index 0000000000..ab550c16bf --- /dev/null +++ b/sysdeps/pthread/tst-cancel32.c @@ -0,0 +1,73 @@ +/* Check if pthread_setcanceltype disables asynchronous cancellation + once cancellation happens (BZ 32782) + + Copyright (C) 2025 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 + . */ + +/* The pthread_setcanceltype is a cancellation entrypoint, and if + asynchronous is enabled and the cancellation starts (on the second + pthread_setcanceltype call), the asynchronous should not restart + the process. */ + +#include + +#define NITER 1000 +#define NTHREADS 8 + +static void +tf_cleanup (void *arg) +{ +} + +static void * +tf (void *closure) +{ + pthread_cleanup_push (tf_cleanup, NULL); + for (;;) + { + /* The only possible failure for pthread_setcanceltype is an + invalid state type. */ + pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL); + pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, NULL); + } + pthread_cleanup_pop (1); + + return NULL; +} + +static void +poll_threads (int nthreads) +{ + pthread_t thr[nthreads]; + for (int i = 0; i < nthreads; i++) + thr[i] = xpthread_create (NULL, tf, NULL); + for (int i = 0; i < nthreads; i++) + xpthread_cancel (thr[i]); + for (int i = 0; i < nthreads; i++) + xpthread_join (thr[i]); +} + +static int +do_test (void) +{ + for (int k = 0; k < NITER; k++) + poll_threads (NTHREADS); + + return 0; +} + +#include