]>
Commit | Line | Data |
---|---|---|
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 | ||
56fc2692 | 22 | #include "TargetConditionals.h" |
b6ace225 | 23 | #if TARGET_OS_IPHONE |
3615a2f7 JF |
24 | #undef __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ |
25 | #define __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ __IPHONE_5_0 | |
56fc2692 JF |
26 | #endif |
27 | ||
3615a2f7 JF |
28 | #include <dlfcn.h> |
29 | #include <pthread.h> | |
30 | #include <unistd.h> | |
31 | ||
32 | #include <mach/mach.h> | |
41676572 | 33 | #include <mach/vm_map.h> |
9d79cefc | 34 | #include <mach/mach_vm.h> |
b6961e53 | 35 | |
8ad1528b | 36 | #include <mach/machine/thread_status.h> |
b6961e53 | 37 | |
3615a2f7 JF |
38 | #ifdef __arm__ |
39 | #include "Mach/Memory.hpp" | |
40 | #endif | |
b6961e53 JF |
41 | |
42 | #include "Baton.hpp" | |
43 | #include "Exception.hpp" | |
44 | #include "Pooling.hpp" | |
45 | #include "Trampoline.t.hpp" | |
46 | ||
6342b70c JF |
47 | extern "C" void CYHandleServer(pid_t); |
48 | ||
3615a2f7 JF |
49 | extern "C" void *_dyld_get_all_image_infos(); |
50 | ||
b6961e53 | 51 | void InjectLibrary(pid_t pid) { |
6342b70c JF |
52 | Dl_info addr; |
53 | _assert(dladdr(reinterpret_cast<void *>(&CYHandleServer), &addr) != 0); | |
3370d0c0 JF |
54 | |
55 | size_t flength(strlen(addr.dli_fname)); | |
56 | char library[flength + 4 + 1]; | |
57 | memcpy(library, addr.dli_fname, flength); | |
58 | library[flength] = '\0'; | |
59 | _assert(strcmp(library + flength - 6, ".dylib") == 0); | |
b6ace225 | 60 | #if !TARGET_OS_IPHONE |
3d1db6b9 | 61 | strcpy(library + flength - 6, "-###.dylib"); |
3615a2f7 | 62 | #endif |
b6961e53 | 63 | |
b6961e53 JF |
64 | mach_port_t self(mach_task_self()), task; |
65 | _krncall(task_for_pid(self, pid, &task)); | |
66 | ||
38ae783d | 67 | task_dyld_info info; |
3615a2f7 JF |
68 | #ifdef __arm__ |
69 | union { | |
70 | struct { | |
71 | uint32_t all_image_info_addr; | |
72 | } info_1; | |
73 | ||
74 | struct { | |
75 | uint32_t all_image_info_addr; | |
76 | uint32_t all_image_info_size; | |
77 | int32_t all_image_info_format; | |
78 | } info32; | |
79 | ||
80 | struct { | |
81 | uint64_t all_image_info_addr; | |
82 | uint64_t all_image_info_size; | |
83 | int32_t all_image_info_format; | |
84 | } info64; | |
85 | } infoXX; | |
86 | ||
87 | mach_msg_type_number_t count(sizeof(infoXX) / sizeof(natural_t)); | |
88 | _krncall(task_info(task, TASK_DYLD_INFO, reinterpret_cast<task_info_t>(&infoXX), &count)); | |
89 | ||
90 | bool broken; | |
91 | ||
92 | switch (count) { | |
93 | case sizeof(infoXX.info_1) / sizeof(natural_t): | |
94 | broken = true; | |
95 | info.all_image_info_addr = infoXX.info_1.all_image_info_addr; | |
96 | info.all_image_info_size = 0; | |
97 | info.all_image_info_format = TASK_DYLD_ALL_IMAGE_INFO_32; | |
98 | break; | |
99 | case sizeof(infoXX.info32) / sizeof(natural_t): | |
100 | broken = true; | |
101 | info.all_image_info_addr = infoXX.info32.all_image_info_addr; | |
102 | info.all_image_info_size = infoXX.info32.all_image_info_size; | |
103 | info.all_image_info_format = infoXX.info32.all_image_info_format; | |
104 | break; | |
105 | case sizeof(infoXX.info64) / sizeof(natural_t): | |
106 | broken = false; | |
107 | info.all_image_info_addr = infoXX.info64.all_image_info_addr; | |
108 | info.all_image_info_size = infoXX.info64.all_image_info_size; | |
109 | info.all_image_info_format = infoXX.info64.all_image_info_format; | |
110 | break; | |
111 | default: | |
112 | _assert(false); | |
113 | } | |
114 | #else | |
115 | mach_msg_type_number_t count(TASK_DYLD_INFO_COUNT); | |
38ae783d JF |
116 | _krncall(task_info(task, TASK_DYLD_INFO, reinterpret_cast<task_info_t>(&info), &count)); |
117 | _assert(count == TASK_DYLD_INFO_COUNT); | |
3615a2f7 | 118 | #endif |
38ae783d | 119 | _assert(info.all_image_info_addr != 0); |
b6961e53 | 120 | |
b6961e53 JF |
121 | thread_act_t thread; |
122 | _krncall(thread_create(task, &thread)); | |
123 | ||
3615a2f7 JF |
124 | thread_state_t bottom; |
125 | thread_state_flavor_t flavor; | |
126 | ||
3db8ce8e JF |
127 | #if defined (__i386__) || defined(__x86_64__) |
128 | x86_thread_state_t state; | |
3615a2f7 JF |
129 | memset(&state, 0, sizeof(state)); |
130 | ||
131 | bottom = reinterpret_cast<thread_state_t>(&state); | |
132 | flavor = MACHINE_THREAD_STATE; | |
133 | count = MACHINE_THREAD_STATE_COUNT; | |
134 | #elif defined(__arm__) || defined(__arm64__) | |
135 | arm_unified_thread_state_t state; | |
136 | memset(&state, 0, sizeof(state)); | |
137 | ||
138 | switch (info.all_image_info_format) { | |
139 | case TASK_DYLD_ALL_IMAGE_INFO_32: | |
140 | bottom = reinterpret_cast<thread_state_t>(&state.ts_32); | |
141 | flavor = ARM_THREAD_STATE; | |
142 | count = ARM_THREAD_STATE_COUNT; | |
143 | state.ash.flavor = ARM_THREAD_STATE32; | |
144 | break; | |
145 | case TASK_DYLD_ALL_IMAGE_INFO_64: | |
146 | bottom = reinterpret_cast<thread_state_t>(&state.ts_64); | |
147 | flavor = ARM_THREAD_STATE64; | |
148 | count = ARM_THREAD_STATE64_COUNT + 1; | |
149 | state.ash.flavor = ARM_THREAD_STATE64; | |
150 | break; | |
151 | default: | |
152 | _assert(false); | |
153 | } | |
3db8ce8e JF |
154 | #else |
155 | #error XXX: implement | |
156 | #endif | |
157 | ||
3615a2f7 JF |
158 | mach_msg_type_number_t read(count); |
159 | _krncall(thread_get_state(thread, flavor, bottom, &read)); | |
160 | _assert(read == count); | |
3db8ce8e JF |
161 | |
162 | Trampoline *trampoline; | |
9d79cefc | 163 | size_t align; |
3db8ce8e JF |
164 | size_t push; |
165 | ||
166 | #if defined(__i386__) || defined(__x86_64__) | |
167 | switch (state.tsh.flavor) { | |
168 | case i386_THREAD_STATE: | |
169 | trampoline = &Trampoline_i386_; | |
9d79cefc | 170 | align = 4; |
3db8ce8e JF |
171 | push = 5; |
172 | break; | |
173 | case x86_THREAD_STATE64: | |
174 | trampoline = &Trampoline_x86_64_; | |
9d79cefc | 175 | align = 8; |
3db8ce8e JF |
176 | push = 2; |
177 | break; | |
178 | default: | |
179 | _assert(false); | |
180 | } | |
3615a2f7 JF |
181 | #elif defined(__arm__) || defined(__arm64__) |
182 | switch (state.ash.flavor) { | |
183 | case ARM_THREAD_STATE32: | |
184 | trampoline = &Trampoline_armv6_; | |
185 | align = 4; | |
186 | push = 0; | |
187 | break; | |
188 | case ARM_THREAD_STATE64: | |
189 | trampoline = &Trampoline_arm64_; | |
190 | align = 8; | |
191 | push = 0; | |
192 | break; | |
193 | default: | |
194 | _assert(false); | |
195 | } | |
b166b11b JF |
196 | #else |
197 | #error XXX: implement | |
198 | #endif | |
b6961e53 | 199 | |
9d79cefc JF |
200 | static const size_t Stack_(8 * 1024); |
201 | size_t length(strlen(library) + 1), depth(sizeof(Baton) + length); | |
202 | depth = (depth + align + 1) / align * align; | |
203 | ||
204 | CYPool pool; | |
205 | uint8_t *local(pool.malloc<uint8_t>(depth)); | |
206 | Baton *baton(reinterpret_cast<Baton *>(local)); | |
207 | ||
208 | baton->dyld = info.all_image_info_addr; | |
209 | baton->pid = getpid(); | |
47f55bc7 | 210 | memset(baton->error, 0, sizeof(baton->error)); |
9d79cefc JF |
211 | memcpy(baton->library, library, length); |
212 | ||
213 | mach_vm_size_t size(depth + Stack_); | |
214 | mach_vm_address_t stack; | |
215 | _krncall(mach_vm_allocate(task, &stack, size, true)); | |
216 | ||
217 | mach_vm_address_t data(stack + Stack_); | |
218 | _krncall(mach_vm_write(task, data, reinterpret_cast<mach_vm_address_t>(baton), depth)); | |
219 | ||
220 | mach_vm_address_t code; | |
221 | _krncall(mach_vm_allocate(task, &code, trampoline->size_, true)); | |
3615a2f7 | 222 | _krncall(mach_vm_write(task, code, reinterpret_cast<vm_offset_t>(trampoline->data_), trampoline->size_)); |
9d79cefc | 223 | _krncall(mach_vm_protect(task, code, trampoline->size_, false, VM_PROT_READ | VM_PROT_EXECUTE)); |
eed4f174 | 224 | |
eed4f174 | 225 | uint32_t frame[push]; |
7c6c5b0a JF |
226 | if (sizeof(frame) != 0) |
227 | memset(frame, 0, sizeof(frame)); | |
b166b11b | 228 | |
3db8ce8e JF |
229 | #if defined(__i386__) || defined(__x86_64__) |
230 | switch (state.tsh.flavor) { | |
231 | case i386_THREAD_STATE: | |
232 | frame[1] = data; | |
233 | state.uts.ts32.__eip = code + trampoline->entry_; | |
234 | state.uts.ts32.__esp = stack + Stack_ - sizeof(frame); | |
235 | break; | |
236 | case x86_THREAD_STATE64: | |
237 | state.uts.ts64.__rdi = data; | |
238 | state.uts.ts64.__rip = code + trampoline->entry_; | |
239 | state.uts.ts64.__rsp = stack + Stack_ - sizeof(frame); | |
240 | break; | |
241 | default: | |
242 | _assert(false); | |
243 | } | |
3615a2f7 JF |
244 | #elif defined(__arm__) || defined(__arm64__) |
245 | switch (state.ash.flavor) { | |
246 | case ARM_THREAD_STATE32: | |
247 | state.ts_32.__r[0] = data; | |
248 | state.ts_32.__pc = code + trampoline->entry_; | |
249 | state.ts_32.__sp = stack + Stack_ - sizeof(frame); | |
250 | ||
251 | if ((state.ts_32.__pc & 0x1) != 0) { | |
252 | state.ts_32.__pc &= ~0x1; | |
253 | state.ts_32.__cpsr |= 0x20; | |
254 | } | |
255 | ||
256 | break; | |
257 | ||
258 | case ARM_THREAD_STATE64: | |
259 | state.ts_64.__x[0] = data; | |
260 | state.ts_64.__pc = code + trampoline->entry_; | |
261 | state.ts_64.__sp = stack + Stack_ - sizeof(frame); | |
262 | break; | |
263 | ||
264 | default: | |
265 | _assert(false); | |
b6961e53 | 266 | } |
b6961e53 JF |
267 | #else |
268 | #error XXX: implement | |
269 | #endif | |
270 | ||
7c6c5b0a | 271 | if (sizeof(frame) != 0) |
9d79cefc | 272 | _krncall(mach_vm_write(task, stack + Stack_ - sizeof(frame), reinterpret_cast<mach_vm_address_t>(frame), sizeof(frame))); |
7c6c5b0a | 273 | |
3615a2f7 | 274 | _krncall(thread_set_state(thread, flavor, bottom, read)); |
b6961e53 JF |
275 | _krncall(thread_resume(thread)); |
276 | ||
3615a2f7 JF |
277 | loop: switch (kern_return_t status = thread_get_state(thread, flavor, bottom, &(read = count))) { |
278 | case KERN_SUCCESS: | |
279 | usleep(10000); | |
280 | goto loop; | |
281 | ||
282 | case KERN_TERMINATED: | |
283 | case MACH_SEND_INVALID_DEST: | |
284 | break; | |
285 | ||
286 | default: | |
287 | _assert(false); | |
288 | } | |
289 | ||
290 | _krncall(mach_port_deallocate(self, thread)); | |
291 | ||
8964aa08 JF |
292 | mach_vm_size_t error(sizeof(baton->error)); |
293 | _krncall(mach_vm_read_overwrite(task, data + offsetof(Baton, error), sizeof(baton->error), reinterpret_cast<mach_vm_address_t>(&baton->error), &error)); | |
294 | _assert(error == sizeof(baton->error)); | |
cd2e82fb JF |
295 | |
296 | if (baton->error[0] != '\0') { | |
297 | baton->error[sizeof(baton->error) - 1] = '\0'; | |
8964aa08 | 298 | CYThrow("%s", baton->error); |
cd2e82fb | 299 | } |
8964aa08 | 300 | |
3615a2f7 JF |
301 | _krncall(mach_vm_deallocate(task, code, trampoline->size_)); |
302 | _krncall(mach_vm_deallocate(task, stack, size)); | |
303 | ||
b166b11b | 304 | _krncall(mach_port_deallocate(self, task)); |
b6961e53 | 305 | } |