Commit 0722234f authored by swift_gan's avatar swift_gan Committed by swift_gan

tweak api

parent 6bce5cfd
...@@ -184,7 +184,7 @@ bool doHookWithInline(JNIEnv* env, ...@@ -184,7 +184,7 @@ bool doHookWithInline(JNIEnv* env,
} }
extern "C" extern "C"
JNIEXPORT jboolean JNICALL JNIEXPORT jint JNICALL
Java_com_swift_sandhook_SandHook_hookMethod(JNIEnv *env, jclass type, jobject originMethod, Java_com_swift_sandhook_SandHook_hookMethod(JNIEnv *env, jclass type, jobject originMethod,
jobject hookMethod, jobject backupMethod, jint hookMode) { jobject hookMethod, jobject backupMethod, jint hookMode) {
...@@ -232,11 +232,9 @@ Java_com_swift_sandhook_SandHook_hookMethod(JNIEnv *env, jclass type, jobject or ...@@ -232,11 +232,9 @@ Java_com_swift_sandhook_SandHook_hookMethod(JNIEnv *env, jclass type, jobject or
label_hook: label_hook:
if (isInlineHook && trampolineManager.canSafeInline(origin)) { if (isInlineHook && trampolineManager.canSafeInline(origin)) {
LOGD("method hook by inline!"); return doHookWithInline(env, origin, hook, backup) ? INLINE : -1;
return static_cast<jboolean>(doHookWithInline(env, origin, hook, backup));
} else { } else {
LOGW("method hook by replacement!"); return doHookWithReplacement(env, origin, hook, backup) ? REPLACE : -1;
return static_cast<jboolean>(doHookWithReplacement(env, origin, hook, backup));
} }
} }
......
...@@ -24,6 +24,11 @@ public class SandHook { ...@@ -24,6 +24,11 @@ public class SandHook {
SandHook.hookModeCallBack = hookModeCallBack; SandHook.hookModeCallBack = hookModeCallBack;
} }
private static HookResultCallBack hookResultCallBack;
public static void setHookResultCallBack(HookResultCallBack hookResultCallBack) {
SandHook.hookResultCallBack = hookResultCallBack;
}
public static Class artMethodClass; public static Class artMethodClass;
public static Field nativePeerField; public static Field nativePeerField;
...@@ -67,23 +72,20 @@ public class SandHook { ...@@ -67,23 +72,20 @@ public class SandHook {
} }
public static synchronized void hook(HookWrapper.HookEntity entity) throws HookErrorException { public static synchronized void hook(HookWrapper.HookEntity entity) throws HookErrorException {
if (entity.target != null && entity.hook != null) {
if (entity == null)
throw new HookErrorException("null entity");
Member target = entity.target;
Method hook = entity.hook;
Method backup = entity.backup;
if (target == null || hook == null)
throw new HookErrorException("null input");
if (globalHookEntityMap.containsKey(entity.target)) if (globalHookEntityMap.containsKey(entity.target))
throw new HookErrorException("method <" + entity.target.getName() + "> has been hooked!"); throw new HookErrorException("method <" + entity.target.getName() + "> has been hooked!");
if (!SandHook.hook(entity.target, entity.hook, entity.backup)) {
throw new HookErrorException("hook method <" + entity.target.getName() + "> error in native!");
}
Log.d("SandHook", "method <" + entity.target.getName() + "> hook success!");
globalHookEntityMap.put(entity.target, entity);
if (entity.backup != null) {
globalBackupMap.put(entity.backup, entity);
}
}
}
public static boolean hook(Member target, Method hook, Method backup) {
if (target == null || hook == null)
return false;
resolveStaticMethod(target); resolveStaticMethod(target);
if (backup != null) { if (backup != null) {
resolveStaticMethod(backup); resolveStaticMethod(backup);
...@@ -98,7 +100,7 @@ public class SandHook { ...@@ -98,7 +100,7 @@ public class SandHook {
mode = hookModeCallBack.hookMode(target); mode = hookModeCallBack.hookMode(target);
} }
boolean res; int res;
if (mode != HookMode.AUTO) { if (mode != HookMode.AUTO) {
res = hookMethod(target, hook, backup, mode); res = hookMethod(target, hook, backup, mode);
} else { } else {
...@@ -106,10 +108,25 @@ public class SandHook { ...@@ -106,10 +108,25 @@ public class SandHook {
res = hookMethod(target, hook, backup, hookMode == null ? HookMode.AUTO : hookMode.value()); res = hookMethod(target, hook, backup, hookMode == null ? HookMode.AUTO : hookMode.value());
} }
if (res && backup != null) { if (res > 0 && backup != null) {
backup.setAccessible(true); backup.setAccessible(true);
} }
return res;
entity.hookMode = res;
if (hookResultCallBack != null) {
hookResultCallBack.hookResult(res > 0, entity);
}
if (res <= 0)
throw new HookErrorException("hook method <" + entity.target.toString() + "> error in native!");
globalHookEntityMap.put(entity.target, entity);
if (entity.backup != null) {
globalBackupMap.put(entity.backup, entity);
}
Log.d("SandHook", "method <" + entity.target.toString() + "> hook <" + (res == HookMode.INLINE ? "inline" : "replacement") + "> success!");
} }
public static void ensureBackupDeclaringClass(Method backupMethod) { public static void ensureBackupDeclaringClass(Method backupMethod) {
...@@ -226,7 +243,7 @@ public class SandHook { ...@@ -226,7 +243,7 @@ public class SandHook {
//default on! //default on!
public static native void setInlineSafeCheck(boolean check); public static native void setInlineSafeCheck(boolean check);
private static native boolean hookMethod(Member originMethod, Method hookMethod, Method backupMethod, int hookMode); private static native int hookMethod(Member originMethod, Method hookMethod, Method backupMethod, int hookMode);
public static native void ensureMethodCached(Method hook, Method backup); public static native void ensureMethodCached(Method hook, Method backup);
...@@ -237,4 +254,9 @@ public class SandHook { ...@@ -237,4 +254,9 @@ public class SandHook {
int hookMode(Member originMethod); int hookMode(Member originMethod);
} }
@FunctionalInterface
public interface HookResultCallBack {
void hookResult(boolean success, HookWrapper.HookEntity hookEntity);
}
} }
...@@ -273,6 +273,7 @@ public class HookWrapper { ...@@ -273,6 +273,7 @@ public class HookWrapper {
public boolean hookIsStub = false; public boolean hookIsStub = false;
public Class[] pars; public Class[] pars;
public int hookMode;
public HookEntity(Member target) { public HookEntity(Member target) {
this.target = target; this.target = target;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment