]> git.saurik.com Git - cycript.git/blob - Trampoline.t.cpp
CYONify pointers as the address of their values.
[cycript.git] / Trampoline.t.cpp
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2013 Jay Freeman (saurik)
3 */
4
5 /* GNU General Public License, Version 3 {{{ */
6 /*
7 * Cycript is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation, either version 3 of the License,
10 * or (at your option) any later version.
11 *
12 * Cycript is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
19 **/
20 /* }}} */
21
22 #include <TargetConditionals.h>
23 #if defined(__arm__) || defined(__arm64__)
24 #undef TARGET_IPHONE_SIMULATOR
25 #define TARGET_IPHONE_SIMULATOR 1
26 #endif
27 #define _PTHREAD_ATTR_T
28 #include <pthread_internals.h>
29 #if defined(__arm__) || defined(__arm64__)
30 #undef TARGET_IPHONE_SIMULATOR
31 #endif
32
33 #include <mach-o/dyld.h>
34 #include <mach-o/dyld_images.h>
35 #include <mach-o/loader.h>
36 #include <mach-o/nlist.h>
37
38 #include "Standard.hpp"
39 #include "Baton.hpp"
40
41 static void $bzero(void *data, size_t size) {
42 char *bytes(reinterpret_cast<char *>(data));
43 for (size_t i(0); i != size; ++i)
44 bytes[i] = 0;
45 }
46
47 static int $strcmp(const char *lhs, const char *rhs) {
48 while (*lhs == *rhs) {
49 if (*lhs == '\0')
50 return 0;
51 ++lhs, ++rhs;
52 } return *lhs < *rhs ? -1 : 1;
53 }
54
55 static void $strlcpy(char *dst, const char *src, size_t size) {
56 if (size == 0)
57 return;
58 size_t i(0);
59 while (i != size - 1) {
60 char value(src[i]);
61 if (value == '\0')
62 break;
63 dst[i++] = value;
64 } dst[i] = '\0';
65 }
66
67 #ifdef __LP64__
68 typedef struct mach_header_64 mach_header_xx;
69 typedef struct nlist_64 nlist_xx;
70 typedef struct segment_command_64 segment_command_xx;
71
72 static const uint32_t LC_SEGMENT_XX = LC_SEGMENT_64;
73 static const uint32_t MH_MAGIC_XX = MH_MAGIC_64;
74 #else
75 typedef struct mach_header mach_header_xx;
76 typedef struct nlist nlist_xx;
77 typedef struct segment_command segment_command_xx;
78
79 static const uint32_t LC_SEGMENT_XX = LC_SEGMENT;
80 static const uint32_t MH_MAGIC_XX = MH_MAGIC;
81 #endif
82
83 #define forlc(command, mach, lc, type) \
84 if (const struct load_command *load_commands = reinterpret_cast<const struct load_command *>(mach + 1)) \
85 if (const struct load_command *lcp = load_commands) \
86 for (uint32_t i(0); i != mach->ncmds; ++i, lcp = reinterpret_cast<const struct load_command *>(reinterpret_cast<const uint8_t *>(lcp) + lcp->cmdsize)) \
87 if ( \
88 lcp->cmdsize % sizeof(long) != 0 || lcp->cmdsize <= 0 || \
89 reinterpret_cast<const uint8_t *>(lcp) + lcp->cmdsize > reinterpret_cast<const uint8_t *>(load_commands) + mach->sizeofcmds \
90 ) \
91 return NULL; \
92 else if (lcp->cmd != lc) \
93 continue; \
94 else if (lcp->cmdsize < sizeof(type)) \
95 return NULL; \
96 else if (const type *command = reinterpret_cast<const type *>(lcp))
97
98 static const mach_header_xx *Library(struct dyld_all_image_infos *infos, const char *name) {
99 for (uint32_t i(0); i != infos->infoArrayCount; ++i) {
100 const dyld_image_info &info(infos->infoArray[i]);
101 const mach_header_xx *mach(reinterpret_cast<const mach_header_xx *>(info.imageLoadAddress));
102 if (mach->magic != MH_MAGIC_XX)
103 continue;
104
105 const char *path(info.imageFilePath);
106 forlc (dylib, mach, LC_ID_DYLIB, dylib_command)
107 path = reinterpret_cast<const char *>(dylib) + dylib->dylib.name.offset;
108 if ($strcmp(path, name) != 0)
109 continue;
110
111 return mach;
112 }
113
114 return NULL;
115 }
116
117 static void *Symbol(const mach_header_xx *mach, const char *name) {
118 const struct symtab_command *stp(NULL);
119 forlc (command, mach, LC_SYMTAB, struct symtab_command)
120 stp = command;
121 if (stp == NULL)
122 return NULL;
123
124 size_t slide(_not(size_t));
125 const nlist_xx *symbols(NULL);
126 const char *strings(NULL);
127
128 forlc (segment, mach, LC_SEGMENT_XX, segment_command_xx) {
129 if (segment->fileoff == 0)
130 slide = reinterpret_cast<size_t>(mach) - segment->vmaddr;
131 if (stp->symoff >= segment->fileoff && stp->symoff < segment->fileoff + segment->filesize)
132 symbols = reinterpret_cast<const nlist_xx *>(stp->symoff - segment->fileoff + segment->vmaddr + slide);
133 if (stp->stroff >= segment->fileoff && stp->stroff < segment->fileoff + segment->filesize)
134 strings = reinterpret_cast<const char *>(stp->stroff - segment->fileoff + segment->vmaddr + slide);
135 }
136
137 if (slide == _not(size_t) || symbols == NULL || strings == NULL)
138 return NULL;
139
140 for (size_t i(0); i != stp->nsyms; ++i) {
141 const nlist_xx *symbol(&symbols[i]);
142 if (symbol->n_un.n_strx == 0 || (symbol->n_type & N_STAB) != 0)
143 continue;
144
145 const char *nambuf(strings + symbol->n_un.n_strx);
146 if ($strcmp(name, nambuf) != 0)
147 continue;
148
149 uintptr_t value(symbol->n_value);
150 if (value == 0)
151 continue;
152
153 #ifdef __arm__
154 if ((symbol->n_desc & N_ARM_THUMB_DEF) != 0)
155 value |= 0x00000001;
156 #endif
157
158 value += slide;
159 return reinterpret_cast<void *>(value);
160 }
161
162 return NULL;
163 }
164
165 template <typename Type_>
166 static _finline void cyset(Type_ &function, const char *name, const mach_header_xx *mach) {
167 function = reinterpret_cast<Type_>(Symbol(mach, name));
168 }
169
170 static _finline const mach_header_xx *Library(Baton *baton, const char *name) {
171 struct dyld_all_image_infos *infos(reinterpret_cast<struct dyld_all_image_infos *>(baton->dyld));
172 return Library(infos, name);
173 }
174
175 void *Routine(void *arg) {
176 Baton *baton(reinterpret_cast<Baton *>(arg));
177
178 const mach_header_xx *dyld(NULL);
179 if (dyld == NULL)
180 dyld = Library(baton, "/usr/lib/system/libdyld.dylib");
181 if (dyld == NULL)
182 dyld = Library(baton, "/usr/lib/libSystem.B.dylib");
183
184 char *(*$dlerror)();
185 cyset($dlerror, "_dlerror", dyld);
186
187 void *(*$dlopen)(const char *, int);
188 cyset($dlopen, "_dlopen", dyld);
189
190 void *handle($dlopen(baton->library, RTLD_LAZY | RTLD_LOCAL));
191 if (handle == NULL) {
192 $strlcpy(baton->error, $dlerror(), sizeof(baton->error));
193 return NULL;
194 }
195
196 void *(*$dlsym)(void *, const char *);
197 cyset($dlsym, "_dlsym", dyld);
198
199 void (*CYHandleServer)(pid_t, char *, size_t);
200 CYHandleServer = reinterpret_cast<void (*)(pid_t, char *, size_t)>($dlsym(handle, "CYHandleServer"));
201 if (CYHandleServer == NULL) {
202 $strlcpy(baton->error, $dlerror(), sizeof(baton->error));
203 return NULL;
204 }
205
206 CYHandleServer(baton->pid, baton->error, sizeof(baton->error));
207 return NULL;
208 }
209
210 extern "C" void Start(Baton *baton) {
211 struct _pthread self;
212 $bzero(&self, sizeof(self));
213
214 const mach_header_xx *pthread(NULL);
215 if (pthread == NULL)
216 pthread = Library(baton, "/usr/lib/system/libsystem_pthread.dylib");
217 if (pthread == NULL)
218 pthread = Library(baton, "/usr/lib/system/libsystem_c.dylib");
219 if (pthread == NULL)
220 pthread = Library(baton, "/usr/lib/libSystem.B.dylib");
221
222 void (*$__pthread_set_self)(void **);
223 cyset($__pthread_set_self, "___pthread_set_self", pthread);
224
225 self.tsd[0] = &self;
226 $__pthread_set_self(&self.tsd[0]);
227
228 int (*$pthread_attr_init)(pthread_attr_t *);
229 cyset($pthread_attr_init, "_pthread_attr_init", pthread);
230
231 #if 0
232 pthread_attr_t attr;
233 $pthread_attr_init(&attr);
234
235 int (*$pthread_attr_setdetachstate)(pthread_attr_t *, int);
236 cyset($pthread_attr_setdetachstate, "_pthread_attr_setdetachstate", pthread);
237
238 $pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
239 #endif
240
241 int (*$pthread_create)(pthread_t *, const pthread_attr_t *, void *(*)(void *), void *);
242 cyset($pthread_create, "_pthread_create", pthread);
243
244 pthread_t thread;
245 $pthread_create(&thread, NULL, &Routine, baton);
246
247 #if 0
248 int (*$pthread_attr_destroy)(pthread_attr_t *);
249 cyset($pthread_attr_destroy, "_pthread_attr_destroy", pthread);
250
251 $pthread_attr_destroy(&attr);
252 #endif
253
254 int (*$pthread_join)(pthread_t, void **);
255 cyset($pthread_join, "_pthread_join", pthread);
256
257 void *status;
258 $pthread_join(thread, &status);
259
260 const mach_header_xx *kernel(NULL);
261 if (kernel == NULL)
262 kernel = Library(baton, "/usr/lib/system/libsystem_kernel.dylib");
263 if (kernel == NULL)
264 kernel = Library(baton, "/usr/lib/libSystem.B.dylib");
265
266 mach_port_t (*$mach_thread_self)();
267 cyset($mach_thread_self, "_mach_thread_self", kernel);
268
269 kern_return_t (*$thread_terminate)(thread_act_t);
270 cyset($thread_terminate, "_thread_terminate", kernel);
271
272 $thread_terminate($mach_thread_self());
273 }