Commit d1c1b69e authored by swift_gan's avatar swift_gan

add hookmode modifier

parent adf97741
package com.swift.sandhook.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface HookMode {
int AUTO = 0;
int INLINE = 1;
int REPLACE = 2;
int value() default AUTO;
}
......@@ -7,6 +7,12 @@ SandHook::TrampolineManager trampolineManager;
int SDK_INT = 0;
enum HookMode {
AUTO = 0,
INLINE = 1,
REPLACE = 2
};
extern "C"
JNIEXPORT jboolean JNICALL
Java_com_swift_sandhook_SandHook_initNative(JNIEnv *env, jclass type, jint sdk) {
......@@ -151,26 +157,38 @@ bool doHookWithInline(JNIEnv* env,
extern "C"
JNIEXPORT jboolean JNICALL
Java_com_swift_sandhook_SandHook_hookMethod(JNIEnv *env, jclass type, jobject originMethod,
jobject hookMethod, jobject backupMethod) {
jobject hookMethod, jobject backupMethod, jint hookMode) {
// TODO
art::mirror::ArtMethod* origin = reinterpret_cast<art::mirror::ArtMethod *>(env->FromReflectedMethod(originMethod));
art::mirror::ArtMethod* hook = reinterpret_cast<art::mirror::ArtMethod *>(env->FromReflectedMethod(hookMethod));
art::mirror::ArtMethod* backup = backupMethod == NULL ? nullptr : reinterpret_cast<art::mirror::ArtMethod *>(env->FromReflectedMethod(backupMethod));
// if (backup != nullptr) {
// memcpy(backup, origin, SandHook::CastArtMethod::size);
// }
bool isInterpreter = SandHook::CastArtMethod::entryPointQuickCompiled->get(origin) == SandHook::CastArtMethod::quickToInterpreterBridge;
bool idJNi = SandHook::CastArtMethod::entryPointQuickCompiled->get(origin) == SandHook::CastArtMethod::genericJniStub;
int mode = reinterpret_cast<int>(hookMode);
if (mode == INLINE) {
if (isInterpreter) {
if (SDK_INT >= ANDROID_N) {
Size threadId = getAddressFromJavaByCallMethod(env, "com/swift/sandhook/SandHook", "getThreadId");
if (compileMethod(origin, reinterpret_cast<void *>(threadId)) &&
SandHook::CastArtMethod::entryPointQuickCompiled->get(origin) !=
SandHook::CastArtMethod::quickToInterpreterBridge) {
return static_cast<jboolean>(doHookWithInline(env, origin, hook, backup));
} else {
return static_cast<jboolean>(doHookWithReplacement(origin, hook, backup));
}
}
} else {
return static_cast<jboolean>(doHookWithInline(env, origin, hook, backup));
}
} else if (mode == REPLACE) {
return static_cast<jboolean>(doHookWithReplacement(origin, hook, backup));
}
// #if defined(__arm__)
// doHookWithReplacement(origin, hook, backup);
// return JNI_TRUE;
// #endif
if (isAbsMethod(origin)) {
if (SDK_INT >= ANDROID_P && BYTE_POINT == 4) {
return static_cast<jboolean>(doHookWithReplacement(origin, hook, backup));
} else if (isAbsMethod(origin)) {
return static_cast<jboolean>(doHookWithReplacement(origin, hook, backup));
} else if (isInterpreter) {
if (SDK_INT >= ANDROID_N) {
......
......@@ -2,10 +2,10 @@ package com.swift.sandhook;
import android.os.Build;
import com.swift.sandhook.annotation.HookMode;
import com.swift.sandhook.wrapper.HookErrorException;
import com.swift.sandhook.wrapper.HookWrapper;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
......@@ -62,7 +62,8 @@ public class SandHook {
if (target instanceof Method) {
((Method)target).setAccessible(true);
}
boolean res = hookMethod(target, hook, backup);
HookMode hookMode = hook.getAnnotation(HookMode.class);
boolean res = hookMethod(target, hook, backup, hookMode == null ? HookMode.AUTO : hookMode.value());
if (res && backup != null) {
backup.setAccessible(true);
}
......@@ -160,7 +161,7 @@ public class SandHook {
private static native boolean initNative(int sdk);
private static native boolean hookMethod(Member originMethod, Method hookMethod, Method backupMethod);
private static native boolean hookMethod(Member originMethod, Method hookMethod, Method backupMethod, int hookMode);
public static native void ensureMethodCached(Method hook, Method backup);
......
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