From patchwork Wed May 13 20:26:27 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 39238 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2F897388B035; Wed, 13 May 2020 20:26:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2F897388B035 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1589401602; bh=C5XFmFn873uIxpf6/6D//Mx+VTC84X5VXCaCZN49KyI=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=aPgR1L2apxWXcbmIZ5OoXA+B5l6+mYIyyRY6DoJCGwLemqYzBBsvt0eGaJl9EU3Zz fbt0MJXYcijslWjvkHp8aI6Mn9T2jBun6tfEk5pnWjC/5oJZsDc8BcM2C68qV0GK85 yq3JKx0guzvRofQDkCBUYGQp+eUKQ0uGF53rfv7g= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qk1-x72b.google.com (mail-qk1-x72b.google.com [IPv6:2607:f8b0:4864:20::72b]) by sourceware.org (Postfix) with ESMTPS id F07D33894E54 for ; Wed, 13 May 2020 20:26:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org F07D33894E54 Received: by mail-qk1-x72b.google.com with SMTP id i5so700590qkl.12 for ; Wed, 13 May 2020 13:26:37 -0700 (PDT) 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:mime-version:content-transfer-encoding; bh=C5XFmFn873uIxpf6/6D//Mx+VTC84X5VXCaCZN49KyI=; b=Au0qjedsm2UBKbBq+S7Q+Wy6dwroQZCy69mwZxlXIkHXIgbWrjWaLsaGAJDrc8zby5 eFLInGcFYAhhyDaGpqnM6PpzMmtlrMEh5cukSKfN9GGcYhOg4T5alxihUUHI5bPZbHl+ qU9F2IJR1uTGQOjiCyaRV2fSlZu1dYF2sQkspQq1sJwrm+1B7tr2/nOa1bC4JhKUO1m2 DcTIk8l4g8wCYesCn6zWy646kph6tHc417+sM2+o3ibcxq08fCJASIqhhUfAM322MsJ6 qRJEO6Xs/J3RymuZmVRhV8sY/kSxyRQ21gqsxFf6mdqgo0NcLDsbpUi4JL7gEUdlXGFJ K+VA== X-Gm-Message-State: AOAM531tzRlZ0/ODvR+VROEQnD/G2WBaokIe0gYVNyq4qc59CxMMMrL5 jJWCebTRQR3+rYWis1HdnB5hZtgHrSA= X-Google-Smtp-Source: ABdhPJzhaH4GX79IHY2VUOHscTxGLGp5+f5ezOiw5tyvAj/d6LK0SUkv70eIAyALdQkDOOMRsgGuNQ== X-Received: by 2002:a05:620a:208f:: with SMTP id e15mr1579256qka.440.1589401596774; Wed, 13 May 2020 13:26:36 -0700 (PDT) Received: from localhost.localdomain ([177.194.48.209]) by smtp.googlemail.com with ESMTPSA id 62sm806465qkh.113.2020.05.13.13.26.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 May 2020 13:26:36 -0700 (PDT) To: libc-alpha@sourceware.org Subject: [RFC 1/4] string: Make strsignal async-signal-safe Date: Wed, 13 May 2020 17:26:27 -0300 Message-Id: <20200513202630.2123238-2-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200513202630.2123238-1-adhemerval.zanella@linaro.org> References: <20200513202630.2123238-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-16.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" To accomplish it a per-thread static buffer is used for unknown signals and the resulting message is not translated (this will be covered by a new function). To setup the per-thread static buffer, two strategies are used: 1. The default one uses a TLS structure, which will be place in the static TLS space. 2. Linux allocates on struct pthread and access it through THREAD_* macros. The default strategy has the disadvantage of increasing libc.so static TLS consumption and thus descreasing the possible surplus used in some scenarios (which might be mitigated by BZ#25051 fix). It is used only on Hurd, where accessing the thread point in single thread case is not straightforward (afaiu, Hurd developers could correct me here). Checked on x86-64-linux-gnu, i686-linux-gnu, powerpc64le-linux-gnu, and s390x-linux-gnu. --- manual/signal.texi | 6 +- nptl/descr.h | 5 +- string/strsignal.c | 119 +++++++------------------ sysdeps/generic/Makefile | 1 + sysdeps/generic/tls_internal-struct.h | 32 +++++++ sysdeps/generic/tls_internal.c | 3 + sysdeps/generic/tls_internal.h | 32 +++++++ sysdeps/unix/sysv/linux/tls_internal.c | 1 + sysdeps/unix/sysv/linux/tls_internal.h | 30 +++++++ time/strftime_l.c | 10 +-- 10 files changed, 139 insertions(+), 100 deletions(-) create mode 100644 sysdeps/generic/tls_internal-struct.h create mode 100644 sysdeps/generic/tls_internal.c create mode 100644 sysdeps/generic/tls_internal.h create mode 100644 sysdeps/unix/sysv/linux/tls_internal.c create mode 100644 sysdeps/unix/sysv/linux/tls_internal.h diff --git a/manual/signal.texi b/manual/signal.texi index 33e6646975..08f0edb306 100644 --- a/manual/signal.texi +++ b/manual/signal.texi @@ -830,8 +830,8 @@ termination status of a child process (@pxref{Process Completion}) or it may come from a signal handler in the same process. @deftypefun {char *} strsignal (int @var{signum}) -@standards{GNU, string.h} -@safety{@prelim{}@mtunsafe{@mtasurace{:strsignal} @mtslocale{}}@asunsafe{@asuinit{} @ascuintl{} @asucorrupt{} @ascuheap{}}@acunsafe{@acuinit{} @acucorrupt{} @acsmem{}}} +@standards{POSIX-1.2008, string.h} +@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}} @c strsignal @mtasurace:strsignal @mtslocale @asuinit @ascuintl @asucorrupt @ascuheap @acucorrupt @acsmem @c uses a static buffer if tsd key creation fails @c [once] init @@ -852,7 +852,7 @@ rewritten on subsequent calls, you should save a copy of it if you need to reference it later. @pindex string.h -This function is a GNU extension, declared in the header file +This function is declared in the header file @file{string.h}. @end deftypefun diff --git a/nptl/descr.h b/nptl/descr.h index e1c7db5473..ee8f4b0e37 100644 --- a/nptl/descr.h +++ b/nptl/descr.h @@ -34,6 +34,7 @@ #include #include #include +#include #ifndef TCB_ALIGNMENT # define TCB_ALIGNMENT sizeof (double) @@ -116,7 +117,6 @@ struct priority_protection_data unsigned int priomap[]; }; - /* Thread descriptor data structure. */ struct pthread { @@ -398,6 +398,9 @@ struct pthread /* Indicates whether is a C11 thread created by thrd_creat. */ bool c11; + /* Used on strsignal. */ + struct tls_internal_t tls_state; + /* This member must be last. */ char end_padding[]; diff --git a/string/strsignal.c b/string/strsignal.c index 1551480026..5fbc3d9a60 100644 --- a/string/strsignal.c +++ b/string/strsignal.c @@ -16,110 +16,55 @@ . */ #include -#include -#include #include -#include -#include - -static __libc_key_t key; - -/* If nonzero the key allocation failed and we should better use a - static buffer than fail. */ -#define BUFFERSIZ 100 -static char local_buf[BUFFERSIZ]; -static char *static_buf; +#include +#include +#include -/* Destructor for the thread-specific data. */ -static void init (void); -static void free_key_mem (void *mem); -static char *getbuffer (void); +#define RT_STR "Real-time signal " +_Static_assert (sizeof (RT_STR) <= strsignal_str_len, + RT_STR " > strsignal_str_len"); +#define UNK_STR "Unknown signal " +_Static_assert (sizeof (UNK_STR) <= strsignal_str_len, + UNK_STR " > strsignal_str_len"); /* Return a string describing the meaning of the signal number SIGNUM. */ char * strsignal (int signum) { - __libc_once_define (static, once); - const char *desc; + const char *desc = NULL; + + if (signum >= 0 && signum <= NSIG + && signum < array_length (__sys_siglist_internal)) + desc = __sys_siglist_internal[signum]; - /* If we have not yet initialized the buffer do it now. */ - __libc_once (once, init); + if (desc != NULL) + return (char *) desc; - if ( + const char* prefix = UNK_STR; #ifdef SIGRTMIN - (signum >= SIGRTMIN && signum <= SIGRTMAX) || -#endif - signum < 0 || signum >= NSIG - || (desc = __sys_siglist_internal[signum]) == NULL) + if (signum >= SIGRTMIN && signum <= SIGRTMAX) { - char *buffer = getbuffer (); - int len; -#ifdef SIGRTMIN - if (signum >= SIGRTMIN && signum <= SIGRTMAX) - len = __snprintf (buffer, BUFFERSIZ - 1, _("Real-time signal %d"), - signum - SIGRTMIN); - else -#endif - len = __snprintf (buffer, BUFFERSIZ - 1, _("Unknown signal %d"), - signum); - if (len >= BUFFERSIZ) - buffer = NULL; - else - buffer[len] = '\0'; - - return buffer; + prefix = RT_STR; + signum -= SIGRTMIN; } +#endif - return (char *) _(desc); -} - - -/* Initialize buffer. */ -static void -init (void) -{ - if (__libc_key_create (&key, free_key_mem)) - /* Creating the key failed. This means something really went - wrong. In any case use a static buffer which is better than - nothing. */ - static_buf = local_buf; -} - - -/* Free the thread specific data, this is done if a thread terminates. */ -static void -free_key_mem (void *mem) -{ - free (mem); - __libc_setspecific (key, NULL); -} - + char *buffer = __glibc_tls_internal ()->strsignal_buf; -/* Return the buffer to be used. */ -static char * -getbuffer (void) -{ - char *result; + char *q = __stpcpy (buffer, prefix); - if (static_buf != NULL) - result = static_buf; - else + if (signum < 0) { - /* We don't use the static buffer and so we have a key. Use it - to get the thread-specific buffer. */ - result = __libc_getspecific (key); - if (result == NULL) - { - /* No buffer allocated so far. */ - result = malloc (BUFFERSIZ); - if (result == NULL) - /* No more memory available. We use the static buffer. */ - result = local_buf; - else - __libc_setspecific (key, result); - } + *q++ = '-'; + signum = abs (signum); } + for (int d = signum; q++, (d /= 10) != 0; ) + continue; + *q = '\0'; + for (int d = signum; *--q = '0' + d % 10, (d /= 10) != 0; ) + continue; - return result; + return buffer; } diff --git a/sysdeps/generic/Makefile b/sysdeps/generic/Makefile index bd4f9425ca..9bed5d7fa2 100644 --- a/sysdeps/generic/Makefile +++ b/sysdeps/generic/Makefile @@ -16,6 +16,7 @@ # . ifeq ($(subdir),string) +sysdep_routines += tls_internal CFLAGS-wordcopy.c += -Wno-uninitialized endif diff --git a/sysdeps/generic/tls_internal-struct.h b/sysdeps/generic/tls_internal-struct.h new file mode 100644 index 0000000000..dac967b2f3 --- /dev/null +++ b/sysdeps/generic/tls_internal-struct.h @@ -0,0 +1,32 @@ +/* Per-thread state. Generic version. + Copyright (C) 2020 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 + . */ + +#ifndef _TLS_INTERNAL_STRUCT_H +#define _TLS_INTERNAL_STRUCT_H 1 + +#include + +enum { strsignal_str_len = 20 }; + +struct tls_internal_t +{ + /* Used on strsignal.c. */ + char strsignal_buf[strsignal_str_len + INT_STRLEN_BOUND (int) + 1]; +}; + +#endif diff --git a/sysdeps/generic/tls_internal.c b/sysdeps/generic/tls_internal.c new file mode 100644 index 0000000000..e988a608b2 --- /dev/null +++ b/sysdeps/generic/tls_internal.c @@ -0,0 +1,3 @@ +#include + +__thread struct tls_internal_t tls_internal; diff --git a/sysdeps/generic/tls_internal.h b/sysdeps/generic/tls_internal.h new file mode 100644 index 0000000000..6edc4e574c --- /dev/null +++ b/sysdeps/generic/tls_internal.h @@ -0,0 +1,32 @@ +/* Per-thread state. Generic version. + Copyright (C) 2020 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 + . */ + +#ifndef _TLS_INTERNAL_H +#define _TLS_INTERNAL_H 1 + +#include + +extern __thread struct tls_internal_t tls_internal attribute_hidden; + +static inline struct tls_internal_t * +__glibc_tls_internal (void) +{ + return &tls_internal; +} + +#endif diff --git a/sysdeps/unix/sysv/linux/tls_internal.c b/sysdeps/unix/sysv/linux/tls_internal.c new file mode 100644 index 0000000000..6e25b021ab --- /dev/null +++ b/sysdeps/unix/sysv/linux/tls_internal.c @@ -0,0 +1 @@ +/* Empty. */ diff --git a/sysdeps/unix/sysv/linux/tls_internal.h b/sysdeps/unix/sysv/linux/tls_internal.h new file mode 100644 index 0000000000..f1d3161e08 --- /dev/null +++ b/sysdeps/unix/sysv/linux/tls_internal.h @@ -0,0 +1,30 @@ +/* Per-thread state. Linux version. + Copyright (C) 2020 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 + . */ + +#ifndef _TLS_INTERNAL_H +#define _TLS_INTERNAL_H 1 + +#include + +static inline struct tls_internal_t * +__glibc_tls_internal (void) +{ + return &THREAD_SELF->tls_state; +} + +#endif diff --git a/time/strftime_l.c b/time/strftime_l.c index a9aae42191..83a62dbb6b 100644 --- a/time/strftime_l.c +++ b/time/strftime_l.c @@ -136,15 +136,7 @@ extern char *tzname[]; # define NULL 0 #endif -#define TYPE_SIGNED(t) ((t) -1 < 0) - -/* Bound on length of the string representing an integer value of type t. - Subtract one for the sign bit if t is signed; - 302 / 1000 is log10 (2) rounded up; - add one for integer division truncation; - add one more for a minus sign if t is signed. */ -#define INT_STRLEN_BOUND(t) \ - ((sizeof (t) * CHAR_BIT - TYPE_SIGNED (t)) * 302 / 1000 + 1 + TYPE_SIGNED (t)) +#include #define TM_YEAR_BASE 1900