]> git.saurik.com Git - cycript.git/blobdiff - Java/Execute.cpp
Try a new (safer) mechanism to get object private.
[cycript.git] / Java / Execute.cpp
index 2156045b75063076140cf17f4e66295338c3715c..f382bc94c32e454d6977c505ffadc25cc225139d 100644 (file)
 #include <sstream>
 #include <vector>
 
+#include <dlfcn.h>
+
 #ifdef __APPLE__
 #include <JavaVM/jni.h>
 #else
 #include <jni.h>
 #endif
 
+#ifdef __ANDROID__
+// XXX: this is deprecated?!?!?!?!?!?!
+#include <sys/system_properties.h>
+#endif
+
 #include "cycript.hpp"
 #include "Error.hpp"
 #include "Execute.hpp"
@@ -70,13 +77,7 @@ _value; })
         return value; \
     }
 
-extern "C" {
-    // Android's jni.h seriously doesn't declare these :/
-    jint JNI_CreateJavaVM(JavaVM **, void **, void *);
-    jint JNI_GetCreatedJavaVMs(JavaVM **, jsize, jsize *);
-}
-
-JNIEnv *GetJNI(JSContextRef context);
+static JNIEnv *GetJNI(JSContextRef context);
 
 #define CYJavaForEachPrimitive \
     CYJavaForEachPrimitive_(Z, z, Boolean, Boolean, boolean) \
@@ -424,6 +425,7 @@ CYJavaForEachPrimitive
     CYJavaEnv_(GetMethodID)
     CYJavaEnv_(GetStaticMethodID)
     CYJavaEnv_(IsSameObject)
+    CYJavaEnv_(RegisterNatives)
 #undef CYJavaEnv_
 
 #define CYJavaEnv_(Code) \
@@ -938,7 +940,7 @@ static bool CYCastJavaArguments(const CYJavaFrame &frame, const CYJavaShorty &sh
 }
 
 static JSValueRef JavaMethod_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
-    CYJavaMethod *internal(reinterpret_cast<CYJavaMethod *>(JSObjectGetPrivate(object)));
+    auto internal(CYJavaMethod::Get(context, object));
     CYJavaObject *self(CYGetJavaObject(context, _this));
     CYJavaEnv jni(self->value_);
 
@@ -968,7 +970,7 @@ CYJavaForEachPrimitive
 } CYCatch(NULL) }
 
 static JSValueRef JavaStaticMethod_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
-    CYJavaMethod *internal(reinterpret_cast<CYJavaMethod *>(JSObjectGetPrivate(object)));
+    auto internal(CYJavaStaticMethod::Get(context, object));
     CYJavaClass *table(CYGetJavaTable(context, _this));
     CYJavaEnv jni(table->value_);
 
@@ -998,7 +1000,7 @@ CYJavaForEachPrimitive
 } CYCatch(NULL) }
 
 static JSObjectRef JavaClass_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
-    CYJavaClass *table(reinterpret_cast<CYJavaClass *>(JSObjectGetPrivate(object)));
+    auto table(CYJavaClass::Get(context, object));
     CYJavaEnv jni(table->value_);
     jclass _class(table->value_);
 
@@ -1023,7 +1025,7 @@ static JSObjectRef JavaClass_callAsConstructor(JSContextRef context, JSObjectRef
 } CYCatch(NULL) }
 
 static bool JavaStaticInterior_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
-    CYJavaStaticInterior *internal(reinterpret_cast<CYJavaStaticInterior *>(JSObjectGetPrivate(object)));
+    auto internal(CYJavaStaticInterior::Get(context, object));
     CYJavaClass *table(internal->table_);
     CYPool pool;
     auto name(CYPoolUTF8String(pool, context, property));
@@ -1034,7 +1036,7 @@ static bool JavaStaticInterior_hasProperty(JSContextRef context, JSObjectRef obj
 }
 
 static JSValueRef JavaStaticInterior_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
-    CYJavaStaticInterior *internal(reinterpret_cast<CYJavaStaticInterior *>(JSObjectGetPrivate(object)));
+    auto internal(CYJavaStaticInterior::Get(context, object));
     CYJavaClass *table(internal->table_);
     CYJavaEnv jni(table->value_);
     CYPool pool;
