Commit 8b9a58a3 authored by swift_gan's avatar swift_gan Committed by ganyao114

FixTemp: filter case "bx pc"

parent e93f103d
...@@ -85,7 +85,11 @@ public class MainActivity extends AppCompatActivity { ...@@ -85,7 +85,11 @@ public class MainActivity extends AppCompatActivity {
testPluginHook(str); testPluginHook(str);
MyApp.initedTest = true; MyApp.initedTest = true;
PendingHookTest.test(); try {
PendingHookTest.test();
} catch (Throwable e) {
}
} }
public static Field getField(Class topClass, String fieldName) throws NoSuchFieldException { public static Field getField(Class topClass, String fieldName) throws NoSuchFieldException {
......
package com.swift.sandhook; package com.swift.sandhook;
import android.util.Log;
import com.swift.sandhook.wrapper.HookErrorException; import com.swift.sandhook.wrapper.HookErrorException;
import com.swift.sandhook.wrapper.HookWrapper; import com.swift.sandhook.wrapper.HookWrapper;
...@@ -47,6 +45,7 @@ public class PendingHookHandler { ...@@ -47,6 +45,7 @@ public class PendingHookHandler {
for (HookWrapper.HookEntity entity:entities) { for (HookWrapper.HookEntity entity:entities) {
HookLog.w("do pending hook for method: " + entity.target.toString()); HookLog.w("do pending hook for method: " + entity.target.toString());
try { try {
entity.initClass = false;
SandHook.hook(entity); SandHook.hook(entity);
} catch (HookErrorException e) { } catch (HookErrorException e) {
HookLog.e("Pending Hook Error!", e); HookLog.e("Pending Hook Error!", e);
......
...@@ -91,7 +91,7 @@ public class SandHook { ...@@ -91,7 +91,7 @@ public class SandHook {
if (SandHookConfig.delayHook && PendingHookHandler.canWork() && ClassStatusUtils.isStaticAndNoInited(entity.target)) { if (SandHookConfig.delayHook && PendingHookHandler.canWork() && ClassStatusUtils.isStaticAndNoInited(entity.target)) {
PendingHookHandler.addPendingHook(entity); PendingHookHandler.addPendingHook(entity);
return; return;
} else { } else if (entity.initClass) {
resolveStaticMethod(target); resolveStaticMethod(target);
} }
......
...@@ -409,6 +409,7 @@ public class HookWrapper { ...@@ -409,6 +409,7 @@ public class HookWrapper {
public boolean hookIsStub = false; public boolean hookIsStub = false;
public boolean resolveDexCache = true; public boolean resolveDexCache = true;
public boolean backupIsStub = true; public boolean backupIsStub = true;
public boolean initClass = true;
public Class[] pars; public Class[] pars;
public int hookMode; public int hookMode;
......
...@@ -57,7 +57,11 @@ void *InlineHookArm32Android::Hook(void *origin, void *replace) { ...@@ -57,7 +57,11 @@ void *InlineHookArm32Android::Hook(void *origin, void *replace) {
//build backup method //build backup method
CodeRelocateA32 relocate = CodeRelocateA32(assembler_backup); CodeRelocateA32 relocate = CodeRelocateA32(assembler_backup);
backup = relocate.Relocate(origin, code_container_inline->Size(), nullptr); try {
backup = relocate.Relocate(origin, code_container_inline->Size(), nullptr);
} catch (ErrorCodeException e) {
return nullptr;
}
#define __ assembler_backup. #define __ assembler_backup.
Label* origin_addr_label = new Label(); Label* origin_addr_label = new Label();
ALIGN_FOR_LDR ALIGN_FOR_LDR
...@@ -99,7 +103,11 @@ bool InlineHookArm32Android::BreakPoint(void *origin, void (*callback)(REG *)) { ...@@ -99,7 +103,11 @@ bool InlineHookArm32Android::BreakPoint(void *origin, void (*callback)(REG *)) {
//build backup method //build backup method
CodeRelocateA32 relocate = CodeRelocateA32(assembler_backup); CodeRelocateA32 relocate = CodeRelocateA32(assembler_backup);
backup = relocate.Relocate(origin, change_mode ? (4 * 2 + 2) : (4 * 2), nullptr); try {
backup = relocate.Relocate(origin, change_mode ? (4 * 2 + 2) : (4 * 2), nullptr);
} catch (ErrorCodeException e) {
return nullptr;
}
#define __ assembler_backup. #define __ assembler_backup.
Label* origin_addr_label = new Label(); Label* origin_addr_label = new Label();
ALIGN_FOR_LDR ALIGN_FOR_LDR
...@@ -161,14 +169,33 @@ void *InlineHookArm32Android::SingleInstHook(void *origin, void *replace) { ...@@ -161,14 +169,33 @@ void *InlineHookArm32Android::SingleInstHook(void *origin, void *replace) {
#undef __ #undef __
//build backup method //build backup method
bool rec_to_thumb = true;
CodeRelocateA32 relocate = CodeRelocateA32(assembler_backup); CodeRelocateA32 relocate = CodeRelocateA32(assembler_backup);
backup = relocate.Relocate(origin, code_container_inline->Size(), nullptr); try {
backup = relocate.Relocate(origin, code_container_inline->Size(), nullptr);
} catch (ErrorCodeException e) {
if (e.Code() == ERROR_SWITCH_TO_ARM32) {
//to arm32 mode
rec_to_thumb = false;
assembler_backup.AllocBufferFirst(4 * 4);
backup = assembler_backup.GetPC();
} else {
return nullptr;
}
}
Addr rec_addr = reinterpret_cast<Addr>(origin_code);
if (rec_to_thumb) {
rec_addr += relocate.cur_offset;
rec_addr = reinterpret_cast<Addr>(GetThumbPC(reinterpret_cast<void *>(rec_addr)));
} else {
rec_addr += 4;
}
#define __ assembler_backup. #define __ assembler_backup.
Label* origin_addr_label = new Label(); Label *origin_addr_label = new Label();
ALIGN_FOR_LDR ALIGN_FOR_LDR
__ Ldr(PC, origin_addr_label); __ Ldr(PC, origin_addr_label);
__ Emit(origin_addr_label); __ Emit(origin_addr_label);
__ Emit((Addr) GetThumbPC(reinterpret_cast<void *>(reinterpret_cast<Addr>(origin_code) + relocate.cur_offset))); __ Emit(rec_addr);
__ Finish(); __ Finish();
#undef __ #undef __
...@@ -208,7 +235,11 @@ bool InlineHookArm32Android::SingleBreakPoint(void *point, BreakCallback callbac ...@@ -208,7 +235,11 @@ bool InlineHookArm32Android::SingleBreakPoint(void *point, BreakCallback callbac
//build backup method //build backup method
CodeRelocateA32 relocate = CodeRelocateA32(assembler_backup); CodeRelocateA32 relocate = CodeRelocateA32(assembler_backup);
backup = relocate.Relocate(point, code_container_inline->Size(), nullptr); try {
backup = relocate.Relocate(point, code_container_inline->Size(), nullptr);
} catch (ErrorCodeException e) {
return nullptr;
}
#define __ assembler_backup. #define __ assembler_backup.
Label* origin_addr_label = new Label(); Label* origin_addr_label = new Label();
ALIGN_FOR_LDR ALIGN_FOR_LDR
......
...@@ -11,17 +11,17 @@ using namespace SandHook::AsmA32; ...@@ -11,17 +11,17 @@ using namespace SandHook::AsmA32;
using namespace SandHook::Utils; using namespace SandHook::Utils;
using namespace SandHook::Decoder; using namespace SandHook::Decoder;
#define __ assemblerA32-> #define __ assembler_a32->
#define IMPL_RELOCATE(T, X) void CodeRelocateA32::relocate_##T##_##X (INST_##T(X)* inst, void* toPc) throw(ErrorCodeException) #define IMPL_RELOCATE(T, X) void CodeRelocateA32::relocate_##T##_##X (INST_##T(X)* inst, void* to_pc) throw(ErrorCodeException)
#define CASE(T, X) \ #define CASE(T, X) \
case ENUM_VALUE(InstCode##T, InstCode##T::X): \ case ENUM_VALUE(InstCode##T, InstCode##T::X): \
relocate_##T##_##X(reinterpret_cast<INST_##T(X)*>(instruction), toPc); \ relocate_##T##_##X(reinterpret_cast<INST_##T(X)*>(instruction), to_pc); \
break; break;
CodeRelocateA32::CodeRelocateA32(AssemblerA32 &assembler) : CodeRelocate(assembler.code_container) { CodeRelocateA32::CodeRelocateA32(AssemblerA32 &assembler) : CodeRelocate(assembler.code_container) {
this->assemblerA32 = &assembler; this->assembler_a32 = &assembler;
} }
bool CodeRelocateA32::Visit(BaseUnit *unit, void *pc) { bool CodeRelocateA32::Visit(BaseUnit *unit, void *pc) {
...@@ -33,7 +33,7 @@ bool CodeRelocateA32::Visit(BaseUnit *unit, void *pc) { ...@@ -33,7 +33,7 @@ bool CodeRelocateA32::Visit(BaseUnit *unit, void *pc) {
return true; return true;
} }
void* CodeRelocateA32::Relocate(void *startPc, Addr len, void *toPc = nullptr) throw(ErrorCodeException) { void* CodeRelocateA32::Relocate(void *startPc, Addr len, void *to_pc = nullptr) throw(ErrorCodeException) {
AutoLock autoLock(relocate_lock); AutoLock autoLock(relocate_lock);
start_addr = reinterpret_cast<Addr>(startPc); start_addr = reinterpret_cast<Addr>(startPc);
if (IsThumbCode(start_addr)) { if (IsThumbCode(start_addr)) {
...@@ -43,7 +43,7 @@ void* CodeRelocateA32::Relocate(void *startPc, Addr len, void *toPc = nullptr) t ...@@ -43,7 +43,7 @@ void* CodeRelocateA32::Relocate(void *startPc, Addr len, void *toPc = nullptr) t
cur_offset = 0; cur_offset = 0;
__ AllocBufferFirst(static_cast<U32>(len * 8)); __ AllocBufferFirst(static_cast<U32>(len * 8));
void* curPc = __ GetPC(); void* curPc = __ GetPC();
if (toPc == nullptr) { if (to_pc == nullptr) {
Disassembler::Get()->Disassemble(startPc, len, *this, true); Disassembler::Get()->Disassemble(startPc, len, *this, true);
} else { } else {
//TODO //TODO
...@@ -51,7 +51,7 @@ void* CodeRelocateA32::Relocate(void *startPc, Addr len, void *toPc = nullptr) t ...@@ -51,7 +51,7 @@ void* CodeRelocateA32::Relocate(void *startPc, Addr len, void *toPc = nullptr) t
return curPc; return curPc;
} }
void* CodeRelocateA32::Relocate(BaseInst *instruction, void *toPc) throw(ErrorCodeException) { void* CodeRelocateA32::Relocate(BaseInst *instruction, void *to_pc) throw(ErrorCodeException) {
void* cur_pc = __ GetPC(); void* cur_pc = __ GetPC();
//insert later AddBind labels //insert later AddBind labels
...@@ -141,8 +141,18 @@ IMPL_RELOCATE(T16, B) { ...@@ -141,8 +141,18 @@ IMPL_RELOCATE(T16, B) {
} }
IMPL_RELOCATE(T16, BX_BLX) { IMPL_RELOCATE(T16, BX_BLX) {
__ Emit(reinterpret_cast<BaseInst*>(inst)); if (*inst->rm == PC) {
inst->Ref(); //maybe mode switch!
if (IsThumbCode(reinterpret_cast<Addr>(inst->GetPC()))) {
__ Nop16();
} else {
//switch to arm mode
throw ErrorCodeException(ERROR_SWITCH_TO_ARM32, "switch to arm32! not impl!");
}
} else {
__ Emit(reinterpret_cast<BaseInst *>(inst));
inst->Ref();
}
} }
IMPL_RELOCATE(T16, CBZ_CBNZ) { IMPL_RELOCATE(T16, CBZ_CBNZ) {
......
...@@ -22,7 +22,7 @@ namespace SandHook { ...@@ -22,7 +22,7 @@ namespace SandHook {
public: public:
CodeRelocateA32(AssemblerA32 &assembler); CodeRelocateA32(AssemblerA32 &assembler);
void* Relocate(BaseInst *instruction, void *toPc) throw(ErrorCodeException) override; void* Relocate(BaseInst *instruction, void *to_pc) throw(ErrorCodeException) override;
void* Relocate(void *startPc, Addr len, void *toPc) throw(ErrorCodeException) override; void* Relocate(void *startPc, Addr len, void *toPc) throw(ErrorCodeException) override;
...@@ -48,7 +48,7 @@ namespace SandHook { ...@@ -48,7 +48,7 @@ namespace SandHook {
private: private:
AssemblerA32* assemblerA32; AssemblerA32* assembler_a32;
}; };
} }
......
...@@ -3,22 +3,37 @@ ...@@ -3,22 +3,37 @@
// //
#pragma once #pragma once
#include <log.h>
#include "exception" #include "exception"
namespace SandHook { namespace SandHook {
namespace Asm { namespace Asm {
constexpr int ERROR_DEFAULT = 0;
constexpr int ERROR_SWITCH_TO_ARM32 = 1;
class ErrorCodeException : public std::exception { class ErrorCodeException : public std::exception {
public: public:
ErrorCodeException(const char *what_) : what_(what_) {} ErrorCodeException(const char *what) : code_(ERROR_DEFAULT), what_(what) {
LOGE("ErrorCodeException: %s", what);
}
ErrorCodeException(const int code, const char *what) : code_(code), what_(what) {
LOGE("ErrorCodeException: %s", what);
}
const char *what() const noexcept override { const char *what() const noexcept override {
return what_; return what_;
} }
const int Code() const {
return code_;
}
private: private:
const int code_;
const char *what_; const char *what_;
}; };
......
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