]> git.saurik.com Git - cycript.git/blame - Trampoline.t.cpp
In trampoline, make $strlcpy not crash given NULL.
[cycript.git] / Trampoline.t.cpp
CommitLineData
b3378a02 1/* Cycript - Optimizing JavaScript Compiler/Runtime
c15969fd 2 * Copyright (C) 2009-2013 Jay Freeman (saurik)
e91fbe93
JF
3*/
4
c15969fd 5/* GNU General Public License, Version 3 {{{ */
e91fbe93 6/*
c15969fd
JF
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.
e91fbe93 11 *
c15969fd
JF
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.
e91fbe93 16 *
c15969fd 17 * You should have received a copy of the GNU General Public License
b3378a02
JF
18 * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
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
47static 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
8964aa08 55static void $strlcpy(char *dst, const char *src, size_t size) {
f6fa3c21
JF
56 if (src == NULL)
57 src = "(null)";
8964aa08
JF
58 if (size == 0)
59 return;
60 size_t i(0);
61 while (i != size - 1) {
62 char value(src[i]);
63 if (value == '\0')
64 break;
65 dst[i++] = value;
66 } dst[i] = '\0';
67}
68
38ae783d
JF
69#ifdef __LP64__
70typedef struct mach_header_64 mach_header_xx;
71typedef struct nlist_64 nlist_xx;
72typedef struct segment_command_64 segment_command_xx;
73
74static const uint32_t LC_SEGMENT_XX = LC_SEGMENT_64;
75static const uint32_t MH_MAGIC_XX = MH_MAGIC_64;
76#else
77typedef struct mach_header mach_header_xx;
78typedef struct nlist nlist_xx;
79typedef struct segment_command segment_command_xx;
80
81static const uint32_t LC_SEGMENT_XX = LC_SEGMENT;
82static const uint32_t MH_MAGIC_XX = MH_MAGIC;
83#endif
84
85#define forlc(command, mach, lc, type) \
86 if (const struct load_command *load_commands = reinterpret_cast<const struct load_command *>(mach + 1)) \
87 if (const struct load_command *lcp = load_commands) \
88 for (uint32_t i(0); i != mach->ncmds; ++i, lcp = reinterpret_cast<const struct load_command *>(reinterpret_cast<const uint8_t *>(lcp) + lcp->cmdsize)) \
89 if ( \
90 lcp->cmdsize % sizeof(long) != 0 || lcp->cmdsize <= 0 || \
91 reinterpret_cast<const uint8_t *>(lcp) + lcp->cmdsize > reinterpret_cast<const uint8_t *>(load_commands) + mach->sizeofcmds \
92 ) \
93 return NULL; \
94 else if (lcp->cmd != lc) \
95 continue; \
96 else if (lcp->cmdsize < sizeof(type)) \
97 return NULL; \
98 else if (const type *command = reinterpret_cast<const type *>(lcp))
99
85b57b57
JF
100static const mach_header_xx *Library(struct dyld_all_image_infos *infos, const char *name) {
101 for (uint32_t i(0); i != infos->infoArrayCount; ++i) {
102 const dyld_image_info &info(infos->infoArray[i]);
103 const mach_header_xx *mach(reinterpret_cast<const mach_header_xx *>(info.imageLoadAddress));
104 if (mach->magic != MH_MAGIC_XX)
105 continue;
106
107 const char *path(info.imageFilePath);
108 forlc (dylib, mach, LC_ID_DYLIB, dylib_command)
109 path = reinterpret_cast<const char *>(dylib) + dylib->dylib.name.offset;
110 if ($strcmp(path, name) != 0)
111 continue;
112
113 return mach;
114 }
38ae783d 115
85b57b57
JF
116 return NULL;
117}
118
119static void *Symbol(const mach_header_xx *mach, const char *name) {
38ae783d
JF
120 const struct symtab_command *stp(NULL);
121 forlc (command, mach, LC_SYMTAB, struct symtab_command)
122 stp = command;
123 if (stp == NULL)
85b57b57 124 return NULL;
38ae783d
JF
125
126 size_t slide(_not(size_t));
127 const nlist_xx *symbols(NULL);
128 const char *strings(NULL);
129
130 forlc (segment, mach, LC_SEGMENT_XX, segment_command_xx) {
131 if (segment->fileoff == 0)
132 slide = reinterpret_cast<size_t>(mach) - segment->vmaddr;
133 if (stp->symoff >= segment->fileoff && stp->symoff < segment->fileoff + segment->filesize)
134 symbols = reinterpret_cast<const nlist_xx *>(stp->symoff - segment->fileoff + segment->vmaddr + slide);
135 if (stp->stroff >= segment->fileoff && stp->stroff < segment->fileoff + segment->filesize)
136 strings = reinterpret_cast<const char *>(stp->stroff - segment->fileoff + segment->vmaddr + slide);
137 }
138
139 if (slide == _not(size_t) || symbols == NULL || strings == NULL)
85b57b57 140 return NULL;
38ae783d
JF
141
142 for (size_t i(0); i != stp->nsyms; ++i) {
143 const nlist_xx *symbol(&symbols[i]);
144 if (symbol->n_un.n_strx == 0 || (symbol->n_type & N_STAB) != 0)
145 continue;
146
147 const char *nambuf(strings + symbol->n_un.n_strx);
148 if ($strcmp(name, nambuf) != 0)
149 continue;
150
151 uintptr_t value(symbol->n_value);
152 if (value == 0)
153 continue;
154
3615a2f7
JF
155#ifdef __arm__
156 if ((symbol->n_desc & N_ARM_THUMB_DEF) != 0)
157 value |= 0x00000001;
158#endif
159
38ae783d
JF
160 value += slide;
161 return reinterpret_cast<void *>(value);
162 }
85b57b57
JF
163
164 return NULL;
165}
38ae783d 166
38ae783d 167template <typename Type_>
85b57b57
JF
168static _finline void cyset(Type_ &function, const char *name, const mach_header_xx *mach) {
169 function = reinterpret_cast<Type_>(Symbol(mach, name));
170}
171
172static _finline const mach_header_xx *Library(Baton *baton, const char *name) {
38ae783d 173 struct dyld_all_image_infos *infos(reinterpret_cast<struct dyld_all_image_infos *>(baton->dyld));
85b57b57 174 return Library(infos, name);
eed4f174
JF
175}
176
49e976ae 177void *Routine(void *arg) {
eed4f174
JF
178 Baton *baton(reinterpret_cast<Baton *>(arg));
179
3615a2f7
JF
180 const mach_header_xx *dyld(NULL);
181 if (dyld == NULL)
182 dyld = Library(baton, "/usr/lib/system/libdyld.dylib");
183 if (dyld == NULL)
184 dyld = Library(baton, "/usr/lib/libSystem.B.dylib");
85b57b57 185
3615a2f7
JF
186 char *(*$dlerror)();
187 cyset($dlerror, "_dlerror", dyld);
38ae783d 188
3615a2f7
JF
189 void *(*$dlopen)(const char *, int);
190 cyset($dlopen, "_dlopen", dyld);
40b1517d 191
3615a2f7 192 void *handle($dlopen(baton->library, RTLD_LAZY | RTLD_LOCAL));
95a2c7e5 193 if (handle == NULL) {
8964aa08 194 $strlcpy(baton->error, $dlerror(), sizeof(baton->error));
95a2c7e5
JF
195 return NULL;
196 }
eed4f174 197
3615a2f7
JF
198 void *(*$dlsym)(void *, const char *);
199 cyset($dlsym, "_dlsym", dyld);
200
8e8e69a7
JF
201 void (*CYHandleServer)(pid_t, char *, size_t);
202 CYHandleServer = reinterpret_cast<void (*)(pid_t, char *, size_t)>($dlsym(handle, "CYHandleServer"));
6e51aaf8 203 if (CYHandleServer == NULL) {
8964aa08 204 $strlcpy(baton->error, $dlerror(), sizeof(baton->error));
6e51aaf8
JF
205 return NULL;
206 }
eed4f174 207
8e8e69a7 208 CYHandleServer(baton->pid, baton->error, sizeof(baton->error));
95a2c7e5 209 return NULL;
eed4f174
JF
210}
211
49e976ae
JF
212extern "C" void Start(Baton *baton) {
213 struct _pthread self;
214 $bzero(&self, sizeof(self));
215
3615a2f7
JF
216 const mach_header_xx *pthread(NULL);
217 if (pthread == NULL)
218 pthread = Library(baton, "/usr/lib/system/libsystem_pthread.dylib");
85b57b57
JF
219 if (pthread == NULL)
220 pthread = Library(baton, "/usr/lib/system/libsystem_c.dylib");
3615a2f7
JF
221 if (pthread == NULL)
222 pthread = Library(baton, "/usr/lib/libSystem.B.dylib");
85b57b57 223
7eb6c2f2 224 void (*$__pthread_set_self)(void **);
85b57b57 225 cyset($__pthread_set_self, "___pthread_set_self", pthread);
38ae783d 226
49e976ae 227 self.tsd[0] = &self;
7eb6c2f2 228 $__pthread_set_self(&self.tsd[0]);
eed4f174 229
3615a2f7
JF
230 int (*$pthread_attr_init)(pthread_attr_t *);
231 cyset($pthread_attr_init, "_pthread_attr_init", pthread);
232
233#if 0
234 pthread_attr_t attr;
235 $pthread_attr_init(&attr);
236
237 int (*$pthread_attr_setdetachstate)(pthread_attr_t *, int);
238 cyset($pthread_attr_setdetachstate, "_pthread_attr_setdetachstate", pthread);
239
240 $pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
241#endif
242
38ae783d 243 int (*$pthread_create)(pthread_t *, const pthread_attr_t *, void *(*)(void *), void *);
85b57b57 244 cyset($pthread_create, "_pthread_create", pthread);
b6961e53
JF
245
246 pthread_t thread;
38ae783d 247 $pthread_create(&thread, NULL, &Routine, baton);
b6961e53 248
3615a2f7
JF
249#if 0
250 int (*$pthread_attr_destroy)(pthread_attr_t *);
251 cyset($pthread_attr_destroy, "_pthread_attr_destroy", pthread);
252
253 $pthread_attr_destroy(&attr);
254#endif
255
3615a2f7
JF
256 int (*$pthread_join)(pthread_t, void **);
257 cyset($pthread_join, "_pthread_join", pthread);
258
259 void *status;
260 $pthread_join(thread, &status);
261
262 const mach_header_xx *kernel(NULL);
263 if (kernel == NULL)
264 kernel = Library(baton, "/usr/lib/system/libsystem_kernel.dylib");
265 if (kernel == NULL)
266 kernel = Library(baton, "/usr/lib/libSystem.B.dylib");
85b57b57 267
38ae783d 268 mach_port_t (*$mach_thread_self)();
85b57b57 269 cyset($mach_thread_self, "_mach_thread_self", kernel);
eed4f174 270
38ae783d 271 kern_return_t (*$thread_terminate)(thread_act_t);
85b57b57 272 cyset($thread_terminate, "_thread_terminate", kernel);
49e976ae 273
38ae783d 274 $thread_terminate($mach_thread_self());
b6961e53 275}