@@ -1056,7 +1058,7 @@ CYJavaForEachPrimitive
 } CYCatch(NULL) }
 
 static bool JavaStaticInterior_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry {
-    CYJavaStaticInterior *internal(reinterpret_cast<CYJavaStaticInterior *>(JSObjectGetPrivate(object)));
+    auto internal(CYJavaStaticInterior::Get(context, object));
     CYJavaClass *table(internal->table_);
     CYJavaEnv jni(table->value_);
     CYPool pool;
@@ -1081,19 +1083,19 @@ CYJavaForEachPrimitive
 } CYCatch(false) }
 
 static void JavaStaticInterior_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
-    CYJavaStaticInterior *internal(reinterpret_cast<CYJavaStaticInterior *>(JSObjectGetPrivate(object)));
+    auto internal(CYJavaStaticInterior::Get(context, object));
     CYJavaClass *table(internal->table_);
     for (const auto &field : table->static_)
         JSPropertyNameAccumulatorAddName(names, CYJSString(field.first));
 }
 
 static JSValueRef JavaClass_getProperty_class(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
-    CYJavaClass *table(reinterpret_cast<CYJavaClass *>(JSObjectGetPrivate(object)));
+    auto table(CYJavaClass::Get(context, object));
     return CYCastJSValue(context, table->value_);
 } CYCatch(NULL) }
 
 static bool JavaInterior_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
-    CYJavaInterior *internal(reinterpret_cast<CYJavaInterior *>(JSObjectGetPrivate(object)));
+    auto internal(CYJavaInterior::Get(context, object));
     CYJavaClass *table(internal->table_);
     CYPool pool;
     auto name(CYPoolUTF8String(pool, context, property));
@@ -1104,7 +1106,7 @@ static bool JavaInterior_hasProperty(JSContextRef context, JSObjectRef object, J
 }
 
 static JSValueRef JavaInterior_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
-    CYJavaInterior *internal(reinterpret_cast<CYJavaInterior *>(JSObjectGetPrivate(object)));
+    auto internal(CYJavaInterior::Get(context, object));
     CYJavaEnv jni(internal->value_);
     CYJavaClass *table(internal->table_);
     CYPool pool;
@@ -1126,7 +1128,7 @@ CYJavaForEachPrimitive
 } CYCatch(NULL) }
 
 static bool JavaInterior_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry {
-    CYJavaInterior *internal(reinterpret_cast<CYJavaInterior *>(JSObjectGetPrivate(object)));
+    auto internal(CYJavaInterior::Get(context, object));
     CYJavaEnv jni(internal->value_);
     CYJavaClass *table(internal->table_);
     CYPool pool;
@@ -1151,30 +1153,30 @@ CYJavaForEachPrimitive
 } CYCatch(false) }
 
 static void JavaInterior_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
-    CYJavaInterior *internal(reinterpret_cast<CYJavaInterior *>(JSObjectGetPrivate(object)));
+    auto internal(CYJavaInterior::Get(context, object));
     CYJavaClass *table(internal->table_);
     for (const auto &field : table->instance_)
         JSPropertyNameAccumulatorAddName(names, CYJSString(field.first));
 }
 
 static JSValueRef JavaObject_getProperty_constructor(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
-    CYJavaObject *internal(reinterpret_cast<CYJavaObject *>(JSObjectGetPrivate(object)));
+    auto internal(CYJavaObject::Get(context, object));
     CYJavaEnv jni(internal->value_);
     return CYGetJavaClass(context, jni.GetObjectClass(internal->value_));
 } CYCatch(NULL) }
 
 static JSValueRef JavaClass_getProperty_$cyi(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
-    CYJavaClass *internal(reinterpret_cast<CYJavaClass *>(JSObjectGetPrivate(object)));
+    auto internal(CYJavaClass::Get(context, object));
     return CYJavaStaticInterior::Make(context, internal->value_, internal);
 } CYCatch(NULL) }
 
 static JSValueRef JavaObject_getProperty_$cyi(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
-    CYJavaObject *internal(reinterpret_cast<CYJavaObject *>(JSObjectGetPrivate(object)));
+    auto internal(CYJavaObject::Get(context, object));
     return CYJavaInterior::Make(context, internal->value_, internal->table_);
 } CYCatch(NULL) }
 
 static JSValueRef JavaClass_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
