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