Commit 09f7d702 authored by swift_gan's avatar swift_gan

tweak so name & add custom classloader

parent b55f58e4
...@@ -11,13 +11,13 @@ cmake_minimum_required(VERSION 3.4.1) ...@@ -11,13 +11,13 @@ cmake_minimum_required(VERSION 3.4.1)
# Gradle automatically packages shared libraries with your APK. # Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library. add_library( # Sets the name of the library.
native-lib lib-sandhook
# Sets the library as a shared library. # Sets the library as a shared library.
SHARED SHARED
# Provides a relative path to your source file(s). # Provides a relative path to your source file(s).
src/main/cpp/native-lib.cpp src/main/cpp/sandhooklib.cpp
src/main/cpp/trampoline/trampoline.cpp src/main/cpp/trampoline/trampoline.cpp
src/main/cpp/trampoline/trampoline_manager.cpp src/main/cpp/trampoline/trampoline_manager.cpp
src/main/cpp/utils/fake_dlfcn.cpp src/main/cpp/utils/fake_dlfcn.cpp
...@@ -43,7 +43,7 @@ find_library( # Sets the name of the path variable. ...@@ -43,7 +43,7 @@ find_library( # Sets the name of the path variable.
# build script, prebuilt third-party libraries, or system libraries. # build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library. target_link_libraries( # Specifies the target library.
native-lib lib-sandhook
# Links the target library to the log library # Links the target library to the log library
# included in the NDK. # included in the NDK.
......
...@@ -24,7 +24,7 @@ public class SandHook { ...@@ -24,7 +24,7 @@ public class SandHook {
public static int testAccessFlag; public static int testAccessFlag;
static { static {
System.loadLibrary("native-lib"); System.loadLibrary("lib-sandhook");
init(); init();
} }
...@@ -47,6 +47,10 @@ public class SandHook { ...@@ -47,6 +47,10 @@ public class SandHook {
HookWrapper.addHookClass(hookWrapperClass); HookWrapper.addHookClass(hookWrapperClass);
} }
public static void addHookClass(ClassLoader classLoader, Class... hookWrapperClass) throws HookErrorException {
HookWrapper.addHookClass(hookWrapperClass);
}
public static boolean hook(Member target, Method hook, Method backup) { public static boolean hook(Member target, Method hook, Method backup) {
if (target == null || hook == null) if (target == null || hook == null)
return false; return false;
......
...@@ -24,13 +24,17 @@ public class HookWrapper { ...@@ -24,13 +24,17 @@ public class HookWrapper {
static Map<Member,HookEntity> globalHookEntityMap = new ConcurrentHashMap<>(); static Map<Member,HookEntity> globalHookEntityMap = new ConcurrentHashMap<>();
public static void addHookClass(Class<?>... classes) throws HookErrorException { public static void addHookClass(Class<?>... classes) throws HookErrorException {
addHookClass(null, classes);
}
public static void addHookClass(ClassLoader classLoader, Class<?>... classes) throws HookErrorException {
for (Class clazz:classes) { for (Class clazz:classes) {
addHookClass(clazz); addHookClass(classLoader, clazz);
} }
} }
public static void addHookClass(Class<?> clazz) throws HookErrorException { public static void addHookClass(ClassLoader classLoader, Class<?> clazz) throws HookErrorException {
Class targetHookClass = getTargetHookClass(clazz); Class targetHookClass = getTargetHookClass(classLoader, clazz);
if (targetHookClass == null) if (targetHookClass == null)
throw new HookErrorException("error hook wrapper class :" + clazz.getName()); throw new HookErrorException("error hook wrapper class :" + clazz.getName());
Map<Member,HookEntity> hookEntityMap = getHookMethods(targetHookClass, clazz); Map<Member,HookEntity> hookEntityMap = getHookMethods(targetHookClass, clazz);
...@@ -194,14 +198,18 @@ public class HookWrapper { ...@@ -194,14 +198,18 @@ public class HookWrapper {
private static Class getTargetHookClass(Class<?> hookWrapperClass) { private static Class getTargetHookClass(ClassLoader classLoader, Class<?> hookWrapperClass) {
HookClass hookClass = hookWrapperClass.getAnnotation(HookClass.class); HookClass hookClass = hookWrapperClass.getAnnotation(HookClass.class);
HookReflectClass hookReflectClass = hookWrapperClass.getAnnotation(HookReflectClass.class); HookReflectClass hookReflectClass = hookWrapperClass.getAnnotation(HookReflectClass.class);
if (hookClass != null) { if (hookClass != null) {
return hookClass.value(); return hookClass.value();
} else if (hookReflectClass != null) { } else if (hookReflectClass != null) {
try { try {
return Class.forName(hookReflectClass.value()); if (classLoader == null) {
return Class.forName(hookReflectClass.value());
} else {
return Class.forName(hookReflectClass.value(), true, classLoader);
}
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
return null; return null;
} }
......
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