]> git.saurik.com Git - cycript.git/blob - Mach/Inject.cpp
Make the ARM thread setup code match i386/x86_64.
[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 #include <mach/mach.h>
24
25 #include <mach/machine/thread_status.h>
26
27 #include <cstdio>
28 #include <pthread.h>
29 #include <unistd.h>
30
31 #include "Baton.hpp"
32 #include "Exception.hpp"
33 #include "Pooling.hpp"
34 #include "Trampoline.t.hpp"
35
36 extern "C" void __pthread_set_self(pthread_t);
37
38 void InjectLibrary(pid_t pid) {
39 const char *library(CY_LIBRARY);
40
41 static const size_t Stack_(8 * 1024);
42 size_t length(strlen(library) + 1), depth(sizeof(Baton) + length);
43 depth = (depth + sizeof(uintptr_t) + 1) / sizeof(uintptr_t) * sizeof(uintptr_t);
44
45 CYPool pool;
46 uint8_t *local(pool.malloc<uint8_t>(depth));
47 Baton *baton(reinterpret_cast<Baton *>(local));
48
49 baton->__pthread_set_self = &__pthread_set_self;
50 baton->pthread_create = &pthread_create;
51
52 baton->mach_thread_self = &mach_thread_self;
53 baton->thread_terminate = &thread_terminate;
54
55 baton->dlerror = &dlerror;
56 baton->dlsym = &dlsym;
57
58 baton->pid = getpid();
59 memcpy(baton->library, library, length);
60
61 vm_size_t size(depth + Stack_);
62
63 mach_port_t self(mach_task_self()), task;
64 _krncall(task_for_pid(self, pid, &task));
65
66 vm_address_t stack;
67 _krncall(vm_allocate(task, &stack, size, true));
68 vm_address_t data(stack + Stack_);
69
70 _krncall(vm_write(task, data, reinterpret_cast<vm_address_t>(baton), depth));
71
72 thread_act_t thread;
73 _krncall(thread_create(task, &thread));
74
75 thread_state_flavor_t flavor;
76 mach_msg_type_number_t count;
77
78 #if defined (__i386__) || defined(__x86_64__)
79 x86_thread_state_t state;
80 flavor = x86_THREAD_STATE;
81 count = x86_THREAD_STATE_COUNT;
82 #elif defined(__arm__)
83 arm_thread_state_t state;
84 flavor = ARM_THREAD_STATE;
85 count = ARM_THREAD_STATE_COUNT;
86 #else
87 #error XXX: implement
88 #endif
89
90 memset(&state, 0, sizeof(state));
91 mach_msg_type_number_t read(count);
92 _krncall(thread_get_state(thread, flavor, reinterpret_cast<thread_state_t>(&state), &read));
93 _assert(read == count);
94
95 Trampoline *trampoline;
96 size_t push;
97
98 #if defined(__i386__) || defined(__x86_64__)
99 switch (state.tsh.flavor) {
100 case i386_THREAD_STATE:
101 trampoline = &Trampoline_i386_;
102 push = 5;
103 break;
104 case x86_THREAD_STATE64:
105 trampoline = &Trampoline_x86_64_;
106 push = 2;
107 break;
108 default:
109 _assert(false);
110 }
111 #elif defined(__arm__)
112 trampoline = &Trampoline_armv6_;
113 push = 0;
114 #else
115 #error XXX: implement
116 #endif
117
118 vm_address_t code;
119 _krncall(vm_allocate(task, &code, trampoline->size_, true));
120 _krncall(vm_write(task, code, reinterpret_cast<vm_address_t>(trampoline->data_), trampoline->size_));
121 _krncall(vm_protect(task, code, trampoline->size_, false, VM_PROT_READ | VM_PROT_EXECUTE));
122
123 uint32_t frame[push];
124 if (sizeof(frame) != 0)
125 memset(frame, 0, sizeof(frame));
126
127 #if defined(__i386__) || defined(__x86_64__)
128 switch (state.tsh.flavor) {
129 case i386_THREAD_STATE:
130 frame[1] = data;
131 state.uts.ts32.__eip = code + trampoline->entry_;
132 state.uts.ts32.__esp = stack + Stack_ - sizeof(frame);
133 break;
134 case x86_THREAD_STATE64:
135 state.uts.ts64.__rdi = data;
136 state.uts.ts64.__rip = code + trampoline->entry_;
137 state.uts.ts64.__rsp = stack + Stack_ - sizeof(frame);
138 break;
139 default:
140 _assert(false);
141 }
142 #elif defined(__arm__)
143 state.__r[0] = data;
144 state.__pc = code + trampoline->entry_;
145 state.__sp = stack + Stack_ - sizeof(frame);
146
147 if ((state.__pc & 0x1) != 0) {
148 state.__pc &= ~0x1;
149 state.__cpsr |= 0x20;
150 }
151 #else
152 #error XXX: implement
153 #endif
154
155 if (sizeof(frame) != 0)
156 _krncall(vm_write(task, stack + Stack_ - sizeof(frame), reinterpret_cast<vm_address_t>(frame), sizeof(frame)));
157
158 _krncall(thread_set_state(thread, flavor, reinterpret_cast<thread_state_t>(&state), count));
159 _krncall(thread_resume(thread));
160
161 _krncall(mach_port_deallocate(self, task));
162 }