Commit 6378231b authored by swift_gan's avatar swift_gan

ensure static method resolved

parent 790083c1
...@@ -84,7 +84,7 @@ public: ...@@ -84,7 +84,7 @@ public:
// // the interpreter. // // the interpreter.
// void* entry_point_from_quick_compiled_code_; // void* entry_point_from_quick_compiled_code_;
// } ptr_sized_fields_; // } ptr_sized_fields_;
uint16_t placeholder[40]; void * placeHolder;
}; };
} }
......
...@@ -109,7 +109,7 @@ bool doHookWithInline(JNIEnv* env, ...@@ -109,7 +109,7 @@ bool doHookWithInline(JNIEnv* env,
memcpy(backupMethod, originMethod, SandHook::CastArtMethod::size); memcpy(backupMethod, originMethod, SandHook::CastArtMethod::size);
Code callOriginCode = hookTrampoline->callOrigin->getCode(); Code callOriginCode = hookTrampoline->callOrigin->getCode();
if (hookTrampoline->callOrigin->isThumCode()) { if (hookTrampoline->callOrigin->isThumbCode()) {
callOriginCode = hookTrampoline->callOrigin->getThumbCodePcAddress(callOriginCode); callOriginCode = hookTrampoline->callOrigin->getThumbCodePcAddress(callOriginCode);
} }
SandHook::CastArtMethod::entryPointQuickCompiled->set(backupMethod, callOriginCode); SandHook::CastArtMethod::entryPointQuickCompiled->set(backupMethod, callOriginCode);
......
...@@ -25,7 +25,7 @@ namespace SandHook { ...@@ -25,7 +25,7 @@ namespace SandHook {
Code templateCode() override { Code templateCode() override {
#if defined(__arm__) #if defined(__arm__)
if (isThumCode()) { if (isThumbCode()) {
return getThumbCodeAddress(reinterpret_cast<Code>(DIRECT_JUMP_TRAMPOLINE_T)); return getThumbCodeAddress(reinterpret_cast<Code>(DIRECT_JUMP_TRAMPOLINE_T));
} else { } else {
return reinterpret_cast<Code>(DIRECT_JUMP_TRAMPOLINE); return reinterpret_cast<Code>(DIRECT_JUMP_TRAMPOLINE);
...@@ -93,7 +93,7 @@ namespace SandHook { ...@@ -93,7 +93,7 @@ namespace SandHook {
Code templateCode() override { Code templateCode() override {
#if defined(__arm__) #if defined(__arm__)
if (isThumCode()) { if (isThumbCode()) {
return getThumbCodeAddress(reinterpret_cast<Code>(INLINE_HOOK_TRAMPOLINE_T)); return getThumbCodeAddress(reinterpret_cast<Code>(INLINE_HOOK_TRAMPOLINE_T));
} else { } else {
return reinterpret_cast<Code>(INLINE_HOOK_TRAMPOLINE); return reinterpret_cast<Code>(INLINE_HOOK_TRAMPOLINE);
...@@ -122,7 +122,7 @@ namespace SandHook { ...@@ -122,7 +122,7 @@ namespace SandHook {
Code templateCode() override { Code templateCode() override {
#if defined(__arm__) #if defined(__arm__)
if (isThumCode()) { if (isThumbCode()) {
return getThumbCodeAddress(reinterpret_cast<Code>(CALL_ORIGIN_TRAMPOLINE_T)); return getThumbCodeAddress(reinterpret_cast<Code>(CALL_ORIGIN_TRAMPOLINE_T));
} else { } else {
return reinterpret_cast<Code>(CALL_ORIGIN_TRAMPOLINE); return reinterpret_cast<Code>(CALL_ORIGIN_TRAMPOLINE);
......
...@@ -118,7 +118,7 @@ namespace SandHook { ...@@ -118,7 +118,7 @@ namespace SandHook {
isThumb = thumb; isThumb = thumb;
} }
bool isThumCode() { bool isThumbCode() {
return isThumb; return isThumb;
} }
......
...@@ -103,7 +103,7 @@ namespace SandHook { ...@@ -103,7 +103,7 @@ namespace SandHook {
goto label_error; goto label_error;
} }
if (directJumpTrampoline->isThumCode()) { if (directJumpTrampoline->isThumbCode()) {
originEntry = directJumpTrampoline->getThumbCodeAddress(originEntry); originEntry = directJumpTrampoline->getThumbCodeAddress(originEntry);
directJumpTrampoline->setExecuteSpace(originEntry); directJumpTrampoline->setExecuteSpace(originEntry);
directJumpTrampoline->setJumpTarget(directJumpTrampoline->getThumbCodePcAddress(inlineHookTrampoline->getCode())); directJumpTrampoline->setJumpTarget(directJumpTrampoline->getThumbCodePcAddress(inlineHookTrampoline->getCode()));
...@@ -124,7 +124,7 @@ namespace SandHook { ...@@ -124,7 +124,7 @@ namespace SandHook {
callOriginTrampoline->setExecuteSpace(callOriginTrampolineSpace); callOriginTrampoline->setExecuteSpace(callOriginTrampolineSpace);
callOriginTrampoline->setOriginMethod(reinterpret_cast<Code>(originMethod)); callOriginTrampoline->setOriginMethod(reinterpret_cast<Code>(originMethod));
Code originCode = nullptr; Code originCode = nullptr;
if (callOriginTrampoline->isThumCode()) { if (callOriginTrampoline->isThumbCode()) {
originCode = callOriginTrampoline->getThumbCodePcAddress(inlineHookTrampoline->getCallOriginCode()); originCode = callOriginTrampoline->getThumbCodePcAddress(inlineHookTrampoline->getCallOriginCode());
#if defined(__arm__) #if defined(__arm__)
Code originRemCode = callOriginTrampoline->getThumbCodePcAddress(originEntry + directJumpTrampoline->getCodeLen()); Code originRemCode = callOriginTrampoline->getThumbCodePcAddress(originEntry + directJumpTrampoline->getCodeLen());
......
...@@ -33,6 +33,9 @@ void initHideApi(JNIEnv* env, int SDK_VERSION) { ...@@ -33,6 +33,9 @@ void initHideApi(JNIEnv* env, int SDK_VERSION) {
} }
bool compileMethod(void* artMethod, void* thread) { bool compileMethod(void* artMethod, void* thread) {
if (jitCompileMethod == nullptr) {
return false;
}
return jitCompileMethod(jitCompilerHandle, artMethod, thread, false); return jitCompileMethod(jitCompilerHandle, artMethod, thread, false);
} }
......
...@@ -15,6 +15,7 @@ import com.swift.sandhook.wrapper.HookErrorException; ...@@ -15,6 +15,7 @@ import com.swift.sandhook.wrapper.HookErrorException;
import com.swift.sandhook.wrapper.HookWrapper; import com.swift.sandhook.wrapper.HookWrapper;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
...@@ -38,13 +39,13 @@ public class MainActivity extends AppCompatActivity { ...@@ -38,13 +39,13 @@ public class MainActivity extends AppCompatActivity {
} }
}); });
methodBeHooked(savedInstanceState); methodBeHooked(hashCode(), 1);
toolbar.postDelayed(new Runnable() { toolbar.postDelayed(new Runnable() {
@Override @Override
public void run() { public void run() {
methodBeHooked(savedInstanceState); methodBeHooked(hashCode(), 3);
Log.w("gy", "dadadad"); Log.w("gy", "dadadad");
} }
}, 3000); }, 3000);
...@@ -72,10 +73,10 @@ public class MainActivity extends AppCompatActivity { ...@@ -72,10 +73,10 @@ public class MainActivity extends AppCompatActivity {
return true; return true;
} }
public static void methodBeHooked(Bundle bundle) { public static int methodBeHooked(int a, int b) {
int a = 1 + 2; a = a + 1 + 2;
int b = a + 3; b = b + a + 3;
Log.e("gy", "not hooked" + b + bundle); return a + b;
} }
@Override @Override
......
...@@ -2,6 +2,10 @@ package com.swift.sandhook; ...@@ -2,6 +2,10 @@ package com.swift.sandhook;
import android.app.Application; import android.app.Application;
import com.swift.sandhook.test.ActivityHooker;
import com.swift.sandhook.test.CustmizeHooker;
import com.swift.sandhook.test.LogHooker;
import com.swift.sandhook.test.ObjectHooker;
import com.swift.sandhook.wrapper.HookErrorException; import com.swift.sandhook.wrapper.HookErrorException;
public class MyApp extends Application { public class MyApp extends Application {
...@@ -9,9 +13,10 @@ public class MyApp extends Application { ...@@ -9,9 +13,10 @@ public class MyApp extends Application {
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
try { try {
SandHook.addHookClass(ActivityHooker.class, ObjectHooker.class); SandHook.addHookClass(LogHooker.class, CustmizeHooker.class, ActivityHooker.class, ObjectHooker.class);
} catch (HookErrorException e) { } catch (HookErrorException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
...@@ -7,6 +7,7 @@ import android.support.annotation.Nullable; ...@@ -7,6 +7,7 @@ import android.support.annotation.Nullable;
import com.swift.sandhook.wrapper.HookErrorException; import com.swift.sandhook.wrapper.HookErrorException;
import com.swift.sandhook.wrapper.HookWrapper; import com.swift.sandhook.wrapper.HookWrapper;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Member; import java.lang.reflect.Member;
import java.lang.reflect.Method; import java.lang.reflect.Method;
...@@ -47,18 +48,28 @@ public class SandHook { ...@@ -47,18 +48,28 @@ public class SandHook {
HookWrapper.addHookClass(hookWrapperClass); HookWrapper.addHookClass(hookWrapperClass);
} }
public static void hook(@NonNull Member target, @NonNull Method hook, @Nullable Method backup) { public static boolean hook(@NonNull Member target, @NonNull Method hook, @Nullable Method backup) {
if (target == null || hook == null)
return false;
resolveStaticMethod(target);
if (backup != null) { if (backup != null) {
resolveBackupMethod(backup); resolveStaticMethod(backup);
SandHookMethodResolver.resolveMethod(hook, backup); SandHookMethodResolver.resolveMethod(hook, backup);
} }
hookMethod(target, hook, backup); if (target instanceof Method) {
((Method)target).setAccessible(true);
}
return hookMethod(target, hook, backup);
} }
private static void resolveBackupMethod(Method method) { private static void resolveStaticMethod(Member method) {
//ignore result, just call to trigger resolve //ignore result, just call to trigger resolve
try { try {
method.invoke(null, null); if (method instanceof Method) {
((Method)method).invoke(null);
} else if (method instanceof Constructor){
((Constructor)method).newInstance(null);
}
} catch (Exception e) { } catch (Exception e) {
} }
} }
......
package com.swift.sandhook; package com.swift.sandhook.test;
import android.app.Activity; import android.app.Activity;
import android.os.Bundle; import android.os.Bundle;
...@@ -15,7 +15,7 @@ public class ActivityHooker { ...@@ -15,7 +15,7 @@ public class ActivityHooker {
@HookMethod("onCreate") @HookMethod("onCreate")
@MethodParams(Bundle.class) @MethodParams(Bundle.class)
public static void onCreate(Activity thiz, Bundle bundle) { public static void onCreate(Activity thiz, Bundle bundle) {
Log.e("ActivityHooker", "hooked success " + thiz); Log.e("ActivityHooker", "hooked onCreate success " + thiz);
onCreateBackup(thiz, bundle); onCreateBackup(thiz, bundle);
} }
...@@ -25,4 +25,15 @@ public class ActivityHooker { ...@@ -25,4 +25,15 @@ public class ActivityHooker {
} }
@HookMethod("onPause")
public static void onPause(Activity thiz) {
Log.e("ActivityHooker", "hooked onPause success " + thiz);
onPauseBackup(thiz);
}
@HookMethodBackup("onPause")
public static void onPauseBackup(Activity thiz) {
}
} }
package com.swift.sandhook.test;
import android.util.Log;
import com.swift.sandhook.MainActivity;
import com.swift.sandhook.wrapper.HookClass;
import com.swift.sandhook.wrapper.HookMethod;
import com.swift.sandhook.wrapper.MethodParams;
@HookClass(MainActivity.class)
public class CustmizeHooker {
@HookMethod("methodBeHooked")
@MethodParams({int.class, int.class})
public static int staticMethodHooked(int a, int b) {
Log.e("CustmizeHooker", "methodBeHooked be hooked");
return 0;
}
}
package com.swift.sandhook; package com.swift.sandhook.test;
import android.util.Log; import android.util.Log;
......
package com.swift.sandhook; package com.swift.sandhook.test;
import android.util.Log; import android.util.Log;
......
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