From patchwork Fri Jun 17 18:43:08 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 70373 Delivered-To: patch@linaro.org Received: by 10.140.28.4 with SMTP id 4csp421281qgy; Fri, 17 Jun 2016 11:43:40 -0700 (PDT) X-Received: by 10.98.54.198 with SMTP id d189mr3871727pfa.39.1466189020244; Fri, 17 Jun 2016 11:43:40 -0700 (PDT) Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id 189si13725253pfu.156.2016.06.17.11.43.39 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 17 Jun 2016 11:43:40 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-70749-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; spf=pass (google.com: domain of libc-alpha-return-70749-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-70749-patch=linaro.org@sourceware.org; dmarc=fail (p=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; q=dns; s= default; b=Z4cWrSvhA4DW7f7HpO6cbHD4eI+dk3EdHnVKyk41zyTh7pYyJbj8J N1H/v7D+drfkB4c2fo9c9s1suD93dyY0A4KPN8DlqmgVRhxcUIGCJx2/nk/cImVH DJyDQ1d8OG89KlLQyOvHyG++jnDuY38QXHlMFgmqV9atPKaRC+fC9c= 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; s=default; bh=uhA2aeqpYPJKZ4UCjR4xD/AoR5k=; b=uOwFjh0HQk+wHTS2BQptpEo93Lej iPaVTB8qyJoaAb6RHBfh0L6UANmgH/HtnD2SL+MZfXO8SSNBuJcta2U6wnSu0nUG 0evMlLCGlcc1eMRucqn9RhRIIJ/RZZJNuFXnBG9HOfyg9rcJuTDcqNjmcMi8bZJJ eRNc64rCrSlkpPo= Received: (qmail 38812 invoked by alias); 17 Jun 2016 18:43:30 -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 38795 invoked by uid 89); 17 Jun 2016 18:43:29 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=sk:INTERNA, 16, 22, Drepper, INLINE_SYSCALL X-HELO: mail-yw0-f177.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:subject:date:message-id; bh=PV2CShyr1UCStu3Q+y8eqCViIp3VvP+tYBTRh9Mp9Dk=; b=ja6Fq8LtBS5fW6xbJ4i+4N/NLEAFowUGW1t60pVMFTScj4J5ZQpRjaTo0MR5TQgAcu eJBUI/XVT/QH6WL5pzuygFQYhrTOa7kp6OfCwV8u8iUG8/eaxiJYW/VjneqVIA0APHUG ilPlmsuci7z7oayVYS1892FOKJgNRgtwo96RRr4SlJYOa/N4i+++DgViZKYwiCOEKOCZ 3DQvUJuxDh98VOmF/UQFlv9K9RXqGH1XZ5pPRSDyxnAC9oJ08vEYNXE+dr/faK6bFC9/ WAxJ7DaexF2az+BPmUiYzAzXz5uoO18Nn+drTJCDpJvW09Xbdb536nYoN0BEqj9j8BFm jn8w== X-Gm-Message-State: ALyK8tJesvAc7ahIzk96oZkyNdYJMQsk77kTn4hfc+H19/7mfN8xyTqgdxbauamVflYXAiyE X-Received: by 10.13.242.130 with SMTP id b124mr2183696ywf.104.1466188997401; Fri, 17 Jun 2016 11:43:17 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH] Refactor Linux raise implementation (BZ#15368) Date: Fri, 17 Jun 2016 15:43:08 -0300 Message-Id: <1466188988-19954-1-git-send-email-adhemerval.zanella@linaro.org> This patch changes both the nptl and libc Linux raise implementation to avoid the issues described in BZ#15368. The strategy used is summarized in bug report first comment: 1. Block all signals (including internal NPTL ones); 2. Get pid and tid directly from syscall (not relying on cached values); 3. Call tgkill; 4. Restore old signal mask. Tested on x86_64 and i686. * sysdeps/unix/sysv/linux/nptl-signals.h (__nptl_clear_internal_signals): New function. (__libc_signal_block_all): Likewise. (__libc_signal_block_app): Likewise. (__libc_signal_restore_set): Likewise. * sysdeps/unix/sysv/linux/pt-raise.c (raise): Use Linux raise.c implementation. * sysdeps/unix/sysv/linux/raise.c (raise): Reimplement to not use the cached pid/tid value in pthread structure. --- ChangeLog | 12 +++++++++ sysdeps/unix/sysv/linux/nptl-signals.h | 37 ++++++++++++++++++++++++++++ sysdeps/unix/sysv/linux/pt-raise.c | 23 +++-------------- sysdeps/unix/sysv/linux/raise.c | 45 ++++++++++++---------------------- 4 files changed, 67 insertions(+), 50 deletions(-) -- 2.7.4 diff --git a/sysdeps/unix/sysv/linux/nptl-signals.h b/sysdeps/unix/sysv/linux/nptl-signals.h index 01f34c2..88a0a32 100644 --- a/sysdeps/unix/sysv/linux/nptl-signals.h +++ b/sysdeps/unix/sysv/linux/nptl-signals.h @@ -39,5 +39,42 @@ __nptl_is_internal_signal (int sig) return (sig == SIGCANCEL) || (sig == SIGTIMER) || (sig == SIGSETXID); } +static inline void +__nptl_clear_internal_signals (sigset_t *set) +{ + __sigdelset (set, SIGCANCEL); + __sigdelset (set, SIGTIMER); + __sigdelset (set, SIGSETXID); +} + +#define SIGALL_SET \ + ((__sigset_t) { .__val = {[0 ... _SIGSET_NWORDS-1 ] = -1 } }) + +static inline int +__libc_signal_block_all (const sigset_t *set) +{ + INTERNAL_SYSCALL_DECL (err); + return INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_BLOCK, &SIGALL_SET, + set, _NSIG / 8); +} + +static inline int +__libc_signal_block_app (const sigset_t *set) +{ + sigset_t allset = SIGALL_SET; + __nptl_clear_internal_signals (&allset); + INTERNAL_SYSCALL_DECL (err); + return INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_BLOCK, &allset, set, + _NSIG / 8); +} + +static inline int +__libc_signal_restore_set (const sigset_t *set) +{ + INTERNAL_SYSCALL_DECL (err); + return INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_SETMASK, set, NULL, + _NSIG / 8); +} + /* Used to communicate with signal handler. */ extern struct xid_command *__xidcmd attribute_hidden; diff --git a/sysdeps/unix/sysv/linux/pt-raise.c b/sysdeps/unix/sysv/linux/pt-raise.c index 715bbe9..5f6dea1 100644 --- a/sysdeps/unix/sysv/linux/pt-raise.c +++ b/sysdeps/unix/sysv/linux/pt-raise.c @@ -1,4 +1,5 @@ -/* Copyright (C) 2002-2016 Free Software Foundation, Inc. +/* ISO C raise function for libpthread. + Copyright (C) 2002-2016 Free Software Foundation, Inc. This file is part of the GNU C Library. Contributed by Ulrich Drepper , 2002. @@ -16,22 +17,4 @@ License along with the GNU C Library; if not, see . */ -#include -#include -#include -#include - - -int -raise (int sig) -{ - /* raise is an async-safe function. It could be called while the - fork function temporarily invalidated the PID field. Adjust for - that. */ - pid_t pid = THREAD_GETMEM (THREAD_SELF, pid); - if (__glibc_unlikely (pid < 0)) - pid = -pid; - - return INLINE_SYSCALL (tgkill, 3, pid, THREAD_GETMEM (THREAD_SELF, tid), - sig); -} +#include diff --git a/sysdeps/unix/sysv/linux/raise.c b/sysdeps/unix/sysv/linux/raise.c index 3795e6e..c6ecd66 100644 --- a/sysdeps/unix/sysv/linux/raise.c +++ b/sysdeps/unix/sysv/linux/raise.c @@ -16,42 +16,27 @@ License along with the GNU C Library; if not, see . */ -#include -#include #include #include -#include - +#include +#include +#include +#include int raise (int sig) { - struct pthread *pd = THREAD_SELF; - pid_t pid = THREAD_GETMEM (pd, pid); - pid_t selftid = THREAD_GETMEM (pd, tid); - if (selftid == 0) - { - /* This system call is not supposed to fail. */ -#ifdef INTERNAL_SYSCALL - INTERNAL_SYSCALL_DECL (err); - selftid = INTERNAL_SYSCALL (gettid, err, 0); -#else - selftid = INLINE_SYSCALL (gettid, 0); -#endif - THREAD_SETMEM (pd, tid, selftid); - - /* We do not set the PID field in the TID here since we might be - called from a signal handler while the thread executes fork. */ - pid = selftid; - } - else - /* raise is an async-safe function. It could be called while the - fork/vfork function temporarily invalidated the PID field. Adjust for - that. */ - if (__glibc_unlikely (pid <= 0)) - pid = (pid & INT_MAX) == 0 ? selftid : -pid; - - return INLINE_SYSCALL (tgkill, 3, pid, selftid, sig); + sigset_t set; + __libc_signal_block_app (&set); + + pid_t pid = __getpid (); + pid_t tid = INLINE_SYSCALL (gettid, 0); + + int ret = INLINE_SYSCALL (tgkill, 3, pid, tid, sig); + + __libc_signal_restore_set (&set); + + return ret; } libc_hidden_def (raise) weak_alias (raise, gsignal)