-    CYJavaClass *internal(reinterpret_cast<CYJavaClass *>(JSObjectGetPrivate(_this)));
+    auto internal(CYJavaClass::Get(context, _this));
     CYJavaEnv jni(internal->value_);
     auto Class$(jni.FindClass("java/lang/Class"));
     auto Class$getCanonicalName(jni.GetMethodID(Class$, "getCanonicalName", "()Ljava/lang/String;"));
@@ -1192,7 +1194,7 @@ static JSValueRef JavaStaticMethod_callAsFunction_toCYON(JSContextRef context, J
 } CYCatch(NULL) }
 
 static JSValueRef JavaArray_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
-    CYJavaArray *internal(reinterpret_cast<CYJavaArray *>(JSObjectGetPrivate(object)));
+    auto internal(CYJavaArray::Get(context, object));
     CYJavaEnv jni(internal->value_);
     if (JSStringIsEqual(property, length_s))
         return CYCastJSValue(context, jni.GetArrayLength(internal->value_));
@@ -1218,7 +1220,7 @@ CYJavaForEachPrimitive
 } CYCatch(NULL) }
 
 static bool JavaArray_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry {
-    CYJavaArray *internal(reinterpret_cast<CYJavaArray *>(JSObjectGetPrivate(object)));
+    auto internal(CYJavaArray::Get(context, object));
     CYJavaEnv jni(internal->value_);
 
     CYPool pool;
@@ -1244,7 +1246,7 @@ CYJavaForEachPrimitive
 } CYCatch(false) }
 
 static JSValueRef JavaPackage_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
-    CYJavaPackage *internal(reinterpret_cast<CYJavaPackage *>(JSObjectGetPrivate(_this)));
+    auto internal(CYJavaPackage::Get(context, _this));
     std::ostringstream name;
     for (auto &package : internal->package_)
         name << package << '.';
@@ -1257,7 +1259,7 @@ static bool CYJavaPackage_hasProperty(JSContextRef context, JSObjectRef object,
 }
 
 static JSValueRef CYJavaPackage_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
-    CYJavaPackage *internal(reinterpret_cast<CYJavaPackage *>(JSObjectGetPrivate(object)));
+    auto internal(CYJavaPackage::Get(context, object));
     CYJavaPackage::Path package(internal->package_);
 
     CYPool pool;
@@ -1299,46 +1301,176 @@ static JNINativeMethod Cycript_[] = {
     {(char *) "handle", (char *) "(JLjava/lang/String;[Ljava/lang/Object;)Ljava/lang/Object;", (void *) &Cycript_handle},
 };
 
