summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorWolfgang Hommel <wolfgang.hommel@unibw.de>2022-02-20 17:39:17 +0100
committerWolfgang Hommel <wolfgang.hommel@unibw.de>2022-02-20 17:39:17 +0100
commit2c02fc08efba44a68e6387e47747bb28982d3dae (patch)
tree5ea819e9402f40139977d71e09ebf2a4362ce54a /src
parent2d5126411bf26525741b44188a44d0e2dd1f484c (diff)
downloadlibfaketime-2c02fc08efba44a68e6387e47747bb28982d3dae.tar.gz
Further dyld interposing for macOS Monterey support (addresses #357)
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.OSX41
-rw-r--r--src/libfaketime.c114
2 files changed, 135 insertions, 20 deletions
diff --git a/src/Makefile.OSX b/src/Makefile.OSX
index d5f4c88..11ea91b 100644
--- a/src/Makefile.OSX
+++ b/src/Makefile.OSX
@@ -3,11 +3,31 @@
#
# * Compilation Defines:
#
+# MACOS_DYLD_INTERPOSE
+# - Use dlyd interposing instead of name-based function interception
+# (required since macOS Monterey)
+#
+# FAKE_SLEEP
+# - Also intercept sleep(), nanosleep(), usleep(), alarm(), [p]poll()
+#
+# FAKE_SETTIME
+# - Intercept clock_settime(), settimeofday(), and adjtime()
+#
+# FAKE_PID
+# - Enable faked values for getpid() calls through FAKETIME_FAKEPID
+#
+# FAKE_RANDOM
+# - Intercept getentropy(). Dangerous for production use.
+# See README about FAKE_RANDOM.
+#
# FAKE_STAT
# - Enables time faking also for files' timestamps.
#
-# NO_ATFILE
-# - Disables support for the fstatat() group of functions
+# FAKE_FILE_TIMESTAMPS, FAKE_UTIME
+# - Enables time faking for the utime* functions. If enabled via
+# FAKE_FILE_TIMESTAMPS, the faking is opt-in at runtime using
+# with the FAKE_UTIME environment variable. If enabled via
+# FAKE_UTIME, the faking is opt-out at runtime. Requires FAKE_STAT.
#
# PTHREAD
# - Define this to enable multithreading support.
@@ -18,23 +38,6 @@
# without this, but the performance impact may require you to
# try it unsynchronized.
#
-# FAKE_SLEEP
-# - Also intercept sleep(), nanosleep(), usleep(), alarm(), [p]poll()
-#
-# MACOS_DYLD_INTERPOSE
-# - Use dlyd interposing instead of name-based function interception
-# (required since macOS Monterey)
-#
-# FAKE_RANDOM
-# - Intercept getentropy(). Dangerous for production use.
-# See README about FAKE_RANDOM.
-#
-# FAKE_SETTIME
-# - Intercept clock_settime(), settimeofday(), and adjtime()
-#
-# FAKE_PID
-# - Enable faked values for getpid() calls through FAKETIME_FAKEPID
-#
# * Compilation addition: second libMT target added for building the pthread-
# enabled library as a separate library
#
diff --git a/src/libfaketime.c b/src/libfaketime.c
index 5d29f49..b47fcb3 100644
--- a/src/libfaketime.c
+++ b/src/libfaketime.c
@@ -36,6 +36,7 @@
#include <time.h>
#ifdef MACOS_DYLD_INTERPOSE
#include <sys/time.h>
+#include <utime.h>
#endif
#include <math.h>
#include <errno.h>
@@ -141,11 +142,13 @@ void do_macos_dyld_interpose(void);
#endif
#ifdef FAKE_FILE_TIMESTAMPS
+#ifndef __APPLE__
struct utimbuf {
time_t actime; /* access time */
time_t modtime; /* modification time */
};
#endif
+#endif
#ifdef FAKE_RANDOM
#include <sys/random.h>
@@ -892,9 +895,15 @@ static inline void fake_statbuf (struct stat *buf) {
unlock_for_stat();
#else
lock_for_stat();
+#ifndef __APPLE__
fake_clock_gettime(CLOCK_REALTIME, &buf->st_ctim);
fake_clock_gettime(CLOCK_REALTIME, &buf->st_atim);
fake_clock_gettime(CLOCK_REALTIME, &buf->st_mtim);
+#else
+ fake_clock_gettime(CLOCK_REALTIME, &buf->st_ctimespec);
+ fake_clock_gettime(CLOCK_REALTIME, &buf->st_atimespec);
+ fake_clock_gettime(CLOCK_REALTIME, &buf->st_mtimespec);
+#endif
unlock_for_stat();
#endif
}
@@ -908,13 +917,45 @@ static inline void fake_stat64buf (struct stat64 *buf) {
unlock_for_stat();
#else
lock_for_stat();
+#ifndef __APPLE__
fake_clock_gettime(CLOCK_REALTIME, &buf->st_ctim);
fake_clock_gettime(CLOCK_REALTIME, &buf->st_atim);
fake_clock_gettime(CLOCK_REALTIME, &buf->st_mtim);
+#else
+ fake_clock_gettime(CLOCK_REALTIME, &buf->st_ctimespec);
+ fake_clock_gettime(CLOCK_REALTIME, &buf->st_atimespec);
+ fake_clock_gettime(CLOCK_REALTIME, &buf->st_mtimespec);
+#endif
unlock_for_stat();
#endif
}
+/* macOS dyld interposing uses the function's real name instead of real_name */
+#ifdef MACOS_DYLD_INTERPOSE
+#define STAT_HANDLER_COMMON(name, buf, fake_statbuf, ...) \
+ if (!initialized) \
+ { \
+ ftpl_init(); \
+ } \
+ if (!CHECK_MISSING_REAL(name)) return -1; \
+ \
+ int result; \
+ DONT_FAKE_TIME(result = name(__VA_ARGS__)); \
+ if (result == -1) \
+ { \
+ return -1; \
+ } \
+ \
+ if (buf != NULL) \
+ { \
+ if (!fake_stat_disabled) \
+ { \
+ if (!dont_fake) fake_statbuf(buf); \
+ } \
+ } \
+ \
+ return result;
+#else
#define STAT_HANDLER_COMMON(name, buf, fake_statbuf, ...) \
if (!initialized) \
{ \
@@ -938,39 +979,56 @@ static inline void fake_stat64buf (struct stat64 *buf) {
} \
\
return result;
-
+#endif
#define STAT_HANDLER(name, buf, ...) \
STAT_HANDLER_COMMON(name, buf, fake_statbuf, __VA_ARGS__)
#define STAT64_HANDLER(name, buf, ...) \
STAT_HANDLER_COMMON(name, buf, fake_stat64buf, __VA_ARGS__)
+#ifdef MACOS_DYLD_INTERPOSE
+int macos_stat (const char *path, struct stat *buf)
+#else
int stat (const char *path, struct stat *buf)
+#endif
{
STAT_HANDLER(stat, buf, path, buf);
}
+#ifdef MACOS_DYLD_INTERPOSE
+int macos_fstat (int fildes, struct stat *buf)
+#else
int fstat (int fildes, struct stat *buf)
+#endif
{
STAT_HANDLER(fstat, buf, fildes, buf);
}
+#ifdef MACOS_DYLD_INTERPOSE
+int macos_lstat (const char *path, struct stat *buf)
+#else
int lstat (const char *path, struct stat *buf)
+#endif
{
STAT_HANDLER(lstat, buf, path, buf);
}
+#ifndef __APPLE__
/* Contributed by Philipp Hachtmann in version 0.6 */
int __xstat (int ver, const char *path, struct stat *buf)
{
STAT_HANDLER(xstat, buf, ver, path, buf);
}
+#endif
+#ifndef __APPLE__
/* Contributed by Philipp Hachtmann in version 0.6 */
int __fxstat (int ver, int fildes, struct stat *buf)
{
STAT_HANDLER(fxstat, buf, ver, fildes, buf);
}
+#endif
+#ifndef __APPLE__
/* Added in v0.8 as suggested by Daniel Kahn Gillmor */
#ifndef NO_ATFILE
int __fxstatat(int ver, int fildes, const char *filename, struct stat *buf, int flag)
@@ -978,25 +1036,33 @@ int __fxstatat(int ver, int fildes, const char *filename, struct stat *buf, int
STAT_HANDLER(fxstatat, buf, ver, fildes, filename, buf, flag);
}
#endif
+#endif
+#ifndef __APPLE__
/* Contributed by Philipp Hachtmann in version 0.6 */
int __lxstat (int ver, const char *path, struct stat *buf)
{
STAT_HANDLER(lxstat, buf, ver, path, buf);
}
+#endif
+#ifndef __APPLE__
/* Contributed by Philipp Hachtmann in version 0.6 */
int __xstat64 (int ver, const char *path, struct stat64 *buf)
{
STAT64_HANDLER(xstat64, buf, ver, path, buf);
}
+#endif
+#ifndef __APPLE__
/* Contributed by Philipp Hachtmann in version 0.6 */
int __fxstat64 (int ver, int fildes, struct stat64 *buf)
{
STAT64_HANDLER(fxstat64, buf, ver, fildes, buf);
}
+#endif
+#ifndef __APPLE__
/* Added in v0.8 as suggested by Daniel Kahn Gillmor */
#ifndef NO_ATFILE
int __fxstatat64 (int ver, int fildes, const char *filename, struct stat64 *buf, int flag)
@@ -1004,16 +1070,23 @@ int __fxstatat64 (int ver, int fildes, const char *filename, struct stat64 *buf,
STAT64_HANDLER(fxstatat64, buf, ver, fildes, filename, buf, flag);
}
#endif
+#endif
+#ifndef __APPLE__
/* Contributed by Philipp Hachtmann in version 0.6 */
int __lxstat64 (int ver, const char *path, struct stat64 *buf)
{
STAT64_HANDLER(lxstat64, buf, ver, path, buf);
}
#endif
+#endif
#ifdef FAKE_FILE_TIMESTAMPS
+#ifdef MACOS_DYLD_INTERPOSE
+int macos_utime(const char *filename, const struct utimbuf *times)
+#else
int utime(const char *filename, const struct utimbuf *times)
+#endif
{
if (!initialized)
{
@@ -1037,11 +1110,19 @@ int utime(const char *filename, const struct utimbuf *times)
ntbuf.modtime = times->modtime - user_offset.tv_sec;
times = &ntbuf;
}
+#ifdef MACOS_DYLD_INTERPOSE
+ DONT_FAKE_TIME(result = utime(filename, times));
+#else
DONT_FAKE_TIME(result = real_utime(filename, times));
+#endif
return result;
}
+#ifdef MACOS_DYLD_INTERPOSE
+int macos_utimes(const char *filename, const struct timeval times[2])
+#else
int utimes(const char *filename, const struct timeval times[2])
+#endif
{
if (!initialized)
{
@@ -1069,7 +1150,11 @@ int utimes(const char *filename, const struct timeval times[2])
timersub(&times[1], &user_offset2, &tn[1]);
times = tn;
}
+#ifdef MACOS_DYLD_INTERPOSE
+ DONT_FAKE_TIME(result = utimes(filename, times));
+#else
DONT_FAKE_TIME(result = real_utimes(filename, times));
+#endif
return result;
}
@@ -1111,7 +1196,11 @@ static void fake_two_timespec(const struct timespec in_times[2], struct timespec
}
}
+#ifdef MACOS_DYLD_INTERPOSE
+int macos_utimensat(int dirfd, const char *filename, const struct timespec times[2], int flags)
+#else
int utimensat(int dirfd, const char *filename, const struct timespec times[2], int flags)
+#endif
{
if (!initialized)
{
@@ -1122,11 +1211,19 @@ int utimensat(int dirfd, const char *filename, const struct timespec times[2], i
int result;
struct timespec tn[2];
fake_two_timespec(times, tn);
+#ifdef MACOS_DYLD_INTERPOSE
+ DONT_FAKE_TIME(result = utimensat(dirfd, filename, tn, flags));
+#else
DONT_FAKE_TIME(result = real_utimensat(dirfd, filename, tn, flags));
+#endif
return result;
}
+#ifdef MACOS_DYLD_INTERPOSE
+int macos_futimens(int fd, const struct timespec times[2])
+#else
int futimens(int fd, const struct timespec times[2])
+#endif
{
if (!initialized)
{
@@ -1137,7 +1234,11 @@ int futimens(int fd, const struct timespec times[2])
int result;
struct timespec tn[2];
fake_two_timespec(times, tn);
+#ifdef MACOS_DYLD_INTERPOSE
+ DONT_FAKE_TIME(result = futimens(fd, tn));
+#else
DONT_FAKE_TIME(result = real_futimens(fd, tn));
+#endif
return result;
}
#endif
@@ -3863,6 +3964,17 @@ void do_macos_dyld_interpose(void) {
#ifdef FAKE_PID
DYLD_INTERPOSE(macos_getpid, getpid);
#endif
+#ifdef FAKE_STAT
+ DYLD_INTERPOSE(macos_stat, stat);
+// DYLD_INTERPOSE(macos_fstat, fstat);
+ DYLD_INTERPOSE(macos_lstat, lstat);
+#endif
+#ifdef FAKE_FILE_TIMESTAMPS
+ DYLD_INTERPOSE(macos_utime, utime);
+ DYLD_INTERPOSE(macos_utimes, utimes);
+ DYLD_INTERPOSE(macos_utimensat, utimensat);
+ DYLD_INTERPOSE(macos_futimens, futimens);
+#endif
}
#endif