From ae78395d83cefa563ca46995857be07ee0ee114d Mon Sep 17 00:00:00 2001 From: Eric Warmenhoven Date: Mon, 14 Aug 2023 09:41:43 -0400 Subject: [PATCH] iOS/tvOS: rework JIT availability checks (#15590) --- pkg/apple/JITSupport.h | 2 +- pkg/apple/JITSupport.m | 48 ++++++++++++++++++++++++++++++++++++++++-- runloop.c | 2 +- verbosity.c | 2 -- 4 files changed, 48 insertions(+), 6 deletions(-) diff --git a/pkg/apple/JITSupport.h b/pkg/apple/JITSupport.h index 8d81691202..b965ac91ce 100644 --- a/pkg/apple/JITSupport.h +++ b/pkg/apple/JITSupport.h @@ -11,7 +11,7 @@ #include -bool jb_has_debugger_attached(void); +bool jit_available(void); bool jb_enable_ptrace_hack(void); void jb_start_altkit(void); diff --git a/pkg/apple/JITSupport.m b/pkg/apple/JITSupport.m index 009d1b53bd..cc5bb5321b 100644 --- a/pkg/apple/JITSupport.m +++ b/pkg/apple/JITSupport.m @@ -10,16 +10,21 @@ #import +#import "JITSupport.h" + #include #include +#include #include #include #include +#include #if defined(HAVE_ALTKIT) @import AltKit; #endif +#include #include "../../verbosity.h" extern int csops(pid_t pid, unsigned int ops, void * useraddr, size_t usersize); @@ -40,7 +45,7 @@ static void *exception_handler(void *argument) { return NULL; } -bool jb_has_debugger_attached(void) { +static bool jb_has_debugger_attached(void) { int flags; return !csops(getpid(), CS_OPS_STATUS, &flags, sizeof(flags)) && flags & CS_DEBUGGED; } @@ -94,7 +99,7 @@ bool jb_enable_ptrace_hack(void) { void jb_start_altkit(void) { #if HAVE_ALTKIT // asking AltKit/AltServer to debug us when we're already debugged is bad, very bad - if (jb_has_debugger_attached()) + if (jit_available()) return; [[ALTServerManager sharedManager] autoconnectWithCompletionHandler:^(ALTServerConnection *connection, NSError *error) { @@ -114,3 +119,42 @@ void jb_start_altkit(void) { [[ALTServerManager sharedManager] startDiscovering]; #endif } + +bool jit_available(void) +{ + static bool canOpenApps = false; + static dispatch_once_t appsOnce = 0; + dispatch_once(&appsOnce, ^{ + DIR *apps = opendir("/Applications"); + if (apps) + { + closedir(apps); + canOpenApps = true; + } + }); + + static bool dylded = false; + static dispatch_once_t dyldOnce = 0; + dispatch_once(&dyldOnce, ^{ + int imageCount = _dyld_image_count(); + for (int i = 0; i < imageCount; i++) + { + if (string_is_equal("/usr/lib/pspawn_payload-stg2.dylib", _dyld_get_image_name(i))) + dylded = true; + } + }); + + static bool doped = false; + static dispatch_once_t dopeOnce = 0; + dispatch_once(&dopeOnce, ^{ + int64_t (*jbdswDebugMe)(void) = dlsym(RTLD_DEFAULT, "jbdswDebugMe"); + if (jbdswDebugMe) + { + int64_t ret = jbdswDebugMe(); + doped = (ret == 0); + } + }); + + /* the debugger could be attached at any time, its value can't be cached */ + return canOpenApps || dylded || doped || jb_has_debugger_attached(); +} diff --git a/runloop.c b/runloop.c index 35fe07a272..07e0ca0703 100644 --- a/runloop.c +++ b/runloop.c @@ -3472,7 +3472,7 @@ bool runloop_environment_cb(unsigned cmd, void *data) case RETRO_ENVIRONMENT_GET_JIT_CAPABLE: { #if defined(HAVE_COCOATOUCH) && TARGET_OS_IOS - *(bool*)data = jb_has_debugger_attached(); + *(bool*)data = jit_available(); #else *(bool*)data = true; #endif diff --git a/verbosity.c b/verbosity.c index 8171be343f..12e427bd18 100644 --- a/verbosity.c +++ b/verbosity.c @@ -314,14 +314,12 @@ void RARCH_LOG_V(const char *tag, const char *fmt, va_list ap) #if defined(HAVE_LIBNX) mutexLock(&g_verbosity->mtx); #endif -#if !TARGET_OS_TV if (fp) { fprintf(fp, "%s ", tag_v); vfprintf(fp, fmt, ap); fflush(fp); } -#endif #if defined(HAVE_LIBNX) mutexUnlock(&g_verbosity->mtx); #endif