From patchwork Wed Jul 17 12:15:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Thibault X-Patchwork-Id: 94067 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 6128D385842C for ; Wed, 17 Jul 2024 12:15:47 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from sonata.ens-lyon.org (sonata.ens-lyon.org [140.77.166.138]) by sourceware.org (Postfix) with ESMTPS id 3DDCE3858D39 for ; Wed, 17 Jul 2024 12:15:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3DDCE3858D39 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=ens-lyon.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=bounce.ens-lyon.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 3DDCE3858D39 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=140.77.166.138 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721218528; cv=none; b=KLRmfIsbpKAPlucoSq8xG3i8NcCgFzEcrj3yOCJJMY12fGZJaWe17j/4YUa90qFSLAUJgccOGWVo1Q/uWizt6F4/cHmZY6FdpNsZc74FlkT1Lrm4XboxmUCo6wk5TdlrWyYlyUYJSJ8NS3HRpM/Y03umtWgSLxSRGhIa6UeY/2A= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721218528; c=relaxed/simple; bh=gTeU7tn/sHHKfH7rAVyU8P7nGDEZwQbghso/lKE2QS8=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=I5PPcEhJqzG2mroAIW4d/yfdl5d5v/7mNmLZ5qw/v4ZIiEhK26YVyMYrTUVxW92Jv5ziEbyFLNTJSGoqb1enCC2wxC8nLH+UIv7BFx2dkayhZY+sdotUHWISKryRFax95x8wU0yAQVU0mlmSmYAN0UIlQMGi0ZGJA+UBlxziUwI= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from localhost (localhost [127.0.0.1]) by sonata.ens-lyon.org (Postfix) with ESMTP id B1853A03B9; Wed, 17 Jul 2024 14:15:24 +0200 (CEST) Received: from sonata.ens-lyon.org ([127.0.0.1]) by localhost (sonata.ens-lyon.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id c7SQfy9XTl_o; Wed, 17 Jul 2024 14:15:24 +0200 (CEST) Received: from begin (nat-inria-interne-52-gw-01-bso.bordeaux.inria.fr [194.199.1.52]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by sonata.ens-lyon.org (Postfix) with ESMTPSA id 681D1A03B8; Wed, 17 Jul 2024 14:15:24 +0200 (CEST) Received: from samy by begin with local (Exim 4.98-RC3) (envelope-from ) id 1sU3Z5-0000000F2sB-2TKK; Wed, 17 Jul 2024 14:15:23 +0200 From: Samuel Thibault To: libc-alpha@sourceware.org Cc: Samuel Thibault , commit-hurd@gnu.org Subject: [hurd,commited] hurd: Fix pthread_self() without libpthread Date: Wed, 17 Jul 2024 14:15:21 +0200 Message-ID: <20240717121521.3585953-1-samuel.thibault@ens-lyon.org> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_STATUS, KAM_SHORT, SPF_HELO_PASS, SPF_PASS, 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 5476f8cd2e68 ("htl: move pthread_self info libc.") moved the htl pthread_self() function from libpthread to libc, replacing the previous libc stub that just returns 0. And 53da64d1cf36 ("htl: Initialize ___pthread_self early") added initialization code which is needed before being able to call pthread_self. It is currently in libpthread, and thus never called before programs can call pthread_self from libc, which then segfaults when accessing _pthread_self()->thread. This moves the initialization to libc itself, as initialized variables, so pthread_self can always be called fine. --- htl/Versions | 1 + sysdeps/mach/hurd/htl/pt-pthread_self.c | 9 ++++++++- sysdeps/mach/hurd/htl/pt-sysdep.c | 12 ++---------- sysdeps/mach/hurd/htl/pt-sysdep.h | 2 ++ 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/htl/Versions b/htl/Versions index e1524117a0..69b79b4f1b 100644 --- a/htl/Versions +++ b/htl/Versions @@ -38,6 +38,7 @@ libc { __pthread_cleanup_stack; __pthread_total; ___pthread_self; + __pthread_init_thread; } } diff --git a/sysdeps/mach/hurd/htl/pt-pthread_self.c b/sysdeps/mach/hurd/htl/pt-pthread_self.c index 04fa283aab..08ef2e4956 100644 --- a/sysdeps/mach/hurd/htl/pt-pthread_self.c +++ b/sysdeps/mach/hurd/htl/pt-pthread_self.c @@ -17,6 +17,13 @@ . */ #include +#include -__thread struct __pthread *___pthread_self; +/* Initial thread structure used before libpthread is initialized, so various + * functions can already work at least basically. */ +struct __pthread __pthread_init_thread = { + .thread = 1, +}; + +__thread struct __pthread *___pthread_self = &__pthread_init_thread; libc_hidden_tls_def (___pthread_self) diff --git a/sysdeps/mach/hurd/htl/pt-sysdep.c b/sysdeps/mach/hurd/htl/pt-sysdep.c index 5372cbf77f..f5c1c246eb 100644 --- a/sysdeps/mach/hurd/htl/pt-sysdep.c +++ b/sysdeps/mach/hurd/htl/pt-sysdep.c @@ -26,10 +26,6 @@ #include #include -/* Initial thread structure used temporarily during initialization, so various - * functions can already work at least basically. */ -static struct __pthread init_thread; - static void reset_pthread_total (void) { @@ -51,10 +47,6 @@ _init_routine (void *stack) /* Already initialized */ return; - /* Initialize early thread structure. */ - init_thread.thread = 1; - ___pthread_self = &init_thread; - /* Initialize the library. */ ___pthread_init (); @@ -116,9 +108,9 @@ _init_routine (void *stack) #endif /* Copy over the thread-specific state */ - assert (!init_thread.thread_specifics); + assert (!__pthread_init_thread.thread_specifics); memcpy (&thread->static_thread_specifics, - &init_thread.static_thread_specifics, + &__pthread_init_thread.static_thread_specifics, sizeof (thread->static_thread_specifics)); ___pthread_self = thread; diff --git a/sysdeps/mach/hurd/htl/pt-sysdep.h b/sysdeps/mach/hurd/htl/pt-sysdep.h index 09d9547a8e..ef7f509611 100644 --- a/sysdeps/mach/hurd/htl/pt-sysdep.h +++ b/sysdeps/mach/hurd/htl/pt-sysdep.h @@ -34,6 +34,8 @@ extern __thread struct __pthread *___pthread_self; libc_hidden_tls_proto (___pthread_self) +extern struct __pthread ___pthread_init_thread; +libc_hidden_proto (___pthread_init_thread) #ifdef DEBUG #define _pthread_self() \