Bug 273085 - clock_gettime(CLOCK_MONOTONIC_COARSE, *struct timespec) fails with 32bit program on 64bit machine
Summary: clock_gettime(CLOCK_MONOTONIC_COARSE, *struct timespec) fails with 32bit prog...
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: kern (show other bugs)
Version: 13.2-RELEASE
Hardware: amd64 Any
: --- Affects Many People
Assignee: freebsd-bugs (Nobody)
URL:
Keywords:
: 273086 (view as bug list)
Depends on:
Blocks:
 
Reported: 2023-08-12 15:17 UTC by Minsoo Choo
Modified: 2023-09-10 16:03 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Minsoo Choo 2023-08-12 15:17:59 UTC
$ cat main.c
#include <stdio.h>
#include <time.h>

int main() {
  struct timespec ts;

  for (int i = 0; i < 100; ++i) {
    clock_gettime(CLOCK_MONOTONIC_COARSE, &ts);
    printf("%d %ld\n", ts.tv_sec, ts.tv_nsec);
  }
  return 0;
}

$ cc -m32 main.c
$ ./a.out
-1 318747501
-1 318747501
-1 318747501
-1 318747501
-1 318747501
-1 318747501
-1 318747501
-1 318747501
-1 318747501
-1 318747501
-1 318747501
-1 318747501
-1 318747501
-1 318747501
-1 318747501
-1 318747501
-1 318747501
-1 318747501
-1 318747501
....


This happens when building the program as 32bit executable on 64bit FreeBSD and run the executable on 64bit FreeBSD.
Comment 1 Mina Galić freebsd_triage 2023-08-12 15:35:29 UTC
*** Bug 273086 has been marked as a duplicate of this bug. ***
Comment 2 Warner Losh freebsd_committer freebsd_triage 2023-08-12 20:58:20 UTC
which architecutre? Amd64, aarch64 or powerpc64?
Comment 3 Konstantin Belousov freebsd_committer freebsd_triage 2023-08-12 21:44:30 UTC
I see it on amd64.  There is something weird with vdso' 32bit timehands.
Comment 4 commit-hook freebsd_committer freebsd_triage 2023-08-12 22:34:47 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=93626d54370292b09cd0ca604b144737109e9071

commit 93626d54370292b09cd0ca604b144737109e9071
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2023-08-12 19:51:37 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2023-08-12 22:34:08 +0000

    tc_fill_vdso_timehands32(): fix

    On 64bit, there is a 4-byte hole in struct vdso_timekeep32 after
    tk_current, if the structure is not packed.  This is due to the MD
    th_x86_pvc_last_systime being 64bit.

    Change amd64 VDSO_TIMEHANDS_MD32 to not use uint64_t, replace it with
    pair of uint32_t, as it is done for all other members.

    PR:     273085
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week

 sys/dev/acpica/acpi_hpet.c | 3 ++-
 sys/x86/include/vdso.h     | 7 ++++++-
 sys/x86/x86/pvclock.c      | 2 +-
 sys/x86/x86/tsc.c          | 3 ++-
 4 files changed, 11 insertions(+), 4 deletions(-)
Comment 5 commit-hook freebsd_committer freebsd_triage 2023-08-19 00:36:47 UTC
A commit in branch stable/13 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=8fe73008036b0770e3d0a5b41e9b845feb46bdf1

commit 8fe73008036b0770e3d0a5b41e9b845feb46bdf1
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2023-08-12 19:51:37 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2023-08-19 00:35:29 +0000

    tc_fill_vdso_timehands32(): fix

    PR:     273085

    (cherry picked from commit 93626d54370292b09cd0ca604b144737109e9071)

 sys/dev/acpica/acpi_hpet.c | 3 ++-
 sys/x86/include/vdso.h     | 7 ++++++-
 sys/x86/x86/pvclock.c      | 2 +-
 sys/x86/x86/tsc.c          | 3 ++-
 4 files changed, 11 insertions(+), 4 deletions(-)