Skip to content

Commit

Permalink
REMOVE : unnecessary call java main() when jni_Onload
Browse files Browse the repository at this point in the history
  • Loading branch information
baiyi.hwj committed Aug 31, 2015
1 parent 539f887 commit 988b6eb
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 85 deletions.
37 changes: 14 additions & 23 deletions dexposed_so/dexposed_art/dexposed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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<void*>(art_quick_dexposed_invoke_handler);
Expand Down Expand Up @@ -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",
Expand Down
1 change: 0 additions & 1 deletion dexposed_so/dexposed_art/dexposed.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
107 changes: 47 additions & 60 deletions dexposed_so/dexposed_dalvik/dexposed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion dexposed_so/dexposed_dalvik/dexposed.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
Expand Down

0 comments on commit 988b6eb

Please sign in to comment.