]> git.saurik.com Git - cycript.git/blame - Trampoline.t.cpp
Fix for-of loops (an internal variable was wrong).
[cycript.git] / Trampoline.t.cpp
CommitLineData
b3378a02 1/* Cycript - Optimizing JavaScript Compiler/Runtime
f95d2598 2 * Copyright (C) 2009-2014 Jay Freeman (saurik)
e91fbe93
JF
3*/
4
f95d2598 5/* GNU Affero General Public License, Version 3 {{{ */
e91fbe93 6/*
f95d2598
JF
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
c15969fd 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f95d2598
JF
15 * GNU Affero General Public License for more details.
16
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
b3378a02 19**/
e91fbe93
JF
20/* }}} */
21
3615a2f7 22#include <TargetConditionals.h>
e1858938 23#if defined(__arm__) || defined(__arm64__)
3615a2f7
JF
24#undef TARGET_IPHONE_SIMULATOR
25#define TARGET_IPHONE_SIMULATOR 1
e1858938 26#endif
b6961e53
JF
27#define _PTHREAD_ATTR_T
28#include <pthread_internals.h>
e1858938 29#if defined(__arm__) || defined(__arm64__)
3615a2f7 30#undef TARGET_IPHONE_SIMULATOR
e1858938 31#endif
b6961e53 32
38ae783d
JF
33#include <mach-o/dyld.h>
34#include <mach-o/dyld_images.h>
35#include <mach-o/loader.h>
38ae783d 36#include <mach-o/nlist.h>
38ae783d 37
eed4f174 38#include "Standard.hpp"
b6961e53
JF
39#include "Baton.hpp"
40
38ae783d
JF
41static 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
3d1db6b9
JF
47__attribute__((__unused__))
48static void $memcpy(char *dst, const char *src, size_t size) {
49 for (size_t i(0); i != size; ++i)
50 dst[i] = src[i];
51}
52
38ae783d
JF
53static int $strcmp(const char *lhs, const char *rhs) {
54 while (*lhs == *rhs) {
55 if (*lhs == '\0')
56 return 0;
57 ++lhs, ++rhs;
58 } return *lhs < *rhs ? -1 : 1;
59}
60
8964aa08 61static void $strlcpy(char *dst, const char *src, size_t size) {
f6fa3c21
JF
62 if (src == NULL)
63 src = "(null)";
8964aa08
JF
64 if (size == 0)
65 return;
66 size_t i(0);
67 while (i != size - 1) {
68 char value(src[i]);
69 if (value == '\0')
70 break;
71 dst[i++] = value;
72 } dst[i] = '\0';
73}
74
3d1db6b9
JF
75__attribute__((__unused__))
76static char *$strstr(const char *haystack, const char *needle) {
77 if (*needle == '\0')
78 return NULL;
79 for (; *haystack != '\0'; ++haystack)
80 for (size_t i(0); ; ++i)
81 if (needle[i] == '\0')
82 return const_cast<char *>(haystack);
83 else if (needle[i] != haystack[i])
84 break;
85 return NULL;
86}
87
88__attribute__((__unused__))
89static size_t $strlen(const char *data) {
90 for (size_t i(0); ; ++i)
91 if (data[i] == '\0')
92 return i;
93}
94
d24f0f42
JF
95__attribute__((__unused__))
96static void $snprintfp(char *dst, size_t size, const void *pointer) {
97 uintptr_t value(reinterpret_cast<uintptr_t>(pointer));
98 char buffer[32];
99 char *end(buffer + sizeof(buffer));
100 *--end = '\0';
101 if (value == 0)
102 *--end = '0';
103 else do {
104 unsigned digit(value & 0xf);
105 value >>= 4;
106 *--end = (digit < 10 ? '0' : 'a' - 10) + digit;
107 } while (value != 0);
108 *--end = 'x';
109 *--end = '0';
110 $strlcpy(dst, end, size);
111}
112
38ae783d
JF
113#ifdef __LP64__
114typedef struct mach_header_64 mach_header_xx;
115typedef struct nlist_64 nlist_xx;
116typedef struct segment_command_64 segment_command_xx;
117
118static const uint32_t LC_SEGMENT_XX = LC_SEGMENT_64;
119static const uint32_t MH_MAGIC_XX = MH_MAGIC_64;
120#else
121typedef struct mach_header mach_header_xx;
122typedef struct nlist nlist_xx;
123typedef struct segment_command segment_command_xx;
124
125static const uint32_t LC_SEGMENT_XX = LC_SEGMENT;
126static const uint32_t MH_MAGIC_XX = MH_MAGIC;
127#endif
128
129#define forlc(command, mach, lc, type) \
130 if (const struct load_command *load_commands = reinterpret_cast<const struct load_command *>(mach + 1)) \
131 if (const struct load_command *lcp = load_commands) \
132 for (uint32_t i(0); i != mach->ncmds; ++i, lcp = reinterpret_cast<const struct load_command *>(reinterpret_cast<const uint8_t *>(lcp) + lcp->cmdsize)) \
133 if ( \
134 lcp->cmdsize % sizeof(long) != 0 || lcp->cmdsize <= 0 || \
135 reinterpret_cast<const uint8_t *>(lcp) + lcp->cmdsize > reinterpret_cast<const uint8_t *>(load_commands) + mach->sizeofcmds \
136 ) \
48d5b4e3 137 break; \
38ae783d
JF
138 else if (lcp->cmd != lc) \
139 continue; \
140 else if (lcp->cmdsize < sizeof(type)) \
48d5b4e3 141 break; \
38ae783d
JF
142 else if (const type *command = reinterpret_cast<const type *>(lcp))
143
85b57b57
JF
144static const mach_header_xx *Library(struct dyld_all_image_infos *infos, const char *name) {
145 for (uint32_t i(0); i != infos->infoArrayCount; ++i) {
146 const dyld_image_info &info(infos->infoArray[i]);
147 const mach_header_xx *mach(reinterpret_cast<const mach_header_xx *>(info.imageLoadAddress));
148 if (mach->magic != MH_MAGIC_XX)
149 continue;
150
151 const char *path(info.imageFilePath);
152 forlc (dylib, mach, LC_ID_DYLIB, dylib_command)
153 path = reinterpret_cast<const char *>(dylib) + dylib->dylib.name.offset;
154 if ($strcmp(path, name) != 0)
155 continue;
156
157 return mach;
158 }
38ae783d 159
85b57b57
JF
160 return NULL;
161}
162
163static void *Symbol(const mach_header_xx *mach, const char *name) {
38ae783d
JF
164 const struct symtab_command *stp(NULL);
165 forlc (command, mach, LC_SYMTAB, struct symtab_command)
166 stp = command;
167 if (stp == NULL)
85b57b57 168 return NULL;
38ae783d
JF
169
170 size_t slide(_not(size_t));
171 const nlist_xx *symbols(NULL);
172 const char *strings(NULL);
173
174 forlc (segment, mach, LC_SEGMENT_XX, segment_command_xx) {
175 if (segment->fileoff == 0)
176 slide = reinterpret_cast<size_t>(mach) - segment->vmaddr;
177 if (stp->symoff >= segment->fileoff && stp->symoff < segment->fileoff + segment->filesize)
178 symbols = reinterpret_cast<const nlist_xx *>(stp->symoff - segment->fileoff + segment->vmaddr + slide);
179 if (stp->stroff >= segment->fileoff && stp->stroff < segment->fileoff + segment->filesize)
180 strings = reinterpret_cast<const char *>(stp->stroff - segment->fileoff + segment->vmaddr + slide);
181 }
182
183 if (slide == _not(size_t) || symbols == NULL || strings == NULL)
85b57b57 184 return NULL;
38ae783d
JF
185
186 for (size_t i(0); i != stp->nsyms; ++i) {
187 const nlist_xx *symbol(&symbols[i]);
188 if (symbol->n_un.n_strx == 0 || (symbol->n_type & N_STAB) != 0)
189 continue;
190
191 const char *nambuf(strings + symbol->n_un.n_strx);
192 if ($strcmp(name, nambuf) != 0)
193 continue;
194
195 uintptr_t value(symbol->n_value);
196 if (value == 0)
197 continue;
198
3615a2f7
JF
199#ifdef __arm__
200 if ((symbol->n_desc & N_ARM_THUMB_DEF) != 0)
201 value |= 0x00000001;
202#endif
203
38ae783d
JF
204 value += slide;
205 return reinterpret_cast<void *>(value);
206 }
85b57b57
JF
207
208 return NULL;
209}
38ae783d 210
38ae783d 211template <typename Type_>
85b57b57
JF
212static _finline void cyset(Type_ &function, const char *name, const mach_header_xx *mach) {
213 function = reinterpret_cast<Type_>(Symbol(mach, name));
214}
215
216static _finline const mach_header_xx *Library(Baton *baton, const char *name) {
38ae783d 217 struct dyld_all_image_infos *infos(reinterpret_cast<struct dyld_all_image_infos *>(baton->dyld));
85b57b57 218 return Library(infos, name);
eed4f174
JF
219}
220
3d1db6b9
JF
221#if defined(__i386__) || defined(__x86_64__)
222static bool Simulator(struct dyld_all_image_infos *infos) {
223 for (uint32_t i(0); i != infos->infoArrayCount; ++i) {
224 const dyld_image_info &info(infos->infoArray[i]);
225 const char *path(info.imageFilePath);
226 if ($strstr(path, "/SDKs/iPhoneSimulator") != NULL)
227 return true;
228 } return false;
229}
230
231static bool Simulator(Baton *baton) {
232 struct dyld_all_image_infos *infos(reinterpret_cast<struct dyld_all_image_infos *>(baton->dyld));
233 return Simulator(infos);
234}
235#endif
236
49e976ae 237void *Routine(void *arg) {
eed4f174
JF
238 Baton *baton(reinterpret_cast<Baton *>(arg));
239
3615a2f7
JF
240 const mach_header_xx *dyld(NULL);
241 if (dyld == NULL)
242 dyld = Library(baton, "/usr/lib/system/libdyld.dylib");
243 if (dyld == NULL)
244 dyld = Library(baton, "/usr/lib/libSystem.B.dylib");
85b57b57 245
3615a2f7
JF
246 char *(*$dlerror)();
247 cyset($dlerror, "_dlerror", dyld);
38ae783d 248
3615a2f7
JF
249 void *(*$dlopen)(const char *, int);
250 cyset($dlopen, "_dlopen", dyld);
40b1517d 251
3d1db6b9
JF
252#if defined(__i386__) || defined(__x86_64__)
253 size_t length($strlen(baton->library));
254 if (length >= 10 && $strcmp(baton->library + length - 10, "-###.dylib") == 0)
255 $memcpy(baton->library + length - 10, Simulator(baton) ? "-sim" : "-sys", 4);
256#endif
257
3615a2f7 258 void *handle($dlopen(baton->library, RTLD_LAZY | RTLD_LOCAL));
95a2c7e5 259 if (handle == NULL) {
8964aa08 260 $strlcpy(baton->error, $dlerror(), sizeof(baton->error));
95a2c7e5
JF
261 return NULL;
262 }
eed4f174 263
3615a2f7
JF
264 void *(*$dlsym)(void *, const char *);
265 cyset($dlsym, "_dlsym", dyld);
266
3d1db6b9
JF
267 void (*CYHandleServer)(pid_t);
268 CYHandleServer = reinterpret_cast<void (*)(pid_t)>($dlsym(handle, "CYHandleServer"));
6e51aaf8 269 if (CYHandleServer == NULL) {
8964aa08 270 $strlcpy(baton->error, $dlerror(), sizeof(baton->error));
6e51aaf8
JF
271 return NULL;
272 }
eed4f174 273
3d1db6b9 274 CYHandleServer(baton->pid);
95a2c7e5 275 return NULL;
eed4f174
JF
276}
277
49e976ae
JF
278extern "C" void Start(Baton *baton) {
279 struct _pthread self;
280 $bzero(&self, sizeof(self));
281
3615a2f7
JF
282 const mach_header_xx *pthread(NULL);
283 if (pthread == NULL)
284 pthread = Library(baton, "/usr/lib/system/libsystem_pthread.dylib");
85b57b57
JF
285 if (pthread == NULL)
286 pthread = Library(baton, "/usr/lib/system/libsystem_c.dylib");
3615a2f7
JF
287 if (pthread == NULL)
288 pthread = Library(baton, "/usr/lib/libSystem.B.dylib");
85b57b57 289
7eb6c2f2 290 void (*$__pthread_set_self)(void **);
85b57b57 291 cyset($__pthread_set_self, "___pthread_set_self", pthread);
38ae783d 292
49e976ae 293 self.tsd[0] = &self;
7eb6c2f2 294 $__pthread_set_self(&self.tsd[0]);
eed4f174 295
3615a2f7
JF
296 int (*$pthread_attr_init)(pthread_attr_t *);
297 cyset($pthread_attr_init, "_pthread_attr_init", pthread);
298
299#if 0
300 pthread_attr_t attr;
301 $pthread_attr_init(&attr);
302
303 int (*$pthread_attr_setdetachstate)(pthread_attr_t *, int);
304 cyset($pthread_attr_setdetachstate, "_pthread_attr_setdetachstate", pthread);
305
306 $pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
307#endif
308
38ae783d 309 int (*$pthread_create)(pthread_t *, const pthread_attr_t *, void *(*)(void *), void *);
85b57b57 310 cyset($pthread_create, "_pthread_create", pthread);
b6961e53
JF
311
312 pthread_t thread;
38ae783d 313 $pthread_create(&thread, NULL, &Routine, baton);
b6961e53 314
3615a2f7
JF
315#if 0
316 int (*$pthread_attr_destroy)(pthread_attr_t *);
317 cyset($pthread_attr_destroy, "_pthread_attr_destroy", pthread);
318
319 $pthread_attr_destroy(&attr);
320#endif
321
3615a2f7
JF
322 int (*$pthread_join)(pthread_t, void **);
323 cyset($pthread_join, "_pthread_join", pthread);
324
325 void *status;
326 $pthread_join(thread, &status);
327
328 const mach_header_xx *kernel(NULL);
329 if (kernel == NULL)
330 kernel = Library(baton, "/usr/lib/system/libsystem_kernel.dylib");
331 if (kernel == NULL)
332 kernel = Library(baton, "/usr/lib/libSystem.B.dylib");
85b57b57 333
38ae783d 334 mach_port_t (*$mach_thread_self)();
85b57b57 335 cyset($mach_thread_self, "_mach_thread_self", kernel);
eed4f174 336
38ae783d 337 kern_return_t (*$thread_terminate)(thread_act_t);
85b57b57 338 cyset($thread_terminate, "_thread_terminate", kernel);
49e976ae 339
38ae783d 340 $thread_terminate($mach_thread_self());
b6961e53 341}