]> git.saurik.com Git - cycript.git/blobdiff - Trampoline.t.cpp
Update copyright for 2014 and relicense to AGPLv3.
[cycript.git] / Trampoline.t.cpp
index f6832bdb47082eeaffe71a72bb890ab5cc484ab2..5a51be04494564541e5150afd21bdadb13b508b8 100644 (file)
@@ -1,34 +1,39 @@
 /* Cycript - Optimizing JavaScript Compiler/Runtime
- * Copyright (C) 2009-2013  Jay Freeman (saurik)
+ * Copyright (C) 2009-2014  Jay Freeman (saurik)
 */
 
-/* GNU General Public License, Version 3 {{{ */
+/* GNU Affero General Public License, Version 3 {{{ */
 /*
- * Cycript is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published
- * by the Free Software Foundation, either version 3 of the License,
- * or (at your option) any later version.
- *
- * Cycript is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Cycript.  If not, see <http://www.gnu.org/licenses/>.
+ * GNU Affero General Public License for more details.
+
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 **/
 /* }}} */
 
+#include <TargetConditionals.h>
+#if defined(__arm__) || defined(__arm64__)
+#undef TARGET_IPHONE_SIMULATOR
+#define TARGET_IPHONE_SIMULATOR 1
+#endif
 #define _PTHREAD_ATTR_T
 #include <pthread_internals.h>
+#if defined(__arm__) || defined(__arm64__)
+#undef TARGET_IPHONE_SIMULATOR
+#endif
 
 #include <mach-o/dyld.h>
 #include <mach-o/dyld_images.h>
 #include <mach-o/loader.h>
-
-extern "C" {
 #include <mach-o/nlist.h>
-}
 
 #include "Standard.hpp"
 #include "Baton.hpp"
@@ -39,6 +44,12 @@ static void $bzero(void *data, size_t size) {
         bytes[i] = 0;
 }
 
+__attribute__((__unused__))
+static void $memcpy(char *dst, const char *src, size_t size) {
+    for (size_t i(0); i != size; ++i)
+        dst[i] = src[i];
+}
+
 static int $strcmp(const char *lhs, const char *rhs) {
     while (*lhs == *rhs) {
         if (*lhs == '\0')
@@ -47,6 +58,58 @@ static int $strcmp(const char *lhs, const char *rhs) {
     } return *lhs < *rhs ? -1 : 1;
 }
 
+static void $strlcpy(char *dst, const char *src, size_t size) {
+    if (src == NULL)
+        src = "(null)";
+    if (size == 0)
+        return;
+    size_t i(0);
+    while (i != size - 1) {
+        char value(src[i]);
+        if (value == '\0')
+            break;
+        dst[i++] = value;
+    } dst[i] = '\0';
+}
+
+__attribute__((__unused__))
+static char *$strstr(const char *haystack, const char *needle) {
+    if (*needle == '\0')
+        return NULL;
+    for (; *haystack != '\0'; ++haystack)
+        for (size_t i(0); ; ++i)
+            if (needle[i] == '\0')
+                return const_cast<char *>(haystack);
+            else if (needle[i] != haystack[i])
+                break;
+    return NULL;
+}
+
+__attribute__((__unused__))
+static size_t $strlen(const char *data) {
+    for (size_t i(0); ; ++i)
+        if (data[i] == '\0')
+            return i;
+}
+
+__attribute__((__unused__))
+static void $snprintfp(char *dst, size_t size, const void *pointer) {
+    uintptr_t value(reinterpret_cast<uintptr_t>(pointer));
+    char buffer[32];
+    char *end(buffer + sizeof(buffer));
+    *--end = '\0';
+    if (value == 0)
+        *--end = '0';
+    else do {
+        unsigned digit(value & 0xf);
+        value >>= 4;
+        *--end = (digit < 10 ? '0' : 'a' - 10) + digit;
+    } while (value != 0);
+    *--end = 'x';
+    *--end = '0';
+    $strlcpy(dst, end, size);
+}
+
 #ifdef __LP64__
 typedef struct mach_header_64 mach_header_xx;
 typedef struct nlist_64 nlist_xx;
@@ -71,11 +134,11 @@ static const uint32_t MH_MAGIC_XX = MH_MAGIC;
                     lcp->cmdsize % sizeof(long) != 0 || lcp->cmdsize <= 0 || \
                     reinterpret_cast<const uint8_t *>(lcp) + lcp->cmdsize > reinterpret_cast<const uint8_t *>(load_commands) + mach->sizeofcmds \
                 ) \
