Skip to content

Commit

Permalink
Further dyld interposing for macOS Monterey support (addresses #357)
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfcw committed Feb 20, 2022
1 parent 2d51264 commit 2c02fc0
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 20 deletions.
41 changes: 22 additions & 19 deletions src/Makefile.OSX
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
#
Expand Down
114 changes: 113 additions & 1 deletion src/libfaketime.c
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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
}
Expand All @@ -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) \
{ \
Expand All @@ -938,82 +979,114 @@ 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)
{
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)
{
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)
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 2c02fc0

Please sign in to comment.