]> git.saurik.com Git - cycript.git/blob - Trampoline.t.cpp
Rule #7
[cycript.git] / Trampoline.t.cpp
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2010 Jay Freeman (saurik)
3 */
4
5 /* GNU Lesser General Public License, Version 3 {{{ */
6 /*
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.
11 *
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.
16 *
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 **/
20 /* }}} */
21
22 #define _PTHREAD_ATTR_T
23 #include <pthread_internals.h>
24
25 #include "Standard.hpp"
26 #include "Baton.hpp"
27
28 template <typename Type_>
29 static _finline void dlset(Baton *baton, Type_ &function, const char *name, void *handle = RTLD_DEFAULT) {
30 function = reinterpret_cast<Type_>(baton->dlsym(handle, name));
31 if (function == NULL)
32 baton->dlerror();
33 }
34
35 #define Framework(framework) \
36 "/System/Library/Frameworks/" #framework ".framework/" #framework
37
38 void *Routine(void *arg) {
39 Baton *baton(reinterpret_cast<Baton *>(arg));
40
41 void *(*dlopen)(const char *, int);
42 dlset(baton, dlopen, "dlopen");
43
44 if (baton->dlsym(RTLD_DEFAULT, "JSEvaluateScript") == NULL)
45 dlopen(Framework(JavaScriptCore), RTLD_GLOBAL | RTLD_LAZY);
46
47 void *(*objc_getClass)(const char *);
48 dlset(baton, objc_getClass, "objc_getClass");
49
50 if (objc_getClass("WebUndefined") == NULL)
51 dlopen(Framework(WebKit), RTLD_GLOBAL | RTLD_LAZY);
52
53 void *handle(dlopen(baton->library, RTLD_LAZY | RTLD_LOCAL));
54 if (handle == NULL) {
55 baton->dlerror();
56 return NULL;
57 }
58
59 void (*CYHandleServer)(pid_t);
60 dlset(baton, CYHandleServer, "CYHandleServer", handle);
61
62 CYHandleServer(baton->pid);
63
64 return NULL;
65 }
66
67 static void $bzero(void *data, size_t size) {
68 char *bytes(reinterpret_cast<char *>(data));
69 for (size_t i(0); i != size; ++i)
70 bytes[i] = 0;
71 }
72
73 extern "C" void Start(Baton *baton) {
74 struct _pthread self;
75 $bzero(&self, sizeof(self));
76
77 // this code comes from _pthread_set_self
78 self.tsd[0] = &self;
79 baton->__pthread_set_self(&self);
80
81 int (*pthread_create)(pthread_t *, const pthread_attr_t *, void *(*)(void *), void *);
82 dlset(baton, pthread_create, "pthread_create");
83
84 pthread_t thread;
85 baton->pthread_create(&thread, NULL, &Routine, baton);
86
87 int (*pthread_join)(pthread_t, void **);
88 dlset(baton, pthread_join, "pthread_join");
89
90 void *result;
91 baton->pthread_join(thread, &result);
92
93 mach_port_t (*mach_thread_self)();
94 dlset(baton, mach_thread_self, "mach_thread_self");
95
96 kern_return_t (*thread_terminate)(thread_act_t);
97 dlset(baton, thread_terminate, "thread_terminate");
98
99 baton->thread_terminate(baton->mach_thread_self());
100 }