]> git.saurik.com Git - cycript.git/blame - Trampoline.t.cpp
Highlight NULL, YES, and NO identifiers as constants.
[cycript.git] / Trampoline.t.cpp
CommitLineData
b3378a02 1/* Cycript - Optimizing JavaScript Compiler/Runtime
8d7447c1 2 * Copyright (C) 2009-2012 Jay Freeman (saurik)
e91fbe93
JF
3*/
4
b3378a02 5/* GNU Lesser General Public License, Version 3 {{{ */
e91fbe93 6/*
b3378a02
JF
7 * Cycript is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU Lesser General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
e91fbe93 11 *
b3378a02
JF
12 * Cycript is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
e91fbe93 16 *
b3378a02
JF
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
19**/
e91fbe93
JF
20/* }}} */
21
b6961e53
JF
22#define _PTHREAD_ATTR_T
23#include <pthread_internals.h>
24
eed4f174 25#include "Standard.hpp"
b6961e53
JF
26#include "Baton.hpp"
27
eed4f174
JF
28template <typename Type_>
29static _finline void dlset(Baton *baton, Type_ &function, const char *name, void *handle = RTLD_DEFAULT) {
30 function = reinterpret_cast<Type_>(baton->dlsym(handle, name));
95a2c7e5
JF
31 if (function == NULL)
32 baton->dlerror();
eed4f174
JF
33}
34
6aeaa7d2
JF
35// XXX: where you find this needs to be relative to CoreFoundation (or something)
36// XXX: this needs to check if the framework is under PrivateFrameworks instead
7cdfdc9f
JF
37#define Framework(framework) \
38 "/System/Library/Frameworks/" #framework ".framework/" #framework
39
49e976ae 40void *Routine(void *arg) {
eed4f174
JF
41 Baton *baton(reinterpret_cast<Baton *>(arg));
42
40b1517d
JF
43 int (*pthread_detach)(pthread_t);
44 dlset(baton, pthread_detach, "pthread_detach");
45
46 pthread_t (*pthread_self)();
47 dlset(baton, pthread_self, "pthread_self");
48
49 pthread_detach(pthread_self());
50
eed4f174
JF
51 void *(*dlopen)(const char *, int);
52 dlset(baton, dlopen, "dlopen");
53
5d7cc6d5 54 if (baton->dlsym(RTLD_DEFAULT, "JSEvaluateScript") == NULL)
7cdfdc9f 55 dlopen(Framework(JavaScriptCore), RTLD_GLOBAL | RTLD_LAZY);
5d7cc6d5
JF
56
57 void *(*objc_getClass)(const char *);
58 dlset(baton, objc_getClass, "objc_getClass");
59
60 if (objc_getClass("WebUndefined") == NULL)
7cdfdc9f 61 dlopen(Framework(WebKit), RTLD_GLOBAL | RTLD_LAZY);
5d7cc6d5 62
eed4f174 63 void *handle(dlopen(baton->library, RTLD_LAZY | RTLD_LOCAL));
95a2c7e5
JF
64 if (handle == NULL) {
65 baton->dlerror();
66 return NULL;
67 }
eed4f174
JF
68
69 void (*CYHandleServer)(pid_t);
70 dlset(baton, CYHandleServer, "CYHandleServer", handle);
6e51aaf8
JF
71 if (CYHandleServer == NULL) {
72 baton->dlerror();
73 return NULL;
74 }
eed4f174 75
6e51aaf8 76 CYHandleServer(baton->pid);
95a2c7e5 77 return NULL;
eed4f174
JF
78}
79
49e976ae
JF
80static void $bzero(void *data, size_t size) {
81 char *bytes(reinterpret_cast<char *>(data));
82 for (size_t i(0); i != size; ++i)
83 bytes[i] = 0;
84}
85
86extern "C" void Start(Baton *baton) {
87 struct _pthread self;
88 $bzero(&self, sizeof(self));
89
90 // this code comes from _pthread_set_self
91 self.tsd[0] = &self;
92 baton->__pthread_set_self(&self);
eed4f174 93
c98ea90d
JF
94 //int (*pthread_create)(pthread_t *, const pthread_attr_t *, void *(*)(void *), void *);
95 //dlset(baton, pthread_create, "pthread_create");
b6961e53
JF
96
97 pthread_t thread;
c98ea90d 98 baton->pthread_create(&thread, NULL, &Routine, baton);
b6961e53 99
c98ea90d
JF
100 //mach_port_t (*mach_thread_self)();
101 //dlset(baton, mach_thread_self, "mach_thread_self");
eed4f174 102
c98ea90d
JF
103 //kern_return_t (*thread_terminate)(thread_act_t);
104 //dlset(baton, thread_terminate, "thread_terminate");
49e976ae 105
c98ea90d 106 baton->thread_terminate(baton->mach_thread_self());
b6961e53 107}