-JNIEnv *GetJNI(JSContextRef context) {
+template <typename Type_>
+static _finline void dlset(Type_ &function, const char *name, void *handle) {
+    function = reinterpret_cast<Type_>(dlsym(handle, name));
+}
+
+jint CYJavaVersion(JNI_VERSION_1_4);
+
+static JNIEnv *CYGetCreatedJava(jint (*$JNI_GetCreatedJavaVMs)(JavaVM **, jsize, jsize *)) {
+    jsize capacity(16);
+    JavaVM *jvms[capacity];
+    jsize size;
+    _jnicall($JNI_GetCreatedJavaVMs(jvms, capacity, &size));
+    if (size == 0)
+        return NULL;
+    JavaVM *jvm(jvms[0]);
+    JNIEnv *jni;
+    _jnicall(jvm->GetEnv(reinterpret_cast<void **>(&jni), CYJavaVersion));
+    return jni;
+}
+
+static JNIEnv *GetJNI_(JSContextRef context) {
     static JavaVM *jvm(NULL);
     static JNIEnv *jni(NULL);
 
     if (jni != NULL)
         return jni;
-    jint version(JNI_VERSION_1_4);
 
-    jsize capacity(16);
-    JavaVM *jvms[capacity];
-    jsize size;
-    _jnicall(JNI_GetCreatedJavaVMs(jvms, capacity, &size));
+    CYPool pool;
+    void *handle(RTLD_DEFAULT);
+    std::string library;
 
-    if (size != 0) {
-        jvm = jvms[0];
-        _jnicall(jvm->GetEnv(reinterpret_cast<void **>(&jni), version));
+    jint (*$JNI_GetCreatedJavaVMs)(JavaVM **jvms, jsize capacity, jsize *size);
+    dlset($JNI_GetCreatedJavaVMs, "JNI_GetCreatedJavaVMs", handle);
+
+    if ($JNI_GetCreatedJavaVMs != NULL) {
+        if (JNIEnv *jni = CYGetCreatedJava($JNI_GetCreatedJavaVMs))
+            return jni;
     } else {
-        CYPool pool;
-        std::vector<JavaVMOption> options;
-
-        {
-            std::ostringstream option;
-            option << "-Djava.class.path=";
-            option << CYPoolLibraryPath(pool) << "/libcycript.jar";
-            if (const char *classpath = getenv("CLASSPATH"))
-                option << ':' << classpath;
-            options.push_back(JavaVMOption{pool.strdup(option.str().c_str()), NULL});
+        std::vector<const char *> guesses;
+
+#ifdef __ANDROID__
+        char android[PROP_VALUE_MAX];
+        if (__system_property_get("persist.sys.dalvik.vm.lib", android) != 0)
+            guesses.push_back(android);
+#endif
+
+        guesses.push_back("libart.so");
+        guesses.push_back("libdvm.so");
+        guesses.push_back("libjvm.so");
+
+        for (const char *guess : guesses) {
+            handle = dlopen(guess, RTLD_LAZY | RTLD_GLOBAL);
+            if (handle != NULL) {
+                library = guess;
+                break;
+            }
         }
 
-        JavaVMInitArgs args;
-        memset(&args, 0, sizeof(args));
-        args.version = version;
-        args.nOptions = options.size();
-        args.options = options.data();
-        _jnicall(JNI_CreateJavaVM(&jvm, reinterpret_cast<void **>(&jni), &args));
+        _assert(library.size() != 0);
+
+        dlset($JNI_GetCreatedJavaVMs, "JNI_GetCreatedJavaVMs", handle);
+        if (JNIEnv *jni = CYGetCreatedJava($JNI_GetCreatedJavaVMs))
+            return jni;
+    }
+
+    std::vector<JavaVMOption> options;
+
+    {
+        std::ostringstream option;
+        option << "-Djava.class.path=";
+        option << CYPoolLibraryPath(pool) << "/libcycript.jar";
+        if (const char *classpath = getenv("CLASSPATH"))
+            option << ':' << classpath;
+        options.push_back(JavaVMOption{pool.strdup(option.str().c_str()), NULL});
+    }
+
+    // To use libnativehelper to access JNI_GetCreatedJavaVMs, you need JniInvocation.
+    // ...but there can only be one JniInvocation, and assuradely the other VM has it.
+    // Essentially, this API makes no sense. We need it for AndroidRuntime, though :/.
+
+    if (void *libnativehelper = dlopen("libnativehelper.so", RTLD_LAZY | RTLD_GLOBAL)) {
+        class JniInvocation$;
+        JniInvocation$ *(*JniInvocation$$init$)(JniInvocation$ *self)(NULL);
+        bool (*JniInvocation$Init)(JniInvocation$ *self, const char *library)(NULL);
+        JniInvocation$ *(*JniInvocation$finalize)(JniInvocation$ *self)(NULL);
+
+        dlset(JniInvocation$$init$, "_ZN13JniInvocationC1Ev", libnativehelper);
+        dlset(JniInvocation$Init, "_ZN13JniInvocation4InitEPKc", libnativehelper);
+        dlset(JniInvocation$finalize, "_ZN13JniInvocationD1Ev", libnativehelper);
+
+        if (JniInvocation$$init$ == NULL)
+            dlclose(libnativehelper);
+        else {
+            // XXX: we should attach a pool to the VM itself and deallocate this there
+            //auto invocation(pool.calloc<JniInvocation$>(1, 1024));
+            //_assert(JniInvocation$finalize != NULL);
+            //pool.atexit(reinterpret_cast<void (*)(void *)>(JniInvocation$finalize), invocation);
+
+            auto invocation(static_cast<JniInvocation$ *>(calloc(1, 1024)));
+            JniInvocation$$init$(invocation);
+
+            _assert(JniInvocation$Init != NULL);
+            JniInvocation$Init(invocation, NULL);
+
+            dlset($JNI_GetCreatedJavaVMs, "JNI_GetCreatedJavaVMs", libnativehelper);
+            if (JNIEnv *jni = CYGetCreatedJava($JNI_GetCreatedJavaVMs))
+                return jni;
+        }
     }
 
-    auto Cycript$(CYJavaEnv(jni).FindClass("Cycript"));
-    _envcall(jni, RegisterNatives(Cycript$, Cycript_, sizeof(Cycript_) / sizeof(Cycript_[0])));
+    if (void *libandroid_runtime = dlopen("libandroid_runtime.so", RTLD_LAZY | RTLD_GLOBAL)) {
+        class AndroidRuntime$;
+        AndroidRuntime$ *(*AndroidRuntime$$init$)(AndroidRuntime$ *self, char *args, unsigned int size)(NULL);
+        int (*AndroidRuntime$startVm)(AndroidRuntime$ *self, JavaVM **jvm, JNIEnv **jni)(NULL);
+        int (*AndroidRuntime$startReg)(JNIEnv *jni)(NULL);
+        int (*AndroidRuntime$addOption)(AndroidRuntime$ *self, const char *option, void *extra)(NULL);
+        int (*AndroidRuntime$addVmArguments)(AndroidRuntime$ *self, int, const char *const argv[])(NULL);
+        AndroidRuntime$ *(*AndroidRuntime$finalize)(AndroidRuntime$ *self)(NULL);
+
+        dlset(AndroidRuntime$$init$, "_ZN7android14AndroidRuntimeC1EPcj", libandroid_runtime);
+        dlset(AndroidRuntime$startVm, "_ZN7android14AndroidRuntime7startVmEPP7_JavaVMPP7_JNIEnv", libandroid_runtime);
+        dlset(AndroidRuntime$startReg, "_ZN7android14AndroidRuntime8startRegEP7_JNIEnv", libandroid_runtime);
+        dlset(AndroidRuntime$addOption, "_ZN7android14AndroidRuntime9addOptionEPKcPv", libandroid_runtime);
+        dlset(AndroidRuntime$addVmArguments, "_ZN7android14AndroidRuntime14addVmArgumentsEiPKPKc", libandroid_runtime);
+        dlset(AndroidRuntime$finalize, "_ZN7android14AndroidRuntimeD1Ev", libandroid_runtime);
+
+        // XXX: it would also be interesting to attach this to a global pool
+        AndroidRuntime$ *runtime(pool.calloc<AndroidRuntime$>(1, 1024));
+
+        _assert(AndroidRuntime$$init$ != NULL);
+        AndroidRuntime$$init$(runtime, NULL, 0);
+
+        if (AndroidRuntime$addOption == NULL) {
+            _assert(AndroidRuntime$addVmArguments != NULL);
+            std::vector<const char *> arguments;
+            for (const auto &option : options)
+                arguments.push_back(option.optionString);
+            AndroidRuntime$addVmArguments(runtime, arguments.size(), arguments.data());
+        } else for (const auto &option : options)
+            AndroidRuntime$addOption(runtime, option.optionString, option.extraInfo);
+
+        int failure;
+
+        _assert(AndroidRuntime$startVm != NULL);
+        failure = AndroidRuntime$startVm(runtime, &jvm, &jni);
+        _assert(failure == 0);
+
+        _assert(AndroidRuntime$startReg != NULL);
+        failure = AndroidRuntime$startReg(jni);
+        _assert(failure == 0);
+
+        return jni;
+    }
+
+    jint (*$JNI_CreateJavaVM)(JavaVM **jvm, void **, void *);
+    dlset($JNI_CreateJavaVM, "JNI_CreateJavaVM", handle);
+
+    JavaVMInitArgs args;
+    memset(&args, 0, sizeof(args));
+    args.version = CYJavaVersion;
+    args.nOptions = options.size();
+    args.options = options.data();
+    _jnicall($JNI_CreateJavaVM(&jvm, reinterpret_cast<void **>(&jni), &args));
+    return jni;
+}
 
+static JNIEnv *GetJNI(JSContextRef context) {
+    CYJavaEnv jni(GetJNI_(context));
+    auto Cycript$(jni.FindClass("Cycript"));
+    jni.RegisterNatives(Cycript$, Cycript_, sizeof(Cycript_) / sizeof(Cycript_[0]));
     return jni;
 }