diff --git a/dexposed_so/dexposed_art/dexposed.cpp b/dexposed_so/dexposed_art/dexposed.cpp index b4063ef..9243cad 100644 --- a/dexposed_so/dexposed_art/dexposed.cpp +++ b/dexposed_so/dexposed_art/dexposed.cpp @@ -59,15 +59,21 @@ namespace art { return false; } - jmethodID dexposedbridgeMainMethod = env->GetStaticMethodID(dexposed_class, - "main", "()V"); - if (dexposedbridgeMainMethod == NULL) { - LOG(ERROR) << "dexposed: Could not find method " << DEXPOSED_CLASS << ".main()"; + return true; + } + + static jboolean initNative(JNIEnv* env, jclass) { + + LOG(INFO) << "dexposed: initNative"; + + dexposed_handle_hooked_method = + env->GetStaticMethodID(dexposed_class, "handleHookedMethod", + "(Ljava/lang/reflect/Member;ILjava/lang/Object;Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;"); + if (dexposed_handle_hooked_method == NULL) { + LOG(ERROR) << "dexposed: Could not find method " << DEXPOSED_CLASS << ".handleHookedMethod()"; env->ExceptionClear(); return false; } - env->CallStaticVoidMethod(dexposed_class, dexposedbridgeMainMethod); - return true; } @@ -81,26 +87,12 @@ namespace art { } int keepLoadingDexposed = dexposedOnVmCreated(env, NULL); + if(keepLoadingDexposed) + initNative(env, NULL); return JNI_VERSION_1_6; } - static jboolean com_taobao_android_dexposed_DexposedBridge_initNative(JNIEnv* env, - jclass) { - - LOG(INFO) << "dexposed: com_taobao_android_dexposed_DexposedBridge_initNative"; - - dexposed_handle_hooked_method = - env->GetStaticMethodID(dexposed_class, "handleHookedMethod", - "(Ljava/lang/reflect/Member;ILjava/lang/Object;Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;"); - if (dexposed_handle_hooked_method == NULL) { - LOG(ERROR) << "dexposed: Could not find method " << DEXPOSED_CLASS << ".handleHookedMethod()"; - env->ExceptionClear(); - return false; - } - return true; - } - extern "C" void art_quick_dexposed_invoke_handler(); static inline const void* GetQuickDexposedInvokeHandler() { return reinterpret_cast(art_quick_dexposed_invoke_handler); @@ -340,7 +332,6 @@ namespace art { static const JNINativeMethod dexposedMethods[] = { - { "initNative", "()Z", (void*) com_taobao_android_dexposed_DexposedBridge_initNative }, { "hookMethodNative", "(Ljava/lang/reflect/Member;Ljava/lang/Class;ILjava/lang/Object;)V", (void*) com_taobao_android_dexposed_DexposedBridge_hookMethodNative }, { "invokeOriginalMethodNative", diff --git a/dexposed_so/dexposed_art/dexposed.h b/dexposed_so/dexposed_art/dexposed.h index 359d606..889b609 100644 --- a/dexposed_so/dexposed_art/dexposed.h +++ b/dexposed_so/dexposed_art/dexposed.h @@ -80,7 +80,6 @@ namespace art { static bool dexposedIsHooked(ArtMethod* method); - static jboolean com_taobao_android_dexposed_DexposedBridge_initNative(JNIEnv* env, jclass clazz); static void com_taobao_android_dexposed_DexposedBridge_hookMethodNative(JNIEnv* env, jclass clazz, jobject javaMethod, jobject declaredClassIndirect, jint slot, jobject additionalInfoIndirect); static int register_com_taobao_android_dexposed_DexposedBridge(JNIEnv* env); diff --git a/dexposed_so/dexposed_dalvik/dexposed.cpp b/dexposed_so/dexposed_dalvik/dexposed.cpp index 0a317d4..ea93983 100644 --- a/dexposed_so/dexposed_dalvik/dexposed.cpp +++ b/dexposed_so/dexposed_dalvik/dexposed.cpp @@ -156,6 +156,7 @@ extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) dexposedInfo(); keepLoadingDexposed = isRunningDalvik(); keepLoadingDexposed = dexposedOnVmCreated(env, NULL); + initNative(env, NULL); return JNI_VERSION_1_6; } @@ -190,21 +191,62 @@ bool dexposedOnVmCreated(JNIEnv* env, const char* className) { return false; } + return true; +} + +static jboolean initNative(JNIEnv* env, jclass clazz) { + + if (!keepLoadingDexposed) { + ALOGE("Not initializing Dexposed because of previous errors\n"); + return false; + } + + ::Thread* self = dvmThreadSelf(); + + dexposedHandleHookedMethod = (Method*) env->GetStaticMethodID(dexposedClass, "handleHookedMethod", + "(Ljava/lang/reflect/Member;ILjava/lang/Object;Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;"); + if (dexposedHandleHookedMethod == NULL) { + LOGE("ERROR: could not find method %s.handleHookedMethod(Member, int, Object, Object, Object[])\n", DEXPOSED_CLASS); + dvmLogExceptionStackTrace(); + env->ExceptionClear(); + keepLoadingDexposed = false; + return false; + } + + Method* dexposedInvokeOriginalMethodNative = (Method*) env->GetStaticMethodID(dexposedClass, "invokeOriginalMethodNative", + "(Ljava/lang/reflect/Member;I[Ljava/lang/Class;Ljava/lang/Class;Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;"); + if (dexposedInvokeOriginalMethodNative == NULL) { + LOGE("ERROR: could not find method %s.invokeOriginalMethodNative(Member, int, Class[], Class, Object, Object[])\n", DEXPOSED_CLASS); + dvmLogExceptionStackTrace(); + env->ExceptionClear(); + keepLoadingDexposed = false; + return false; + } + dvmSetNativeFunc(dexposedInvokeOriginalMethodNative, com_taobao_android_dexposed_DexposedBridge_invokeOriginalMethodNative, NULL); - jmethodID dexposedbridgeMainMethod = env->GetStaticMethodID(dexposedClass, "main","()V"); - if (dexposedbridgeMainMethod == NULL) { - ALOGE("ERROR: could not find method %s.main()\n", DEXPOSED_CLASS); + Method* dexposedInvokeSuperNative = (Method*) env->GetStaticMethodID(dexposedClass, "invokeSuperNative", + "(Ljava/lang/Object;[Ljava/lang/Object;Ljava/lang/reflect/Member;Ljava/lang/Class;[Ljava/lang/Class;Ljava/lang/Class;I)Ljava/lang/Object;"); + if (dexposedInvokeSuperNative == NULL) { + LOGE("ERROR: could not find method %s.dexposedInvokeNonVirtual(Object, Object[], Class, Class[], Class, int, boolean)\n", DEXPOSED_CLASS); dvmLogExceptionStackTrace(); env->ExceptionClear(); keepLoadingDexposed = false; return false; + } + dvmSetNativeFunc(dexposedInvokeSuperNative, com_taobao_android_dexposed_DexposedBridge_invokeSuperNative, NULL); + + objectArrayClass = dvmFindArrayClass("[Ljava/lang/Object;", NULL); + if (objectArrayClass == NULL) { + LOGE("Error while loading Object[] class"); + dvmLogExceptionStackTrace(); + env->ExceptionClear(); + keepLoadingDexposed = false; + return false; } - env->CallStaticVoidMethod(dexposedClass, dexposedbridgeMainMethod); return true; } - static bool dexposedInitMemberOffsets(JNIEnv* env) { PTR_gDvmJit = dlsym(RTLD_DEFAULT, "gDvmJit"); @@ -379,60 +421,6 @@ static void patchReturnTrue(uintptr_t function) { //////////////////////////////////////////////////////////// // JNI methods //////////////////////////////////////////////////////////// - -static jboolean com_taobao_android_dexposed_DexposedBridge_initNative(JNIEnv* env, jclass clazz) { - - if (!keepLoadingDexposed) { - ALOGE("Not initializing Dexposed because of previous errors\n"); - return false; - } - - ::Thread* self = dvmThreadSelf(); - - dexposedHandleHookedMethod = (Method*) env->GetStaticMethodID(dexposedClass, "handleHookedMethod", - "(Ljava/lang/reflect/Member;ILjava/lang/Object;Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;"); - if (dexposedHandleHookedMethod == NULL) { - LOGE("ERROR: could not find method %s.handleHookedMethod(Member, int, Object, Object, Object[])\n", DEXPOSED_CLASS); - dvmLogExceptionStackTrace(); - env->ExceptionClear(); - keepLoadingDexposed = false; - return false; - } - - Method* dexposedInvokeOriginalMethodNative = (Method*) env->GetStaticMethodID(dexposedClass, "invokeOriginalMethodNative", - "(Ljava/lang/reflect/Member;I[Ljava/lang/Class;Ljava/lang/Class;Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;"); - if (dexposedInvokeOriginalMethodNative == NULL) { - LOGE("ERROR: could not find method %s.invokeOriginalMethodNative(Member, int, Class[], Class, Object, Object[])\n", DEXPOSED_CLASS); - dvmLogExceptionStackTrace(); - env->ExceptionClear(); - keepLoadingDexposed = false; - return false; - } - dvmSetNativeFunc(dexposedInvokeOriginalMethodNative, com_taobao_android_dexposed_DexposedBridge_invokeOriginalMethodNative, NULL); - - Method* dexposedInvokeSuperNative = (Method*) env->GetStaticMethodID(dexposedClass, "invokeSuperNative", - "(Ljava/lang/Object;[Ljava/lang/Object;Ljava/lang/reflect/Member;Ljava/lang/Class;[Ljava/lang/Class;Ljava/lang/Class;I)Ljava/lang/Object;"); - if (dexposedInvokeSuperNative == NULL) { - LOGE("ERROR: could not find method %s.dexposedInvokeNonVirtual(Object, Object[], Class, Class[], Class, int, boolean)\n", DEXPOSED_CLASS); - dvmLogExceptionStackTrace(); - env->ExceptionClear(); - keepLoadingDexposed = false; - return false; - } - dvmSetNativeFunc(dexposedInvokeSuperNative, com_taobao_android_dexposed_DexposedBridge_invokeSuperNative, NULL); - - objectArrayClass = dvmFindArrayClass("[Ljava/lang/Object;", NULL); - if (objectArrayClass == NULL) { - LOGE("Error while loading Object[] class"); - dvmLogExceptionStackTrace(); - env->ExceptionClear(); - keepLoadingDexposed = false; - return false; - } - - return true; -} - static void com_taobao_android_dexposed_DexposedBridge_hookMethodNative(JNIEnv* env, jclass clazz, jobject reflectedMethodIndirect, jobject declaredClassIndirect, jint slot, jobject additionalInfoIndirect) { // Usage errors? @@ -576,7 +564,6 @@ static void com_taobao_android_dexposed_DexposedBridge_invokeOriginalMethodNativ static const JNINativeMethod dexposedMethods[] = { {"hookMethodNative", "(Ljava/lang/reflect/Member;Ljava/lang/Class;ILjava/lang/Object;)V", (void*)com_taobao_android_dexposed_DexposedBridge_hookMethodNative}, - {"initNative", "()Z", (void*)com_taobao_android_dexposed_DexposedBridge_initNative}, }; static int register_com_taobao_android_dexposed_DexposedBridge(JNIEnv* env) { diff --git a/dexposed_so/dexposed_dalvik/dexposed.h b/dexposed_so/dexposed_dalvik/dexposed.h index ed6bf51..934542d 100644 --- a/dexposed_so/dexposed_dalvik/dexposed.h +++ b/dexposed_so/dexposed_dalvik/dexposed.h @@ -72,6 +72,7 @@ struct DexposedHookInfo { void dexposedInfo(); bool isRunningDalvik(); bool dexposedOnVmCreated(JNIEnv* env, const char* className); +static jboolean initNative(JNIEnv* env, jclass clazz); static bool dexposedInitMemberOffsets(JNIEnv* env); static inline void dexposedSetObjectArrayElement(const ArrayObject* obj, int index, Object* val); @@ -83,7 +84,6 @@ static void patchReturnTrue(uintptr_t function); static inline bool dexposedIsHooked(const Method* method); // JNI methods -static jboolean com_taobao_android_dexposed_DexposedBridge_initNative(JNIEnv* env, jclass clazz); static void com_taobao_android_dexposed_DexposedBridge_hookMethodNative(JNIEnv* env, jclass clazz, jobject reflectedMethodIndirect, jobject declaredClassIndirect, jint slot, jobject additionalInfoIndirect); static void com_taobao_android_dexposed_DexposedBridge_invokeOriginalMethodNative(const u4* args, JValue* pResult, const Method* method, ::Thread* self);