RetroArch/pkg/apple/JITSupport.m

113 lines
4.3 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-21 21:21:34 -10: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>
#include <dlfcn.h>
#include <mach/mach.h>
#include <mach-o/loader.h>
#include <mach-o/getsect.h>
#include <pthread.h>
#if defined(HAVE_ALTKIT)
@import AltKit;
#endif
#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-21 21:21:34 -10: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 */
static void *exception_handler(void *argument) {
mach_port_t port = *(mach_port_t *)argument;
mach_msg_server(exc_server, 2048, port, 0);
return NULL;
}
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-21 21:21:34 -10:00
int flags;
return !csops(getpid(), CS_OPS_STATUS, &flags, sizeof(flags)) && flags & CS_DEBUGGED;
}
bool jb_enable_ptrace_hack(void) {
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-21 21:21:34 -10:00
}
return true;
}
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())
return;
[[ALTServerManager sharedManager] autoconnectWithCompletionHandler:^(ALTServerConnection *connection, NSError *error) {
if (error)
return;
[connection enableUnsignedCodeExecutionWithCompletionHandler:^(BOOL success, NSError *error) {
if (success)
[[ALTServerManager sharedManager] stopDiscovering];
else
RARCH_WARN("AltServer failed: %s\n", [error.description UTF8String]);
[connection disconnect];
}];
}];
[[ALTServerManager sharedManager] startDiscovering];
#endif
}