RetroArch/pkg/apple/JITSupport.m

135 lines
4.8 KiB
Mathematica
Raw Normal View History

[iOS] New iOS 13 project to support Swift; Custom keyboard and touch mouse support (#13435) * Support for Swift, added emulator keyboard * fixed toggle key handling using magic number hack for now * fixed keyboard transparency slider for now with suboptimal fix; add gesture recognizer to hide/show keyboard * Support CocoaView extensions in Swift; move keyboard delegate impl and setup to swift extension * moved keyboard view model creation out of EmulatorKeyboard * implement key pressed delegate in swift extension * added input method for directly sending RETROK_* codes to support a touchscreen keyboard; assign keyboard model delegates; updated keyboard layout (added F1-F12 keys); change shift, control and alt keys to be modifiers * enable focus mode when custom keyboard is shown; enable/disable overlay when custom keyboard is toggled * Specify -DHAVE_OPENGLES2 instead of -DHAVE_OPENGLES3 since glsym_es3.h does not compile in iOS 14.5 Fix tvOS build using compiler flags * Create new project for iOS 13 deploy target; add check for deploy target to conditionally compile for new iOS 13 specific feature (custom keyboard) * force disable core info caching for iOS, use opengl es2 for debug * Add flag for iOS custom keyboard - 3-finger swipe up to show, 3-finger swipe down to hide * use OpenGLES2 instead; using ES3 results in compile time errors on iOS 14.5 * code cleanup * Updated references to -DDONT_WANT_ARM_ASM_OPTIMIZATIONS flag * Add JIT support for non-jailbroken devices * iOS: Add support for touch mouse handler * Added a HAVE_IOS_TOUCHMOUSE preprocessor macro so that it builds under the iOS11_Metal xcode project * Changed click-and-drag behavior to double tap hold and drag * Visual improvements to the emulator keyboard: updated colors, improved key-press effect
2022-02-22 07:21:34 +00:00
//
// JITSupport.m
// RetroArchiOS
//
// Created by Yoshi Sugawara on 9/25/21.
// Copyright © 2021 RetroArch. All rights reserved.
//
// Copied from UTMApp, original author: osy
//
#import <Foundation/Foundation.h>
#import "JITSupport.h"
[iOS] New iOS 13 project to support Swift; Custom keyboard and touch mouse support (#13435) * Support for Swift, added emulator keyboard * fixed toggle key handling using magic number hack for now * fixed keyboard transparency slider for now with suboptimal fix; add gesture recognizer to hide/show keyboard * Support CocoaView extensions in Swift; move keyboard delegate impl and setup to swift extension * moved keyboard view model creation out of EmulatorKeyboard * implement key pressed delegate in swift extension * added input method for directly sending RETROK_* codes to support a touchscreen keyboard; assign keyboard model delegates; updated keyboard layout (added F1-F12 keys); change shift, control and alt keys to be modifiers * enable focus mode when custom keyboard is shown; enable/disable overlay when custom keyboard is toggled * Specify -DHAVE_OPENGLES2 instead of -DHAVE_OPENGLES3 since glsym_es3.h does not compile in iOS 14.5 Fix tvOS build using compiler flags * Create new project for iOS 13 deploy target; add check for deploy target to conditionally compile for new iOS 13 specific feature (custom keyboard) * force disable core info caching for iOS, use opengl es2 for debug * Add flag for iOS custom keyboard - 3-finger swipe up to show, 3-finger swipe down to hide * use OpenGLES2 instead; using ES3 results in compile time errors on iOS 14.5 * code cleanup * Updated references to -DDONT_WANT_ARM_ASM_OPTIMIZATIONS flag * Add JIT support for non-jailbroken devices * iOS: Add support for touch mouse handler * Added a HAVE_IOS_TOUCHMOUSE preprocessor macro so that it builds under the iOS11_Metal xcode project * Changed click-and-drag behavior to double tap hold and drag * Visual improvements to the emulator keyboard: updated colors, improved key-press effect
2022-02-22 07:21:34 +00:00
#include <dlfcn.h>
#include <mach/mach.h>
#include <mach-o/dyld.h>
[iOS] New iOS 13 project to support Swift; Custom keyboard and touch mouse support (#13435) * Support for Swift, added emulator keyboard * fixed toggle key handling using magic number hack for now * fixed keyboard transparency slider for now with suboptimal fix; add gesture recognizer to hide/show keyboard * Support CocoaView extensions in Swift; move keyboard delegate impl and setup to swift extension * moved keyboard view model creation out of EmulatorKeyboard * implement key pressed delegate in swift extension * added input method for directly sending RETROK_* codes to support a touchscreen keyboard; assign keyboard model delegates; updated keyboard layout (added F1-F12 keys); change shift, control and alt keys to be modifiers * enable focus mode when custom keyboard is shown; enable/disable overlay when custom keyboard is toggled * Specify -DHAVE_OPENGLES2 instead of -DHAVE_OPENGLES3 since glsym_es3.h does not compile in iOS 14.5 Fix tvOS build using compiler flags * Create new project for iOS 13 deploy target; add check for deploy target to conditionally compile for new iOS 13 specific feature (custom keyboard) * force disable core info caching for iOS, use opengl es2 for debug * Add flag for iOS custom keyboard - 3-finger swipe up to show, 3-finger swipe down to hide * use OpenGLES2 instead; using ES3 results in compile time errors on iOS 14.5 * code cleanup * Updated references to -DDONT_WANT_ARM_ASM_OPTIMIZATIONS flag * Add JIT support for non-jailbroken devices * iOS: Add support for touch mouse handler * Added a HAVE_IOS_TOUCHMOUSE preprocessor macro so that it builds under the iOS11_Metal xcode project * Changed click-and-drag behavior to double tap hold and drag * Visual improvements to the emulator keyboard: updated colors, improved key-press effect
2022-02-22 07:21:34 +00:00
#include <mach-o/loader.h>
#include <mach-o/getsect.h>
#include <pthread.h>
#include <dirent.h>
[iOS] New iOS 13 project to support Swift; Custom keyboard and touch mouse support (#13435) * Support for Swift, added emulator keyboard * fixed toggle key handling using magic number hack for now * fixed keyboard transparency slider for now with suboptimal fix; add gesture recognizer to hide/show keyboard * Support CocoaView extensions in Swift; move keyboard delegate impl and setup to swift extension * moved keyboard view model creation out of EmulatorKeyboard * implement key pressed delegate in swift extension * added input method for directly sending RETROK_* codes to support a touchscreen keyboard; assign keyboard model delegates; updated keyboard layout (added F1-F12 keys); change shift, control and alt keys to be modifiers * enable focus mode when custom keyboard is shown; enable/disable overlay when custom keyboard is toggled * Specify -DHAVE_OPENGLES2 instead of -DHAVE_OPENGLES3 since glsym_es3.h does not compile in iOS 14.5 Fix tvOS build using compiler flags * Create new project for iOS 13 deploy target; add check for deploy target to conditionally compile for new iOS 13 specific feature (custom keyboard) * force disable core info caching for iOS, use opengl es2 for debug * Add flag for iOS custom keyboard - 3-finger swipe up to show, 3-finger swipe down to hide * use OpenGLES2 instead; using ES3 results in compile time errors on iOS 14.5 * code cleanup * Updated references to -DDONT_WANT_ARM_ASM_OPTIMIZATIONS flag * Add JIT support for non-jailbroken devices * iOS: Add support for touch mouse handler * Added a HAVE_IOS_TOUCHMOUSE preprocessor macro so that it builds under the iOS11_Metal xcode project * Changed click-and-drag behavior to double tap hold and drag * Visual improvements to the emulator keyboard: updated colors, improved key-press effect
2022-02-22 07:21:34 +00:00
#include <string/stdstring.h>
#include <file/file_path.h>
2024-04-18 13:09:46 +00:00
#include <retro_miscellaneous.h>
#include "../../verbosity.h"
[iOS] New iOS 13 project to support Swift; Custom keyboard and touch mouse support (#13435) * Support for Swift, added emulator keyboard * fixed toggle key handling using magic number hack for now * fixed keyboard transparency slider for now with suboptimal fix; add gesture recognizer to hide/show keyboard * Support CocoaView extensions in Swift; move keyboard delegate impl and setup to swift extension * moved keyboard view model creation out of EmulatorKeyboard * implement key pressed delegate in swift extension * added input method for directly sending RETROK_* codes to support a touchscreen keyboard; assign keyboard model delegates; updated keyboard layout (added F1-F12 keys); change shift, control and alt keys to be modifiers * enable focus mode when custom keyboard is shown; enable/disable overlay when custom keyboard is toggled * Specify -DHAVE_OPENGLES2 instead of -DHAVE_OPENGLES3 since glsym_es3.h does not compile in iOS 14.5 Fix tvOS build using compiler flags * Create new project for iOS 13 deploy target; add check for deploy target to conditionally compile for new iOS 13 specific feature (custom keyboard) * force disable core info caching for iOS, use opengl es2 for debug * Add flag for iOS custom keyboard - 3-finger swipe up to show, 3-finger swipe down to hide * use OpenGLES2 instead; using ES3 results in compile time errors on iOS 14.5 * code cleanup * Updated references to -DDONT_WANT_ARM_ASM_OPTIMIZATIONS flag * Add JIT support for non-jailbroken devices * iOS: Add support for touch mouse handler * Added a HAVE_IOS_TOUCHMOUSE preprocessor macro so that it builds under the iOS11_Metal xcode project * Changed click-and-drag behavior to double tap hold and drag * Visual improvements to the emulator keyboard: updated colors, improved key-press effect
2022-02-22 07:21:34 +00:00
extern int csops(pid_t pid, unsigned int ops, void * useraddr, size_t usersize);
extern boolean_t exc_server(mach_msg_header_t *, mach_msg_header_t *);
extern int ptrace(int request, pid_t pid, caddr_t addr, int data);
#define CS_OPS_STATUS 0 /* return status */
#define CS_KILL 0x00000200 /* kill process if it becomes invalid */
#define CS_DEBUGGED 0x10000000 /* process is currently or has previously been debugged and allowed to run with invalid pages */
#define PT_TRACE_ME 0 /* child declares it's being traced */
#define PT_SIGEXC 12 /* signals as exceptions for current_proc */
2023-05-08 14:29:40 +00:00
#if !TARGET_OS_TV
2024-09-14 20:11:20 +00:00
static void *exception_handler(void *argument) {
[iOS] New iOS 13 project to support Swift; Custom keyboard and touch mouse support (#13435) * Support for Swift, added emulator keyboard * fixed toggle key handling using magic number hack for now * fixed keyboard transparency slider for now with suboptimal fix; add gesture recognizer to hide/show keyboard * Support CocoaView extensions in Swift; move keyboard delegate impl and setup to swift extension * moved keyboard view model creation out of EmulatorKeyboard * implement key pressed delegate in swift extension * added input method for directly sending RETROK_* codes to support a touchscreen keyboard; assign keyboard model delegates; updated keyboard layout (added F1-F12 keys); change shift, control and alt keys to be modifiers * enable focus mode when custom keyboard is shown; enable/disable overlay when custom keyboard is toggled * Specify -DHAVE_OPENGLES2 instead of -DHAVE_OPENGLES3 since glsym_es3.h does not compile in iOS 14.5 Fix tvOS build using compiler flags * Create new project for iOS 13 deploy target; add check for deploy target to conditionally compile for new iOS 13 specific feature (custom keyboard) * force disable core info caching for iOS, use opengl es2 for debug * Add flag for iOS custom keyboard - 3-finger swipe up to show, 3-finger swipe down to hide * use OpenGLES2 instead; using ES3 results in compile time errors on iOS 14.5 * code cleanup * Updated references to -DDONT_WANT_ARM_ASM_OPTIMIZATIONS flag * Add JIT support for non-jailbroken devices * iOS: Add support for touch mouse handler * Added a HAVE_IOS_TOUCHMOUSE preprocessor macro so that it builds under the iOS11_Metal xcode project * Changed click-and-drag behavior to double tap hold and drag * Visual improvements to the emulator keyboard: updated colors, improved key-press effect
2022-02-22 07:21:34 +00:00
mach_port_t port = *(mach_port_t *)argument;
mach_msg_server(exc_server, 2048, port, 0);
return NULL;
}
2024-09-14 20:11:20 +00:00
#endif
[iOS] New iOS 13 project to support Swift; Custom keyboard and touch mouse support (#13435) * Support for Swift, added emulator keyboard * fixed toggle key handling using magic number hack for now * fixed keyboard transparency slider for now with suboptimal fix; add gesture recognizer to hide/show keyboard * Support CocoaView extensions in Swift; move keyboard delegate impl and setup to swift extension * moved keyboard view model creation out of EmulatorKeyboard * implement key pressed delegate in swift extension * added input method for directly sending RETROK_* codes to support a touchscreen keyboard; assign keyboard model delegates; updated keyboard layout (added F1-F12 keys); change shift, control and alt keys to be modifiers * enable focus mode when custom keyboard is shown; enable/disable overlay when custom keyboard is toggled * Specify -DHAVE_OPENGLES2 instead of -DHAVE_OPENGLES3 since glsym_es3.h does not compile in iOS 14.5 Fix tvOS build using compiler flags * Create new project for iOS 13 deploy target; add check for deploy target to conditionally compile for new iOS 13 specific feature (custom keyboard) * force disable core info caching for iOS, use opengl es2 for debug * Add flag for iOS custom keyboard - 3-finger swipe up to show, 3-finger swipe down to hide * use OpenGLES2 instead; using ES3 results in compile time errors on iOS 14.5 * code cleanup * Updated references to -DDONT_WANT_ARM_ASM_OPTIMIZATIONS flag * Add JIT support for non-jailbroken devices * iOS: Add support for touch mouse handler * Added a HAVE_IOS_TOUCHMOUSE preprocessor macro so that it builds under the iOS11_Metal xcode project * Changed click-and-drag behavior to double tap hold and drag * Visual improvements to the emulator keyboard: updated colors, improved key-press effect
2022-02-22 07:21:34 +00:00
static bool jb_has_debugger_attached(void) {
[iOS] New iOS 13 project to support Swift; Custom keyboard and touch mouse support (#13435) * Support for Swift, added emulator keyboard * fixed toggle key handling using magic number hack for now * fixed keyboard transparency slider for now with suboptimal fix; add gesture recognizer to hide/show keyboard * Support CocoaView extensions in Swift; move keyboard delegate impl and setup to swift extension * moved keyboard view model creation out of EmulatorKeyboard * implement key pressed delegate in swift extension * added input method for directly sending RETROK_* codes to support a touchscreen keyboard; assign keyboard model delegates; updated keyboard layout (added F1-F12 keys); change shift, control and alt keys to be modifiers * enable focus mode when custom keyboard is shown; enable/disable overlay when custom keyboard is toggled * Specify -DHAVE_OPENGLES2 instead of -DHAVE_OPENGLES3 since glsym_es3.h does not compile in iOS 14.5 Fix tvOS build using compiler flags * Create new project for iOS 13 deploy target; add check for deploy target to conditionally compile for new iOS 13 specific feature (custom keyboard) * force disable core info caching for iOS, use opengl es2 for debug * Add flag for iOS custom keyboard - 3-finger swipe up to show, 3-finger swipe down to hide * use OpenGLES2 instead; using ES3 results in compile time errors on iOS 14.5 * code cleanup * Updated references to -DDONT_WANT_ARM_ASM_OPTIMIZATIONS flag * Add JIT support for non-jailbroken devices * iOS: Add support for touch mouse handler * Added a HAVE_IOS_TOUCHMOUSE preprocessor macro so that it builds under the iOS11_Metal xcode project * Changed click-and-drag behavior to double tap hold and drag * Visual improvements to the emulator keyboard: updated colors, improved key-press effect
2022-02-22 07:21:34 +00:00
int flags;
return !csops(getpid(), CS_OPS_STATUS, &flags, sizeof(flags)) && flags & CS_DEBUGGED;
}
bool jb_enable_ptrace_hack(void) {
2023-05-08 14:29:40 +00:00
#if !TARGET_OS_TV
[iOS] New iOS 13 project to support Swift; Custom keyboard and touch mouse support (#13435) * Support for Swift, added emulator keyboard * fixed toggle key handling using magic number hack for now * fixed keyboard transparency slider for now with suboptimal fix; add gesture recognizer to hide/show keyboard * Support CocoaView extensions in Swift; move keyboard delegate impl and setup to swift extension * moved keyboard view model creation out of EmulatorKeyboard * implement key pressed delegate in swift extension * added input method for directly sending RETROK_* codes to support a touchscreen keyboard; assign keyboard model delegates; updated keyboard layout (added F1-F12 keys); change shift, control and alt keys to be modifiers * enable focus mode when custom keyboard is shown; enable/disable overlay when custom keyboard is toggled * Specify -DHAVE_OPENGLES2 instead of -DHAVE_OPENGLES3 since glsym_es3.h does not compile in iOS 14.5 Fix tvOS build using compiler flags * Create new project for iOS 13 deploy target; add check for deploy target to conditionally compile for new iOS 13 specific feature (custom keyboard) * force disable core info caching for iOS, use opengl es2 for debug * Add flag for iOS custom keyboard - 3-finger swipe up to show, 3-finger swipe down to hide * use OpenGLES2 instead; using ES3 results in compile time errors on iOS 14.5 * code cleanup * Updated references to -DDONT_WANT_ARM_ASM_OPTIMIZATIONS flag * Add JIT support for non-jailbroken devices * iOS: Add support for touch mouse handler * Added a HAVE_IOS_TOUCHMOUSE preprocessor macro so that it builds under the iOS11_Metal xcode project * Changed click-and-drag behavior to double tap hold and drag * Visual improvements to the emulator keyboard: updated colors, improved key-press effect
2022-02-22 07:21:34 +00:00
bool debugged = jb_has_debugger_attached();
// Thanks to this comment: https://news.ycombinator.com/item?id=18431524
// We use this hack to allow mmap with PROT_EXEC (which usually requires the
// dynamic-codesigning entitlement) by tricking the process into thinking
// that Xcode is debugging it. We abuse the fact that JIT is needed to
// debug the process.
if (ptrace(PT_TRACE_ME, 0, NULL, 0) < 0) {
return false;
}
// ptracing ourselves confuses the kernel and will cause bad things to
// happen to the system (hangs) if an exception or signal occurs. Setup
// some "safety nets" so we can cause the process to exit in a somewhat sane
// state. We only need to do this if the debugger isn't attached. (It'll do
// this itself, and if we do it we'll interfere with its normal operation
// anyways.)
if (!debugged) {
// First, ensure that signals are delivered as Mach software exceptions
ptrace(PT_SIGEXC, 0, NULL, 0);
// then ensure that this exception goes through our exception handler.
// I think it's OK to just watch for EXC_SOFTWARE because the other
// exceptions (e.g. EXC_BAD_ACCESS, EXC_BAD_INSTRUCTION, and friends)
// will end up being delivered as signals anyways, and we can get them
// once they're resent as a software exception.
mach_port_t port = MACH_PORT_NULL;
mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port);
mach_port_insert_right(mach_task_self(), port, port, MACH_MSG_TYPE_MAKE_SEND);
task_set_exception_ports(mach_task_self(), EXC_MASK_SOFTWARE, port, EXCEPTION_DEFAULT, THREAD_STATE_NONE);
pthread_t thread;
pthread_create(&thread, NULL, exception_handler, (void *)&port);
} else {
// JIT code frequently causes an EXC_BAD_ACCESS exception that lldb
// cannot be convinced to ignore. Instead we can set up a nul handler
// that effectively causes it to be ignored. Note that this sometimes
// also hides actual crashes from the debugger.
task_set_exception_ports(mach_task_self(), EXC_MASK_BAD_ACCESS, MACH_PORT_NULL, EXCEPTION_DEFAULT, THREAD_STATE_NONE);
[iOS] New iOS 13 project to support Swift; Custom keyboard and touch mouse support (#13435) * Support for Swift, added emulator keyboard * fixed toggle key handling using magic number hack for now * fixed keyboard transparency slider for now with suboptimal fix; add gesture recognizer to hide/show keyboard * Support CocoaView extensions in Swift; move keyboard delegate impl and setup to swift extension * moved keyboard view model creation out of EmulatorKeyboard * implement key pressed delegate in swift extension * added input method for directly sending RETROK_* codes to support a touchscreen keyboard; assign keyboard model delegates; updated keyboard layout (added F1-F12 keys); change shift, control and alt keys to be modifiers * enable focus mode when custom keyboard is shown; enable/disable overlay when custom keyboard is toggled * Specify -DHAVE_OPENGLES2 instead of -DHAVE_OPENGLES3 since glsym_es3.h does not compile in iOS 14.5 Fix tvOS build using compiler flags * Create new project for iOS 13 deploy target; add check for deploy target to conditionally compile for new iOS 13 specific feature (custom keyboard) * force disable core info caching for iOS, use opengl es2 for debug * Add flag for iOS custom keyboard - 3-finger swipe up to show, 3-finger swipe down to hide * use OpenGLES2 instead; using ES3 results in compile time errors on iOS 14.5 * code cleanup * Updated references to -DDONT_WANT_ARM_ASM_OPTIMIZATIONS flag * Add JIT support for non-jailbroken devices * iOS: Add support for touch mouse handler * Added a HAVE_IOS_TOUCHMOUSE preprocessor macro so that it builds under the iOS11_Metal xcode project * Changed click-and-drag behavior to double tap hold and drag * Visual improvements to the emulator keyboard: updated colors, improved key-press effect
2022-02-22 07:21:34 +00:00
}
2023-05-08 14:29:40 +00:00
#endif
[iOS] New iOS 13 project to support Swift; Custom keyboard and touch mouse support (#13435) * Support for Swift, added emulator keyboard * fixed toggle key handling using magic number hack for now * fixed keyboard transparency slider for now with suboptimal fix; add gesture recognizer to hide/show keyboard * Support CocoaView extensions in Swift; move keyboard delegate impl and setup to swift extension * moved keyboard view model creation out of EmulatorKeyboard * implement key pressed delegate in swift extension * added input method for directly sending RETROK_* codes to support a touchscreen keyboard; assign keyboard model delegates; updated keyboard layout (added F1-F12 keys); change shift, control and alt keys to be modifiers * enable focus mode when custom keyboard is shown; enable/disable overlay when custom keyboard is toggled * Specify -DHAVE_OPENGLES2 instead of -DHAVE_OPENGLES3 since glsym_es3.h does not compile in iOS 14.5 Fix tvOS build using compiler flags * Create new project for iOS 13 deploy target; add check for deploy target to conditionally compile for new iOS 13 specific feature (custom keyboard) * force disable core info caching for iOS, use opengl es2 for debug * Add flag for iOS custom keyboard - 3-finger swipe up to show, 3-finger swipe down to hide * use OpenGLES2 instead; using ES3 results in compile time errors on iOS 14.5 * code cleanup * Updated references to -DDONT_WANT_ARM_ASM_OPTIMIZATIONS flag * Add JIT support for non-jailbroken devices * iOS: Add support for touch mouse handler * Added a HAVE_IOS_TOUCHMOUSE preprocessor macro so that it builds under the iOS11_Metal xcode project * Changed click-and-drag behavior to double tap hold and drag * Visual improvements to the emulator keyboard: updated colors, improved key-press effect
2022-02-22 07:21:34 +00:00
return true;
}
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();
}