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