From patchwork Fri Dec 20 06:16:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 103492 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 AD1F23858CD1 for ; Fri, 20 Dec 2024 06:23:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AD1F23858CD1 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=Tf61w4Rh X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id 608B63858CDA for ; Fri, 20 Dec 2024 06:16:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 608B63858CDA Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 608B63858CDA Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1734675400; cv=none; b=Nvnaq32WsxGfoFQ8DPf7HwHCP9YlDGcTt+mY8MybBCP2N9ZXKV5KMrhogFDpJ7Z2nMtutpXg/TyLj0hXPite4flcnBBX0ozDQ1UXfjvOCfSGSPxdF6uCbFXKm+jyMD+i4dH8hsGIh9MbFHVL/AnbHJ1iYZ/2WCqo2G1avy01c6o= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1734675400; c=relaxed/simple; bh=7S9p0FQxFiveZAvSpY3uuYU2Aikaf1xpouwJ3DAAZzs=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=C7Ism85P1bjw2HdfQ+XcFqlG9pZiJAajgQNvMWlDnhoZdfVjQbcvnPzk/rV9v9mCP/9UUocjMKAsuFvcFdSu0tDS6StyvybSe6uB4NCHZlq6ADCfPHdSzuhuJ2yg4HWxT6sTXMHONPqUEzz6SVLC2qI/K5AV4aJy1WlEnmRDDl8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 608B63858CDA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1734675400; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=bHPgdPaiYKOFsjNbImLsnhOtb7yfVJuWWOM/XERi+/Q=; b=Tf61w4RhaYruDErvbQGTTL/BCaYaeaTh46f2cFe4hRU2dAM5ANDhYAhk5J7Pseno3CUpa+ 3+bHG7gizNr+nXd6VpDMjaEfqMfn+oGcVwyLQAX3j48+WpyMAH/jd4/8oF9dQbitLtoTEb Zo/EqB/LdBbMnFkwTkQ+mLgTSQLPK7A= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-1-tKNHe13jNceGZxXpRDc0Tw-1; Fri, 20 Dec 2024 01:16:38 -0500 X-MC-Unique: tKNHe13jNceGZxXpRDc0Tw-1 X-Mimecast-MFC-AGG-ID: tKNHe13jNceGZxXpRDc0Tw Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id C0D171955D50 for ; Fri, 20 Dec 2024 06:16:37 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.192.21]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id B48A21955F77 for ; Fri, 20 Dec 2024 06:16:36 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH v2 4/4] ctype: Fallback initialization of TLS using relocations (bug 19341, bug 32483) In-Reply-To: Message-ID: References: X-From-Line: af43d2cd9998bd06fc1ee47542906457fd08f333 Mon Sep 17 00:00:00 2001 Date: Fri, 20 Dec 2024 07:16:33 +0100 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: jrs8xh9VuNmxqiRbivuf4EmJ2frRZNjXBM0mwFS6rpA_1734675397 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org This ensures that the ctype data pointers in TLS are valid in secondary namespaces even without initialization via __ctype_init. --- ctype/Makefile | 17 ++++++++ ctype/ctype-info.c | 14 ++++-- ctype/tst-ctype-tls-dlmopen.c | 2 + ctype/tst-ctype-tls-dlopen-static.c | 2 + ctype/tst-ctype-tls-mod.c | 37 ++++++++++++++++ ctype/tst-ctype-tls-skeleton.c | 67 +++++++++++++++++++++++++++++ 6 files changed, 136 insertions(+), 3 deletions(-) create mode 100644 ctype/tst-ctype-tls-dlmopen.c create mode 100644 ctype/tst-ctype-tls-dlopen-static.c create mode 100644 ctype/tst-ctype-tls-mod.c create mode 100644 ctype/tst-ctype-tls-skeleton.c diff --git a/ctype/Makefile b/ctype/Makefile index 3e09938bd1..b7cd5f2282 100644 --- a/ctype/Makefile +++ b/ctype/Makefile @@ -36,6 +36,23 @@ aux := ctype-info tests := \ test_ctype \ + tst-ctype-tls-dlmopen \ + tst-ctype-tls-dlopen-static \ # tests +tests-static := \ + tst-ctype-tls-dlopen-static \ + # tests-static + +modules-names := \ + tst-ctype-tls-mod \ + # modules-names + include ../Rules + +$(objpfx)tst-ctype-tls-dlmopen: $(shared-thread-library) +$(objpfx)tst-ctype-tls-dlmopen.out: $(objpfx)tst-ctype-tls-mod.so +$(objpfx)tst-ctype-tls-dlopen-static: $(static-thread-library) +$(objpfx)tst-ctype-tls-dlopen-static.out: $(objpfx)tst-ctype-tls-mod.so +tst-ctype-tls-dlopen-static-ENV = \ + LD_LIBRARY_PATH=$(ld-library-path):$(common-objpfx):$(common-objpfx)elf diff --git a/ctype/ctype-info.c b/ctype/ctype-info.c index 94e312d91f..621bd4239c 100644 --- a/ctype/ctype-info.c +++ b/ctype/ctype-info.c @@ -19,9 +19,17 @@ #include #include -__thread const uint16_t * __libc_tsd_CTYPE_B; -__thread const int32_t * __libc_tsd_CTYPE_TOLOWER; -__thread const int32_t * __libc_tsd_CTYPE_TOUPPER; +/* Fallback initialization using relocations. See the _nl_C_locobj + initializers in locale/xlocale.c. Usually, this is overwritten by + __ctype_init before user code runs, but this does not happen for + threads in secondary namespaces. With the initializers, secondary + namespaces at least get locale data from the C locale. */ +__thread const uint16_t * __libc_tsd_CTYPE_B + = (const uint16_t *) _nl_C_LC_CTYPE_class + 128; +__thread const int32_t * __libc_tsd_CTYPE_TOLOWER + = (const int32_t *) _nl_C_LC_CTYPE_tolower + 128; +__thread const int32_t * __libc_tsd_CTYPE_TOUPPER + = (const int32_t *) _nl_C_LC_CTYPE_toupper + 128; void diff --git a/ctype/tst-ctype-tls-dlmopen.c b/ctype/tst-ctype-tls-dlmopen.c new file mode 100644 index 0000000000..f7eeb65551 --- /dev/null +++ b/ctype/tst-ctype-tls-dlmopen.c @@ -0,0 +1,2 @@ +#define DO_STATIC_TEST 0 +#include "tst-ctype-tls-skeleton.c" diff --git a/ctype/tst-ctype-tls-dlopen-static.c b/ctype/tst-ctype-tls-dlopen-static.c new file mode 100644 index 0000000000..c2c09c362c --- /dev/null +++ b/ctype/tst-ctype-tls-dlopen-static.c @@ -0,0 +1,2 @@ +#define DO_STATIC_TEST 1 +#include "tst-ctype-tls-skeleton.c" diff --git a/ctype/tst-ctype-tls-mod.c b/ctype/tst-ctype-tls-mod.c new file mode 100644 index 0000000000..dba689701f --- /dev/null +++ b/ctype/tst-ctype-tls-mod.c @@ -0,0 +1,37 @@ +/* Wrappers for macros in a secondary namespace. + Copyright (C) 2024 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 + +int +my_isalpha (int ch) +{ + return isalpha (ch); +} + +int +my_toupper (int ch) +{ + return toupper (ch); +} + +int +my_tolower (int ch) +{ + return tolower (ch); +} diff --git a/ctype/tst-ctype-tls-skeleton.c b/ctype/tst-ctype-tls-skeleton.c new file mode 100644 index 0000000000..421c984f70 --- /dev/null +++ b/ctype/tst-ctype-tls-skeleton.c @@ -0,0 +1,67 @@ +/* Test that in a secondary namespace works. + Copyright (C) 2024 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 + . */ + +/* Before this file is included, define DO_STATIC_TEST to 0 or 1. + With 0, dlmopen is used for the test. With 1, dlopen is used. */ + +#include +#include +#include +#include +#include +#include + +static int (*my_isalpha) (int); +static int (*my_toupper) (int); +static int (*my_tolower) (int); + +static void * +checks (void *ignore) +{ + TEST_VERIFY (my_isalpha ('a')); + TEST_VERIFY (!my_isalpha ('0')); + TEST_COMPARE (my_toupper ('a'), 'A'); + TEST_COMPARE (my_toupper ('A'), 'A'); + TEST_COMPARE (my_tolower ('a'), 'a'); + TEST_COMPARE (my_tolower ('A'), 'a'); + return NULL; +} + +static int +do_test (void) +{ + char *dso = xasprintf ("%s/ctype/tst-ctype-tls-mod.so", support_objdir_root); +#if DO_STATIC_TEST + void *handle = xdlopen (dso, RTLD_LAZY); +#else + void *handle = xdlmopen (LM_ID_NEWLM, dso, RTLD_LAZY); +#endif + my_isalpha = xdlsym (handle, "my_isalpha"); + my_toupper = xdlsym (handle, "my_toupper"); + my_tolower = xdlsym (handle, "my_tolower"); + + checks (NULL); + xpthread_join (xpthread_create (NULL, checks, NULL)); + + xdlclose (handle); + free (dso); + + return 0; +} + +#include