From patchwork Tue Sep 29 18:16:04 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 144900 X-Patchwork-Delegate: fweimer@redhat.com Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D00C04BBCDB8 for ; Tue, 29 Sep 2026 18:16:55 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D00C04BBCDB8 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=YdyILzTN X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-vs2-x0d.google.com (mail-vs2-x0d.google.com [IPv6:2a00:1450:4864:3a::d]) by sourceware.org (Postfix) with ESMTPS id 46E374BB8F60 for ; Tue, 29 Sep 2026 18:16:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 46E374BB8F60 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 46E374BB8F60 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:3a::d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1790705780; cv=none; b=oNSv9VVxxBx+FE/jkTYpb3nIqT4DaFL7xYvd5YHRNUA+bqL0Kvu3EbvdMsSNaDEA7VEwhtz543vgkaUIYuMSk47vCj2kabE6IlpxhcgAESnvpnWVTsR4wXtxuNwWXJWFg0i+quMuz/6+xuI0oNcu9KQZfS+WK0k9iN1CkSKAI6g= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1790705780; c=relaxed/simple; bh=BVE1uHPCxRdF+F+akzSsWJS0mhJ2zlNgRapPpThEuGg=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=UWqIc7hGrgiDJRBu2+VvjYgPscztalgDE2E8p8FjQoNbDmyaCb3SleYrdcEuJ8U+JVYU/+6NPKq21tksLmxKnIDywl4vdiq6cs7PQsOKxGtc8uukUPfnX0BVqMc3cE5i24msEwRlfK5XnVFqJNcJ0tBZXsLTMajGT+g4r9WqAnA= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=YdyILzTN DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 46E374BB8F60 Received: by mail-vs2-x0d.google.com with SMTP id 71dfb90a1353d-5c979c5eca8so3371104e0c.2 for ; Tue, 29 Sep 2026 11:16:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1790705780; x=1791310580; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=c3RhHzqSpe+w18mgE3dybN0vzK2byTf76qs9t1XzABY=; b=YdyILzTNJoCphYyh+1H0IWxaQIiqGXC3Jfs0uS+NxBFtw5rOu+ptAfC4v/OmPiixDv 1mz2C/9EVexp7gunTly/ZATq4TFyB8yoiF4cjwpXnJ7FGbONQNO+hV36jSSrs/A9jzsD jNeLKudlslPfeJv/UMp9okQRNaLQYMNA0aAlHDFhfPZhGUIktTKuMO9rr260CVktgh2u LXgLZbLP7iUQEdmaWTnN5VcjzoedE2+sdh2/9lwQ4vLe/b75qUKkN0Emt+xXhC0FTmWT t/RJLl5/IP9hItxwvr2uib3Ocd3eGdFwvWjyxdn7hdXLdha4PbaM/g9Oiwam548dMoQe I7Cw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790705780; x=1791310580; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=c3RhHzqSpe+w18mgE3dybN0vzK2byTf76qs9t1XzABY=; b=qN+9CqzdojO38J8RVpmuLdBMx1y1yWZO+iVcyJavUExGYteVVweaFV97/URn6qrZKz i75SrXOILfjQHQN9An35pVrWJLuwtY6kMVw2s5MpQR5d1tuoFzz4uFxbHaiCoemVAwkG xuzr5UbZlX4bHR5fpCX3kXvs0h2is/kEOc3JQIHgu1KrmzZ7nnPO7oMm8XMA9J2syuUQ y+BjBsWyDdhdvlWEHYiZkDmDTC6iN5RqxDaVvF2Q3jmBZTxAWtfValC3BNfIhm2p1Dku rX/6Dh9jUjIoF83Zw65ob8eBjQSkMioEtBO/NBGvXWflGi2nwroQlfo9l6el0tL7RZCy CD7Q== X-Gm-Message-State: AFq9FYJxbTDKvEf/xlQQfLFyxnAMRA02/yNW85vBPlxyq1HFI8aztL0k xTMDyuZrZEiCLTcX6umG6kbcpgmGA1pGuXbOMJirnVnTLVpiU9s6NrTl5Z8d/BtnUFyPO3AdFku spdzr X-Gm-Gg: AYBFou07sIBq+CgGqHV5Wl/mJCCqBaNQ4Nx2iGg41PsPfSiO3MxfQdCQS6FV6FkwdWV 4rrHLA6istF1FWu9Ge46pS7tUdP+3MAXkrfjZ2aok1pxG/XsQAC0OFPDxPIIQDYYtJKNRedUYkt Xo8jBkzgrXGARHVbRhHqCoJY/nyPBjfFTa7kCnSNusid3xRLPgdTWSGkgsyZ8ea20sNv+CJcrmW uLF1x8PtbXDLP1awk5kP8dhXRxvvgesXJW8rkCX2k/YYcusY33cSj0xUpCFRSLL66yLrz/vS08m +FaS5tZZtQT7X77nbObVfyxVFFRTpJg6/t6uBpC2p36Oh9SL3RJyQpQeM14+6xh23/IdvfGCyBE dyhetk1bE6p0FBPS44wXI7XL/NWTQlis+oneEkQUTe/LCdyatLieswdnR3jeEGHDZLPFsk+80QG 9Dq4OSP5IptlMobP8pP0UgoJK50oC+9cDI2LB3RnWj3pVGB28wQBDt3kbUvnzb6CZnJd7u95de3 QboxNYuPsWDYRzUowLXUeoDnKQ97TuPNtmqyCn6KPhexfE= X-Received: by 2002:a05:6122:887:b0:5d4:acd5:2835 with SMTP id 71dfb90a1353d-5d5195dcbcdmr96889e0c.15.1790705779482; Tue, 29 Sep 2026 11:16:19 -0700 (PDT) Received: from mandiga ([2804:1b3:a7c1:43a:4c1a:55:e961:9ce9]) by smtp.gmail.com with ESMTPSA id 71dfb90a1353d-5d4e969c5b6sm561209e0c.12.2026.09.29.11.16.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Sep 2026 11:16:18 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Matthias Goergens Subject: [PATCH v3] linux: Fix stat layout divergence under pre-POSIX.1-2008 macros [BZ #32119] Date: Tue, 29 Sep 2026 15:16:04 -0300 Message-ID: <20260929181613.767968-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.53.0 MIME-Version: 1.0 X-Spam-Status: No, score=-12.6 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_NONE, TXREP shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on 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 The struct stat layout must not depend on the feature-test macros in use. Currently there are three issues: - bits/struct_stat_time64_helper.h tests __BYTE_ORDER without including . In strict pre-POSIX.1-2008 modes both operands are undefined, the big-endian member order is selected on little-endian targets, which displaces the time64 nanosecond fields on targets whose bits/struct_stat.h does not already include (x86, mips o32/n32, microblazeel). - The same helper adds two reserved words only when POSIX.1-2008 interfaces are hidden, making the public time64 structure larger under older feature-test macros. - The generic bits/struct_stat.h declares the strict-mode timestamps as a bare seconds/nanoseconds pair with no endianness handling or explicit padding. On 32-bit ports with 64-bit time_t whose ABI aligns 64-bit types to 4 bytes, the timestamps end up packed 12 bytes apart instead of 16 (arc); on big-endian the nanoseconds also land on the wrong side of the padding (or1k). This triggers with plain -D_POSIX_C_SOURCE=200112L, without any _TIME_BITS opt-in, since time_t is always 64-bit on such ports. Fix the helper by including and removing the reserved words, restoring the pre-refactoring (4e8521333be) time64 layout; and give the generic header the same endianness-aware nanosecond padding as . All feature-test-macro modes now use the layout of the latest standard (default and POSIX.1-2008 with _TIME_BITS=64), which is also the internal __stat64_t64 layout. The stat functions never set the reserved words, so older strict-mode objects that allocate the larger structure keep working; and with the public and internal layouts identical, callback interfaces that write into libc-owned buffers (e.g. glob with GLOB_ALTDIRFUNC) stay correct. The resulting sizeof (struct stat) changes, with _TIME_BITS=64 for the first two rows (the time32 layouts are unchanged) and with any feature-test macro combination on the time64-only arc and or1k: ABI default and pre-POSIX.1-2008 POSIX.1-2008 strict modes i686, csky, m68k, microblaze, sh 108 (unchanged) 116 -> 108 arm, hppa, mips o32/n32, powerpc32, sparc32 112 (unchanged) 120 -> 112 arc, or1k 128 (unchanged) 116 -> 128 A new compile-only test checks that sizeof and the member offsets of struct stat do not vary across feature-test-macro modes. Co-authored-by: Matthias Goergens Signed-off-by: Matthias Goergens Tested-by: Matthias Goergens --- sysdeps/unix/sysv/linux/Makefile | 11 +++ sysdeps/unix/sysv/linux/bits/struct_stat.h | 36 ++++--- .../linux/bits/struct_stat_time64_helper.h | 5 +- .../unix/sysv/linux/tst-stat-layout-time64.py | 94 +++++++++++++++++++ 4 files changed, 131 insertions(+), 15 deletions(-) create mode 100644 sysdeps/unix/sysv/linux/tst-stat-layout-time64.py diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile index 2cf0e98f0d2..5707de99907 100644 --- a/sysdeps/unix/sysv/linux/Makefile +++ b/sysdeps/unix/sysv/linux/Makefile @@ -425,6 +425,17 @@ $(objpfx)tst-sched-consts.out: ../sysdeps/unix/sysv/linux/tst-sched-consts.py < /dev/null > $@ 2>&1; $(evaluate-test) $(objpfx)tst-sched-consts.out: $(sysdeps-linux-python-deps) +tests-special += \ + $(objpfx)tst-stat-layout-time64.out \ + # tests-special +$(objpfx)tst-stat-layout-time64.out: \ + ../sysdeps/unix/sysv/linux/tst-stat-layout-time64.py + $(sysdeps-linux-python) \ + ../sysdeps/unix/sysv/linux/tst-stat-layout-time64.py \ + $(sysdeps-linux-python-cc) \ + < /dev/null > $@ 2>&1; $(evaluate-test) +$(objpfx)tst-stat-layout-time64.out: $(sysdeps-linux-python-deps) + tst-rseq-disable-TUNABLES += glibc.pthread.rseq=0 tst-rseq-disable-static-TUNABLES += glibc.pthread.rseq=0 diff --git a/sysdeps/unix/sysv/linux/bits/struct_stat.h b/sysdeps/unix/sysv/linux/bits/struct_stat.h index e912c3f6ba5..cdbe1f8f762 100644 --- a/sysdeps/unix/sysv/linux/bits/struct_stat.h +++ b/sysdeps/unix/sysv/linux/bits/struct_stat.h @@ -41,6 +41,22 @@ int __##name##_pad __attribute__((__aligned__ (__alignof__ (type64)))); type name #endif +/* The pre-POSIX.1-2008 timestamp fields must match the layout of the + 'struct timespec' members used in the POSIX.1-2008 case, including the + padding required when the seconds field is wider than the word size. */ +#if __WORDSIZE == 64 \ + || (defined __SYSCALL_WORDSIZE && __SYSCALL_WORDSIZE == 64) \ + || (__TIMESIZE == 32 && !defined __USE_TIME64_REDIRECTS) +# define __fieldts(name) \ + __time_t name; unsigned long int name ## nsec +#elif __BYTE_ORDER == __BIG_ENDIAN +# define __fieldts(name) \ + __time_t name; int: 32; unsigned long int name ## nsec +#else +# define __fieldts(name) \ + __time_t name; unsigned long int name ## nsec; int: 32 +#endif + struct stat { __dev_t st_dev; /* Device. */ @@ -69,12 +85,9 @@ struct stat # define st_mtime st_mtim.tv_sec # define st_ctime st_ctim.tv_sec #else - __time_t st_atime; /* Time of last access. */ - unsigned long int st_atimensec; /* Nscecs of last access. */ - __time_t st_mtime; /* Time of last modification. */ - unsigned long int st_mtimensec; /* Nsecs of last modification. */ - __time_t st_ctime; /* Time of last status change. */ - unsigned long int st_ctimensec; /* Nsecs of last status change. */ + __fieldts (st_atime); /* Time of last access. */ + __fieldts (st_mtime); /* Time of last modification. */ + __fieldts (st_ctime); /* Time of last status change. */ #endif int __glibc_reserved[2]; }; @@ -107,17 +120,16 @@ struct stat64 struct timespec st_mtim; /* Time of last modification. */ struct timespec st_ctim; /* Time of last status change. */ #else - __time_t st_atime; /* Time of last access. */ - unsigned long int st_atimensec; /* Nscecs of last access. */ - __time_t st_mtime; /* Time of last modification. */ - unsigned long int st_mtimensec; /* Nsecs of last modification. */ - __time_t st_ctime; /* Time of last status change. */ - unsigned long int st_ctimensec; /* Nsecs of last status change. */ + __fieldts (st_atime); /* Time of last access. */ + __fieldts (st_mtime); /* Time of last modification. */ + __fieldts (st_ctime); /* Time of last status change. */ #endif int __glibc_reserved[2]; }; #endif +#undef __fieldts + /* Tell code we have these members. */ #define _STATBUF_ST_BLKSIZE #define _STATBUF_ST_RDEV diff --git a/sysdeps/unix/sysv/linux/bits/struct_stat_time64_helper.h b/sysdeps/unix/sysv/linux/bits/struct_stat_time64_helper.h index e0b78b314ad..0ce85352cbd 100644 --- a/sysdeps/unix/sysv/linux/bits/struct_stat_time64_helper.h +++ b/sysdeps/unix/sysv/linux/bits/struct_stat_time64_helper.h @@ -16,6 +16,8 @@ License along with the GNU C Library. If not, see . */ +#include + /* Content of internal __stat64_t64 struct. */ __dev_t st_dev; /* Device. */ __ino64_t st_ino; /* file serial number. */ @@ -59,8 +61,5 @@ __fieldts64 (st_mtime); __fieldts64 (st_ctime); - unsigned long int __glibc_reserved4; - unsigned long int __glibc_reserved5; - # undef __fieldts64 #endif diff --git a/sysdeps/unix/sysv/linux/tst-stat-layout-time64.py b/sysdeps/unix/sysv/linux/tst-stat-layout-time64.py new file mode 100644 index 00000000000..2c6dd66a491 --- /dev/null +++ b/sysdeps/unix/sysv/linux/tst-stat-layout-time64.py @@ -0,0 +1,94 @@ +#!/usr/bin/python3 +# Check that feature-test macros do not change the time64 stat layout. +# Copyright (C) 2026 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 +# . + +import argparse +import sys + +import glibcextract + +MODES = { + 'POSIX.1-1996': '#define _POSIX_C_SOURCE 199506L', + 'POSIX.1-2001': '#define _POSIX_C_SOURCE 200112L', + 'POSIX.1-2008': '#define _POSIX_C_SOURCE 200809L', + 'XPG6': '#define _XOPEN_SOURCE 600', +} + + +def compute_stat_layout(cc, mode_define): + # The nanosecond members are named differently in the two helper + # branches: st_Xtim.tv_nsec with __USE_XOPEN2K8, st_Xtimensec + #otherwise. + sym_data = [ + '#undef _GNU_SOURCE', + mode_define, + '#define _TIME_BITS 64', + '#define _FILE_OFFSET_BITS 64', + '#include ', + '#include ', + 'START', + ('sizeof_stat', 'sizeof (struct stat)'), + ('st_dev', 'offsetof (struct stat, st_dev)'), + ('st_ino', 'offsetof (struct stat, st_ino)'), + ('st_mode', 'offsetof (struct stat, st_mode)'), + ('st_nlink', 'offsetof (struct stat, st_nlink)'), + ('st_uid', 'offsetof (struct stat, st_uid)'), + ('st_gid', 'offsetof (struct stat, st_gid)'), + ('st_rdev', 'offsetof (struct stat, st_rdev)'), + ('st_size', 'offsetof (struct stat, st_size)'), + ('st_blksize', 'offsetof (struct stat, st_blksize)'), + ('st_blocks', 'offsetof (struct stat, st_blocks)'), + ('st_atime', 'offsetof (struct stat, st_atime)'), + ('st_mtime', 'offsetof (struct stat, st_mtime)'), + ('st_ctime', 'offsetof (struct stat, st_ctime)'), + '#ifdef __USE_XOPEN2K8', + ('st_atimensec', 'offsetof (struct stat, st_atim.tv_nsec)'), + ('st_mtimensec', 'offsetof (struct stat, st_mtim.tv_nsec)'), + ('st_ctimensec', 'offsetof (struct stat, st_ctim.tv_nsec)'), + '#else', + ('st_atimensec', 'offsetof (struct stat, st_atimensec)'), + ('st_mtimensec', 'offsetof (struct stat, st_mtimensec)'), + ('st_ctimensec', 'offsetof (struct stat, st_ctimensec)'), + '#endif', + ] + return glibcextract.compute_c_consts(sym_data, cc) + + +def main(): + parser = argparse.ArgumentParser( + description='Check that feature-test macros do not change ' + 'the time64 stat layout.') + parser.add_argument('--cc', metavar='CC', + help='C compiler (including options) to use') + args = parser.parse_args() + default_layout = compute_stat_layout(args.cc, '#define _GNU_SOURCE 1') + status = 0 + for mode, mode_define in sorted(MODES.items()): + mode_layout = compute_stat_layout(args.cc, mode_define) + for name, value in default_layout.items(): + if mode_layout[name] != value: + print('FAIL: %s: %s is %s, %s in default mode' + % (mode, name, mode_layout[name], value)) + status = 1 + if status == 0: + print('PASS: struct stat layout is feature-test-macro invariant') + sys.exit(status) + + +if __name__ == '__main__': + main()