From patchwork Tue Oct 1 12:14:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 1991447 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=HvkX1Tsq; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4XHxj94nSkz1xtY for ; Tue, 1 Oct 2024 22:15:25 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 79462386F464 for ; Tue, 1 Oct 2024 12:15:23 +0000 (GMT) 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.133.124]) by sourceware.org (Postfix) with ESMTP id 3FB9F386D60B for ; Tue, 1 Oct 2024 12:14:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3FB9F386D60B 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 3FB9F386D60B Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727784899; cv=none; b=UGpauFG6fU++s10O9n3VlQTkaJLGxUEYiYyUeuYaQBkfGYhK6kYxQSjs8/Hbm0v4hzZs87iiJO0u80hSnVxKHtPALFoIHzOfeWt0Wi5Kd84fyEtQ8mzjB3FaAIew7Kruh2d1SHFREqqmOIIr/IXxCsg8yZNwJk8Wu4iJDGVzx7A= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727784899; c=relaxed/simple; bh=LZWftjp30i5unpc2jGd03sikZuHquHYMmB/DSEKTwyc=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=OAbjfIvxyHVCJjAzF4XnPoNCYcygpYvUcxPp/4US5SbuUcGkX3D+NYYFVq0yxuh2wnUpwfYaDYXr37p22JrUhYOqClbABnML0EUNsIJQr0wczGon9yS/d1SyQ3MMKvTdV/PczfhLtiKUCa9Gu/JgwFppDNhpwh8Zz3zF57KtiHs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1727784896; 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=UuFkKTfJYMW3UkTiDtzbOw7UkH6blNh/WPKXRRlPpV0=; b=HvkX1TsqQF6KDOoYpu9jZ9BbriWFD8LG+DL3VjhJyfGHWHCv9gXqBHeSytvbJx8f1dob+f oSHZvFdeoEgdPkKh+ITlpTqJT0u3gJtS7g+zFthBT4hjH7TyQLXEKsxPU3sOQKcoV8QSMz 0OCeFKSdWo9dstGWJvcjS3T8R56/Hb4= Received: from mx-prod-mc-04.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-214-Y6Uuwi7tMVWwuoygmO9FjA-1; Tue, 01 Oct 2024 08:14:54 -0400 X-MC-Unique: Y6Uuwi7tMVWwuoygmO9FjA-1 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (unknown [10.30.177.15]) (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-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id BCDE0196D199 for ; Tue, 1 Oct 2024 12:14:53 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.45.224.151]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id CA1F71979066 for ; Tue, 1 Oct 2024 12:14:52 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH 2/4] time: Push tzset lock into callers of time functions In-Reply-To: Message-ID: <1bd8c60cfeecff73b7183c7dc3699f55c71c629c.1727784647.git.fweimer@redhat.com> References: X-From-Line: 1bd8c60cfeecff73b7183c7dc3699f55c71c629c Mon Sep 17 00:00:00 2001 Date: Tue, 01 Oct 2024 14:14:49 +0200 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-22.9 required=5.0 tests=BAYES_00, DKIM_INVALID, DKIM_SIGNED, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, 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~incoming=patchwork.ozlabs.org@sourceware.org This fixes a harmless data race on the cached offset used by __mktime_internal. --- include/time.h | 23 ----------------------- time/gmtime.c | 8 ++++++-- time/localtime.c | 8 ++++++-- time/mktime-internal.h | 4 +++- time/mktime.c | 36 +++++++++++++++++++++++++++++------- time/strftime_l.c | 1 + time/timegm.c | 11 +++++++++++ time/tzfile.c | 1 + time/tzset.c | 20 ++++++++++++-------- time/tzset.h | 34 ++++++++++++++++++++++++++++++++++ 10 files changed, 103 insertions(+), 43 deletions(-) create mode 100644 time/tzset.h diff --git a/include/time.h b/include/time.h index f599eeed4e..e9c083646a 100644 --- a/include/time.h +++ b/include/time.h @@ -49,24 +49,6 @@ extern const unsigned short int __mon_yday[2][13] attribute_hidden; /* Defined in localtime.c. */ extern struct tm _tmbuf attribute_hidden; -/* Defined in tzset.c. */ -extern char *__tzstring (const char *string) attribute_hidden; - -extern int __use_tzfile attribute_hidden; - -extern void __tzfile_read (const char *file, size_t extra, - char **extrap) attribute_hidden; -extern void __tzfile_compute (__time64_t timer, int use_localtime, - long int *leap_correct, int *leap_hit, - struct tm *tp) attribute_hidden; -extern void __tzfile_default (const char *std, const char *dst, - int stdoff, int dstoff) - attribute_hidden; -extern void __tzset_parse_tz (const char *tz) attribute_hidden; -extern void __tz_compute (__time64_t timer, struct tm *tm, int use_localtime) - __THROW attribute_hidden; - - #if __TIMESIZE == 64 # define __itimerspec64 itimerspec #else @@ -275,11 +257,6 @@ extern int __offtime (__time64_t __timer, extern char *__asctime_r (const struct tm *__tp, char *__buf) attribute_hidden; -extern void __tzset (void) attribute_hidden; - -/* Prototype for the internal function to get information based on TZ. */ -extern struct tm *__tz_convert (__time64_t timer, int use_localtime, - struct tm *tp) attribute_hidden; extern int __nanosleep (const struct timespec *__requested_time, struct timespec *__remaining); diff --git a/time/gmtime.c b/time/gmtime.c index 9f39c36bd8..ad8fc48966 100644 --- a/time/gmtime.c +++ b/time/gmtime.c @@ -17,13 +17,17 @@ . */ #include +#include /* Return the `struct tm' representation of *T in UTC, using *TP to store the result. */ struct tm * __gmtime64_r (const __time64_t *t, struct tm *tp) { - return __tz_convert (*t, 0, tp); + __libc_lock_lock (__tzset_lock); + struct tm *result = __tz_convert (*t, 0, tp); + __libc_lock_unlock (__tzset_lock); + return result; } /* Provide a 32-bit variant if needed. */ @@ -48,7 +52,7 @@ weak_alias (__gmtime_r, gmtime_r) struct tm * __gmtime64 (const __time64_t *t) { - return __tz_convert (*t, 0, &_tmbuf); + return __gmtime64_r (t, &_tmbuf); } /* Provide a 32-bit variant if needed. */ diff --git a/time/localtime.c b/time/localtime.c index 77d19ff5e7..f19d552673 100644 --- a/time/localtime.c +++ b/time/localtime.c @@ -17,6 +17,7 @@ . */ #include +#include /* C89 says that localtime and gmtime return the same pointer. Although C99 and later relax this to let localtime and gmtime @@ -30,7 +31,10 @@ struct tm _tmbuf; struct tm * __localtime64_r (const __time64_t *t, struct tm *tp) { - return __tz_convert (*t, 1, tp); + __libc_lock_lock (__tzset_lock); + struct tm *result = __tz_convert (*t, 1, tp); + __libc_lock_unlock (__tzset_lock); + return result; } /* Provide a 32-bit variant if needed. */ @@ -54,7 +58,7 @@ weak_alias (__localtime_r, localtime_r) struct tm * __localtime64 (const __time64_t *t) { - return __tz_convert (*t, 1, &_tmbuf); + return __localtime64_r (t, &_tmbuf); } libc_hidden_def (__localtime64) diff --git a/time/mktime-internal.h b/time/mktime-internal.h index 3e2848c121..93299effc3 100644 --- a/time/mktime-internal.h +++ b/time/mktime-internal.h @@ -73,6 +73,8 @@ typedef int mktime_offset_t; /* Subroutine of mktime. Return the time_t representation of TP and normalize TP, given that a struct tm * maps to a time_t. If LOCAL, the mapping is performed by localtime_r, otherwise by gmtime_r. - Record next guess for localtime-gmtime offset in *OFFSET. */ + Record next guess for localtime-gmtime offset in *OFFSET. + + If _LIBC, the caller must lock __tzset_lock. */ extern __time64_t __mktime_internal (struct tm *tp, bool local, mktime_offset_t *offset) attribute_hidden; diff --git a/time/mktime.c b/time/mktime.c index 8196faa49c..669bc46c34 100644 --- a/time/mktime.c +++ b/time/mktime.c @@ -42,6 +42,9 @@ #endif #include +#ifdef _LIBC +# include +#endif #include #include @@ -256,11 +259,15 @@ tm_diff (long_int year, long_int yday, int hour, int min, int sec, static struct tm * convert_time (bool local, long_int t, struct tm *tm) { +#ifdef _LIBC + return __tz_convert (t, local, tm); +#else __time64_t x = t; if (local) return __localtime64_r (&x, tm); else return __gmtime64_r (&x, tm); +#endif } /* Convert *T to a broken down time in *TP (as if by localtime if @@ -318,7 +325,9 @@ ranged_convert (bool local, long_int *t, struct tm *tp) If *OFFSET's guess is correct, only one reverse mapping call is needed. If successful, set *TP to the canonicalized struct tm; otherwise leave *TP alone, return ((time_t) -1) and set errno. - This function is external because it is used also by timegm.c. */ + This function is external because it is used also by timegm.c. + + If _LIBC, the caller must lock __tzset_lock. */ __time64_t __mktime_internal (struct tm *tp, bool local, mktime_offset_t *offset) { @@ -530,7 +539,19 @@ __mktime_internal (struct tm *tp, bool local, mktime_offset_t *offset) #endif /* _LIBC || NEED_MKTIME_WORKING || NEED_MKTIME_INTERNAL */ -#if defined _LIBC || NEED_MKTIME_WORKING || NEED_MKTIME_WINDOWS +#ifdef _LIBC +__time64_t +__mktime64 (struct tm *tp) +{ + __libc_lock_lock (__tzset_lock); + __tzset_unlocked (); + static mktime_offset_t localtime_offset; + __time64_t result = __mktime_internal (tp, true, &localtime_offset); + __libc_lock_unlock (__tzset_lock); + return result; +} +#else /* !_LIBC */ +# if defined NEED_MKTIME_WORKING || NEED_MKTIME_WINDOWS /* Convert *TP to a __time64_t value. */ __time64_t @@ -541,15 +562,16 @@ __mktime64 (struct tm *tp) be set as if the tzset() function had been called. */ __tzset (); -# if defined _LIBC || NEED_MKTIME_WORKING +# ifdef NEED_MKTIME_WORKING static mktime_offset_t localtime_offset; return __mktime_internal (tp, true, &localtime_offset); -# else -# undef mktime +# else +# undef mktime return mktime (tp); -# endif +# endif } -#endif /* _LIBC || NEED_MKTIME_WORKING || NEED_MKTIME_WINDOWS */ +# endif /* NEED_MKTIME_WORKING || NEED_MKTIME_WINDOWS */ +#endif /* !_LIBC */ #if defined _LIBC && __TIMESIZE != 64 diff --git a/time/strftime_l.c b/time/strftime_l.c index 77adec9050..e5c5d5fc60 100644 --- a/time/strftime_l.c +++ b/time/strftime_l.c @@ -33,6 +33,7 @@ # define MULTIBYTE_IS_FORMAT_SAFE 1 # define STDC_HEADERS 1 # include "../locale/localeinfo.h" +# include #endif #if defined emacs && !defined HAVE_BCOPY diff --git a/time/timegm.c b/time/timegm.c index 1f1f66c61b..4dda728ae3 100644 --- a/time/timegm.c +++ b/time/timegm.c @@ -24,6 +24,10 @@ #include #include +#ifdef _LIBC +# include +#endif + #include "mktime-internal.h" __time64_t @@ -31,7 +35,14 @@ __timegm64 (struct tm *tmp) { static mktime_offset_t gmtime_offset; tmp->tm_isdst = 0; +#ifdef _LIBC + __libc_lock_lock (__tzset_lock); + __time64_t result = __mktime_internal (tmp, false, &gmtime_offset); + __libc_lock_unlock (__tzset_lock); + return result; +#else return __mktime_internal (tmp, false, &gmtime_offset); +#endif } #if defined _LIBC && __TIMESIZE != 64 diff --git a/time/tzfile.c b/time/tzfile.c index 964a9b7f12..5d4a6054df 100644 --- a/time/tzfile.c +++ b/time/tzfile.c @@ -27,6 +27,7 @@ #include #include #include +#include #include diff --git a/time/tzset.c b/time/tzset.c index 98a7064970..b0a0bcf68f 100644 --- a/time/tzset.c +++ b/time/tzset.c @@ -16,13 +16,13 @@ . */ #include -#include #include #include #include #include #include #include +#include #include @@ -37,7 +37,7 @@ weak_alias (__daylight, daylight) weak_alias (__timezone, timezone) /* This locks all the state variables in tzfile.c and this file. */ -__libc_lock_define_initialized (static, tzset_lock) +__libc_lock_define_initialized (, __tzset_lock) /* This structure contains all the information about a timezone given in the POSIX standard TZ envariable. */ @@ -544,9 +544,8 @@ __tz_compute (__time64_t timer, struct tm *tm, int use_localtime) #undef tzset void -__tzset (void) +__tzset_unlocked (void) { - __libc_lock_lock (tzset_lock); tzset_internal (1); @@ -557,8 +556,16 @@ __tzset (void) __tzname[1] = (char *) tz_rules[1].name; } - __libc_lock_unlock (tzset_lock); } + +void +__tzset (void) +{ + __libc_lock_lock (__tzset_lock); + __tzset_unlocked (); + __libc_lock_unlock (__tzset_lock); +} + weak_alias (__tzset, tzset) /* Return the `struct tm' representation of TIMER in the local timezone. @@ -569,7 +576,6 @@ __tz_convert (__time64_t timer, int use_localtime, struct tm *tp) long int leap_correction; int leap_extra_secs; - __libc_lock_lock (tzset_lock); /* Update internal database according to current TZ setting. POSIX.1 8.3.7.2 says that localtime_r is not required to set tzname. @@ -589,8 +595,6 @@ __tz_convert (__time64_t timer, int use_localtime, struct tm *tp) leap_extra_secs = 0; } - __libc_lock_unlock (tzset_lock); - if (tp) { if (! use_localtime) diff --git a/time/tzset.h b/time/tzset.h new file mode 100644 index 0000000000..254540aa05 --- /dev/null +++ b/time/tzset.h @@ -0,0 +1,34 @@ +#ifndef _TZSET_H +#define _TZSET_H + +#include +#include + +/* Defined in tzset.c. */ +extern char *__tzstring (const char *string) attribute_hidden; + +extern int __use_tzfile attribute_hidden; + +extern void __tzfile_read (const char *file, size_t extra, + char **extrap) attribute_hidden; +extern void __tzfile_compute (__time64_t timer, int use_localtime, + long int *leap_correct, int *leap_hit, + struct tm *tp) attribute_hidden; +extern void __tzfile_default (const char *std, const char *dst, + int stdoff, int dstoff) + attribute_hidden; +extern void __tzset_parse_tz (const char *tz) attribute_hidden; +extern void __tz_compute (__time64_t timer, struct tm *tm, int use_localtime) + __THROW attribute_hidden; + +__libc_lock_define (extern, __tzset_lock attribute_hidden); + +extern void __tzset (void) attribute_hidden; +extern void __tzset_unlocked (void) attribute_hidden; + +/* Prototype for the internal function to get information based on TZ. + Caller needs to lock __tzset_lock. */ +extern struct tm *__tz_convert (__time64_t timer, int use_localtime, + struct tm *tp) attribute_hidden; + +#endif /* _TZSET_H */