]> git.saurik.com Git - cycript.git/blob - Mach/Inject.cpp
Use dlsym(), not struct Baton.
[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 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(reinterpret_cast<uint8_t *>(apr_palloc(pool, depth)));
47 Baton *baton(reinterpret_cast<Baton *>(local));
48
49 baton->__pthread_set_self = &__pthread_set_self;
50
51 baton->dlerror = &dlerror;
52 baton->dlsym = &dlsym;
53
54 baton->pid = getpid();
55 memcpy(baton->library, library, length);
56
57 vm_size_t size(depth + Stack_);
58
59 mach_port_t self(mach_task_self()), task;
60 _krncall(task_for_pid(self, pid, &task));
61
62 vm_address_t stack;
63 _krncall(vm_allocate(task, &stack, size, true));
64 vm_address_t data(stack + Stack_);
65
66 vm_write(task, data, reinterpret_cast<vm_address_t>(baton), depth);
67
68 thread_act_t thread;
69 _krncall(thread_create(task, &thread));
70
71 thread_state_flavor_t flavor;
72 mach_msg_type_number_t count;
73 size_t push;
74
75 Trampoline *trampoline;
76
77 #if defined(__arm__)
78 trampoline = &Trampoline_arm_;
79 arm_thread_state_t state;
80 flavor = ARM_THREAD_STATE;
81 count = ARM_THREAD_STATE_COUNT;
82 push = 0;
83 #elif defined(__i386__)
84 trampoline = &Trampoline_i386_;
85 i386_thread_state_t state;
86 flavor = i386_THREAD_STATE;
87 count = i386_THREAD_STATE_COUNT;
88 push = 5;
89 #elif defined(__x86_64__)
90 trampoline = &Trampoline_x86_64_;
91 x86_thread_state64_t state;
92 flavor = x86_THREAD_STATE64;
93 count = x86_THREAD_STATE64_COUNT;
94 push = 2;
95 #else
96 #error XXX: implement
97 #endif
98
99 vm_address_t code;
100 _krncall(vm_allocate(task, &code, trampoline->size_, true));
101 vm_write(task, code, reinterpret_cast<vm_address_t>(trampoline->data_), trampoline->size_);
102 _krncall(vm_protect(task, code, trampoline->size_, false, VM_PROT_READ | VM_PROT_EXECUTE));
103
104 /*
105 printf("_ptss:%p\n", baton->__pthread_set_self);
106 printf("dlsym:%p\n", baton->dlsym);
107 printf("code:%zx\n", (size_t) code);
108 */
109
110 uint32_t frame[push];
111 if (sizeof(frame) != 0)
112 memset(frame, 0, sizeof(frame));
113 memset(&state, 0, sizeof(state));
114
115 mach_msg_type_number_t read(count);
116 _krncall(thread_get_state(thread, flavor, reinterpret_cast<thread_state_t>(&state), &read));
117 _assert(count == count);
118
119 #if defined(__arm__)
120 state.r[0] = data;
121 state.sp = stack + Stack_;
122 state.pc = code + trampoline->entry_;
123
124 if ((state.pc & 0x1) != 0) {
125 state.pc &= ~0x1;
126 state.cpsr |= 0x20;
127 }
128 #elif defined(__i386__)
129 frame[1] = data;
130
131 state.__eip = code + trampoline->entry_;
132 state.__esp = stack + Stack_ - sizeof(frame);
133 #elif defined(__x86_64__)
134 frame[0] = 0xdeadbeef;
135 state.__rdi = data;
136 state.__rip = code + trampoline->entry_;
137 state.__rsp = stack + Stack_ - sizeof(frame);
138 #else
139 #error XXX: implement
140 #endif
141
142 if (sizeof(frame) != 0)
143 vm_write(task, stack + Stack_ - sizeof(frame), reinterpret_cast<vm_address_t>(frame), sizeof(frame));
144
145 _krncall(thread_set_state(thread, flavor, reinterpret_cast<thread_state_t>(&state), count));
146 _krncall(thread_resume(thread));
147
148 _krncall(mach_port_deallocate(self, task));
149 }