]> git.saurik.com Git - cycript.git/blob - Mach/Inject.cpp
Got true JS booleans working in GNUstep.
[cycript.git] / Mach / Inject.cpp
1 #include <dlfcn.h>
2 #include <mach/mach.h>
3
4 extern "C" {
5 #include <mach-o/nlist.h>
6 }
7
8 #include <cstdio>
9 #include <pthread.h>
10 #include <unistd.h>
11
12 #include "Baton.hpp"
13 #include "Exception.hpp"
14 #include "Pooling.hpp"
15 #include "Trampoline.t.hpp"
16
17 extern "C" void __pthread_set_self(pthread_t);
18
19 template <typename Type_>
20 static void nlset(Type_ &function, struct nlist *nl, size_t index) {
21 struct nlist &name(nl[index]);
22 uintptr_t value(name.n_value);
23 _assert(value != 0);
24 if ((name.n_desc & N_ARM_THUMB_DEF) != 0)
25 value |= 0x00000001;
26 function = value;
27 }
28
29 void InjectLibrary(pid_t pid) {
30 // XXX: break this into the build environment
31 const char *library("/usr/lib/libcycript.dylib");
32
33 static const size_t Stack_(8 * 1024);
34 size_t length(strlen(library) + 1), depth(sizeof(Baton) + length);
35 depth = (depth + sizeof(uintptr_t) + 1) / sizeof(uintptr_t) * sizeof(uintptr_t);
36
37 CYPool pool;
38 uint8_t *local(reinterpret_cast<uint8_t *>(apr_palloc(pool, depth)));
39 Baton *baton(reinterpret_cast<Baton *>(local));
40
41 uintptr_t set_self_internal;
42 uintptr_t set_self_external;
43
44 struct nlist nl[3];
45 memset(nl, 0, sizeof(nl));
46 nl[0].n_un.n_name = (char *) "__pthread_set_self";
47 nl[1].n_un.n_name = (char *) "___pthread_set_self";
48 nlist("/usr/lib/libSystem.B.dylib", nl);
49 nlset(set_self_internal, nl, 0);
50 nlset(set_self_external, nl, 1);
51
52 baton->_pthread_set_self = reinterpret_cast<void (*)(pthread_t)>(reinterpret_cast<uintptr_t>(&__pthread_set_self) - set_self_external + set_self_internal);
53
54 baton->pthread_create = &pthread_create;
55 baton->pthread_join = &pthread_join;
56
57 baton->dlopen = &dlopen;
58 baton->dlsym = &dlsym;
59
60 baton->mach_thread_self = &mach_thread_self;
61 baton->thread_terminate = &thread_terminate;
62
63 baton->pid = getpid();
64 memcpy(baton->library, library, length);
65
66 vm_size_t size(depth + Stack_);
67
68 mach_port_t self(mach_task_self()), task;
69 _krncall(task_for_pid(self, pid, &task));
70
71 vm_address_t stack;
72 _krncall(vm_allocate(task, &stack, size, true));
73 vm_address_t data(stack + Stack_);
74
75 vm_write(task, data, reinterpret_cast<vm_address_t>(baton), depth);
76
77 vm_address_t code;
78 _krncall(vm_allocate(task, &code, sizeof(Trampoline_), true));
79 vm_write(task, code, reinterpret_cast<vm_address_t>(Trampoline_), sizeof(Trampoline_));
80 _krncall(vm_protect(task, code, sizeof(Trampoline_), false, VM_PROT_READ | VM_PROT_EXECUTE));
81
82 thread_act_t thread;
83 _krncall(thread_create(task, &thread));
84
85 thread_state_flavor_t flavor;
86 mach_msg_type_number_t count;
87 size_t push;
88
89 #if defined(__arm__)
90 arm_thread_state_t state;
91 flavor = ARM_THREAD_STATE;
92 count = ARM_THREAD_STATE_COUNT;
93 push = 0;
94 #elif defined(__i386__) || defined(__x86_64__)
95 i386_thread_state_t state;
96 flavor = i386_THREAD_STATE;
97 count = i386_THREAD_STATE_COUNT;
98 push = 5;
99 #else
100 #error XXX: implement
101 #endif
102
103 uintptr_t frame[push];
104 if (sizeof(frame) != 0)
105 memset(frame, 0, sizeof(frame));
106
107 memset(&state, 0, sizeof(state));
108
109 mach_msg_type_number_t read(count);
110 _krncall(thread_get_state(thread, flavor, reinterpret_cast<thread_state_t>(&state), &read));
111 _assert(count == count);
112
113 #if defined(__arm__)
114 state.r[0] = data;
115 state.sp = stack + Stack_;
116 state.pc = code;
117
118 if ((state.pc & 0x1) != 0) {
119 state.pc &= ~0x1;
120 state.cpsr |= 0x20;
121 }
122 #elif defined(__i386__) || defined(__x86_64__)
123 frame[0] = 0;
124 frame[1] = data;
125
126 state.__eip = code;
127 state.__esp = stack + Stack_ - sizeof(frame);
128 #else
129 #error XXX: implement
130 #endif
131
132 if (sizeof(frame) != 0)
133 vm_write(task, stack + Stack_ - sizeof(frame), reinterpret_cast<vm_address_t>(frame), sizeof(frame));
134
135 _krncall(thread_set_state(thread, flavor, reinterpret_cast<thread_state_t>(&state), count));
136 _krncall(thread_resume(thread));
137
138 _krncall(mach_port_deallocate(self, task));
139 }