]> git.saurik.com Git - cycript.git/blob - Mach/Inject.cpp
92a395ec371f29f21b9c66c669327be97c70d6f8
[cycript.git] / Mach / Inject.cpp
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2010 Jay Freeman (saurik)
3 */
4
5 /* GNU Lesser General Public License, Version 3 {{{ */
6 /*
7 * Cycript is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU Lesser General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * Cycript is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
16 *
17 * You should have received a copy of the GNU Lesser 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/i386/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 #include <substrate.h>
37
38 extern "C" void _pthread_start(pthread_t, mach_port_t, void *(*)(void *), void *, size_t, unsigned int);
39
40 void InjectLibrary(pid_t pid) {
41 const char *library(CY_LIBRARY);
42
43 static const size_t Stack_(8 * 1024);
44 size_t length(strlen(library) + 1), depth(sizeof(Baton) + length);
45 depth = (depth + sizeof(uintptr_t) + 1) / sizeof(uintptr_t) * sizeof(uintptr_t);
46
47 CYPool pool;
48 uint8_t *local(reinterpret_cast<uint8_t *>(apr_palloc(pool, depth)));
49 Baton *baton(reinterpret_cast<Baton *>(local));
50
51 baton->_pthread_start = reinterpret_cast<void (*)(pthread_t, mach_port_t, void *(*)(void *), void *, size_t, unsigned int)>(MSFindSymbol(NULL, "__pthread_start"));
52 baton->dlerror = &dlerror;
53 baton->dlsym = &dlsym;
54
55 baton->pid = getpid();
56 memcpy(baton->library, library, length);
57
58 vm_size_t size(depth + Stack_);
59
60 mach_port_t self(mach_task_self()), task;
61 _krncall(task_for_pid(self, pid, &task));
62
63 vm_address_t stack;
64 _krncall(vm_allocate(task, &stack, size, true));
65 vm_address_t data(stack + Stack_);
66
67 vm_write(task, data, reinterpret_cast<vm_address_t>(baton), depth);
68
69 thread_act_t thread;
70 _krncall(thread_create(task, &thread));
71
72 thread_state_flavor_t flavor;
73 mach_msg_type_number_t count;
74 size_t push;
75
76 Trampoline *trampoline;
77
78 #if defined(__arm__)
79 trampoline = &Trampoline_arm_;
80 arm_thread_state_t state;
81 flavor = ARM_THREAD_STATE;
82 count = ARM_THREAD_STATE_COUNT;
83 push = 0;
84 #elif defined(__i386__)
85 trampoline = &Trampoline_i386_;
86 i386_thread_state_t state;
87 flavor = i386_THREAD_STATE;
88 count = i386_THREAD_STATE_COUNT;
89 push = 5;
90 #elif defined(__x86_64__)
91 trampoline = &Trampoline_x86_64_;
92 x86_thread_state64_t state;
93 flavor = x86_THREAD_STATE64;
94 count = x86_THREAD_STATE64_COUNT;
95 push = 2;
96 #else
97 #error XXX: implement
98 #endif
99
100 vm_address_t code;
101 _krncall(vm_allocate(task, &code, trampoline->size_, true));
102 vm_write(task, code, reinterpret_cast<vm_address_t>(trampoline->data_), trampoline->size_);
103 _krncall(vm_protect(task, code, trampoline->size_, false, VM_PROT_READ | VM_PROT_EXECUTE));
104
105 /*
106 printf("_pts:%p\n", baton->_pthread_start);
107 printf("dlerror:%p\n", baton->dlerror);
108 printf("dlsym:%p\n", baton->dlsym);
109 printf("code:%zx\n", (size_t) code);
110 */
111
112 uint32_t frame[push];
113 if (sizeof(frame) != 0)
114 memset(frame, 0, sizeof(frame));
115 memset(&state, 0, sizeof(state));
116
117 mach_msg_type_number_t read(count);
118 _krncall(thread_get_state(thread, flavor, reinterpret_cast<thread_state_t>(&state), &read));
119 _assert(count == count);
120
121 #if defined(__arm__)
122 state.r[0] = data;
123 state.sp = stack + Stack_;
124 state.pc = code + trampoline->entry_;
125
126 if ((state.pc & 0x1) != 0) {
127 state.pc &= ~0x1;
128 state.cpsr |= 0x20;
129 }
130 #elif defined(__i386__)
131 frame[1] = data;
132
133 state.__eip = code + trampoline->entry_;
134 state.__esp = stack + Stack_ - sizeof(frame);
135 #elif defined(__x86_64__)
136 frame[0] = 0xdeadbeef;
137 state.__rdi = data;
138 state.__rip = code + trampoline->entry_;
139 state.__rsp = stack + Stack_ - sizeof(frame);
140 #else
141 #error XXX: implement
142 #endif
143
144 if (sizeof(frame) != 0)
145 vm_write(task, stack + Stack_ - sizeof(frame), reinterpret_cast<vm_address_t>(frame), sizeof(frame));
146
147 _krncall(thread_set_state(thread, flavor, reinterpret_cast<thread_state_t>(&state), count));
148 _krncall(thread_resume(thread));
149
150 _krncall(mach_port_deallocate(self, task));
151 }