1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2010 Jay Freeman (saurik)
5 /* GNU Lesser General Public License, Version 3 {{{ */
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.
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.
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/>.
23 #include <mach/mach.h>
25 #include <mach/i386/thread_status.h>
32 #include "Exception.hpp"
33 #include "Pooling.hpp"
34 #include "Trampoline.t.hpp"
36 extern "C" void __pthread_set_self(pthread_t
);
38 void InjectLibrary(pid_t pid
) {
39 const char *library(CY_LIBRARY
);
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);
46 uint8_t *local(reinterpret_cast<uint8_t *>(apr_palloc(pool
, depth
)));
47 Baton
*baton(reinterpret_cast<Baton
*>(local
));
49 baton
->__pthread_set_self
= &__pthread_set_self
;
51 baton
->pthread_create
= &pthread_create
;
52 baton
->pthread_join
= &pthread_join
;
54 baton
->dlopen
= &dlopen
;
55 baton
->dlerror
= &dlerror
;
56 baton
->dlsym
= &dlsym
;
58 baton
->mach_thread_self
= &mach_thread_self
;
59 baton
->thread_terminate
= &thread_terminate
;
61 baton
->pid
= getpid();
62 memcpy(baton
->library
, library
, length
);
64 vm_size_t
size(depth
+ Stack_
);
66 mach_port_t
self(mach_task_self()), task
;
67 _krncall(task_for_pid(self
, pid
, &task
));
70 _krncall(vm_allocate(task
, &stack
, size
, true));
71 vm_address_t
data(stack
+ Stack_
);
73 vm_write(task
, data
, reinterpret_cast<vm_address_t
>(baton
), depth
);
76 _krncall(thread_create(task
, &thread
));
78 thread_state_flavor_t flavor
;
79 mach_msg_type_number_t count
;
82 Trampoline
*trampoline
;
85 trampoline
= &Trampoline_arm_
;
86 arm_thread_state_t state
;
87 flavor
= ARM_THREAD_STATE
;
88 count
= ARM_THREAD_STATE_COUNT
;
90 #elif defined(__i386__)
91 trampoline
= &Trampoline_i386_
;
92 i386_thread_state_t state
;
93 flavor
= i386_THREAD_STATE
;
94 count
= i386_THREAD_STATE_COUNT
;
96 #elif defined(__x86_64__)
97 trampoline
= &Trampoline_x86_64_
;
98 x86_thread_state64_t state
;
99 flavor
= x86_THREAD_STATE64
;
100 count
= x86_THREAD_STATE64_COUNT
;
103 #error XXX: implement
107 _krncall(vm_allocate(task
, &code
, trampoline
->size_
, true));
108 vm_write(task
, code
, reinterpret_cast<vm_address_t
>(trampoline
->data_
), trampoline
->size_
);
109 _krncall(vm_protect(task
, code
, trampoline
->size_
, false, VM_PROT_READ
| VM_PROT_EXECUTE
));
112 printf("_ptss:%p\n", baton->__pthread_set_self);
113 printf("dlsym:%p\n", baton->dlsym);
114 printf("code:%zx\n", (size_t) code);
117 uint32_t frame
[push
];
118 if (sizeof(frame
) != 0)
119 memset(frame
, 0, sizeof(frame
));
120 memset(&state
, 0, sizeof(state
));
122 mach_msg_type_number_t
read(count
);
123 _krncall(thread_get_state(thread
, flavor
, reinterpret_cast<thread_state_t
>(&state
), &read
));
124 _assert(count
== count
);
128 state
.sp
= stack
+ Stack_
;
129 state
.pc
= code
+ trampoline
->entry_
;
131 if ((state
.pc
& 0x1) != 0) {
135 #elif defined(__i386__)
138 state
.__eip
= code
+ trampoline
->entry_
;
139 state
.__esp
= stack
+ Stack_
- sizeof(frame
);
140 #elif defined(__x86_64__)
141 frame
[0] = 0xdeadbeef;
143 state
.__rip
= code
+ trampoline
->entry_
;
144 state
.__rsp
= stack
+ Stack_
- sizeof(frame
);
146 #error XXX: implement
149 if (sizeof(frame
) != 0)
150 vm_write(task
, stack
+ Stack_
- sizeof(frame
), reinterpret_cast<vm_address_t
>(frame
), sizeof(frame
));
152 _krncall(thread_set_state(thread
, flavor
, reinterpret_cast<thread_state_t
>(&state
), count
));
153 _krncall(thread_resume(thread
));
155 _krncall(mach_port_deallocate(self
, task
));