Skip to content

Commit

Permalink
iOS/tvOS: rework JIT availability checks (#15590)
Browse files Browse the repository at this point in the history
  • Loading branch information
warmenhoven authored Aug 14, 2023
1 parent 946c198 commit ae78395
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pkg/apple/JITSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

#include <stdbool.h>

bool jb_has_debugger_attached(void);
bool jit_available(void);
bool jb_enable_ptrace_hack(void);
void jb_start_altkit(void);

Expand Down
48 changes: 46 additions & 2 deletions pkg/apple/JITSupport.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,21 @@

#import <Foundation/Foundation.h>

#import "JITSupport.h"

#include <dlfcn.h>
#include <mach/mach.h>
#include <mach-o/dyld.h>
#include <mach-o/loader.h>
#include <mach-o/getsect.h>
#include <pthread.h>
#include <dirent.h>

#if defined(HAVE_ALTKIT)
@import AltKit;
#endif

#include <string/stdstring.h>
#include "../../verbosity.h"

extern int csops(pid_t pid, unsigned int ops, void * useraddr, size_t usersize);
Expand All @@ -40,7 +45,7 @@
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;
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
}
2 changes: 1 addition & 1 deletion runloop.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions verbosity.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ae78395

Please sign in to comment.