]>
Commit | Line | Data |
---|---|---|
b3378a02 JF |
1 | /* Cycript - Optimizing JavaScript Compiler/Runtime |
2 | * Copyright (C) 2009-2010 Jay Freeman (saurik) | |
e91fbe93 JF |
3 | */ |
4 | ||
b3378a02 | 5 | /* GNU Lesser General Public License, Version 3 {{{ */ |
e91fbe93 | 6 | /* |
b3378a02 JF |
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. | |
e91fbe93 | 11 | * |
b3378a02 JF |
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. | |
e91fbe93 | 16 | * |
b3378a02 JF |
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 | **/ | |
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 | ||
49e976ae | 36 | extern "C" void __pthread_set_self(pthread_t); |
b6961e53 | 37 | |
b6961e53 | 38 | void InjectLibrary(pid_t pid) { |
794e88e7 | 39 | const char *library(CY_LIBRARY); |
b6961e53 JF |
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))); | |
b6961e53 | 47 | Baton *baton(reinterpret_cast<Baton *>(local)); |
b6961e53 | 48 | |
49e976ae JF |
49 | baton->__pthread_set_self = &__pthread_set_self; |
50 | ||
c98ea90d JF |
51 | baton->pthread_create = &pthread_create; |
52 | baton->pthread_join = &pthread_join; | |
53 | ||
54 | baton->mach_thread_self = &mach_thread_self; | |
55 | baton->thread_terminate = &thread_terminate; | |
56 | ||
95a2c7e5 | 57 | baton->dlerror = &dlerror; |
b166b11b JF |
58 | baton->dlsym = &dlsym; |
59 | ||
b166b11b JF |
60 | baton->pid = getpid(); |
61 | memcpy(baton->library, library, length); | |
62 | ||
b6961e53 JF |
63 | vm_size_t size(depth + Stack_); |
64 | ||
65 | mach_port_t self(mach_task_self()), task; | |
66 | _krncall(task_for_pid(self, pid, &task)); | |
67 | ||
7c6c5b0a JF |
68 | vm_address_t stack; |
69 | _krncall(vm_allocate(task, &stack, size, true)); | |
70 | vm_address_t data(stack + Stack_); | |
71 | ||
b6961e53 JF |
72 | vm_write(task, data, reinterpret_cast<vm_address_t>(baton), depth); |
73 | ||
b6961e53 JF |
74 | thread_act_t thread; |
75 | _krncall(thread_create(task, &thread)); | |
76 | ||
77 | thread_state_flavor_t flavor; | |
78 | mach_msg_type_number_t count; | |
7c6c5b0a | 79 | size_t push; |
b6961e53 | 80 | |
eed4f174 JF |
81 | Trampoline *trampoline; |
82 | ||
b6961e53 | 83 | #if defined(__arm__) |
8ad1528b | 84 | trampoline = &Trampoline_armv6_; |
b6961e53 | 85 | arm_thread_state_t state; |
b6961e53 JF |
86 | flavor = ARM_THREAD_STATE; |
87 | count = ARM_THREAD_STATE_COUNT; | |
7c6c5b0a | 88 | push = 0; |
eed4f174 JF |
89 | #elif defined(__i386__) |
90 | trampoline = &Trampoline_i386_; | |
7c6c5b0a JF |
91 | i386_thread_state_t state; |
92 | flavor = i386_THREAD_STATE; | |
93 | count = i386_THREAD_STATE_COUNT; | |
94 | push = 5; | |
eed4f174 JF |
95 | #elif defined(__x86_64__) |
96 | trampoline = &Trampoline_x86_64_; | |
97 | x86_thread_state64_t state; | |
98 | flavor = x86_THREAD_STATE64; | |
99 | count = x86_THREAD_STATE64_COUNT; | |
100 | push = 2; | |
b166b11b JF |
101 | #else |
102 | #error XXX: implement | |
103 | #endif | |
b6961e53 | 104 | |
eed4f174 JF |
105 | vm_address_t code; |
106 | _krncall(vm_allocate(task, &code, trampoline->size_, true)); | |
107 | vm_write(task, code, reinterpret_cast<vm_address_t>(trampoline->data_), trampoline->size_); | |
108 | _krncall(vm_protect(task, code, trampoline->size_, false, VM_PROT_READ | VM_PROT_EXECUTE)); | |
109 | ||
80ba2053 | 110 | /* |
49e976ae | 111 | printf("_ptss:%p\n", baton->__pthread_set_self); |
eed4f174 JF |
112 | printf("dlsym:%p\n", baton->dlsym); |
113 | printf("code:%zx\n", (size_t) code); | |
80ba2053 | 114 | */ |
eed4f174 JF |
115 | |
116 | uint32_t frame[push]; | |
7c6c5b0a JF |
117 | if (sizeof(frame) != 0) |
118 | memset(frame, 0, sizeof(frame)); | |
b166b11b | 119 | memset(&state, 0, sizeof(state)); |
b6961e53 | 120 | |
b166b11b JF |
121 | mach_msg_type_number_t read(count); |
122 | _krncall(thread_get_state(thread, flavor, reinterpret_cast<thread_state_t>(&state), &read)); | |
123 | _assert(count == count); | |
124 | ||
125 | #if defined(__arm__) | |
8ad1528b JF |
126 | state.__r[0] = data; |
127 | state.__sp = stack + Stack_; | |
128 | state.__pc = code + trampoline->entry_; | |
b6961e53 | 129 | |
8ad1528b JF |
130 | if ((state.__pc & 0x1) != 0) { |
131 | state.__pc &= ~0x1; | |
132 | state.__cpsr |= 0x20; | |
b6961e53 | 133 | } |
eed4f174 | 134 | #elif defined(__i386__) |
7c6c5b0a JF |
135 | frame[1] = data; |
136 | ||
eed4f174 | 137 | state.__eip = code + trampoline->entry_; |
7c6c5b0a | 138 | state.__esp = stack + Stack_ - sizeof(frame); |
eed4f174 JF |
139 | #elif defined(__x86_64__) |
140 | frame[0] = 0xdeadbeef; | |
141 | state.__rdi = data; | |
142 | state.__rip = code + trampoline->entry_; | |
143 | state.__rsp = stack + Stack_ - sizeof(frame); | |
b6961e53 JF |
144 | #else |
145 | #error XXX: implement | |
146 | #endif | |
147 | ||
7c6c5b0a JF |
148 | if (sizeof(frame) != 0) |
149 | vm_write(task, stack + Stack_ - sizeof(frame), reinterpret_cast<vm_address_t>(frame), sizeof(frame)); | |
150 | ||
b166b11b | 151 | _krncall(thread_set_state(thread, flavor, reinterpret_cast<thread_state_t>(&state), count)); |
b6961e53 JF |
152 | _krncall(thread_resume(thread)); |
153 | ||
b166b11b | 154 | _krncall(mach_port_deallocate(self, task)); |
b6961e53 | 155 | } |