From patchwork Mon Dec 11 19:06:46 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 121446 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp3168889qgn; Mon, 11 Dec 2017 11:09:32 -0800 (PST) X-Google-Smtp-Source: ACJfBoshTvxmZi3JMTt5iXcwRFnatfqXcOqTLZdUIy4udZ1V/8etX4JUmUMZtk2tftkyQzmfiHlV X-Received: by 10.159.195.8 with SMTP id bd8mr1335251plb.322.1513019372268; Mon, 11 Dec 2017 11:09:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1513019372; cv=none; d=google.com; s=arc-20160816; b=Ig6VaE9lVZ2xvEaxh05xGVz/KLovdqngtxpEPuBqHtTWBk7kVfRVGXqfJR+sU06aoi zGKstPgu2q4dAa3rjnInsNaO3TPsUSJheaKXL1yCOBueCTQW7plJJD2q8MWAhxDnDJjk lhCLSKUhsjf0ibFxaRtgwXFwec9rbShLWIKlK2gjYb/1O4b5vp7RVI/GV6rievQHSw6M F58yfq0Q3DpX8W90jlWdtbcEoBpv3nO4rkeOSN8jtmrOHOb2ii7er+yj2YRadwzFWJP7 TZ6O19pPeD4/t1jou4YoneeHvEYYqRKB4dgRKIzikRzSTre0Z8aaldGSDzscdAgx155U 7fiQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:to:from:delivered-to :sender:list-help:list-post:list-archive:list-subscribe :list-unsubscribe:list-id:precedence:mailing-list:dkim-signature :domainkey-signature:arc-authentication-results; bh=ly6gPr9mm7efbVLFzdv2hbfsfIj+Wz2CI3ulOToOizQ=; b=fhZxClW2/pwOvMzOpduoT4RXcu7Bg6Yjnf4JM7Nx3fwh3OtnZmOb/IkA8hFz58rDZp WElRsXFauNJ1XhVj2Xtps82z0VnEpgdAHtZrqhh92ISEaF4Yqpjii1ULJfBPCJJipvMl 3mgm1NgyqOg5kQLrR+rzUFVuidOZsqGS+hOU+5yT/7VqMryfPVLgbru5ykj2g4qpRz1n NpYBkytqISTtdSvnhxGUN06pgKM0vpNU8Q65S7F9GL4shV8jkDhA0dtTUeoehp5bSUnN yxXQoIrlM9PjwUE7uQXKugXfqlb30zzKNTOPEnTO6TkpciN8oS0Mt3Hzwc526Zqz5xJj HsKg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=IYwrsj2O; spf=pass (google.com: domain of libc-alpha-return-88023-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-88023-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id h80si11280093pfh.32.2017.12.11.11.09.31 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 11 Dec 2017 11:09:32 -0800 (PST) Received-SPF: pass (google.com: domain of libc-alpha-return-88023-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=IYwrsj2O; spf=pass (google.com: domain of libc-alpha-return-88023-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-88023-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=PumMfQuBkfnv93jfmEvEvfLSS04bkrx +XZWcVpySS/ZiJD5LirK3Wxa5dUK4+2aqX3qaXj+ge3tU86BPIcOedQRfU64oe0B 1EOYPPRzpX7dc1TvrCJWRfYlM9DCE4UWvQT5v2KXAJWpurEgRXuaVJpryw/ceWk0 3DfGJDz9G4BA= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; s=default; bh=sYTTgTxcv/NpfVGEJpDb+tRbHDE=; b=IYwrs j2OmYusT/bymZwLXWbj31r2U414glzrnxzW+fDAGISoVT5bQ3H5AyMPj0hKI/Ciz 0PjrAszubW4mLiRcV8d00/YRJnZ7+G/3gzFQu7XpBIKiAdzJeKyp36/PoKXsrF6u rg86F3IiN0q8IYh4v/O5+vYF+yrGEH9wHVHmRM= Received: (qmail 41806 invoked by alias); 11 Dec 2017 19:07:28 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 41726 invoked by uid 89); 11 Dec 2017 19:07:27 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-22.7 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, KAM_STOCKGEN, RCVD_IN_DNSWL_NONE, SPF_PASS, UNSUBSCRIBE_BODY autolearn=ham version=3.3.2 spammy=sk:cancel, leakage, arrives, interruption X-HELO: mail-qt0-f170.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=ly6gPr9mm7efbVLFzdv2hbfsfIj+Wz2CI3ulOToOizQ=; b=Grop1giPxOL+4rEpX3XgqLpDrbkrrAmYeaj3kRH5sT1dTTCVP+ZC1/gSNuHGvqNa00 Jzt8y4YTK4jSLeo1WzdR5xf+OdUlByYGcXAHDpvD6Djb7HeCK3qQwv6rkavJupJZiNOw NZ0HGpjBh/uQw8ZR4KOWiAdSzGpZqS/bi39IagEoLF5JrfZfuczRc0hnDOhhh55SBIdB l+BC1VJyLQu34Mb9+a7dq47xvF1bxBoIwJbACyIEITws7VaO8piIBkdf3s/Y4RoEqdCM UK6snznM5L/ZbUVczeHT5KmwTD7WERi57UApnLZjLcN+JAE3UvYqc3OGvwlk51xFOlZq HKcQ== X-Gm-Message-State: AKGB3mIN/QGOM/hrGZp/RxR5XQQenuQ+ajZjYrdm1zgomHpVvROX280E hDKHpy6KnQ0W9kkqyVrsA9H7O5RxDWs= X-Received: by 10.200.41.249 with SMTP id 54mr2096244qtt.312.1513019233500; Mon, 11 Dec 2017 11:07:13 -0800 (PST) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 02/19] nptl: Fix Race conditions in pthread cancellation (BZ#12683) Date: Mon, 11 Dec 2017 17:06:46 -0200 Message-Id: <1513019223-7603-3-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1513019223-7603-1-git-send-email-adhemerval.zanella@linaro.org> References: <1513019223-7603-1-git-send-email-adhemerval.zanella@linaro.org> This patches fixes some race conditions in NPTL cancellation code by redefining how cancellable syscalls are defined and handled. Current approach is to enable asynchronous cancellation prior to making the syscall and restore the previous cancellation type once the syscall returns. As decribed in BZ#12683, this approach shows 2 important problems: 1. Cancellation can act after the syscall has returned from kernel, but before userspace saves the return value. It might result in a resource leak if the syscall allocated a resource or a side effect (partial read/write), and there is no way to program handle it with cancellation handlers. 2. If a signal is handled while the thread is blocked at a cancellable syscall, the entire signal handler runs with asynchronous cancellation enabled. This can lead to issues if the signal handler call functions which are async-signal-safe but not async-cancel-safe. For cancellation to work correctly, there are 5 points at which the cancellation signal could arrive: 1. Before the final "testcancel" and before the syscall is made. 2. Between the "testcancel" and the syscall. 3. While the syscall is blocked and no side effects have yet taken place. 4. While the syscall is blocked but with some side effects already having taken place (e.g. a partial read or write). 5. After the syscall has returned. And GLIBC wants to act on cancellation in cases 1, 2, and 3 but not in case 4 or 5. The proposed solution follows: * Handling case 1 is trivial: do a conditional branch based on whether the thread has received a cancellation request; * Case 2 can be caught by the signal handler determining that the saved program counter (from the ucontext_t) is in some address range beginning just before the "testcancel" and ending with the syscall instruction. * In this case, except for certain syscalls that ALWAYS fail with EINTR even for non-interrupting signals, the kernel will reset the program counter to point at the syscall instruction during signal handling, so that the syscall is restarted when the signal handler returns. So, from the signal handler's standpoint, this looks the same as case 2, and thus it's taken care of. * In this case, the kernel cannot restart the syscall; when it's interrupted by a signal, the kernel must cause the syscall to return with whatever partial result it obtained (e.g. partial read or write). * In this case, the saved program counter points just after the syscall instruction, so the signal handler won't act on cancellation. This one is equal to 4. since the program counter is past the syscall instruction already. Another case that needs handling is syscalls that fail with EINTR even when the signal handler is non-interrupting. In this case, the syscall wrapper code can just check the cancellation flag when the errno result is EINTR, and act on cancellation if it's set. The proposed GLIBC adjustments are: 1. Remove the enable_asynccancel/disable_asynccancel function usage in syscall definition and instead make them call a common symbol that will check if cancellation is enabled (__syscall_cancel at nptl/libc-cancellation.c), call the arch-specific cancellable entry-point (__syscall_cancel_arch) and cancel the thread when required. 2. Provide a arch-specific symbol that contains global markers. These markers will be used in SIGCANCEL handler to check if the interruption has been called in a valid syscall and if the syscalls has been completed or not. A default version is provided (sysdeps/unix/sysv/linux/syscall_cancel.c), however the markers may not be set on correct expected places depeding of how INTERNAL_SYSCALL_NCS is implemented by the underlying architecture. In this case arch-specific implementation should be provided. 3. Rewrite SIGCANCEL asynchronous handler to check for both cancelling type and if current IP from signal handler falls between the global markes and act accordingly (sigcancel_handler at nptl/nptl-init.c). 4. Adjust nptl/pthread_cancel.c to send an signal instead of acting directly. This avoid synchronization issues when updating the cancellation status and also focus the logic on signal handler and cancellation syscall code. 5. Adjust pthread code to replace CANCEL_ASYNC/CANCEL_RESET calls to appropriated cancelable futex syscalls. 6. Adjust libc code to replace LIBC_CANCEL_ASYNC/LIBC_CANCEL_RESET to appropriated cancelable syscalls. 7. Adjust 'lowlevellock-futex.h' arch-specific implementations to provide cancelable futex calls (used in libpthread code). This patch adds the proposed changes to NPTL. The code leaves all the ports broken without further patches in the list. * nptl/Makefile [routines]: Add syscall_cancel object. [libpthread-routines]: Remove cancellation object. (CFLAGS-cancellation.c): Remove rule. (CFLAGS-syscall_cancel.c): New rule. (tests): Add tst-cancel28. * nptl/Versions [GLIBC_PRIVATE] (libc): Add __syscall_cancel, __syscall_cancel_arch_start, and __syscall_cancel_arch_end. * nptl/cancellation.c: Remove file. * nptl/descr.h (CANCELING_BIT): Remove define. (CANCELING_BITMASK): Likewise. (CANCEL_RESTMASK): Adjust value with CANCELED_BIT remove. * nptl/libc-cancellation.c (__syscall_cancel): Add non-cancellable implementation for loader and cancellable one for libc. (__syscall_do_cancel): New function: cancel call for syscall wrappers. * nptl/lll_timedlock_wait.c (__lll_timedlock_wait): Use cancellable futex operation. (__lll_timedwait_tid): Likewise. * nptl/nptl-init.c (sigcancel_handler): Rewrite function to avoid race conditions. (__pthread_initialize_minimal_internal): Add SA_RESTART to SIGCANCEL handler. * nptl/pt-system.c [LIBC_CANCEL_HANDLED]: Remove definition. * io/creat.c (LIBC_CANCEL_HANDLED): Likewise. * io/ppoll.c [ppoll] (LIBC_CANCEL_HANDLED): Likewise. * misc/pselect [__pselect] (LIBC_CANCEL_HANDLED): Likewise. * sysdeps/posix/pause.c (LIBC_CANCEL_HANDLED): Likewise. * sysdeps/unix/sysv/linux/generic/creat.c (LIBC_CANCEL_HANDLED): Likewise. * nptl/pthreadP.h (__do_cancel): Rewrite to both disable asynchronous cancellation and setting the thread as cancelled. (CANCEL_ASYNC): Remove definition. (CANCEL_RESET): Likewise. (LIBC_CANCEL_ASYNC): Likewise. (LIBC_CANCEL_RESET): Likewise. (LIBC_CANCEL_HANDLED): Likewise. (__syscall_cancel_arch): Add prototype. (__pthread_enable_asynccancel): Remove prototype. (__pthread_disable_asynccancel): Likewise. (__libc_enable_asynccancel): Likewise. (__libc_disable_asynccancel): Likewise. (__librt_enable_asynccancel): Likewise. (__librt_disable_asynccancel): Likewise. (__syscall_cancel_arch): Add prototype. (__syscall_do_cancel): Likewise. * nptl/pthread_cancel.c (pthread_cancel): Rewrite to just set CANCELLED_BIT and call __pthread_kill. * nptl/pthread_create.c (start_thread): Likewise. * nptl/pthread_timedjoin.c (pthread_timedjoin_np): Likewise. * nptl/sem_timedwait.c (sem_timedwait): Likewise. * nptl/sem_wait.c (__new_sem_wait): Likewise. * nptl/sem_waitcommon.c (futex_abstimed_wait): Likewise. * sysdeps/nptl/aio_misc.h (AIO_MISC_WAIT): Likewise. * sysdeps/nptl/gai_misc.h (GAI_MISC_WAIT): Likewise. * sysdeps/posix/sigpause.c (do_sigpause): Likewise. * sysdeps/posix/sigwait.c (__sigwait): Likewise. * sysdeps/posix/waitid.c (__waitid): Likewise. * sysdeps/nptl/lowlevellock.h (lll_wait_tid): Likewise. * sysdeps/posix/open64.c (__libc_open64): Likewise. * sysdeps/unix/sysv/linux/sigwait.c (__sigwait): Likewise. * nptl/pthread_exit.c (pthread_exit): Rewrite to set EXITING_BIT before call __pthread_unwind. * nptl/pthread_join.c (pthread_join): Remove CANCEL_ASYNC/CANCEL_RESET usage. * rt/Makefile [CFLAGS-librt-cancellation.c]: Remove rule. * sysdeps/generic/sysdep-cancel.h (LIBC_CANCEL_ASYNC): Remove define. (LIBC_CANCEL_RESET): Likewise. (LIBC_CANCEL_HANDLED): Likewise. * sysdeps/unix/sysv/linux/clock_nanosleep.c (__clock_nanosleep): Likewise. * sysdeps/unix/sysv/linux/fcntl.c (__libc_fcntl): Likewise. * sysdeps/unix/sysv/linux/generic/wordsize-32/fcntl.c (__libc_fcntl): Likewise. * sysdeps/nptl/Makefile [$(subdir) = rt] (librt-sysdep_routines): Remove librt-cancellation object. [$(subdir) = rt] (librt-cancellation.c): Remove rule. * sysdeps/nptl/librt-cancellation.c: Remove file. * sysdeps/unix/sysv/linux/futex-internal.h (lll_futex_wait_cancel): Use lll_futex_timed_wait_cancel. (futex_reltimed_wait_cancelable): Likewise. (futex_abstimed_wait_cancelable)): Use lll_futex_timed_wait_bitset_cancel. * sysdeps/unix/sysv/linux/lowlevellock-futex.h (lll_futex_wait_cancel): New macro. (lll_futex_timed_wait_cancel): Likewise. (lll_futex_timed_wait_bitset_cancel): Likewise. * sysdeps/unix/sysdep.h (SYSCALL_CANCEL): New macro: cancelable syscall calls. (INTERNAL_SYSCALL_NCS_CALL): New macro. (__syscall_cancel): New prototype. * sysdeps/unix/sysv/linux/socketcall.h (SOCKETCALL): Use __SSC macros. (SOCKETCALL_CANCEL): Use SYSCALL_CANCEL macros. * sysdeps/generic/sysdep-cancel.h (LIBC_CANCEL_ASYNC): Remove define. (LIBC_CANCEL_RESET): Likewise. (LIBC_CANCEL_HANDLED): Likewise. * sysdeps/unix/sysv/linux/pthread_kill.c (__pthread_kill): Allow SIGCANCEL to be sent. * nptl/tst-cancel28.c: New file. * sysdeps/unix/sysv/linux/syscall_cancel.c: Likewise. * support/temp_file.c (create_temp_fifo): New function. * support/temp_file.h (create_temp_fifo): Likewise. Signed-off-by: Adhemerval Zanella --- ChangeLog | 85 ++++++++++++++++++++++ io/creat.c | 3 - io/ppoll.c | 2 - manual/llio.texi | 4 +- misc/pselect.c | 2 - nptl/Makefile | 8 +-- nptl/Versions | 3 + nptl/cancellation.c | 101 --------------------------- nptl/descr.h | 15 ++-- nptl/libc-cancellation.c | 47 ++++++++++++- nptl/lll_timedlock_wait.c | 2 +- nptl/lll_timedwait_tid.c | 3 +- nptl/nptl-init.c | 78 +++++++++++++-------- nptl/pthreadP.h | 73 +++++++------------ nptl/pthread_cancel.c | 68 ++++-------------- nptl/pthread_create.c | 7 +- nptl/pthread_exit.c | 9 ++- nptl/pthread_join.c | 10 ++- nptl/pthread_timedjoin.c | 9 +-- nptl/sem_wait.c | 8 +-- nptl/tst-cancel28.c | 94 +++++++++++++++++++++++++ rt/Makefile | 1 - support/temp_file.c | 23 ++++++ support/temp_file.h | 6 ++ sysdeps/generic/sigcontextinfo.h | 15 ++++ sysdeps/generic/sysdep-cancel.h | 3 - sysdeps/nptl/Makefile | 3 +- sysdeps/nptl/aio_misc.h | 13 +--- sysdeps/nptl/gai_misc.h | 13 +--- sysdeps/nptl/librt-cancellation.c | 24 ------- sysdeps/nptl/lowlevellock.h | 5 +- sysdeps/posix/open64.c | 12 +--- sysdeps/posix/pause.c | 2 - sysdeps/posix/sigpause.c | 3 - sysdeps/posix/sigwait.c | 9 +-- sysdeps/posix/waitid.c | 11 +-- sysdeps/sparc/sparc32/lowlevellock.c | 3 +- sysdeps/unix/sysdep.h | 96 +++++++++++++++++++++---- sysdeps/unix/sysv/linux/clock_nanosleep.c | 20 +----- sysdeps/unix/sysv/linux/creat.c | 2 - sysdeps/unix/sysv/linux/creat64.c | 2 - sysdeps/unix/sysv/linux/futex-internal.h | 18 ++--- sysdeps/unix/sysv/linux/lowlevellock-futex.h | 34 ++++++++- sysdeps/unix/sysv/linux/pthread_kill.c | 5 +- sysdeps/unix/sysv/linux/sigwait.c | 3 - sysdeps/unix/sysv/linux/sigwaitinfo.c | 3 - sysdeps/unix/sysv/linux/socketcall.h | 37 ++++++++-- sysdeps/unix/sysv/linux/syscall_cancel.c | 61 ++++++++++++++++ sysdeps/unix/sysv/linux/sysdep.h | 17 +++++ 49 files changed, 641 insertions(+), 434 deletions(-) delete mode 100644 nptl/cancellation.c create mode 100644 nptl/tst-cancel28.c delete mode 100644 sysdeps/nptl/librt-cancellation.c create mode 100644 sysdeps/unix/sysv/linux/syscall_cancel.c -- 2.7.4 diff --git a/io/creat.c b/io/creat.c index 4e095d6..031cc78 100644 --- a/io/creat.c +++ b/io/creat.c @@ -27,6 +27,3 @@ creat (const char *file, mode_t mode) { return __open (file, O_WRONLY|O_CREAT|O_TRUNC, mode); } - -/* __open handles cancellation. */ -LIBC_CANCEL_HANDLED (); diff --git a/io/ppoll.c b/io/ppoll.c index e2919a7..f8cb3a4 100644 --- a/io/ppoll.c +++ b/io/ppoll.c @@ -70,7 +70,5 @@ ppoll (struct pollfd *fds, nfds_t nfds, const struct timespec *timeout, } #ifndef ppoll -/* __poll handles cancellation. */ -LIBC_CANCEL_HANDLED (); libc_hidden_def (ppoll); #endif diff --git a/manual/llio.texi b/manual/llio.texi index 8b2f599..20f494f 100644 --- a/manual/llio.texi +++ b/manual/llio.texi @@ -2436,13 +2436,13 @@ aiocb64}, since the LFS transparently replaces the old interface. @c sigemptyset ok @c sigaddset ok @c setjmp ok -@c CANCEL_ASYNC -> pthread_enable_asynccancel ok +@c __pthread_setcanceltype ok @c do_cancel ok @c pthread_unwind ok @c Unwind_ForcedUnwind or longjmp ok [@ascuheap @acsmem?] @c lll_lock @asulock @aculock @c lll_unlock @asulock @aculock -@c CANCEL_RESET -> pthread_disable_asynccancel ok +@c __pthread_setcanceltype ok @c lll_futex_wait ok @c ->start_routine ok ----- @c call_tls_dtors @asulock @ascuheap @aculock @acsmem diff --git a/misc/pselect.c b/misc/pselect.c index 2292219..17492f0 100644 --- a/misc/pselect.c +++ b/misc/pselect.c @@ -73,6 +73,4 @@ __pselect (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, } #ifndef __pselect weak_alias (__pselect, pselect) -/* __select handles cancellation. */ -LIBC_CANCEL_HANDLED (); #endif diff --git a/nptl/Makefile b/nptl/Makefile index 36bf25f..a586f74 100644 --- a/nptl/Makefile +++ b/nptl/Makefile @@ -30,7 +30,7 @@ install-lib-ldscripts := libpthread.so routines = alloca_cutoff forward libc-lowlevellock libc-cancellation \ libc-cleanup libc_pthread_init libc_multiple_threads \ - register-atfork unregister-atfork + register-atfork unregister-atfork syscall_cancel shared-only-routines = forward # We need to provide certain routines for compatibility with existing @@ -116,7 +116,6 @@ libpthread-routines = nptl-init vars events version pt-interp \ cleanup cleanup_defer cleanup_compat \ cleanup_defer_compat unwind \ pt-longjmp pt-cleanup\ - cancellation \ lowlevellock \ lll_timedlock_wait lll_timedwait_tid \ pt-fork pt-vfork \ @@ -165,7 +164,6 @@ CFLAGS-pthread_setcanceltype.c = -fexceptions -fasynchronous-unwind-tables # These are internal functions which similar functionality as setcancelstate # and setcanceltype. -CFLAGS-cancellation.c = -fasynchronous-unwind-tables CFLAGS-libc-cancellation.c = -fasynchronous-unwind-tables # Calling pthread_exit() must cause the registered cancel handlers to @@ -224,6 +222,8 @@ CFLAGS-fsync.c = -fexceptions -fasynchronous-unwind-tables CFLAGS-pt-system.c = -fexceptions +CFLAGS-syscall_cancel.c = -fexceptions + LDLIBS-tst-once5 = -lstdc++ CFLAGS-tst-thread_local1.o = -std=gnu++11 LDLIBS-tst-thread_local1 = -lstdc++ @@ -272,7 +272,7 @@ tests = tst-attr1 tst-attr2 tst-attr3 tst-default-attr \ tst-cancel11 tst-cancel12 tst-cancel13 tst-cancel14 tst-cancel15 \ tst-cancel16 tst-cancel17 tst-cancel18 tst-cancel19 tst-cancel20 \ tst-cancel21 tst-cancel22 tst-cancel23 tst-cancel24 tst-cancel25 \ - tst-cancel26 tst-cancel27 \ + tst-cancel26 tst-cancel27 tst-cancel28 \ tst-cancel-self tst-cancel-self-cancelstate \ tst-cancel-self-canceltype tst-cancel-self-testcancel \ tst-cleanup0 tst-cleanup1 tst-cleanup2 tst-cleanup3 tst-cleanup4 \ diff --git a/nptl/Versions b/nptl/Versions index 0ae5def..734d47a 100644 --- a/nptl/Versions +++ b/nptl/Versions @@ -36,6 +36,9 @@ libc { __libc_pthread_init; __libc_current_sigrtmin_private; __libc_current_sigrtmax_private; __libc_allocate_rtsig_private; + __syscall_cancel; + __syscall_cancel_arch_start; + __syscall_cancel_arch_end; } } diff --git a/nptl/cancellation.c b/nptl/cancellation.c deleted file mode 100644 index f3a3805..0000000 --- a/nptl/cancellation.c +++ /dev/null @@ -1,101 +0,0 @@ -/* Copyright (C) 2002-2017 Free Software Foundation, Inc. - This file is part of the GNU C Library. - Contributed by Ulrich Drepper , 2002. - - 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 "pthreadP.h" -#include - - -/* The next two functions are similar to pthread_setcanceltype() but - more specialized for the use in the cancelable functions like write(). - They do not need to check parameters etc. */ -int -attribute_hidden -__pthread_enable_asynccancel (void) -{ - struct pthread *self = THREAD_SELF; - int oldval = THREAD_GETMEM (self, cancelhandling); - - while (1) - { - int newval = oldval | CANCELTYPE_BITMASK; - - if (newval == oldval) - break; - - int curval = THREAD_ATOMIC_CMPXCHG_VAL (self, cancelhandling, newval, - oldval); - if (__glibc_likely (curval == oldval)) - { - if (CANCEL_ENABLED_AND_CANCELED_AND_ASYNCHRONOUS (newval)) - { - THREAD_SETMEM (self, result, PTHREAD_CANCELED); - __do_cancel (); - } - - break; - } - - /* Prepare the next round. */ - oldval = curval; - } - - return oldval; -} - - -void -attribute_hidden -__pthread_disable_asynccancel (int oldtype) -{ - /* If asynchronous cancellation was enabled before we do not have - anything to do. */ - if (oldtype & CANCELTYPE_BITMASK) - return; - - struct pthread *self = THREAD_SELF; - int newval; - - int oldval = THREAD_GETMEM (self, cancelhandling); - - while (1) - { - newval = oldval & ~CANCELTYPE_BITMASK; - - int curval = THREAD_ATOMIC_CMPXCHG_VAL (self, cancelhandling, newval, - oldval); - if (__glibc_likely (curval == oldval)) - break; - - /* Prepare the next round. */ - oldval = curval; - } - - /* We cannot return when we are being canceled. Upon return the - thread might be things which would have to be undone. The - following loop should loop until the cancellation signal is - delivered. */ - while (__builtin_expect ((newval & (CANCELING_BITMASK | CANCELED_BITMASK)) - == CANCELING_BITMASK, 0)) - { - futex_wait_simple ((unsigned int *) &self->cancelhandling, newval, - FUTEX_PRIVATE); - newval = THREAD_GETMEM (self, cancelhandling); - } -} diff --git a/nptl/descr.h b/nptl/descr.h index c83b17b..c3e1cc0 100644 --- a/nptl/descr.h +++ b/nptl/descr.h @@ -278,23 +278,20 @@ struct pthread /* Bit set if asynchronous cancellation mode is selected. */ #define CANCELTYPE_BIT 1 #define CANCELTYPE_BITMASK (0x01 << CANCELTYPE_BIT) - /* Bit set if canceling has been initiated. */ -#define CANCELING_BIT 2 -#define CANCELING_BITMASK (0x01 << CANCELING_BIT) - /* Bit set if canceled. */ -#define CANCELED_BIT 3 + /* Bit set if threads is canceled. */ +#define CANCELED_BIT 2 #define CANCELED_BITMASK (0x01 << CANCELED_BIT) /* Bit set if thread is exiting. */ -#define EXITING_BIT 4 +#define EXITING_BIT 3 #define EXITING_BITMASK (0x01 << EXITING_BIT) /* Bit set if thread terminated and TCB is freed. */ -#define TERMINATED_BIT 5 +#define TERMINATED_BIT 4 #define TERMINATED_BITMASK (0x01 << TERMINATED_BIT) /* Bit set if thread is supposed to change XID. */ -#define SETXID_BIT 6 +#define SETXID_BIT 5 #define SETXID_BITMASK (0x01 << SETXID_BIT) /* Mask for the rest. Helps the compiler to optimize. */ -#define CANCEL_RESTMASK 0xffffff80 +#define CANCEL_RESTMASK 0xffffffc0 #define CANCEL_ENABLED_AND_CANCELED(value) \ (((value) & (CANCELSTATE_BITMASK | CANCELED_BITMASK | EXITING_BITMASK \ diff --git a/nptl/libc-cancellation.c b/nptl/libc-cancellation.c index cb675ce..b013435 100644 --- a/nptl/libc-cancellation.c +++ b/nptl/libc-cancellation.c @@ -16,9 +16,50 @@ License along with the GNU C Library; if not, see . */ +#include +#include #include "pthreadP.h" +/* Cancellation function called by all cancellable syscalls. */ +long int +__syscall_cancel (__syscall_arg_t nr, __syscall_arg_t a1, + __syscall_arg_t a2, __syscall_arg_t a3, + __syscall_arg_t a4, __syscall_arg_t a5, + __syscall_arg_t a6) +{ + pthread_t self = (pthread_t) THREAD_SELF; + volatile struct pthread *pd = (volatile struct pthread *) self; + long int result; -#define __pthread_enable_asynccancel __libc_enable_asynccancel -#define __pthread_disable_asynccancel __libc_disable_asynccancel -#include + /* If cancellation is not enabled, call the syscall directly. */ + if (pd->cancelhandling & CANCELSTATE_BITMASK) + { + INTERNAL_SYSCALL_DECL (err); + result = INTERNAL_SYSCALL_NCS_CALL (nr, err, a1, a2, a3, a4, a5, a6); + if (INTERNAL_SYSCALL_ERROR_P (result, err)) + return -INTERNAL_SYSCALL_ERRNO (result, err); + return result; + } + + /* Call the arch-specific entry points that contains the globals markers + to be checked by SIGCANCEL handler. */ + result = __syscall_cancel_arch (&pd->cancelhandling, nr, a1, a2, a3, a4, a5, + a6); + + if ((result == -EINTR) + && (pd->cancelhandling & CANCELED_BITMASK) + && !(pd->cancelhandling & CANCELSTATE_BITMASK)) + __syscall_do_cancel (); + + return result; +} +libc_hidden_def (__syscall_cancel) + +/* Since __do_cancel is a always inline function, this creates a symbol the + arch-specific symbol can call to cancel the thread. */ +void +__cleanup_fct_attribute attribute_hidden __attribute ((noreturn)) +__syscall_do_cancel (void) +{ + __do_cancel (); +} diff --git a/nptl/lll_timedlock_wait.c b/nptl/lll_timedlock_wait.c index 604953c..630c028 100644 --- a/nptl/lll_timedlock_wait.c +++ b/nptl/lll_timedlock_wait.c @@ -52,7 +52,7 @@ __lll_timedlock_wait (int *futex, const struct timespec *abstime, int private) return ETIMEDOUT; /* If *futex == 2, wait until woken or timeout. */ - lll_futex_timed_wait (futex, 2, &rt, private); + lll_futex_timed_wait_cancel (futex, 2, &rt, private); } return 0; diff --git a/nptl/lll_timedwait_tid.c b/nptl/lll_timedwait_tid.c index e4c8de0..950d1aa 100644 --- a/nptl/lll_timedwait_tid.c +++ b/nptl/lll_timedwait_tid.c @@ -62,7 +62,8 @@ __lll_timedwait_tid (int *tidp, const struct timespec *abstime) The kernel up to version 3.16.3 does not use the private futex operations for futex wake-up when the clone terminates. */ - if (lll_futex_timed_wait (tidp, tid, &rt, LLL_SHARED) == -ETIMEDOUT) + if (lll_futex_timed_wait_cancel (tidp, tid, &rt, LLL_SHARED) + == -ETIMEDOUT) return ETIMEDOUT; } diff --git a/nptl/nptl-init.c b/nptl/nptl-init.c index 869e926..33ee7c4 100644 --- a/nptl/nptl-init.c +++ b/nptl/nptl-init.c @@ -38,6 +38,7 @@ #include #include #include +#include #ifndef TLS_MULTIPLE_THREADS_IN_TCB /* Pointer to the corresponding variable in libc. */ @@ -176,49 +177,65 @@ __nptl_set_robust (struct pthread *self) #ifdef SIGCANCEL + +extern const char __syscall_cancel_arch_start[1]; +extern const char __syscall_cancel_arch_end[1]; + +/* Workaround for architectures which either does not define the mask + as a sigset (alpha) or does not call sa_sigaction with a ucontext_t + as third argument (sparc). */ +# ifndef UCONTEXT_SIGMASK +# define UCONTEXT_SIGMASK(cxt) \ + &(((ucontext_t*) (cxt))->uc_sigmask) +# endif + /* For asynchronous cancellation we use a signal. This is the handler. */ static void sigcancel_handler (int sig, siginfo_t *si, void *ctx) { + INTERNAL_SYSCALL_DECL (err); + pid_t pid = INTERNAL_SYSCALL_CALL (getpid, err); + /* Safety check. It would be possible to call this function for other signals and send a signal from another process. This is not correct and might even be a security problem. Try to catch as many incorrect invocations as possible. */ if (sig != SIGCANCEL - || si->si_pid != __getpid() + || si->si_pid != pid || si->si_code != SI_TKILL) return; struct pthread *self = THREAD_SELF; + volatile struct pthread *pd = (volatile struct pthread *) self; - int oldval = THREAD_GETMEM (self, cancelhandling); - while (1) + if (((pd->cancelhandling & (CANCELSTATE_BITMASK)) != 0) + || ((pd->cancelhandling & CANCELED_BITMASK) == 0)) + return; + + /* Add SIGCANCEL on ignored sigmask to avoid the handler to be called + again. */ + sigset_t *set = UCONTEXT_SIGMASK (ctx); + __sigaddset (set, SIGCANCEL); + + /* Check if asynchronous cancellation mode is set and if interrupted + instruction pointer falls within the cancellable syscall bridge. For + interruptable syscalls that might generate external side-effects (partial + reads or writes, for instance), the kernel will set the IP to after + '__syscall_cancel_arch_end', thus disabling the cancellation and allowing + the process to handle such conditions. */ + uintptr_t pc = ucontext_get_pc (ctx); + if (pd->cancelhandling & CANCELTYPE_BITMASK + || (pc >= (uintptr_t) __syscall_cancel_arch_start + && pc < (uintptr_t) __syscall_cancel_arch_end)) { - /* We are canceled now. When canceled by another thread this flag - is already set but if the signal is directly send (internally or - from another process) is has to be done here. */ - int newval = oldval | CANCELING_BITMASK | CANCELED_BITMASK; - - if (oldval == newval || (oldval & EXITING_BITMASK) != 0) - /* Already canceled or exiting. */ - break; - - int curval = THREAD_ATOMIC_CMPXCHG_VAL (self, cancelhandling, newval, - oldval); - if (curval == oldval) - { - /* Set the return value. */ - THREAD_SETMEM (self, result, PTHREAD_CANCELED); - - /* Make sure asynchronous cancellation is still enabled. */ - if ((newval & CANCELTYPE_BITMASK) != 0) - /* Run the registered destructors and terminate the thread. */ - __do_cancel (); - - break; - } - - oldval = curval; + THREAD_ATOMIC_BIT_SET (self, cancelhandling, EXITING_BIT); + THREAD_SETMEM (self, result, PTHREAD_CANCELED); + + INTERNAL_SYSCALL_CALL (rt_sigprocmask, err, SIG_SETMASK, set, NULL, + _NSIG / 8); + + __do_cancel (); + return; } } #endif @@ -373,7 +390,10 @@ __pthread_initialize_minimal_internal (void) cannot install the handler we do not abort. Maybe we should, but it is only asynchronous cancellation which is affected. */ sa.sa_sigaction = sigcancel_handler; - sa.sa_flags = SA_SIGINFO; + /* The signal handle should be non-interruptible to avoid the risk of + spurious EINTR caused by SIGCANCEL sent to process or if pthread_cancel + is called while cancellation is disabled in the target thread. */ + sa.sa_flags = SA_SIGINFO | SA_RESTART; (void) __libc_sigaction (SIGCANCEL, &sa, NULL); # endif diff --git a/nptl/pthreadP.h b/nptl/pthreadP.h index 713000e..35d99f6 100644 --- a/nptl/pthreadP.h +++ b/nptl/pthreadP.h @@ -291,51 +291,37 @@ __do_cancel (void) { struct pthread *self = THREAD_SELF; - /* Make sure we get no more cancellations. */ - THREAD_ATOMIC_BIT_SET (self, cancelhandling, EXITING_BIT); + /* Make sure we get no more cancellations by clearing the cancel + state. */ + int oldval = THREAD_GETMEM (self, cancelhandling); + while (1) + { + int newval = (oldval | CANCELSTATE_BITMASK); + newval &= ~(CANCELTYPE_BITMASK); + if (oldval == newval) + break; + + int curval = THREAD_ATOMIC_CMPXCHG_VAL (self, cancelhandling, newval, + oldval); + if (__glibc_likely (curval == oldval)) + break; + oldval = curval; + } + + THREAD_SETMEM (self, result, PTHREAD_CANCELED); __pthread_unwind ((__pthread_unwind_buf_t *) THREAD_GETMEM (self, cleanup_jmp_buf)); } -/* Set cancellation mode to asynchronous. */ -#define CANCEL_ASYNC() \ - __pthread_enable_asynccancel () -/* Reset to previous cancellation mode. */ -#define CANCEL_RESET(oldtype) \ - __pthread_disable_asynccancel (oldtype) - -#if IS_IN (libc) -/* Same as CANCEL_ASYNC, but for use in libc.so. */ -# define LIBC_CANCEL_ASYNC() \ - __libc_enable_asynccancel () -/* Same as CANCEL_RESET, but for use in libc.so. */ -# define LIBC_CANCEL_RESET(oldtype) \ - __libc_disable_asynccancel (oldtype) -# define LIBC_CANCEL_HANDLED() \ - __asm (".globl " __SYMBOL_PREFIX "__libc_enable_asynccancel"); \ - __asm (".globl " __SYMBOL_PREFIX "__libc_disable_asynccancel") -#elif IS_IN (libpthread) -# define LIBC_CANCEL_ASYNC() CANCEL_ASYNC () -# define LIBC_CANCEL_RESET(val) CANCEL_RESET (val) -# define LIBC_CANCEL_HANDLED() \ - __asm (".globl " __SYMBOL_PREFIX "__pthread_enable_asynccancel"); \ - __asm (".globl " __SYMBOL_PREFIX "__pthread_disable_asynccancel") -#elif IS_IN (librt) -# define LIBC_CANCEL_ASYNC() \ - __librt_enable_asynccancel () -# define LIBC_CANCEL_RESET(val) \ - __librt_disable_asynccancel (val) -# define LIBC_CANCEL_HANDLED() \ - __asm (".globl " __SYMBOL_PREFIX "__librt_enable_asynccancel"); \ - __asm (".globl " __SYMBOL_PREFIX "__librt_disable_asynccancel") -#else -# define LIBC_CANCEL_ASYNC() 0 /* Just a dummy value. */ -# define LIBC_CANCEL_RESET(val) ((void)(val)) /* Nothing, but evaluate it. */ -# define LIBC_CANCEL_HANDLED() /* Nothing. */ -#endif +extern long int __syscall_cancel_arch (volatile int *, __syscall_arg_t nr, + __syscall_arg_t arg1, __syscall_arg_t arg2, __syscall_arg_t arg3, + __syscall_arg_t arg4, __syscall_arg_t arg5, __syscall_arg_t arg6); +libc_hidden_proto (__syscall_cancel_arch); +extern void __syscall_do_cancel (void) + __cleanup_fct_attribute attribute_hidden __attribute ((__noreturn__)); /* Internal prototypes. */ @@ -503,8 +489,6 @@ extern int __pthread_kill (pthread_t threadid, int signo); extern void __pthread_exit (void *value) __attribute__ ((__noreturn__)); extern int __pthread_join (pthread_t threadid, void **thread_return); extern int __pthread_setcanceltype (int type, int *oldtype); -extern int __pthread_enable_asynccancel (void) attribute_hidden; -extern void __pthread_disable_asynccancel (int oldtype) attribute_hidden; extern void __pthread_testcancel (void); #if IS_IN (libpthread) @@ -540,15 +524,6 @@ extern int __pthread_cond_wait_2_0 (pthread_cond_2_0_t *cond, extern int __pthread_getaffinity_np (pthread_t th, size_t cpusetsize, cpu_set_t *cpuset); -/* The two functions are in libc.so and not exported. */ -extern int __libc_enable_asynccancel (void) attribute_hidden; -extern void __libc_disable_asynccancel (int oldtype) attribute_hidden; - - -/* The two functions are in librt.so and not exported. */ -extern int __librt_enable_asynccancel (void) attribute_hidden; -extern void __librt_disable_asynccancel (int oldtype) attribute_hidden; - #if IS_IN (libpthread) /* Special versions which use non-exported functions. */ extern void __pthread_cleanup_push (struct _pthread_cleanup_buffer *buffer, diff --git a/nptl/pthread_cancel.c b/nptl/pthread_cancel.c index 742dfe6..04d9b89 100644 --- a/nptl/pthread_cancel.c +++ b/nptl/pthread_cancel.c @@ -37,67 +37,23 @@ __pthread_cancel (pthread_t th) #ifdef SHARED pthread_cancel_init (); #endif - int result = 0; - int oldval; - int newval; - do - { - again: - oldval = pd->cancelhandling; - newval = oldval | CANCELING_BITMASK | CANCELED_BITMASK; - /* Avoid doing unnecessary work. The atomic operation can - potentially be expensive if the bug has to be locked and - remote cache lines have to be invalidated. */ - if (oldval == newval) - break; + THREAD_ATOMIC_BIT_SET (pd, cancelhandling, CANCELED_BIT); - /* If the cancellation is handled asynchronously just send a - signal. We avoid this if possible since it's more - expensive. */ - if (CANCEL_ENABLED_AND_CANCELED_AND_ASYNCHRONOUS (newval)) - { - /* Mark the cancellation as "in progress". */ - if (atomic_compare_and_exchange_bool_acq (&pd->cancelhandling, - oldval | CANCELING_BITMASK, - oldval)) - goto again; - -#ifdef SIGCANCEL - /* The cancellation handler will take care of marking the - thread as canceled. */ - pid_t pid = __getpid (); - - INTERNAL_SYSCALL_DECL (err); - int val = INTERNAL_SYSCALL_CALL (tgkill, err, pid, pd->tid, - SIGCANCEL); - if (INTERNAL_SYSCALL_ERROR_P (val, err)) - result = INTERNAL_SYSCALL_ERRNO (val, err); -#else - /* It should be impossible to get here at all, since - pthread_setcanceltype should never have allowed - PTHREAD_CANCEL_ASYNCHRONOUS to be set. */ - abort (); -#endif - - break; - } - - /* A single-threaded process should be able to kill itself, since - there is nothing in the POSIX specification that says that it - cannot. So we set multiple_threads to true so that cancellation - points get executed. */ - THREAD_SETMEM (THREAD_SELF, header.multiple_threads, 1); + /* A single-threaded process should be able to kill itself, since there is + nothing in the POSIX specification that says that it cannot. So we set + multiple_threads to true so that cancellation points get executed. */ + THREAD_SETMEM (THREAD_SELF, header.multiple_threads, 1); #ifndef TLS_MULTIPLE_THREADS_IN_TCB - __pthread_multiple_threads = *__libc_multiple_threads_ptr = 1; + __pthread_multiple_threads = *__libc_multiple_threads_ptr = 1; #endif - } - /* Mark the thread as canceled. This has to be done - atomically since other bits could be modified as well. */ - while (atomic_compare_and_exchange_bool_acq (&pd->cancelhandling, newval, - oldval)); - return result; + /* Avoid signaling when thread attempts cancel itself (pthread_kill + is expensive). */ + if (pd == THREAD_SELF && !(pd->cancelhandling & CANCELTYPE_BITMASK)) + return 0; + + return __pthread_kill (th, SIGCANCEL); } weak_alias (__pthread_cancel, pthread_cancel) diff --git a/nptl/pthread_create.c b/nptl/pthread_create.c index 51ae60d..64526e2 100644 --- a/nptl/pthread_create.c +++ b/nptl/pthread_create.c @@ -412,7 +412,7 @@ START_THREAD_DEFN /* If the parent was running cancellation handlers while creating the thread the new thread inherited the signal mask. Reset the cancellation signal mask. */ - if (__glibc_unlikely (pd->parent_cancelhandling & CANCELING_BITMASK)) + if (__glibc_unlikely (pd->parent_cancelhandling & CANCELED_BITMASK)) { INTERNAL_SYSCALL_DECL (err); sigset_t mask; @@ -444,7 +444,8 @@ START_THREAD_DEFN have ownership (see CONCURRENCY NOTES above). */ if (__glibc_unlikely (pd->stopped_start)) { - int oldtype = CANCEL_ASYNC (); + int ct; + __pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, &ct); /* Get the lock the parent locked to force synchronization. */ lll_lock (pd->lock, LLL_PRIVATE); @@ -454,7 +455,7 @@ START_THREAD_DEFN /* And give it up right away. */ lll_unlock (pd->lock, LLL_PRIVATE); - CANCEL_RESET (oldtype); + __pthread_setcanceltype (ct, NULL); } LIBC_PROBE (pthread_start, 3, (pthread_t) pd, pd->start_routine, pd->arg); diff --git a/nptl/pthread_exit.c b/nptl/pthread_exit.c index 7209769..82ef4f8 100644 --- a/nptl/pthread_exit.c +++ b/nptl/pthread_exit.c @@ -23,9 +23,14 @@ void __pthread_exit (void *value) { - THREAD_SETMEM (THREAD_SELF, result, value); + struct pthread *self = THREAD_SELF; - __do_cancel (); + THREAD_SETMEM (self, result, value); + + THREAD_ATOMIC_BIT_SET (self, cancelhandling, EXITING_BIT); + + __pthread_unwind ((__pthread_unwind_buf_t *) + THREAD_GETMEM (self, cleanup_jmp_buf)); } weak_alias (__pthread_exit, pthread_exit) diff --git a/nptl/pthread_join.c b/nptl/pthread_join.c index afc8c37..8255a7d 100644 --- a/nptl/pthread_join.c +++ b/nptl/pthread_join.c @@ -52,7 +52,7 @@ __pthread_join (pthread_t threadid, void **thread_return) return EINVAL; struct pthread *self = THREAD_SELF; - int result = 0; + int result = 0, ct; LIBC_PROBE (pthread_join, 1, threadid); @@ -62,12 +62,12 @@ __pthread_join (pthread_t threadid, void **thread_return) pthread_cleanup_push (cleanup, &pd->joinid); /* Switch to asynchronous cancellation. */ - int oldtype = CANCEL_ASYNC (); + __pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, &ct); if ((pd == self || (self->joinid == pd && (pd->cancelhandling - & (CANCELING_BITMASK | CANCELED_BITMASK | EXITING_BITMASK + & (CANCELED_BITMASK | EXITING_BITMASK | TERMINATED_BITMASK)) == 0)) && !CANCEL_ENABLED_AND_CANCELED (self->cancelhandling)) /* This is a deadlock situation. The threads are waiting for each @@ -89,9 +89,7 @@ __pthread_join (pthread_t threadid, void **thread_return) /* Wait for the child. */ lll_wait_tid (pd->tid); - - /* Restore cancellation mode. */ - CANCEL_RESET (oldtype); + __pthread_setcanceltype (ct, NULL); /* Remove the handler. */ pthread_cleanup_pop (0); diff --git a/nptl/pthread_timedjoin.c b/nptl/pthread_timedjoin.c index 567c171..75a9cfe 100644 --- a/nptl/pthread_timedjoin.c +++ b/nptl/pthread_timedjoin.c @@ -35,7 +35,7 @@ pthread_timedjoin_np (pthread_t threadid, void **thread_return, { struct pthread *self; struct pthread *pd = (struct pthread *) threadid; - int result; + int result, ct; /* Make sure the descriptor is valid. */ if (INVALID_NOT_TERMINATED_TD_P (pd)) @@ -72,15 +72,12 @@ pthread_timedjoin_np (pthread_t threadid, void **thread_return, pthread_cleanup_push (cleanup, &pd->joinid); /* Switch to asynchronous cancellation. */ - int oldtype = CANCEL_ASYNC (); - + __pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, &ct); /* Wait for the child. */ result = lll_timedwait_tid (pd->tid, abstime); - - /* Restore cancellation mode. */ - CANCEL_RESET (oldtype); + __pthread_setcanceltype (ct, NULL); /* Remove the handler. */ pthread_cleanup_pop (0); diff --git a/nptl/sem_wait.c b/nptl/sem_wait.c index 625bf08..b6f4312 100644 --- a/nptl/sem_wait.c +++ b/nptl/sem_wait.c @@ -56,14 +56,8 @@ __old_sem_wait (sem_t *sem) if (atomic_decrement_if_positive (futex) > 0) return 0; - /* Enable asynchronous cancellation. Required by the standard. */ - int oldtype = __pthread_enable_asynccancel (); - /* Always assume the semaphore is shared. */ - err = lll_futex_wait (futex, 0, LLL_SHARED); - - /* Disable asynchronous cancellation. */ - __pthread_disable_asynccancel (oldtype); + err = lll_futex_wait_cancel (futex, 0, LLL_SHARED); } while (err == 0 || err == -EWOULDBLOCK); diff --git a/nptl/tst-cancel28.c b/nptl/tst-cancel28.c new file mode 100644 index 0000000..e086e9b --- /dev/null +++ b/nptl/tst-cancel28.c @@ -0,0 +1,94 @@ +/* Check side-effect act for cancellable syscalls (BZ #12683). + Copyright (C) 2017 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 + . */ + +/* This testcase checks if there is resource leakage if the syscall has + returned from kernelspace, but before userspace saves the return + value. The 'leaker' thread should be able to close the file descriptor + if the resource is already allocated, meaning that if the cancellation + signal arrives *after* the open syscal return from kernel, the + side-effect should be visible to application. */ + +#include +#include +#include +#include +#include + +#include +#include +#include + +static void * +writeopener (void *arg) +{ + int fd; + for (;;) + { + fd = open (arg, O_WRONLY); + close (fd); + } + return NULL; +} + +static void * +leaker (void *arg) +{ + int fd = open (arg, O_RDONLY); + pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, 0); + close (fd); + return NULL; +} + + +#define ITER_COUNT 1000 +#define MAX_FILENO 1024 + +static int +do_test (void) +{ + struct stat st; + int i; + + char *name = NULL; + support_create_temp_fifo ("tst-cancel28", &name); + + srand (1); + + xpthread_create (NULL, writeopener, name); + for (i = 0; i < ITER_COUNT; i++) + { + pthread_t td = xpthread_create (NULL, leaker, name); + struct timespec ts = + { .tv_nsec = rand () % 100000, .tv_sec = 0 }; + nanosleep (&ts, NULL); + /* Ignore pthread_cancel result because it might be the + case when pthread_cancel is called when thread is already + exited. */ + pthread_cancel (td); + xpthread_join (td); + } + + for (i = STDERR_FILENO+1; i < MAX_FILENO; i++) + if (!fstat (i, &st)) + FAIL_EXIT1 ("leaked fd %d", i); + + return 0; +} + +#define TIMEOUT 10 +#include diff --git a/rt/Makefile b/rt/Makefile index 9740dc2..232e543 100644 --- a/rt/Makefile +++ b/rt/Makefile @@ -64,7 +64,6 @@ CFLAGS-aio_suspend.c = -fexceptions CFLAGS-mq_timedreceive.c = -fexceptions -fasynchronous-unwind-tables CFLAGS-mq_timedsend.c = -fexceptions -fasynchronous-unwind-tables CFLAGS-clock_nanosleep.c = -fexceptions -fasynchronous-unwind-tables -CFLAGS-librt-cancellation.c = -fasynchronous-unwind-tables LDFLAGS-rt.so = -Wl,--enable-new-dtags,-z,nodelete diff --git a/support/temp_file.c b/support/temp_file.c index 547263a..dad13dc 100644 --- a/support/temp_file.c +++ b/support/temp_file.c @@ -86,6 +86,29 @@ create_temp_file (const char *base, char **filename) return fd; } +int +support_create_temp_fifo (const char *base, char **fifoname) +{ + char *fname = xasprintf ("%s/%sXXXXXX", test_dir, base); + mktemp (fname); + + int fd = mkfifo (fname, 0600); + if (fd == -1) + { + printf ("cannot open temporary fifo '%s': %m\n", fname); + free (fname); + return -1; + } + + add_temp_file (fname); + if (fifoname != NULL) + *fifoname = fname; + else + free (fname); + + return fd; +} + char * support_create_temp_directory (const char *base) { diff --git a/support/temp_file.h b/support/temp_file.h index 3b8563e..66bfd1c 100644 --- a/support/temp_file.h +++ b/support/temp_file.h @@ -32,6 +32,12 @@ void add_temp_file (const char *name); *FILENAME. */ int create_temp_file (const char *base, char **filename); +/* Create a temporary fifo. Return the opened file descriptor on + success, or -1 on failure. Write the file name to *FILENAME if + FILENAME is not NULL. In this case, the caller is expected to free + *FILENAME. */ +int support_create_temp_fifo (const char *name, char **fifoname); + /* Create a temporary directory and schedule it for deletion. BASE is used as a prefix for the unique directory name, which the function returns. The caller should free this string. */ diff --git a/sysdeps/generic/sigcontextinfo.h b/sysdeps/generic/sigcontextinfo.h index d25a787..2ca20e9 100644 --- a/sysdeps/generic/sigcontextinfo.h +++ b/sysdeps/generic/sigcontextinfo.h @@ -16,6 +16,11 @@ License along with the GNU C Library; if not, see . */ +#ifndef _SIGCONTEXTINFO_H +#define _SIGCONTEXTINFO_H + +#include + /* In general we cannot provide any information. */ #define SIGCONTEXT struct sigcontext * #define SIGCONTEXT_EXTRA_ARGS @@ -24,3 +29,13 @@ #define GET_STACK(ctx) ((void *) 0) #define CALL_SIGHANDLER(handler, signo, ctx) \ (handler)((signo), SIGCONTEXT_EXTRA_ARGS (ctx)) + +/* Obtain the Program Counter from third argument in signal handler set + with SA_SIGINFO. */ +static inline uintptr_t +ucontext_get_pc (void *ctx) +{ + return 0; +} + +#endif diff --git a/sysdeps/generic/sysdep-cancel.h b/sysdeps/generic/sysdep-cancel.h index ba6a1e0..5c84b44 100644 --- a/sysdeps/generic/sysdep-cancel.h +++ b/sysdeps/generic/sysdep-cancel.h @@ -3,6 +3,3 @@ /* No multi-thread handling enabled. */ #define SINGLE_THREAD_P (1) #define RTLD_SINGLE_THREAD_P (1) -#define LIBC_CANCEL_ASYNC() 0 /* Just a dummy value. */ -#define LIBC_CANCEL_RESET(val) ((void)(val)) /* Nothing, but evaluate it. */ -#define LIBC_CANCEL_HANDLED() /* Nothing. */ diff --git a/sysdeps/nptl/Makefile b/sysdeps/nptl/Makefile index 4f4f4ff..708cab5 100644 --- a/sysdeps/nptl/Makefile +++ b/sysdeps/nptl/Makefile @@ -21,8 +21,7 @@ libpthread-sysdep_routines += errno-loc endif ifeq ($(subdir),rt) -librt-sysdep_routines += timer_routines librt-cancellation -CFLAGS-librt-cancellation.c += -fexceptions -fasynchronous-unwind-tables +librt-sysdep_routines += timer_routines tests += tst-mqueue8x CFLAGS-tst-mqueue8x.c += -fexceptions diff --git a/sysdeps/nptl/aio_misc.h b/sysdeps/nptl/aio_misc.h index 47b1a36..817a75c 100644 --- a/sysdeps/nptl/aio_misc.h +++ b/sysdeps/nptl/aio_misc.h @@ -34,22 +34,18 @@ #define AIO_MISC_WAIT(result, futex, timeout, cancel) \ do { \ - volatile unsigned int *futexaddr = &futex; \ + unsigned int *futexaddr = (unsigned int *)&futex; \ unsigned int oldval = futex; \ \ if (oldval != 0) \ { \ pthread_mutex_unlock (&__aio_requests_mutex); \ \ - int oldtype; \ - if (cancel) \ - oldtype = LIBC_CANCEL_ASYNC (); \ - \ int status; \ do \ { \ - status = futex_reltimed_wait ((unsigned int *) futexaddr, oldval, \ - timeout, FUTEX_PRIVATE); \ + status = futex_reltimed_wait_cancelable (futexaddr, oldval, \ + timeout, FUTEX_PRIVATE); \ if (status != EAGAIN) \ break; \ \ @@ -57,9 +53,6 @@ } \ while (oldval != 0); \ \ - if (cancel) \ - LIBC_CANCEL_RESET (oldtype); \ - \ if (status == EINTR) \ result = EINTR; \ else if (status == ETIMEDOUT) \ diff --git a/sysdeps/nptl/gai_misc.h b/sysdeps/nptl/gai_misc.h index 8b2a2c1..df6b434 100644 --- a/sysdeps/nptl/gai_misc.h +++ b/sysdeps/nptl/gai_misc.h @@ -35,22 +35,18 @@ #define GAI_MISC_WAIT(result, futex, timeout, cancel) \ do { \ - volatile unsigned int *futexaddr = &futex; \ + unsigned int *futexaddr = (unsigned int *)&futex; \ unsigned int oldval = futex; \ \ if (oldval != 0) \ { \ pthread_mutex_unlock (&__gai_requests_mutex); \ \ - int oldtype; \ - if (cancel) \ - oldtype = LIBC_CANCEL_ASYNC (); \ - \ int status; \ do \ { \ - status = futex_reltimed_wait ((unsigned int *) futexaddr, oldval, \ - timeout, FUTEX_PRIVATE); \ + status = futex_reltimed_wait_cancelable (futexaddr, oldval, \ + timeout, FUTEX_PRIVATE); \ if (status != EAGAIN) \ break; \ \ @@ -58,9 +54,6 @@ } \ while (oldval != 0); \ \ - if (cancel) \ - LIBC_CANCEL_RESET (oldtype); \ - \ if (status == EINTR) \ result = EINTR; \ else if (status == ETIMEDOUT) \ diff --git a/sysdeps/nptl/librt-cancellation.c b/sysdeps/nptl/librt-cancellation.c deleted file mode 100644 index 1932242..0000000 --- a/sysdeps/nptl/librt-cancellation.c +++ /dev/null @@ -1,24 +0,0 @@ -/* Copyright (C) 2002-2017 Free Software Foundation, Inc. - This file is part of the GNU C Library. - Contributed by Ulrich Drepper , 2002. - - 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 - - -#define __pthread_enable_asynccancel __librt_enable_asynccancel -#define __pthread_disable_asynccancel __librt_disable_asynccancel -#include diff --git a/sysdeps/nptl/lowlevellock.h b/sysdeps/nptl/lowlevellock.h index 54e3c28..0d806bc 100644 --- a/sysdeps/nptl/lowlevellock.h +++ b/sysdeps/nptl/lowlevellock.h @@ -180,12 +180,13 @@ extern int __lll_timedlock_wait (int *futex, const struct timespec *, wake-up when the clone terminates. The memory location contains the thread ID while the clone is running and is reset to zero by the kernel afterwards. The kernel up to version 3.16.3 does not use the private futex - operations for futex wake-up when the clone terminates. */ + operations for futex wake-up when the clone terminates. + Both lll_wait_tid and lll_timewait_tid acts as cancellation points. */ #define lll_wait_tid(tid) \ do { \ __typeof (tid) __tid; \ while ((__tid = (tid)) != 0) \ - lll_futex_wait (&(tid), __tid, LLL_SHARED);\ + lll_futex_wait_cancel (&(tid), __tid, LLL_SHARED);\ } while (0) extern int __lll_timedwait_tid (int *, const struct timespec *) diff --git a/sysdeps/posix/open64.c b/sysdeps/posix/open64.c index dd1b4d6..8e6f2b9 100644 --- a/sysdeps/posix/open64.c +++ b/sysdeps/posix/open64.c @@ -34,16 +34,8 @@ __libc_open64 (const char *file, int oflag, ...) va_end (arg); } - if (SINGLE_THREAD_P) - return __libc_open (file, oflag | O_LARGEFILE, mode); - - int oldtype = LIBC_CANCEL_ASYNC (); - - int result = __libc_open (file, oflag | O_LARGEFILE, mode); - - LIBC_CANCEL_RESET (oldtype); - - return result; + /* __libc_open should be a cancellation point. */ + return __libc_open (file, oflag | O_LARGEFILE, mode); } weak_alias (__libc_open64, __open64) libc_hidden_weak (__open64) diff --git a/sysdeps/posix/pause.c b/sysdeps/posix/pause.c index 7c17f49..f875939 100644 --- a/sysdeps/posix/pause.c +++ b/sysdeps/posix/pause.c @@ -38,5 +38,3 @@ __libc_pause (void) return __sigsuspend (&set); } weak_alias (__libc_pause, pause) - -LIBC_CANCEL_HANDLED (); /* sigsuspend handles our cancellation. */ diff --git a/sysdeps/posix/sigpause.c b/sysdeps/posix/sigpause.c index a61e056..d58e3e8 100644 --- a/sysdeps/posix/sigpause.c +++ b/sysdeps/posix/sigpause.c @@ -70,6 +70,3 @@ __xpg_sigpause (int sig) return __sigpause (sig, 1); } strong_alias (__xpg_sigpause, __libc___xpg_sigpause) - -/* __sigsuspend handles cancellation. */ -LIBC_CANCEL_HANDLED (); diff --git a/sysdeps/posix/sigwait.c b/sysdeps/posix/sigwait.c index d807dbf..f86cce2 100644 --- a/sysdeps/posix/sigwait.c +++ b/sysdeps/posix/sigwait.c @@ -88,13 +88,8 @@ __sigwait (const sigset_t *set, int *sig) if (SINGLE_THREAD_P) return do_sigwait (set, sig); - int oldtype = LIBC_CANCEL_ASYNC (); - - int result = do_sigwait (set, sig); - - LIBC_CANCEL_RESET (oldtype); - - return result; + /* do_sigwait should be a cancellation point. */ + return do_sigwait (set, sig); } libc_hidden_def (__sigwait) weak_alias (__sigwait, sigwait) diff --git a/sysdeps/posix/waitid.c b/sysdeps/posix/waitid.c index 2c23cdb..9925700 100644 --- a/sysdeps/posix/waitid.c +++ b/sysdeps/posix/waitid.c @@ -151,16 +151,7 @@ OUR_WAITID (idtype_t idtype, id_t id, siginfo_t *infop, int options) int __waitid (idtype_t idtype, id_t id, siginfo_t *infop, int options) { - if (SINGLE_THREAD_P) - return do_waitid (idtype, id, infop, options); - - int oldtype = LIBC_CANCEL_ASYNC (); - - int result = do_waitid (idtype, id, infop, options); - - LIBC_CANCEL_RESET (oldtype); - - return result; + return do_waitid (idtype, id, infop, options); } weak_alias (__waitid, waitid) strong_alias (__waitid, __libc_waitid) diff --git a/sysdeps/sparc/sparc32/lowlevellock.c b/sysdeps/sparc/sparc32/lowlevellock.c index e502bf6..af3b227 100644 --- a/sysdeps/sparc/sparc32/lowlevellock.c +++ b/sysdeps/sparc/sparc32/lowlevellock.c @@ -122,7 +122,8 @@ __lll_timedwait_tid (int *tidp, const struct timespec *abstime) /* Wait until thread terminates. The kernel so far does not use the private futex operations for this. */ - if (lll_futex_timed_wait (tidp, tid, &rt, LLL_SHARED) == -ETIMEDOUT) + if (lll_futex_timed_wait_cancel (tidp, tid, &rt, LLL_SHARED) + == -ETIMEDOUT) return ETIMEDOUT; } diff --git a/sysdeps/unix/sysdep.h b/sysdeps/unix/sysdep.h index c308547..10669ae 100644 --- a/sysdeps/unix/sysdep.h +++ b/sysdeps/unix/sysdep.h @@ -24,6 +24,9 @@ #define SYSCALL__(name, args) PSEUDO (__##name, name, args) #define SYSCALL(name, args) PSEUDO (name, name, args) +#ifndef __ASSEMBLER__ +# include + #define __SYSCALL_CONCAT_X(a,b) a##b #define __SYSCALL_CONCAT(a,b) __SYSCALL_CONCAT_X (a, b) @@ -57,6 +60,29 @@ #define INTERNAL_SYSCALL_CALL(...) \ __INTERNAL_SYSCALL_DISP (__INTERNAL_SYSCALL, __VA_ARGS__) +#define __INTERNAL_SYSCALL_NCS0(name, err) \ + INTERNAL_SYSCALL_NCS (name, err, 0) +#define __INTERNAL_SYSCALL_NCS1(name, err, a1) \ + INTERNAL_SYSCALL_NCS (name, err, 1, a1) +#define __INTERNAL_SYSCALL_NCS2(name, err, a1, a2) \ + INTERNAL_SYSCALL_NCS (name, err, 2, a1, a2) +#define __INTERNAL_SYSCALL_NCS3(name, err, a1, a2, a3) \ + INTERNAL_SYSCALL_NCS (name, err, 3, a1, a2, a3) +#define __INTERNAL_SYSCALL_NCS4(name, err, a1, a2, a3, a4) \ + INTERNAL_SYSCALL_NCS (name, err, 4, a1, a2, a3, a4) +#define __INTERNAL_SYSCALL_NCS5(name, err, a1, a2, a3, a4, a5) \ + INTERNAL_SYSCALL_NCS (name, err, 5, a1, a2, a3, a4, a5) +#define __INTERNAL_SYSCALL_NCS6(name, err, a1, a2, a3, a4, a5, a6) \ + INTERNAL_SYSCALL_NCS (name, err, 6, a1, a2, a3, a4, a5, a6) +#define __INTERNAL_SYSCALL_NCS7(name, err, a1, a2, a3, a4, a5, a6, a7) \ + INTERNAL_SYSCALL_NCS (name, err, 7, a1, a2, a3, a4, a5, a6, a7) + +/* Issue a syscall defined by syscall number plus any other argument required. + It is similar to INTERNAL_SYSCALL_NCS macro, but without the need to pass + the expected argument number as third parameter. */ +#define INTERNAL_SYSCALL_NCS_CALL(...) \ + __INTERNAL_SYSCALL_DISP (__INTERNAL_SYSCALL_NCS, __VA_ARGS__) + #define __INLINE_SYSCALL0(name) \ INLINE_SYSCALL (name, 0) #define __INLINE_SYSCALL1(name, a1) \ @@ -88,19 +114,65 @@ #define INLINE_SYSCALL_CALL(...) \ __INLINE_SYSCALL_DISP (__INLINE_SYSCALL, __VA_ARGS__) -#define SYSCALL_CANCEL(...) \ - ({ \ - long int sc_ret; \ - if (SINGLE_THREAD_P) \ - sc_ret = INLINE_SYSCALL_CALL (__VA_ARGS__); \ - else \ - { \ - int sc_cancel_oldtype = LIBC_CANCEL_ASYNC (); \ - sc_ret = INLINE_SYSCALL_CALL (__VA_ARGS__); \ - LIBC_CANCEL_RESET (sc_cancel_oldtype); \ - } \ - sc_ret; \ + +/* Cancellation macros. */ +#ifndef __SSC +typedef long int __syscall_arg_t; +# define __SSC(__x) ((__syscall_arg_t) (__x)) +#endif + +long int __syscall_cancel (__syscall_arg_t nr, __syscall_arg_t arg1, + __syscall_arg_t arg2, __syscall_arg_t arg3, + __syscall_arg_t arg4, __syscall_arg_t arg5, + __syscall_arg_t arg6); +libc_hidden_proto (__syscall_cancel); + +#define __SYSCALL_CANCEL0(name) \ + (__syscall_cancel)(__NR_##name, 0, 0, 0, 0, 0, 0) +#define __SYSCALL_CANCEL1(name, a1) \ + (__syscall_cancel)(__NR_##name, __SSC(a1), 0, 0, 0, 0, 0) +#define __SYSCALL_CANCEL2(name, a1, a2) \ + (__syscall_cancel)(__NR_##name, __SSC(a1), __SSC(a2), 0, 0, 0, 0) +#define __SYSCALL_CANCEL3(name, a1, a2, a3) \ + (__syscall_cancel)(__NR_##name, __SSC(a1), __SSC(a2), __SSC(a3), 0, 0, 0) +#define __SYSCALL_CANCEL4(name, a1, a2, a3, a4) \ + (__syscall_cancel)(__NR_##name, __SSC(a1), __SSC(a2), __SSC(a3), \ + __SSC(a4), 0, 0) +#define __SYSCALL_CANCEL5(name, a1, a2, a3, a4, a5) \ + (__syscall_cancel)(__NR_##name, __SSC(a1), __SSC(a2), __SSC(a3), \ + __SSC(a4), __SSC(a5), 0) +#define __SYSCALL_CANCEL6(name, a1, a2, a3, a4, a5, a6) \ + (__syscall_cancel)(__NR_##name, __SSC(a1), __SSC(a2), __SSC(a3), \ + __SSC(a4), __SSC(a5), __SSC(a6)) + +#define __SYSCALL_CANCEL_NARGS_X(a,b,c,d,e,f,g,h,n,...) n +#define __SYSCALL_CANCEL_NARGS(...) \ + __SYSCALL_CANCEL_NARGS_X (__VA_ARGS__,7,6,5,4,3,2,1,0,) +#define __SYSCALL_CANCEL_CONCAT_X(a,b) a##b +#define __SYSCALL_CANCEL_CONCAT(a,b) __SYSCALL_CANCEL_CONCAT_X (a, b) +#define __SYSCALL_CANCEL_DISP(b,...) \ + __SYSCALL_CANCEL_CONCAT (b,__SYSCALL_CANCEL_NARGS(__VA_ARGS__))(__VA_ARGS__) + +#define __SYSCALL_CANCEL_CALL(...) \ + __SYSCALL_CANCEL_DISP (__SYSCALL_CANCEL, __VA_ARGS__) + +#define SYSCALL_CANCEL_NCS(name, nr, args...) \ + __SYSCALL_CANCEL_CALL (name, nr, args) + + +/* The loader does not need to handle thread cancellation, use direct + syscall instead. */ +#if IS_IN (rtld) +# define SYSCALL_CANCEL(...) INLINE_SYSCALL_CALL (__VA_ARGS__) +#else +# define SYSCALL_CANCEL(...) \ + ({ \ + long int sc_ret = __SYSCALL_CANCEL_CALL (__VA_ARGS__); \ + SYSCALL_CANCEL_RET ((sc_ret)); \ }) +#endif + +#endif /* __ASSEMBLER__ */ /* Machine-dependent sysdep.h files are expected to define the macro PSEUDO (function_name, syscall_name) to emit assembly code to define the diff --git a/sysdeps/unix/sysv/linux/clock_nanosleep.c b/sysdeps/unix/sysv/linux/clock_nanosleep.c index 93bc4cf..b9a835a 100644 --- a/sysdeps/unix/sysv/linux/clock_nanosleep.c +++ b/sysdeps/unix/sysv/linux/clock_nanosleep.c @@ -28,27 +28,13 @@ int __clock_nanosleep (clockid_t clock_id, int flags, const struct timespec *req, struct timespec *rem) { - INTERNAL_SYSCALL_DECL (err); - int r; - if (clock_id == CLOCK_THREAD_CPUTIME_ID) return EINVAL; if (clock_id == CLOCK_PROCESS_CPUTIME_ID) clock_id = MAKE_PROCESS_CPUCLOCK (0, CPUCLOCK_SCHED); - if (SINGLE_THREAD_P) - r = INTERNAL_SYSCALL (clock_nanosleep, err, 4, clock_id, flags, req, rem); - else - { - int oldstate = LIBC_CANCEL_ASYNC (); - - r = INTERNAL_SYSCALL (clock_nanosleep, err, 4, clock_id, flags, req, - rem); - - LIBC_CANCEL_RESET (oldstate); - } - - return (INTERNAL_SYSCALL_ERROR_P (r, err) - ? INTERNAL_SYSCALL_ERRNO (r, err) : 0); + /* If the call is interrupted by a signal handler or encounters an error, + it returns a positive value similar to errno. */ + return -SYSCALL_CANCEL_NCS (clock_nanosleep, clock_id, flags, req, rem); } weak_alias (__clock_nanosleep, clock_nanosleep) diff --git a/sysdeps/unix/sysv/linux/creat.c b/sysdeps/unix/sysv/linux/creat.c index 31e0248..124d734 100644 --- a/sysdeps/unix/sysv/linux/creat.c +++ b/sysdeps/unix/sysv/linux/creat.c @@ -35,6 +35,4 @@ __creat (const char *file, mode_t mode) } weak_alias (__creat, creat) -LIBC_CANCEL_HANDLED (); - #endif diff --git a/sysdeps/unix/sysv/linux/creat64.c b/sysdeps/unix/sysv/linux/creat64.c index 709c660..2404fea 100644 --- a/sysdeps/unix/sysv/linux/creat64.c +++ b/sysdeps/unix/sysv/linux/creat64.c @@ -37,5 +37,3 @@ weak_alias (__creat64, creat64) strong_alias (__creat64, __creat) weak_alias (__creat64, creat) #endif - -LIBC_CANCEL_HANDLED (); diff --git a/sysdeps/unix/sysv/linux/futex-internal.h b/sysdeps/unix/sysv/linux/futex-internal.h index 1386807..a73fec8 100644 --- a/sysdeps/unix/sysv/linux/futex-internal.h +++ b/sysdeps/unix/sysv/linux/futex-internal.h @@ -83,10 +83,7 @@ static __always_inline int futex_wait_cancelable (unsigned int *futex_word, unsigned int expected, int private) { - int oldtype; - oldtype = __pthread_enable_asynccancel (); - int err = lll_futex_timed_wait (futex_word, expected, NULL, private); - __pthread_disable_asynccancel (oldtype); + int err = lll_futex_timed_wait_cancel (futex_word, expected, NULL, private); switch (err) { case 0: @@ -137,10 +134,7 @@ futex_reltimed_wait_cancelable (unsigned int *futex_word, unsigned int expected, const struct timespec *reltime, int private) { - int oldtype; - oldtype = __pthread_enable_asynccancel (); - int err = lll_futex_timed_wait (futex_word, expected, reltime, private); - __pthread_disable_asynccancel (oldtype); + int err = lll_futex_timed_wait_cancel (futex_word, expected, reltime, private); switch (err) { case 0: @@ -200,11 +194,9 @@ futex_abstimed_wait_cancelable (unsigned int *futex_word, despite them being valid. */ if (__glibc_unlikely ((abstime != NULL) && (abstime->tv_sec < 0))) return ETIMEDOUT; - int oldtype; - oldtype = __pthread_enable_asynccancel (); - int err = lll_futex_timed_wait_bitset (futex_word, expected, abstime, - FUTEX_CLOCK_REALTIME, private); - __pthread_disable_asynccancel (oldtype); + int err = lll_futex_timed_wait_bitset_cancel (futex_word, expected, abstime, + FUTEX_CLOCK_REALTIME, + private); switch (err) { case 0: diff --git a/sysdeps/unix/sysv/linux/lowlevellock-futex.h b/sysdeps/unix/sysv/linux/lowlevellock-futex.h index bb4fbae..ba6bd99 100644 --- a/sysdeps/unix/sysv/linux/lowlevellock-futex.h +++ b/sysdeps/unix/sysv/linux/lowlevellock-futex.h @@ -89,6 +89,12 @@ ? -INTERNAL_SYSCALL_ERRNO (__ret, __err) : 0); \ }) +#define lll_futex_syscall_cp(...) \ + ({ \ + long int __ret = __SYSCALL_CANCEL_CALL (__VA_ARGS__); \ + __ret; \ + }) + #define lll_futex_wait(futexp, val, private) \ lll_futex_timed_wait (futexp, val, NULL, private) @@ -140,6 +146,32 @@ private), \ nr_wake, nr_move, mutex, val) -#endif /* !__ASSEMBLER__ */ +/* Cancellable futex macros. */ +#define lll_futex_wait_cancel(futexp, val, private) \ + lll_futex_timed_wait_cancel (futexp, val, NULL, private) + +#define lll_futex_timed_wait_cancel(futexp, val, timeout, private) \ + ({ \ + long int __ret; \ + int __op = FUTEX_WAIT; \ + __ret = lll_futex_syscall_cp (futex, futexp, \ + __lll_private_flag (__op, private), \ + val, timeout); \ + __ret; \ + }) + +#define lll_futex_timed_wait_bitset_cancel(futexp, val, timeout, \ + clockbit, private) \ + ({ \ + long int __ret; \ + int __op = FUTEX_WAIT_BITSET | clockbit; \ + __ret = lll_futex_syscall_cp (futex, futexp, \ + __lll_private_flag (__op, private), \ + val, timeout, 0, \ + FUTEX_BITSET_MATCH_ANY); \ + __ret; \ + }) + +# endif /* !__ASSEMBLER__ */ #endif /* lowlevellock-futex.h */ diff --git a/sysdeps/unix/sysv/linux/pthread_kill.c b/sysdeps/unix/sysv/linux/pthread_kill.c index cd6f16e..01377b0 100644 --- a/sysdeps/unix/sysv/linux/pthread_kill.c +++ b/sysdeps/unix/sysv/linux/pthread_kill.c @@ -42,9 +42,8 @@ __pthread_kill (pthread_t threadid, int signo) /* Not a valid thread handle. */ return ESRCH; - /* Disallow sending the signal we use for cancellation, timers, - for the setxid implementation. */ - if (signo == SIGCANCEL || signo == SIGTIMER || signo == SIGSETXID) + /* Disallow sending the signal we use for setxid implementation. */ + if (signo == SIGSETXID) return EINVAL; /* We have a special syscall to do the work. */ diff --git a/sysdeps/unix/sysv/linux/sigwait.c b/sysdeps/unix/sysv/linux/sigwait.c index 443c3ad..86d6a54 100644 --- a/sysdeps/unix/sysv/linux/sigwait.c +++ b/sysdeps/unix/sysv/linux/sigwait.c @@ -37,6 +37,3 @@ __sigwait (const sigset_t *set, int *sig) libc_hidden_def (__sigwait) weak_alias (__sigwait, sigwait) strong_alias (__sigwait, __libc_sigwait) - -/* __sigtimedwait handles cancellation. */ -LIBC_CANCEL_HANDLED (); diff --git a/sysdeps/unix/sysv/linux/sigwaitinfo.c b/sysdeps/unix/sysv/linux/sigwaitinfo.c index 75ddd52..738c0d4 100644 --- a/sysdeps/unix/sysv/linux/sigwaitinfo.c +++ b/sysdeps/unix/sysv/linux/sigwaitinfo.c @@ -28,6 +28,3 @@ __sigwaitinfo (const sigset_t *set, siginfo_t *info) libc_hidden_def (__sigwaitinfo) weak_alias (__sigwaitinfo, sigwaitinfo) strong_alias (__sigwaitinfo, __libc_sigwaitinfo) - -/* __sigtimedwait handles cancellation. */ -LIBC_CANCEL_HANDLED (); diff --git a/sysdeps/unix/sysv/linux/socketcall.h b/sysdeps/unix/sysv/linux/socketcall.h index 75f70d0..e5a3a7a 100644 --- a/sysdeps/unix/sysv/linux/socketcall.h +++ b/sysdeps/unix/sysv/linux/socketcall.h @@ -87,16 +87,39 @@ }) -#if IS_IN (libc) -# define __pthread_enable_asynccancel __libc_enable_asynccancel -# define __pthread_disable_asynccancel __libc_disable_asynccancel -#endif +#define __SOCKETCALL_CANCEL1(__name, __a1) \ + SYSCALL_CANCEL_NCS (socketcall, __name, \ + ((long int [1]) { (long int) __a1 })) +#define __SOCKETCALL_CANCEL2(__name, __a1, __a2) \ + SYSCALL_CANCEL_NCS (socketcall, __name, \ + ((long int [2]) { (long int) __a1, (long int) __a2 })) +#define __SOCKETCALL_CANCEL3(__name, __a1, __a2, __a3) \ + SYSCALL_CANCEL_NCS (socketcall, __name, \ + ((long int [3]) { (long int) __a1, (long int) __a2, (long int) __a3 })) +#define __SOCKETCALL_CANCEL4(__name, __a1, __a2, __a3, __a4) \ + SYSCALL_CANCEL_NCS (socketcall, __name, \ + ((long int [4]) { (long int) __a1, (long int) __a2, (long int) __a3, \ + (long int) __a4 })) +#define __SOCKETCALL_CANCEL5(__name, __a1, __a2, __a3, __a4, __a5) \ + SYSCALL_CANCEL_NCS (socketcall, __name, \ + ((long int [5]) { (long int) __a1, (long int) __a2, (long int) __a3, \ + (long int) __a4, (long int) __a5 })) +#define __SOCKETCALL_CANCEL6(__name, __a1, __a2, __a3, __a4, __a5, __a6) \ + SYSCALL_CANCEL_NCS (socketcall, __name, \ + ((long int [6]) { (long int) __a1, (long int) __a2, (long int) __a3, \ + (long int) __a4, (long int) __a5, (long int) __a6 })) + +#define __SOCKETCALL_CANCEL(...) __SOCKETCALL_DISP (__SOCKETCALL_CANCEL,\ + __VA_ARGS__) #define SOCKETCALL_CANCEL(name, args...) \ ({ \ - int oldtype = LIBC_CANCEL_ASYNC (); \ - long int sc_ret = __SOCKETCALL (SOCKOP_##name, args); \ - LIBC_CANCEL_RESET (oldtype); \ + long int sc_ret = __SOCKETCALL_CANCEL (SOCKOP_##name, args); \ + if (sc_ret > -4096UL) \ + { \ + __set_errno (-sc_ret); \ + sc_ret = -1L; \ + } \ sc_ret; \ }) diff --git a/sysdeps/unix/sysv/linux/syscall_cancel.c b/sysdeps/unix/sysv/linux/syscall_cancel.c new file mode 100644 index 0000000..66c87a2 --- /dev/null +++ b/sysdeps/unix/sysv/linux/syscall_cancel.c @@ -0,0 +1,61 @@ +/* Default cancellation syscall bridge. + Copyright (C) 2017 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 + +#define ADD_LABEL(__label) \ + asm volatile ( \ + ".global " __label "\t\n" \ + ".type " __label ",@function\t\n" \ + __label ":\n"); + +/* This is the generic version of the cancellable syscall code which + adds the label guards (__syscall_cancel_arch_{start,end}) used + on SIGCANCEL handler (sigcancel_handler at nptl-init.c) to check if + the cancelled syscall have side-effects that need to be signaled to + program. + + An important contrainst should be observed when using this generic + implementation: the __syscall_cancel_arch_end should point to the + immediate next instruction after the syscall one. This is because + kernel will signal interrupted syscall with side effects by setting + the signal frame Program Counter right after the syscall instruction. + + If the INTERNAL_SYSCALL_NCS macro use more instructions to get the + error condition from kernel (from instance on powerpc) or uses an + out of the line helper (such as ARM), one should either adjust the + macro or provide a custom __syscall_cancel_arch implementation. */ +long int +__syscall_cancel_arch (volatile int *ch, __syscall_arg_t nr, + __syscall_arg_t a1, __syscall_arg_t a2, + __syscall_arg_t a3, __syscall_arg_t a4, + __syscall_arg_t a5, __syscall_arg_t a6) +{ + ADD_LABEL ("__syscall_cancel_arch_start"); + if (__glibc_unlikely (*ch & CANCELED_BITMASK)) + __syscall_do_cancel(); + + INTERNAL_SYSCALL_DECL(err); + long int result = INTERNAL_SYSCALL_NCS (nr, err, 6, a1, a2, a3, a4, a5, a6); + ADD_LABEL ("__syscall_cancel_arch_end"); + if (INTERNAL_SYSCALL_ERROR_P (result, err)) + return -INTERNAL_SYSCALL_ERRNO (result, err); + return result; +} +libc_hidden_def (__syscall_cancel_arch) diff --git a/sysdeps/unix/sysv/linux/sysdep.h b/sysdeps/unix/sysv/linux/sysdep.h index 1c24766..22d70c0 100644 --- a/sysdeps/unix/sysv/linux/sysdep.h +++ b/sysdeps/unix/sysv/linux/sysdep.h @@ -27,6 +27,23 @@ -1l; \ }) +/* Check error from cancellable syscall and set errno accordingly. + Linux uses a negative return value to indicate syscall errors + and since version 2.1 the return value of a system call might be + negative even if the call succeeded (e.g., the `lseek' system call + might return a large offset). + Current contract is kernel make sure the no syscall returns a value + in -1 .. -4095 as a valid result so we can savely test with -4095. */ +#define SYSCALL_CANCEL_RET(__ret) \ + ({ \ + if (__ret > -4096UL) \ + { \ + __set_errno (-__ret); \ + __ret = -1; \ + } \ + __ret; \ + }) + /* Provide a dummy argument that can be used to force register alignment for register pairs if required by the syscall ABI. */ #ifdef __ASSUME_ALIGNED_REGISTER_PAIRS