Compile iOS Cycript against Telesphoreo ncurses.
[cycript.git] / Mach / Inject.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 <dlfcn.h>
23
24 #include <mach/mach.h>
25
26 #ifdef __APPLE__
27 #include "TargetConditionals.h"
28 #endif
29
30 #ifdef TARGET_OS_IPHONE
31 #include <mach/vm_map.h>
32 #define mach_vm_allocate vm_allocate
33 #define mach_vm_protect vm_protect
34 #define mach_vm_write vm_write
35 #define mach_vm_address_t vm_address_t
36 #else
37 #include <mach/mach_vm.h>
38 #endif
39
40 #include <mach/machine/thread_status.h>
41
42 #include <cstdio>
43 #include <pthread.h>
44 #include <unistd.h>
45
46 #include "Baton.hpp"
47 #include "Exception.hpp"
48 #include "Pooling.hpp"
49 #include "Trampoline.t.hpp"
50
51 extern "C" void CYHandleServer(pid_t);
52
53 void InjectLibrary(pid_t pid) {
54 Dl_info addr;
55 _assert(dladdr(reinterpret_cast<void *>(&CYHandleServer), &addr) != 0);
56
57 size_t flength(strlen(addr.dli_fname));
58 char library[flength + 4 + 1];
59 memcpy(library, addr.dli_fname, flength);
60 library[flength] = '\0';
61 _assert(strcmp(library + flength - 6, ".dylib") == 0);
62 strcpy(library + flength - 6, "-any.dylib");
63
64 mach_port_t self(mach_task_self()), task;
65 _krncall(task_for_pid(self, pid, &task));
66
67 mach_msg_type_number_t count;
68
69 task_dyld_info info;
70 count = TASK_DYLD_INFO_COUNT;
71 _krncall(task_info(task, TASK_DYLD_INFO, reinterpret_cast<task_info_t>(&info), &count));
72 _assert(count == TASK_DYLD_INFO_COUNT);
73 _assert(info.all_image_info_addr != 0);
74
75 thread_act_t thread;
76 _krncall(thread_create(task, &thread));
77
78 #if defined (__i386__) || defined(__x86_64__)
79 x86_thread_state_t state;
80 #elif defined(__arm__)
81 arm_thread_state_t state;
82 #else
83 #error XXX: implement
84 #endif
85
86 memset(&state, 0, sizeof(state));
87 mach_msg_type_number_t read(MACHINE_THREAD_STATE_COUNT);
88 _krncall(thread_get_state(thread, MACHINE_THREAD_STATE, reinterpret_cast<thread_state_t>(&state), &read));
89 _assert(read == MACHINE_THREAD_STATE_COUNT);
90
91 Trampoline *trampoline;
92 size_t align;
93 size_t push;
94
95 #if defined(__i386__) || defined(__x86_64__)
96 switch (state.tsh.flavor) {
97 case i386_THREAD_STATE:
98 trampoline = &Trampoline_i386_;
99 align = 4;
100 push = 5;
101 break;
102 case x86_THREAD_STATE64:
103 trampoline = &Trampoline_x86_64_;
104 align = 8;
105 push = 2;
106 break;
107 default:
108 _assert(false);
109 }
110 #elif defined(__arm__)
111 trampoline = &Trampoline_armv6_;
112 align = 4;
113 push = 0;
114 #else
115 #error XXX: implement
116 #endif
117
118 static const size_t Stack_(8 * 1024);
119 size_t length(strlen(library) + 1), depth(sizeof(Baton) + length);
120 depth = (depth + align + 1) / align * align;
121
122 CYPool pool;
123 uint8_t *local(pool.malloc<uint8_t>(depth));
124 Baton *baton(reinterpret_cast<Baton *>(local));
125
126 baton->dyld = info.all_image_info_addr;
127 baton->pid = getpid();
128 memcpy(baton->library, library, length);
129
130 mach_vm_size_t size(depth + Stack_);
131 mach_vm_address_t stack;
132 _krncall(mach_vm_allocate(task, &stack, size, true));
133
134 mach_vm_address_t data(stack + Stack_);
135 _krncall(mach_vm_write(task, data, reinterpret_cast<mach_vm_address_t>(baton), depth));
136
137 mach_vm_address_t code;
138 _krncall(mach_vm_allocate(task, &code, trampoline->size_, true));
139 _krncall(mach_vm_write(task, code, reinterpret_cast<mach_vm_address_t>(trampoline->data_), trampoline->size_));
140 _krncall(mach_vm_protect(task, code, trampoline->size_, false, VM_PROT_READ | VM_PROT_EXECUTE));
141
142 uint32_t frame[push];
143 if (sizeof(frame) != 0)
144 memset(frame, 0, sizeof(frame));
145
146 #if defined(__i386__) || defined(__x86_64__)
147 switch (state.tsh.flavor) {
148 case i386_THREAD_STATE:
149 frame[1] = data;
150 state.uts.ts32.__eip = code + trampoline->entry_;
151 state.uts.ts32.__esp = stack + Stack_ - sizeof(frame);
152 break;
153 case x86_THREAD_STATE64:
154 state.uts.ts64.__rdi = data;
155 state.uts.ts64.__rip = code + trampoline->entry_;
156 state.uts.ts64.__rsp = stack + Stack_ - sizeof(frame);
157 break;
158 default:
159 _assert(false);
160 }
161 #elif defined(__arm__)
162 state.__r[0] = data;
163 state.__pc = code + trampoline->entry_;
164 state.__sp = stack + Stack_ - sizeof(frame);
165
166 if ((state.__pc & 0x1) != 0) {
167 state.__pc &= ~0x1;
168 state.__cpsr |= 0x20;
169 }
170 #else
171 #error XXX: implement
172 #endif
173
174 if (sizeof(frame) != 0)
175 _krncall(mach_vm_write(task, stack + Stack_ - sizeof(frame), reinterpret_cast<mach_vm_address_t>(frame), sizeof(frame)));
176
177 _krncall(thread_set_state(thread, MACHINE_THREAD_STATE, reinterpret_cast<thread_state_t>(&state), MACHINE_THREAD_STATE_COUNT));
178 _krncall(thread_resume(thread));
179
180 _krncall(mach_port_deallocate(self, task));
181 }