]> git.saurik.com Git - cycript.git/blame - Mach/Inject.cpp
Find all remote symbols via dyld_all_image_infos.
[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
JF
22#include <dlfcn.h>
23#include <mach/mach.h>
24
8ad1528b 25#include <mach/machine/thread_status.h>
b6961e53
JF
26
27#include <cstdio>
28#include <pthread.h>
b166b11b 29#include <unistd.h>
b6961e53
JF
30
31#include "Baton.hpp"
32#include "Exception.hpp"
33#include "Pooling.hpp"
34#include "Trampoline.t.hpp"
35
b6961e53 36void InjectLibrary(pid_t pid) {
794e88e7 37 const char *library(CY_LIBRARY);
b6961e53
JF
38
39 static const size_t Stack_(8 * 1024);
40 size_t length(strlen(library) + 1), depth(sizeof(Baton) + length);
41 depth = (depth + sizeof(uintptr_t) + 1) / sizeof(uintptr_t) * sizeof(uintptr_t);
42
43 CYPool pool;
0cbeddf8 44 uint8_t *local(pool.malloc<uint8_t>(depth));
b6961e53 45 Baton *baton(reinterpret_cast<Baton *>(local));
b6961e53 46
b166b11b
JF
47 baton->pid = getpid();
48 memcpy(baton->library, library, length);
49
b6961e53
JF
50 mach_port_t self(mach_task_self()), task;
51 _krncall(task_for_pid(self, pid, &task));
52
38ae783d
JF
53 mach_msg_type_number_t count;
54
55 task_dyld_info info;
56 count = TASK_DYLD_INFO_COUNT;
57 _krncall(task_info(task, TASK_DYLD_INFO, reinterpret_cast<task_info_t>(&info), &count));
58 _assert(count == TASK_DYLD_INFO_COUNT);
59 _assert(info.all_image_info_addr != 0);
60 baton->dyld = info.all_image_info_addr;
61
62 vm_size_t size(depth + Stack_);
7c6c5b0a
JF
63 vm_address_t stack;
64 _krncall(vm_allocate(task, &stack, size, true));
7c6c5b0a 65
38ae783d 66 vm_address_t data(stack + Stack_);
c5e3cd69 67 _krncall(vm_write(task, data, reinterpret_cast<vm_address_t>(baton), depth));
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;
91 size_t push;
92
93#if defined(__i386__) || defined(__x86_64__)
94 switch (state.tsh.flavor) {
95 case i386_THREAD_STATE:
96 trampoline = &Trampoline_i386_;
97 push = 5;
98 break;
99 case x86_THREAD_STATE64:
100 trampoline = &Trampoline_x86_64_;
101 push = 2;
102 break;
103 default:
104 _assert(false);
105 }
106#elif defined(__arm__)
107 trampoline = &Trampoline_armv6_;
7c6c5b0a 108 push = 0;
b166b11b
JF
109#else
110 #error XXX: implement
111#endif
b6961e53 112
eed4f174
JF
113 vm_address_t code;
114 _krncall(vm_allocate(task, &code, trampoline->size_, true));
c5e3cd69 115 _krncall(vm_write(task, code, reinterpret_cast<vm_address_t>(trampoline->data_), trampoline->size_));
eed4f174
JF
116 _krncall(vm_protect(task, code, trampoline->size_, false, VM_PROT_READ | VM_PROT_EXECUTE));
117
eed4f174 118 uint32_t frame[push];
7c6c5b0a
JF
119 if (sizeof(frame) != 0)
120 memset(frame, 0, sizeof(frame));
b166b11b 121
3db8ce8e
JF
122#if defined(__i386__) || defined(__x86_64__)
123 switch (state.tsh.flavor) {
124 case i386_THREAD_STATE:
125 frame[1] = data;
126 state.uts.ts32.__eip = code + trampoline->entry_;
127 state.uts.ts32.__esp = stack + Stack_ - sizeof(frame);
128 break;
129 case x86_THREAD_STATE64:
130 state.uts.ts64.__rdi = data;
131 state.uts.ts64.__rip = code + trampoline->entry_;
132 state.uts.ts64.__rsp = stack + Stack_ - sizeof(frame);
133 break;
134 default:
135 _assert(false);
136 }
137#elif defined(__arm__)
8ad1528b 138 state.__r[0] = data;
8ad1528b 139 state.__pc = code + trampoline->entry_;
3bf9dd69 140 state.__sp = stack + Stack_ - sizeof(frame);
b6961e53 141
8ad1528b
JF
142 if ((state.__pc & 0x1) != 0) {
143 state.__pc &= ~0x1;
144 state.__cpsr |= 0x20;
b6961e53 145 }
b6961e53
JF
146#else
147 #error XXX: implement
148#endif
149
7c6c5b0a 150 if (sizeof(frame) != 0)
c5e3cd69 151 _krncall(vm_write(task, stack + Stack_ - sizeof(frame), reinterpret_cast<vm_address_t>(frame), sizeof(frame)));
7c6c5b0a 152
b166b11b 153 _krncall(thread_set_state(thread, flavor, reinterpret_cast<thread_state_t>(&state), count));
b6961e53
JF
154 _krncall(thread_resume(thread));
155
b166b11b 156 _krncall(mach_port_deallocate(self, task));
b6961e53 157}