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: 872836 Delivered-To: patch@linaro.org Received: by 2002:a5d:5343:0:b0:38f:210b:807b with SMTP id t3csp118275wrv; Wed, 12 Mar 2025 12:18:56 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUgs5R5wUWXTY3Z4WJaWs7tQjarEClVmXz5jtmEhP+CTBmDGqH/gG/c56vjfcPTJ9AhjTh32g==@linaro.org X-Google-Smtp-Source: AGHT+IFtEfaEGdZFz0qi5MbXLLd3TY2hr1UV/JfWKzZ+A3jCJWGyGvEMSJcfpQNQ3XsFgBlU+G47 X-Received: by 2002:a05:6214:da9:b0:6e8:fcc6:35b6 with SMTP id 6a1803df08f44-6e9006012b3mr315154806d6.2.1741807136547; Wed, 12 Mar 2025 12:18:56 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1741807136; cv=pass; d=google.com; s=arc-20240605; b=fP8h3iayPGkVMZYrXHdiz+LNxnPnNgzr/yiepaUELxi+DJT77IbimY+cMsFdZdZnz1 chwYk7e33ePz3Yd1JWNEmEUbtYvDj3ngsuHOPqSb46CaWeJLovwAM7URyPlZjADjQb7J jInt/Gc4LP7BQgXKA4CL5Zp/q85P48Fjs7kWnZuq8PglD2NilyseQ8EkoZSWtE0wkOzp v7IBh5mvQzU0bQR/rCYlewzUeXtK+kcTJmw79ySylg9DlD8rQ9aQ3KkOlU2YW0CeZCQe DZf+JiDyk66Bt2mzhXn01LqMpwmSp76HFqesf7gVFFCR1gv00CFNFbMBZjERVfgUG0He sanw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :dkim-filter:arc-filter:dmarc-filter:delivered-to:dkim-filter; bh=ct4hG+IObtd09D0D2TBxOAThIm0AWOS9Sw8/xR3OH5g=; fh=UCVZ7S1lBcEAg62ZuvyIjHIDpE1rX0WJm+RYA5LwILk=; b=Wgs3+L2iHVtDKx/h8QXBI7rn7KVl2gkgxnieICiN/jIDX3R6yDS1cVgLBN9yg7boPy /KM05FF3pAu4fXO+dVVvC1Nym3z1hETfNMx54Zelp3AAuG6gH3g1XKKPvqJzQhGk27JN 5NVJrmGKnvz+0+Pbf6HqttSbjSXi/57oD6dgctMD1JvB95XbzFbBbgGYa+bnvi6CA5J6 oRasuIMSuvpBg2lGGZ/GxNNgM4Kpx4+yMUL99gjEaVQCUjgKlEl5gWnENLbGB/3omR4I +fKq6z75djuz+oQTINsKLwN3qivCDZ7+x+D1Dr19+IVkWzBWpYT5wIYlUWSdgVeytWDb 2TOQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Q5r1l7fU; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="libc-alpha-bounces~patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id 6a1803df08f44-6e8f707d3f8si140601076d6.17.2025.03.12.12.18.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Mar 2025 12:18:56 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Q5r1l7fU; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces~patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="libc-alpha-bounces~patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1D6FE3858429 for ; Wed, 12 Mar 2025 19:18:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1D6FE3858429 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-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-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~patch=linaro.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 Reviewed-by: Florian Weimer --- 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