Commit b8775fba authored by swift_gan's avatar swift_gan

fix thumb code offset

parent 6ad5dca1
...@@ -115,11 +115,7 @@ bool doHookWithInline(JNIEnv* env, ...@@ -115,11 +115,7 @@ bool doHookWithInline(JNIEnv* env,
//backup //backup
memcpy(backupMethod, originMethod, SandHook::CastArtMethod::size); memcpy(backupMethod, originMethod, SandHook::CastArtMethod::size);
Code callOriginCode = hookTrampoline->callOrigin->getCode(); SandHook::CastArtMethod::entryPointQuickCompiled->set(backupMethod, hookTrampoline->callOrigin->getCode());
if (hookTrampoline->callOrigin->isThumbCode()) {
callOriginCode = hookTrampoline->callOrigin->getThumbCodePcAddress(callOriginCode);
}
SandHook::CastArtMethod::entryPointQuickCompiled->set(backupMethod, callOriginCode);
if (SDK_INT >= ANDROID_N) { if (SDK_INT >= ANDROID_N) {
disableCompilable(backupMethod); disableCompilable(backupMethod);
......
...@@ -106,6 +106,7 @@ namespace SandHook { ...@@ -106,6 +106,7 @@ namespace SandHook {
class Trampoline { class Trampoline {
public: public:
Code code;
Trampoline() = default; Trampoline() = default;
...@@ -152,7 +153,11 @@ namespace SandHook { ...@@ -152,7 +153,11 @@ namespace SandHook {
} }
Code getCode() { Code getCode() {
return code; if (isThumbCode()) {
return getThumbCodePcAddress(code);
} else {
return code;
}
} }
Size getCodeLen() { Size getCodeLen() {
...@@ -169,7 +174,7 @@ namespace SandHook { ...@@ -169,7 +174,7 @@ namespace SandHook {
//tweak imm of a 32bit asm code //tweak imm of a 32bit asm code
void tweakOpImm(Size codeOffset, unsigned char imm) { void tweakOpImm(Size codeOffset, unsigned char imm) {
Code32Bit code32Bit; Code32Bit code32Bit;
code32Bit.code = *reinterpret_cast<uint32_t*>(((Size)getCode() + codeOffset)); code32Bit.code = *reinterpret_cast<uint32_t*>(((Size)code + codeOffset));
if (isBigEnd()) { if (isBigEnd()) {
code32Bit.op.op4 = imm; code32Bit.op.op4 = imm;
} else { } else {
...@@ -193,7 +198,6 @@ namespace SandHook { ...@@ -193,7 +198,6 @@ namespace SandHook {
virtual Size codeLength() = 0; virtual Size codeLength() = 0;
virtual Code templateCode() = 0; virtual Code templateCode() = 0;
private: private:
Code code;
Code tempCode; Code tempCode;
Size codeLen; Size codeLen;
bool isThumb = false; bool isThumb = false;
......
...@@ -91,7 +91,11 @@ namespace SandHook { ...@@ -91,7 +91,11 @@ namespace SandHook {
inlineHookTrampoline->setOriginMethod(reinterpret_cast<Code>(originMethod)); inlineHookTrampoline->setOriginMethod(reinterpret_cast<Code>(originMethod));
inlineHookTrampoline->setHookMethod(reinterpret_cast<Code>(hookMethod)); inlineHookTrampoline->setHookMethod(reinterpret_cast<Code>(hookMethod));
inlineHookTrampoline->setEntryCodeOffset(quickCompileOffset); inlineHookTrampoline->setEntryCodeOffset(quickCompileOffset);
inlineHookTrampoline->setOriginCode(getEntryCode(originMethod)); if (inlineHookTrampoline->isThumbCode()) {
inlineHookTrampoline->setOriginCode(inlineHookTrampoline->getThumbCodeAddress(getEntryCode(originMethod)));
} else {
inlineHookTrampoline->setOriginCode(getEntryCode(originMethod));
}
hookTrampoline->inlineSecondory = inlineHookTrampoline; hookTrampoline->inlineSecondory = inlineHookTrampoline;
//注入 EntryCode //注入 EntryCode
...@@ -105,12 +109,10 @@ namespace SandHook { ...@@ -105,12 +109,10 @@ namespace SandHook {
if (directJumpTrampoline->isThumbCode()) { if (directJumpTrampoline->isThumbCode()) {
originEntry = directJumpTrampoline->getThumbCodeAddress(originEntry); originEntry = directJumpTrampoline->getThumbCodeAddress(originEntry);
directJumpTrampoline->setExecuteSpace(originEntry);
directJumpTrampoline->setJumpTarget(directJumpTrampoline->getThumbCodePcAddress(inlineHookTrampoline->getCode()));
} else {
directJumpTrampoline->setExecuteSpace(originEntry);
directJumpTrampoline->setJumpTarget(inlineHookTrampoline->getCode());
} }
directJumpTrampoline->setExecuteSpace(originEntry);
directJumpTrampoline->setJumpTarget(inlineHookTrampoline->getCode());
hookTrampoline->inlineJump = directJumpTrampoline; hookTrampoline->inlineJump = directJumpTrampoline;
//备份原始方法 //备份原始方法
......
...@@ -74,10 +74,15 @@ public class MainActivity extends AppCompatActivity { ...@@ -74,10 +74,15 @@ public class MainActivity extends AppCompatActivity {
return true; return true;
} }
@Override
protected void onPause() {
super.onPause();
}
public static int methodBeHooked(int a, int b) { public static int methodBeHooked(int a, int b) {
a = a + 1 + 2; a = a + 1 + 2;
b = b + a + 3; b = b + a + 3;
//Toast.makeText(MyApp.context, "call origin!", Toast.LENGTH_SHORT).show(); Log.e("MainActivity", "call methodBeHooked origin");
return a + b; return a + b;
} }
......
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