-                    return NULL; \
+                    break; \
                 else if (lcp->cmd != lc) \
                     continue; \
                 else if (lcp->cmdsize < sizeof(type)) \
-                    return NULL; \
+                    break; \
                 else if (const type *command = reinterpret_cast<const type *>(lcp))
 
 static const mach_header_xx *Library(struct dyld_all_image_infos *infos, const char *name) {
@@ -133,6 +196,11 @@ static void *Symbol(const mach_header_xx *mach, const char *name) {
         if (value == 0)
             continue;
 
+#ifdef __arm__
+        if ((symbol->n_desc & N_ARM_THUMB_DEF) != 0)
+            value |= 0x00000001;
+#endif
+
         value += slide;
         return reinterpret_cast<void *>(value);
     }
@@ -140,18 +208,6 @@ static void *Symbol(const mach_header_xx *mach, const char *name) {
     return NULL;
 }
 
-struct Dynamic {
-    char *(*dlerror)();
-    void *(*dlsym)(void *, const char *);
-};
-
-template <typename Type_>
-static _finline void dlset(Dynamic *dynamic, Type_ &function, const char *name, void *handle = RTLD_DEFAULT) {
-    function = reinterpret_cast<Type_>(dynamic->dlsym(handle, name));
-    if (function == NULL)
-        dynamic->dlerror();
-}
-
 template <typename Type_>
 static _finline void cyset(Type_ &function, const char *name, const mach_header_xx *mach) {
     function = reinterpret_cast<Type_>(Symbol(mach, name));
@@ -162,36 +218,56 @@ static _finline const mach_header_xx *Library(Baton *baton, const char *name) {
     return Library(infos, name);
 }
 
-void *Routine(void *arg) {
-    Baton *baton(reinterpret_cast<Baton *>(arg));
+#if defined(__i386__) || defined(__x86_64__)
+static bool Simulator(struct dyld_all_image_infos *infos) {
+    for (uint32_t i(0); i != infos->infoArrayCount; ++i) {
+        const dyld_image_info &info(infos->infoArray[i]);
+        const char *path(info.imageFilePath);
+        if ($strstr(path, "/SDKs/iPhoneSimulator") != NULL)
+            return true;
+    } return false;
+}
 
-    const mach_header_xx *dyld(Library(baton, "/usr/lib/system/libdyld.dylib"));
+static bool Simulator(Baton *baton) {
+    struct dyld_all_image_infos *infos(reinterpret_cast<struct dyld_all_image_infos *>(baton->dyld));
+    return Simulator(infos);
+}
+#endif
 
-    Dynamic dynamic;
-    cyset(dynamic.dlerror, "_dlerror", dyld);
-    cyset(dynamic.dlsym, "_dlsym", dyld);
+void *Routine(void *arg) {
+    Baton *baton(reinterpret_cast<Baton *>(arg));
 
-    int (*pthread_detach)(pthread_t);
-    dlset(&dynamic, pthread_detach, "pthread_detach");
+    const mach_header_xx *dyld(NULL);
+    if (dyld == NULL)
+        dyld = Library(baton, "/usr/lib/system/libdyld.dylib");
+    if (dyld == NULL)
+        dyld = Library(baton, "/usr/lib/libSystem.B.dylib");
 
-    pthread_t (*pthread_self)();
-    dlset(&dynamic, pthread_self, "pthread_self");
+    char *(*$dlerror)();
+    cyset($dlerror, "_dlerror", dyld);
 
-    pthread_detach(pthread_self());
+    void *(*$dlopen)(const char *, int);
+    cyset($dlopen, "_dlopen", dyld);
 
-    void *(*dlopen)(const char *, int);
-    dlset(&dynamic, dlopen, "dlopen");
+#if defined(__i386__) || defined(__x86_64__)
+    size_t length($strlen(baton->library));
+    if (length >= 10 && $strcmp(baton->library + length - 10, "-###.dylib") == 0)
+        $memcpy(baton->library + length - 10, Simulator(baton) ? "-sim" : "-sys", 4);
+#endif
 
-    void *handle(dlopen(baton->library, RTLD_LAZY | RTLD_LOCAL));
+    void *handle($dlopen(baton->library, RTLD_LAZY | RTLD_LOCAL));
     if (handle == NULL) {
-        dynamic.dlerror();
+        $strlcpy(baton->error, $dlerror(), sizeof(baton->error));
         return NULL;
     }
 
+    void *(*$dlsym)(void *, const char *);
+    cyset($dlsym, "_dlsym", dyld);
+
     void (*CYHandleServer)(pid_t);
-    dlset(&dynamic, CYHandleServer, "CYHandleServer", handle);
+    CYHandleServer = reinterpret_cast<void (*)(pid_t)>($dlsym(handle, "CYHandleServer"));
     if (CYHandleServer == NULL) {
-        dynamic.dlerror();
+        $strlcpy(baton->error, $dlerror(), sizeof(baton->error));
         return NULL;
     }
 
@@ -203,15 +279,32 @@ extern "C" void Start(Baton *baton) {
     struct _pthread self;
     $bzero(&self, sizeof(self));
 
-    const mach_header_xx *pthread(Library(baton, "/usr/lib/system/libsystem_pthread.dylib"));
+    const mach_header_xx *pthread(NULL);
+    if (pthread == NULL)
+        pthread = Library(baton, "/usr/lib/system/libsystem_pthread.dylib");
     if (pthread == NULL)
         pthread = Library(baton, "/usr/lib/system/libsystem_c.dylib");
+    if (pthread == NULL)
+        pthread = Library(baton, "/usr/lib/libSystem.B.dylib");
 
-    void (*$__pthread_set_self)(pthread_t);
+    void (*$__pthread_set_self)(void **);
     cyset($__pthread_set_self, "___pthread_set_self", pthread);
 
     self.tsd[0] = &self;
-    $__pthread_set_self(&self);
+    $__pthread_set_self(&self.tsd[0]);
+
+    int (*$pthread_attr_init)(pthread_attr_t *);
+    cyset($pthread_attr_init, "_pthread_attr_init", pthread);
+
+#if 0
+    pthread_attr_t attr;
+    $pthread_attr_init(&attr);
+
+    int (*$pthread_attr_setdetachstate)(pthread_attr_t *, int);
+    cyset($pthread_attr_setdetachstate, "_pthread_attr_setdetachstate", pthread);
+
+    $pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+#endif
 
     int (*$pthread_create)(pthread_t *, const pthread_attr_t *, void *(*)(void *), void *);
     cyset($pthread_create, "_pthread_create", pthread);
@@ -219,7 +312,24 @@ extern "C" void Start(Baton *baton) {
     pthread_t thread;
     $pthread_create(&thread, NULL, &Routine, baton);
 
-    const mach_header_xx *kernel(Library(baton, "/usr/lib/system/libsystem_kernel.dylib"));
+#if 0
+    int (*$pthread_attr_destroy)(pthread_attr_t *);
+    cyset($pthread_attr_destroy, "_pthread_attr_destroy", pthread);
+
+    $pthread_attr_destroy(&attr);
+#endif
+
+    int (*$pthread_join)(pthread_t, void **);
+    cyset($pthread_join, "_pthread_join", pthread);
+
+    void *status;
+    $pthread_join(thread, &status);
+
+    const mach_header_xx *kernel(NULL);
+    if (kernel == NULL)
+        kernel = Library(baton, "/usr/lib/system/libsystem_kernel.dylib");
+    if (kernel == NULL)
+        kernel = Library(baton, "/usr/lib/libSystem.B.dylib");
 
     mach_port_t (*$mach_thread_self)();
     cyset($mach_thread_self, "_mach_thread_self", kernel);