Skip to content

Commit

Permalink
Merge pull request hrydgard#12421 from sbingner/jitfix
Browse files Browse the repository at this point in the history
Update iOS JIT hack
  • Loading branch information
hrydgard authored Oct 15, 2019
2 parents aca18a1 + e3971dc commit 73bf609
Showing 1 changed file with 49 additions and 11 deletions.
60 changes: 49 additions & 11 deletions ios/main.mm
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,53 @@
#include "base/NativeApp.h"
#include "profiler/profiler.h"

#define CS_OPS_STATUS 0 /* return status */
#define CS_DEBUGGED 0x10000000 /* process is currently or has previously been debugged and allowed to run with invalid pages */
#define CS_OPS_STATUS 0 /* return status */
#define CS_DEBUGGED 0x10000000 /* process is currently or has previously been debugged and allowed to run with invalid pages */
#define PT_ATTACHEXC 14 /* attach to running process with signal exception */
#define PT_DETACH 11 /* stop tracing a process */
int csops(pid_t pid, unsigned int ops, void * useraddr, size_t usersize);
#define ptrace(a, b, c, d) syscall(SYS_ptrace, a, b, c, d)

bool get_debugged() {
int flags;
int rv = csops(getpid(), CS_OPS_STATUS, &flags, sizeof(flags));
if (rv==0 && flags&CS_DEBUGGED) return true;

pid_t pid = fork();
if (pid > 0) {
int st,rv,i=0;
do {
usleep(500);
rv = waitpid(pid, &st, 0);
} while (rv<0 && i++<10);
if (rv<0) fprintf(stderr, "Unable to wait for child?\n");
} else if (pid == 0) {
pid_t ppid = getppid();
int rv = ptrace(PT_ATTACHEXC, ppid, 0, 0);
if (rv) {
perror("Unable to attach to process");
exit(1);
}
for (int i=0; i<100; i++) {
usleep(1000);
errno = 0;
rv = ptrace(PT_DETACH, ppid, 0, 0);
if (rv==0) break;
}
if (rv) {
perror("Unable to detach from process");
exit(1);
}
exit(0);
} else {
perror("Unable to fork");
}

rv = csops(getpid(), CS_OPS_STATUS, &flags, sizeof(flags));
if (rv==0 && flags&CS_DEBUGGED) return true;

return false;
}


std::string System_GetProperty(SystemProperty prop) {
Expand Down Expand Up @@ -118,15 +162,9 @@ void Vibrate(int mode) {

int main(int argc, char *argv[])
{
// see https://github.com/hrydgard/ppsspp/issues/11905#issuecomment-476871010
uint32_t flags;
csops(getpid(), CS_OPS_STATUS, &flags, 0);
if (flags & CS_DEBUGGED){
//being run either under a debugger or under Electra already
}
else{
// Simulates a debugger. Makes it possible to use JIT (though only W^X)
syscall(SYS_ptrace, 0 /*PTRACE_TRACEME*/, 0, 0, 0);
// see https://github.com/hrydgard/ppsspp/issues/11905
if (!get_debugged()) {
fprintf(stderr, "Unable to obtain CS_DEBUGGED - I will probably die now.\n");
}

PROFILE_INIT();
Expand Down

0 comments on commit 73bf609

Please sign in to comment.