Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fake 64-bit time on 32-bit systems #487

Merged
merged 4 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/libfaketime.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,20 @@ struct utimbuf {
#include <sys/random.h>
#endif

/* __timespec64 is needed for clock_gettime64 on 32-bit architectures */
struct __timespec64
{
uint64_t tv_sec; /* Seconds */
uint32_t tv_nsec; /* this is 32-bit, apparently! */
};

/* __timespec64 is needed for clock_gettime64 on 32-bit architectures */
struct __timeval64
{
uint64_t tv_sec; /* Seconds */
uint64_t tv_usec; /* this is 64-bit, apparently! */
};

/*
* Per thread variable, which we turn on inside real_* calls to avoid modifying
* time multiple times of for the whole process to prevent faking time
Expand Down Expand Up @@ -201,6 +215,7 @@ static time_t (*real_time) (time_t *);
static int (*real_ftime) (struct timeb *);
static int (*real_gettimeofday) (struct timeval *, void *);
static int (*real_clock_gettime) (clockid_t clk_id, struct timespec *tp);
static int (*real_clock_gettime64) (clockid_t clk_id, struct __timespec64 *tp);
#ifdef TIME_UTC
static int (*real_timespec_get) (struct timespec *ts, int base);
#endif
Expand Down Expand Up @@ -2417,6 +2432,50 @@ int clock_gettime(clockid_t clk_id, struct timespec *tp)
return result;
}

/* this is used by 32-bit architectures only */
int __clock_gettime64(clockid_t clk_id, struct __timespec64 *tp64)
{
struct timespec tp;
int result;

result = clock_gettime(clk_id, &tp);
tp64->tv_sec = tp.tv_sec;
tp64->tv_nsec = tp.tv_nsec;
return result;
}

/* this is used by 32-bit architectures only */
int __gettimeofday64(struct __timeval64 *tv64, void *tz)
{
struct timeval tv;
int result;

result = gettimeofday(&tv, tz);
tv64->tv_sec = tv.tv_sec;
tv64->tv_usec = tv.tv_usec;
return result;
}

/* this is used by 32-bit architectures only */
uint64_t __time64(uint64_t *write_out)
{
struct timespec tp;
uint64_t output;
int error;

error = clock_gettime(CLOCK_REALTIME, &tp);
if (error == -1)
{
return (uint64_t)error;
}
output = tp.tv_sec;

if (write_out)
{
*write_out = output;
}
return output;
}

#ifdef TIME_UTC
#ifdef MACOS_DYLD_INTERPOSE
Expand Down Expand Up @@ -2760,6 +2819,11 @@ static void ftpl_really_init(void)
{
real_clock_gettime = dlsym(RTLD_NEXT, "clock_gettime");
}
real_clock_gettime64 = dlsym(RTLD_NEXT, "clock_gettime64");
if (NULL == real_clock_gettime64)
{
real_clock_gettime64 = dlsym(RTLD_NEXT, "__clock_gettime64");
}
#ifdef FAKE_TIMERS
#if defined(__sun)
real_timer_gettime_233 = dlsym(RTLD_NEXT, "timer_gettime");
Expand Down
2 changes: 1 addition & 1 deletion test/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CC = gcc

CFLAGS += -std=gnu99 -Wall -DFAKE_STAT -Werror -Wextra $(FAKETIME_COMPILE_CFLAGS)
CFLAGS += -std=gnu99 -Wall -DFAKE_STAT -Werror -Wextra $(FAKETIME_COMPILE_CFLAGS) -U_FILE_OFFSET_BITS -U_TIME_BITS
LDFLAGS += -lrt -lpthread

SRC = timetest.c
Expand Down