From 0e2dbe4ae15593895f8143281aecfc7ca9436889 Mon Sep 17 00:00:00 2001 From: usertam Date: Sat, 7 Jun 2025 20:28:33 +0800 Subject: [PATCH 1/2] libfaketime.c: correct macro from __APPLEOSX__ to __APPLE__ This fixes the recursive pthread_once deadlock on darwin platforms. It looks something like this: Trace/BPT trap: 5 BUG IN CLIENT OF LIBPLATFORM: Trying to recursively lock an os_once_t The macro __APPLEOSX__ is never defined, instead __APPLE__ should be used. This mistake inadvertently caused system_time_from_system() to always take the linux code path on darwin, leading to recursive calls during ftpl_init(). This was exposed by PR #488 which removed the ad-hoc recursion detection that previously masked this issue. --- src/libfaketime.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libfaketime.c b/src/libfaketime.c index d148c2d..a456995 100644 --- a/src/libfaketime.c +++ b/src/libfaketime.c @@ -236,7 +236,7 @@ static int (*real_pthread_cond_destroy_232) (pthread_cond_t *); static pthread_rwlock_t monotonic_conds_lock; #endif -#ifndef __APPLEOSX__ +#ifndef __APPLE__ #ifdef FAKE_TIMERS static int (*real_timer_settime_22) (int timerid, int flags, const struct itimerspec *new_value, struct itimerspec * old_value); @@ -282,7 +282,7 @@ static int (*real_pselect) (int nfds, fd_set *restrict readfds, static int (*real_sem_timedwait) (sem_t*, const struct timespec*); static int (*real_sem_clockwait) (sem_t *sem, clockid_t clockid, const struct timespec *abstime); #endif -#ifdef __APPLEOSX__ +#ifdef __APPLE__ static int (*real_clock_get_time) (clock_serv_t clock_serv, mach_timespec_t *cur_timeclockid_t); static int apple_clock_gettime (clockid_t clk_id, struct timespec *tp); static clock_serv_t clock_serv_real; @@ -694,7 +694,7 @@ static void get_fake_monotonic_setting(int* current_value) /* Get system time from system for all clocks */ static void system_time_from_system (struct system_time_s * systime) { -#ifdef __APPLEOSX__ +#ifdef __APPLE__ /* from https://stackoverflow.com/questions/5167269/clock-gettime-alternative-in-mac-os-x */ clock_serv_t cclock; mach_timespec_t mts; @@ -2784,7 +2784,7 @@ static void ftpl_really_init(void) exit(-1); } #endif -#ifdef __APPLEOSX__ +#ifdef __APPLE__ real_clock_get_time = dlsym(RTLD_NEXT, "clock_get_time"); real_clock_gettime = apple_clock_gettime; #else @@ -3545,7 +3545,7 @@ int fake_gettimeofday(struct timeval *tv) * ======================================================================= */ -#ifdef __APPLEOSX__ +#ifdef __APPLE__ /* * clock_gettime implementation for __APPLE__ * @note It always behave like being called with CLOCK_REALTIME. From 0277016bb5a33b412bd13af9905908524b709c7f Mon Sep 17 00:00:00 2001 From: usertam Date: Sat, 7 Jun 2025 20:41:30 +0800 Subject: [PATCH 2/2] Makefile.OSX: add -fptrauth-* flags for arm64e to work properly Particularly we need -fptrauth-calls, so when pthread_once indirectly calls ftpl_really_init, it won't fail PAC. --- src/Makefile.OSX | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Makefile.OSX b/src/Makefile.OSX index 9bdf922..e056f37 100644 --- a/src/Makefile.OSX +++ b/src/Makefile.OSX @@ -64,10 +64,16 @@ LIB_LDFLAGS += -dynamiclib -current_version 0.9.11 -compatibility_version 0.7 # Therefore, we now compile for two ABIs at the same time, producing a fat library of arm64e and arm64, # so in the end the OS gets to pick which architecture it wants at runtime. + +# In addition, we need to enable signing and authentication of indirect calls (-fptrauth-calls); +# otherwise in ftpl_init, pthread_once will indirectly call ftpl_really_init, which then fail PAC. +# Ideally this should be a compiler default for the arm64e ABI, but apparently not. + ARCH := $(shell uname -m) ifeq ($(ARCH),arm64) CFLAGS += -arch arm64e -arch arm64 + CFLAGS += -fptrauth-calls -fptrauth-returns endif SONAME = 1