Skip to content

Commit bc89261

Browse files
author
rrrfff
committed
fixs for Java.NullPointerException
fixs for Java.NullPointerException when looking for backup method by stack method name
1 parent d30d0cc commit bc89261

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

java/src/apk/andhook/ArtHook.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,16 @@ public static void hook(Method origin, Method replace) {
4040
Pair.create(replace.getDeclaringClass().getName(),
4141
replace.getName()), backUp);
4242

43+
// Log.d("ART", replace.getDeclaringClass().getName() + ":" + replace.getName());
44+
4345
// 2. replace method
4446
hookNoBackup(origin, replace);
4547
}
4648

4749
public static Object callOrigin(Object receiver, Object... params) {
4850
final StackTraceElement currentStack = Thread.currentThread()
4951
.getStackTrace()[4];
52+
// Log.d("ART", currentStack.getClassName() + ":" + currentStack.getMethodName());
5053
final Method method = sBackups.get(Pair.create(
5154
currentStack.getClassName(), currentStack.getMethodName()));
5255

java/src/apk/andhook/DalvikHook.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ public static void hook(Method origin, Method replace) {
6161
final int backUpSlot = dvmHookNative(origin, replace);
6262
// @TODO Overload method is not supported
6363
sBackups.put(
64-
Pair.create(replace.getDeclaringClass().getName(),
65-
replace.getName()), backUpSlot);
64+
Pair.create(origin.getDeclaringClass().getName(),
65+
origin.getName()), backUpSlot);
6666
}
6767

6868
private static int getBackupMethodSlot() {

jni/andhook.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ static __typeof__(&dvmThreadSelf) g_dvmThreadSelf;
4747

4848
//-------------------------------------------------------------------------
4949

50-
static int __native_hook(JNIEnv *env, Method *vm_origin, jobject target)
50+
static int __native_hook(JNIEnv *env, Method *vm_origin, Method *vm_target)
5151
{
5252
// Method *vm_origin = reinterpret_cast<Method *>(env->FromReflectedMethod(origin));
53-
Method *vm_target = reinterpret_cast<Method *>(env->FromReflectedMethod(target));
53+
// Method *vm_target = reinterpret_cast<Method *>(env->FromReflectedMethod(target));
5454

5555
// Dalvik puts private, static, and constructors into non-virtual table
5656
// g_dvmIsDirectMethod(vm_origin)
@@ -86,7 +86,8 @@ static int __native_hook(JNIEnv *env, Method *vm_origin, jobject target)
8686
static void JNICALL dvmHookNativeNoBackup(JNIEnv *env, jclass, jobject origin, jobject target)
8787
{
8888
Method *vm_origin = reinterpret_cast<Method *>(env->FromReflectedMethod(origin));
89-
__native_hook(env, vm_origin, target);
89+
Method *vm_target = reinterpret_cast<Method *>(env->FromReflectedMethod(target));
90+
__native_hook(env, vm_origin, vm_target);
9091
}
9192

9293
//-------------------------------------------------------------------------
@@ -101,10 +102,14 @@ static jint JNICALL dvmHookNative(JNIEnv *env, jclass, jobject origin, jobject t
101102

102103
Method *vm_origin = reinterpret_cast<Method *>(env->FromReflectedMethod(origin));
103104
g_backups[slot] = *vm_origin;
104-
105-
if (__native_hook(env, vm_origin, target) == 0) {
105+
Method *vm_target = reinterpret_cast<Method *>(env->FromReflectedMethod(target));
106+
if (__native_hook(env, vm_origin, vm_target) == 0) {
106107
CLEAR_METHOD_FLAG(&g_backups[slot], ACC_PUBLIC);
107108
SET_METHOD_FLAG(&g_backups[slot], ACC_PRIVATE);
109+
110+
// fixs for Java.NullPointerException when looking for
111+
// backup method by stack method name, but target method may not be found later
112+
vm_target->name = vm_origin->name;
108113
} //if
109114

110115
return slot;

0 commit comments

Comments
 (0)