From patchwork Sat May 4 15:41:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicolas Boulenguez X-Patchwork-Id: 1931370 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.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 4VWsPH3D90z1xnT for ; Sun, 5 May 2024 01:42:26 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E944E3858C53 for ; Sat, 4 May 2024 15:42:23 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp4-g21.free.fr (smtp4-g21.free.fr [212.27.42.4]) by sourceware.org (Postfix) with ESMTPS id 0C15A3858C53 for ; Sat, 4 May 2024 15:41:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0C15A3858C53 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=debian.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=free.fr ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 0C15A3858C53 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=212.27.42.4 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714837283; cv=none; b=YkUHO4cc0Mynk8yvfodKUeJwkOQPxskX677byoS2Ji+Dnpexp2UqvZgRE4s2qOMHx7RG/H7Ln2SPW8XhN9jxQdmKf4MuwWsQ6i8zB2U0nQIlS3HTx4IvmZGCOi5IztlatngFOEEQ0sXxddiWRO1uhRRgNPo3i9xJK+BPkI8WAk4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1714837283; c=relaxed/simple; bh=yZ7yWCub+G5hHz0Il/eajtM6qc+sOYZ3rTTT9lPtYUA=; h=Date:From:To:Subject:Message-ID:MIME-Version; b=MPI/8qSg8ep2ZqWIRRFL2dc+EVMwhTcxKsOpj8DGzwECC7ffxoM09+tU+YKoQAZ/z+N31AmzjNq27VRaMMoKfCC5oknHPcKeH6m2SAgLMucLlJwQEm19RQ4MxpNL+kgtishNAgYwIjxh0s88TWuKv0rixwEuRmvJjwsVyPX+l1Y= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from nicolasthinkpad.home (unknown [IPv6:2a01:e0a:91b:e980:f165:10f:69f1:8c00]) by smtp4-g21.free.fr (Postfix) with ESMTPS id 4520019F73F for ; Sat, 4 May 2024 17:41:08 +0200 (CEST) Received: from nicolas by nicolasthinkpad.home with local (Exim 4.96) (envelope-from ) id 1s3HVb-0003h1-2N for gcc-patches@gcc.gnu.org; Sat, 04 May 2024 17:41:07 +0200 Date: Sat, 4 May 2024 17:41:07 +0200 From: Nicolas Boulenguez To: gcc-patches@gcc.gnu.org Subject: [PATCH v8] ada: fix timeval timespec on 32 bits archs with 64 bits time_t [PR114065] Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-8.1 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, JMQ_SPF_NEUTRAL, KAM_ASCII_DIVIDERS, KAM_DMARC_STATUS, KAM_LOTSOFHASH, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TO_NO_BRKTS_PCNT, 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: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org PR ada/114065 Ada.Calendar.Clock returns nonsense on 32 bits architectures with 64 bits time_t, because gettimeofday is then a preprocessor macro for __gettimeofday64, so linking from Ada with "gettimeofday" gets the wrong version. On such architectures and also on x32, some Ada declarations for the time{val,spec} C structs are wrong (for example timespec.tv_nsec is a not a long int on x32). This also results in wrong results for Clock. The attached commits * merge or remove all declarations for timeval and timespec * calls the 64 bit functions when __USE_TIME_BITS64 * fixes various overflows with 32 bits Duration detected during tests * removes a lot of duplicated code The bug attachements (v7) contains a python script testing s-c_time.adb for each combination of type sizes. The whole patch queue, applied on gcc-13, builds on all Debian architectures and fixes the original issue on arm{el,hf}. From 0f2eba5fd9b6c5a0cd67f5cf6a13e5650ebe4514 Mon Sep 17 00:00:00 2001 From: Nicolas Boulenguez Date: Fri, 26 Apr 2024 20:08:21 +0200 Subject: [PATCH v8 9/9] Ada: select 64 bits time functions from GNU libc when __USE_TIME_BITS64 [PR114065] else Ada.Calendar returns random dates on system affected by the Y2038 Glibc transition (pr114065). PR ada/114065 Signed-off-by: Nicolas Boulenguez --- gcc/ada/libgnarl/a-exetim__posix.adb | 5 ++++- gcc/ada/libgnarl/s-osinte__gnu.ads | 14 ++++++++++---- gcc/ada/libgnarl/s-osinte__kfreebsd-gnu.ads | 14 ++++++++++---- gcc/ada/libgnarl/s-osinte__linux.ads | 10 +++++++--- gcc/ada/libgnat/g-spogwa.adb | 4 +++- gcc/ada/libgnat/s-optide.adb | 3 ++- gcc/ada/libgnat/s-osprim__posix.adb | 4 +++- gcc/ada/libgnat/s-osprim__posix2008.adb | 3 ++- gcc/ada/s-oscons-tmplt.c | 16 ++++++++++++++++ 9 files changed, 57 insertions(+), 16 deletions(-) diff --git a/gcc/ada/libgnarl/a-exetim__posix.adb b/gcc/ada/libgnarl/a-exetim__posix.adb index db873fb..7fbd6c1 100644 --- a/gcc/ada/libgnarl/a-exetim__posix.adb +++ b/gcc/ada/libgnarl/a-exetim__posix.adb @@ -35,6 +35,7 @@ with Ada.Task_Identification; use Ada.Task_Identification; with Ada.Unchecked_Conversion; with System.C_Time; +with System.OS_Constants; with System.Tasking; with System.OS_Interface; use System.OS_Interface; with System.Task_Primitives.Operations; use System.Task_Primitives.Operations; @@ -115,7 +116,9 @@ package body Ada.Execution_Time is (clock_id : Interfaces.C.int; tp : access System.C_Time.timespec) return int; - pragma Import (C, clock_gettime, "clock_gettime"); + pragma Import (C, clock_gettime, + (if System.OS_Constants.Glibc_Use_Time_Bits64 + then "__clock_gettime64" else "clock_gettime")); -- Function from the POSIX.1b Realtime Extensions library function pthread_getcpuclockid diff --git a/gcc/ada/libgnarl/s-osinte__gnu.ads b/gcc/ada/libgnarl/s-osinte__gnu.ads index b4597c5..90ef2d5 100644 --- a/gcc/ada/libgnarl/s-osinte__gnu.ads +++ b/gcc/ada/libgnarl/s-osinte__gnu.ads @@ -39,6 +39,7 @@ -- Preelaborate. This package is designed to be a bottom-level (leaf) package with Interfaces.C; +with System.Os_Constants; with System.C_Time; with Ada.Unchecked_Conversion; @@ -208,7 +209,8 @@ package System.OS_Interface is -- Indicates whether time slicing is supported (i.e SCHED_RR is supported) function nanosleep (rqtp, rmtp : access C_Time.timespec) return int; - pragma Import (C, nanosleep, "nanosleep"); + pragma Import (C, nanosleep, (if OS_Constants.Glibc_Use_Time_Bits64 + then "__nanosleep64" else "nanosleep")); type clockid_t is new int; CLOCK_REALTIME : constant clockid_t := 0; @@ -218,12 +220,14 @@ package System.OS_Interface is (clock_id : clockid_t; tp : access C_Time.timespec) return int; - pragma Import (C, clock_gettime, "clock_gettime"); + pragma Import (C, clock_gettime, (if OS_Constants.Glibc_Use_Time_Bits64 + then "__clock_gettime64" else "clock_gettime")); function clock_getres (clock_id : clockid_t; res : access C_Time.timespec) return int; - pragma Import (C, clock_getres, "clock_getres"); + pragma Import (C, clock_getres, (if OS_Constants.Glibc_Use_Time_Bits64 + then "__clock_getres64" else "clock_getres")); -- From: /usr/include/unistd.h function sysconf (name : int) return long; @@ -477,7 +481,9 @@ package System.OS_Interface is (cond : access pthread_cond_t; mutex : access pthread_mutex_t; abstime : access C_Time.timespec) return int; - pragma Import (C, pthread_cond_timedwait, "pthread_cond_timedwait"); + pragma Import (C, pthread_cond_timedwait, + (if OS_Constants.Glibc_Use_Time_Bits64 then "__pthread_cond_timedwait64" + else "pthread_cond_timedwait")); Relative_Timed_Wait : constant Boolean := False; -- pthread_cond_timedwait requires an absolute delay time diff --git a/gcc/ada/libgnarl/s-osinte__kfreebsd-gnu.ads b/gcc/ada/libgnarl/s-osinte__kfreebsd-gnu.ads index 9b40c9a..59e6f79 100644 --- a/gcc/ada/libgnarl/s-osinte__kfreebsd-gnu.ads +++ b/gcc/ada/libgnarl/s-osinte__kfreebsd-gnu.ads @@ -40,6 +40,7 @@ with Ada.Unchecked_Conversion; with Interfaces.C; +with System.OS_Constants; with System.C_Time; package System.OS_Interface is @@ -203,7 +204,8 @@ package System.OS_Interface is -- Indicates whether time slicing is supported (i.e SCHED_RR is supported) function nanosleep (rqtp, rmtp : access C_Time.timespec) return int; - pragma Import (C, nanosleep, "nanosleep"); + pragma Import (C, nanosleep, (if OS_Constants.Glibc_Use_Time_Bits64 + then "__nanosleep64" else "nanosleep")); type clockid_t is new int; CLOCK_REALTIME : constant clockid_t := 0; @@ -212,12 +214,14 @@ package System.OS_Interface is (clock_id : clockid_t; tp : access C_Time.timespec) return int; - pragma Import (C, clock_gettime, "clock_gettime"); + pragma Import (C, clock_gettime, (if OS_Constants.Glibc_Use_Time_Bits64 + then "__clock_gettime64" else "clock_gettime"); function clock_getres (clock_id : clockid_t; res : access C_Time.timespec) return int; - pragma Import (C, clock_getres, "clock_getres"); + pragma Import (C, clock_getres, (if OS_Constants.Glibc_Use_Time_Bits64 + then "__clock_getres64" else "clock_getres"); function sysconf (name : int) return long; pragma Import (C, sysconf); @@ -420,7 +424,9 @@ package System.OS_Interface is (cond : access pthread_cond_t; mutex : access pthread_mutex_t; abstime : access C_Time.timespec) return int; - pragma Import (C, pthread_cond_timedwait, "pthread_cond_timedwait"); + pragma Import (C, pthread_cond_timedwait, + (if OS_Constants.Glibc_Use_Time_Bits64 then "__pthread_cond_timedwait64" + else "pthread_cond_timedwait"); -------------------------- -- POSIX.1c Section 13 -- diff --git a/gcc/ada/libgnarl/s-osinte__linux.ads b/gcc/ada/libgnarl/s-osinte__linux.ads index 9de227e..7a0d37d 100644 --- a/gcc/ada/libgnarl/s-osinte__linux.ads +++ b/gcc/ada/libgnarl/s-osinte__linux.ads @@ -229,12 +229,14 @@ package System.OS_Interface is function clock_gettime (clock_id : clockid_t; tp : access C_Time.timespec) return int; - pragma Import (C, clock_gettime, "clock_gettime"); + pragma Import (C, clock_gettime, (if OS_Constants.Glibc_Use_Time_Bits64 + then "__clock_gettime64" else "clock_gettime")); function clock_getres (clock_id : clockid_t; res : access C_Time.timespec) return int; - pragma Import (C, clock_getres, "clock_getres"); + pragma Import (C, clock_getres, (if OS_Constants.Glibc_Use_Time_Bits64 + then "__clock_getres64" else "clock_getres")); function sysconf (name : int) return long; pragma Import (C, sysconf); @@ -445,7 +447,9 @@ package System.OS_Interface is (cond : access pthread_cond_t; mutex : access pthread_mutex_t; abstime : access C_Time.timespec) return int; - pragma Import (C, pthread_cond_timedwait, "pthread_cond_timedwait"); + pragma Import (C, pthread_cond_timedwait, + (if OS_Constants.Glibc_Use_Time_Bits64 then "__pthread_cond_timedwait64" + else "pthread_cond_timedwait")); -------------------------- -- POSIX.1c Section 13 -- diff --git a/gcc/ada/libgnat/g-spogwa.adb b/gcc/ada/libgnat/g-spogwa.adb index 530a6cd..598f3fc 100644 --- a/gcc/ada/libgnat/g-spogwa.adb +++ b/gcc/ada/libgnat/g-spogwa.adb @@ -42,7 +42,9 @@ is writefds : access FD_Set_Type; exceptfds : access FD_Set_Type; timeout : access System.C_Time.timeval) return Integer - with Import => True, Convention => Stdcall, External_Name => "select"; + with Import => True, Convention => Stdcall, + External_Name => (if System.OS_Constants.Glibc_Use_Time_Bits64 + then "__select64" else "select"); Timeout_V : aliased System.C_Time.timeval; Timeout_A : access System.C_Time.timeval; diff --git a/gcc/ada/libgnat/s-optide.adb b/gcc/ada/libgnat/s-optide.adb index 4ad16a2..a9025d0 100644 --- a/gcc/ada/libgnat/s-optide.adb +++ b/gcc/ada/libgnat/s-optide.adb @@ -41,7 +41,8 @@ is return Integer with Import, Convention => C, - External_Name => "nanosleep"; + External_Name => (if OS_Constants.Glibc_Use_Time_Bits64 + then "__nanosleep64" else "nanosleep"); Request : aliased C_Time.timespec; Remaind : aliased C_Time.timespec; diff --git a/gcc/ada/libgnat/s-osprim__posix.adb b/gcc/ada/libgnat/s-osprim__posix.adb index b331d5c..f647968 100644 --- a/gcc/ada/libgnat/s-osprim__posix.adb +++ b/gcc/ada/libgnat/s-osprim__posix.adb @@ -31,6 +31,7 @@ -- This version is for POSIX-like operating systems with System.C_Time; +with System.OS_Constants; package body System.OS_Primitives is @@ -47,7 +48,8 @@ package body System.OS_Primitives is function gettimeofday (Tv : access C_Time.timeval; Tz : System.Address := System.Null_Address) return Integer; - pragma Import (C, gettimeofday, "gettimeofday"); + pragma Import (C, gettimeofday, (if OS_Constants.Glibc_Use_Time_Bits64 + then "__gettimeofday64" else "gettimeofday")); begin -- The return codes for gettimeofday are as follows (from man pages): diff --git a/gcc/ada/libgnat/s-osprim__posix2008.adb b/gcc/ada/libgnat/s-osprim__posix2008.adb index 7f44efa..0dbde83 100644 --- a/gcc/ada/libgnat/s-osprim__posix2008.adb +++ b/gcc/ada/libgnat/s-osprim__posix2008.adb @@ -54,7 +54,8 @@ package body System.OS_Primitives is function clock_gettime (clock_id : clockid_t; tp : access C_Time.timespec) return int; - pragma Import (C, clock_gettime, "clock_gettime"); + pragma Import (C, clock_gettime, (if OS_Constants.Glibc_Use_Time_Bits64 + thon "__clock_gettime64" else "clock_gettime")); begin Result := clock_gettime (CLOCK_REALTIME, TS'Unchecked_Access); diff --git a/gcc/ada/s-oscons-tmplt.c b/gcc/ada/s-oscons-tmplt.c index 5beb929..8551d78 100644 --- a/gcc/ada/s-oscons-tmplt.c +++ b/gcc/ada/s-oscons-tmplt.c @@ -1775,6 +1775,22 @@ CNS(MAX_tv_sec, "") CND(SIZEOF_tv_nsec, "tv_nsec, long except on x32"); } +/* + + -- Functions with time_t suseconds_t timeval timespec parameters like + -- clock_get{res,time} gettimeofday nanosleep + -- pthread_cond_{,timed}wait select + -- must be imported from the GNU C library with + -- External_Name => (if Glibc_Use_Time_Bits64 then "__foo64" else "foo") + -- The test is safe in files that do not require GNU specifically. + +*/ +#ifdef __USE_TIME_BITS64 + C("Glibc_Use_Time_Bits64", Boolean, "True", "Y2038 Glibc transition") +#else + C("Glibc_Use_Time_Bits64", Boolean, "False", "Y2038 Glibc transition") +#endif + /* -- Sizes of various data types -- 2.39.2