From f2c357f9fb922258dac9f808df0143e548c4bfdc Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Mon, 4 Jan 2016 11:34:59 -0800 Subject: [PATCH] Find JVM with dlopen and register Android natives. --- Java/Execute.cpp | 198 +++++++++++++++++++++++++++++++++++++++-------- Pooling.hpp | 7 ++ configure | 57 -------------- configure.ac | 2 - 4 files changed, 171 insertions(+), 93 deletions(-) diff --git a/Java/Execute.cpp b/Java/Execute.cpp index 2156045..471f71c 100644 --- a/Java/Execute.cpp +++ b/Java/Execute.cpp @@ -23,12 +23,19 @@ #include #include +#include + #ifdef __APPLE__ #include #else #include #endif +#ifdef __ANDROID__ +// XXX: this is deprecated?!?!?!?!?!?! +#include +#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) \ @@ -1299,46 +1301,174 @@ static JNINativeMethod Cycript_[] = { {(char *) "handle", (char *) "(JLjava/lang/String;[Ljava/lang/Object;)Ljava/lang/Object;", (void *) &Cycript_handle}, }; -JNIEnv *GetJNI(JSContextRef context) { +template +static _finline void dlset(Type_ &function, const char *name, void *handle) { + function = reinterpret_cast(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(&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(&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 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 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(&jni), &args)); + _assert(library.size() != 0); + + dlset($JNI_GetCreatedJavaVMs, "JNI_GetCreatedJavaVMs", handle); + if (JNIEnv *jni = CYGetCreatedJava($JNI_GetCreatedJavaVMs)) + return jni; } - auto Cycript$(CYJavaEnv(jni).FindClass("Cycript")); - _envcall(jni, RegisterNatives(Cycript$, Cycript_, sizeof(Cycript_) / sizeof(Cycript_[0]))); + std::vector 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); + + // XXX: we should attach a pool to the VM itself and deallocate this there + //auto invocation(pool.calloc(1, 1024)); + //_assert(JniInvocation$finalize != NULL); + //pool.atexit(reinterpret_cast(JniInvocation$finalize), invocation); + + auto invocation(static_cast(calloc(1, 1024))); + + _assert(JniInvocation$$init$ != NULL); + 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; + } + + 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(1, 1024)); + + _assert(AndroidRuntime$$init$ != NULL); + AndroidRuntime$$init$(runtime, NULL, 0); + + if (AndroidRuntime$addOption == NULL) { + _assert(AndroidRuntime$addVmArguments != NULL); + std::vector 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(&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; } diff --git a/Pooling.hpp b/Pooling.hpp index b67f262..fc9d7eb 100644 --- a/Pooling.hpp +++ b/Pooling.hpp @@ -115,6 +115,13 @@ class CYPool { return reinterpret_cast(data); } + template + Type_ *calloc(size_t count, size_t size, size_t alignment = CYAlignment) { + Type_ *data(malloc(count * size, alignment)); + memset(data, 0, count * size); + return data; + } + char *strdup(const char *data) { if (data == NULL) return NULL; diff --git a/configure b/configure index 0326e92..154012d 100755 --- a/configure +++ b/configure @@ -23126,63 +23126,6 @@ if test "x$ac_cv_header_jni_h" = xyes; then : CY_JAVA=1 - { $as_echo "$as_me:${as_lineno-$LINENO}: checking for library containing JNI_GetCreatedJavaVMs" >&5 -$as_echo_n "checking for library containing JNI_GetCreatedJavaVMs... " >&6; } -if ${ac_cv_search_JNI_GetCreatedJavaVMs+:} false; then : - $as_echo_n "(cached) " >&6 -else - ac_func_search_save_LIBS=$LIBS -cat confdefs.h - <<_ACEOF >conftest.$ac_ext -/* end confdefs.h. */ - -/* Override any GCC internal prototype to avoid an error. - Use char because int might match the return type of a GCC - builtin and then its argument prototype would still apply. */ -#ifdef __cplusplus -extern "C" -#endif -char JNI_GetCreatedJavaVMs (); -int -main () -{ -return JNI_GetCreatedJavaVMs (); - ; - return 0; -} -_ACEOF -for ac_lib in '' art dvm; do - if test -z "$ac_lib"; then - ac_res="none required" - else - ac_res=-l$ac_lib - LIBS="-l$ac_lib $ac_func_search_save_LIBS" - fi - if ac_fn_cxx_try_link "$LINENO"; then : - ac_cv_search_JNI_GetCreatedJavaVMs=$ac_res -fi -rm -f core conftest.err conftest.$ac_objext \ - conftest$ac_exeext - if ${ac_cv_search_JNI_GetCreatedJavaVMs+:} false; then : - break -fi -done -if ${ac_cv_search_JNI_GetCreatedJavaVMs+:} false; then : - -else - ac_cv_search_JNI_GetCreatedJavaVMs=no -fi -rm conftest.$ac_ext -LIBS=$ac_func_search_save_LIBS -fi -{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_search_JNI_GetCreatedJavaVMs" >&5 -$as_echo "$ac_cv_search_JNI_GetCreatedJavaVMs" >&6; } -ac_res=$ac_cv_search_JNI_GetCreatedJavaVMs -if test "$ac_res" != no; then : - test "$ac_res" = "none required" || LIBS="$ac_res $LIBS" - - -fi - else diff --git a/configure.ac b/configure.ac index 0ee1cb2..5ae7a76 100644 --- a/configure.ac +++ b/configure.ac @@ -225,8 +225,6 @@ AS_CASE([$CY_EXECUTE], [1], [ AC_SUBST([CY_JAVA], [1]) ], [AC_CHECK_HEADER([jni.h], [ AC_SUBST([CY_JAVA], [1]) - AC_SEARCH_LIBS([JNI_GetCreatedJavaVMs], [art dvm], [ - ]) ], [ AC_SUBST([CY_JAVA], [0]) ])])]) -- 2.45.2