Commit c7fbe9ee authored by swift_gan's avatar swift_gan Committed by swift_gan

[NativeHook]fix arm32

parent 99178dd4
...@@ -165,4 +165,8 @@ void AssemblerA32::Adr(RegisterA32 &rd, Label *label) { ...@@ -165,4 +165,8 @@ void AssemblerA32::Adr(RegisterA32 &rd, Label *label) {
Emit(reinterpret_cast<Unit<Base>*>(new INST_T16(ADR)(rd, *label))); Emit(reinterpret_cast<Unit<Base>*>(new INST_T16(ADR)(rd, *label)));
} }
void AssemblerA32::Nop16() {
Mov(IP, IP);
}
...@@ -67,6 +67,8 @@ namespace SandHook { ...@@ -67,6 +67,8 @@ namespace SandHook {
void Adr(RegisterA32& rd, Label* label); void Adr(RegisterA32& rd, Label* label);
void Nop16();
public: public:
CodeContainer codeContainer = CodeContainer(nullptr); CodeContainer codeContainer = CodeContainer(nullptr);
}; };
......
...@@ -19,11 +19,11 @@ void *InlineHookArm32Android::inlineHook(void *origin, void *replace) { ...@@ -19,11 +19,11 @@ void *InlineHookArm32Android::inlineHook(void *origin, void *replace) {
AutoLock lock(hookLock); AutoLock lock(hookLock);
void* originCode = origin; void* originCode = origin;
if (isThumbCode(reinterpret_cast<Addr>(origin))) { if (isThumbCode((Addr)origin)) {
originCode = getThumbCodeAddress(origin); originCode = getThumbCodeAddress(origin);
} }
if (isThumbCode(reinterpret_cast<Addr>(replace))) { if (isThumbCode((Addr)origin)) {
replace = getThumbPC(replace); replace = getThumbPC(replace);
} }
...@@ -39,17 +39,15 @@ void *InlineHookArm32Android::inlineHook(void *origin, void *replace) { ...@@ -39,17 +39,15 @@ void *InlineHookArm32Android::inlineHook(void *origin, void *replace) {
Label* target_addr_label = new Label(); Label* target_addr_label = new Label();
__ Ldr(PC, target_addr_label); __ Ldr(PC, target_addr_label);
__ Emit(target_addr_label); __ Emit(target_addr_label);
__ Emit(reinterpret_cast<Addr>(replace)); __ Emit((Addr)replace);
#undef __ #undef __
//build backup method //build backup method
CodeRelocateA32 relocate = CodeRelocateA32(assemblerBackup); CodeRelocateA32 relocate = CodeRelocateA32(assemblerBackup);
backup = relocate.relocate(origin, codeContainerInline->size(), nullptr); backup = relocate.relocate(origin, codeContainerInline->size(), nullptr);
#define __ assemblerBackup. #define __ assemblerBackup.
Label* origin_addr_label = new Label(); __ Mov(IP ,(Addr) getThumbPC(reinterpret_cast<void *>((Addr)originCode + relocate.curOffset)));
__ Ldr(PC, origin_addr_label); __ Bx(IP);
__ Emit(origin_addr_label);
__ Emit((Addr) getThumbPC(reinterpret_cast<void *>(reinterpret_cast<Addr>(originCode) + relocate.curOffset)));
__ finish(); __ finish();
#undef __ #undef __
......
...@@ -168,11 +168,11 @@ T16_LDR_LIT::T16_LDR_LIT(Off offset, RegisterA32 &rt) : offset(offset), rt(&rt) ...@@ -168,11 +168,11 @@ T16_LDR_LIT::T16_LDR_LIT(Off offset, RegisterA32 &rt) : offset(offset), rt(&rt)
} }
Off T16_LDR_LIT::getImmPCOffset() { Off T16_LDR_LIT::getImmPCOffset() {
return get()->imm8 >> 2; return get()->imm8 << 2;
} }
Addr T16_LDR_LIT::getImmPCOffsetTarget() { Addr T16_LDR_LIT::getImmPCOffsetTarget() {
return RoundDown((Addr) getPC() + offset, 4); return ALIGN((Addr) getPC() + offset, 4);
} }
void T16_LDR_LIT::onOffsetApply(Off offset) { void T16_LDR_LIT::onOffsetApply(Off offset) {
......
...@@ -45,7 +45,7 @@ Off T32_B32::getImmPCOffset() { ...@@ -45,7 +45,7 @@ Off T32_B32::getImmPCOffset() {
U32 S = get()->S; U32 S = get()->S;
U32 imm21 = COMBINE(get()->imm10, get()->imm11, 11); U32 imm21 = COMBINE(get()->imm10, get()->imm11, 11);
if (get()->X == 0) { if (get()->X == 0) {
imm21 -= BIT(imm21, 0); imm21 &= ~(1 << 0);
} }
U32 i1 = !(get()->J1 ^ S); U32 i1 = !(get()->J1 ^ S);
U32 i2 = !(get()->J2 ^ S); U32 i2 = !(get()->J2 ^ S);
...@@ -67,6 +67,9 @@ void T32_B32::assembler() { ...@@ -67,6 +67,9 @@ void T32_B32::assembler() {
U32 imm24 = TruncateToUint25(offset) >> 1; U32 imm24 = TruncateToUint25(offset) >> 1;
get()->imm11 = BITS(imm24, 0, 10); get()->imm11 = BITS(imm24, 0, 10);
get()->imm10 = BITS(imm24, 11, 20); get()->imm10 = BITS(imm24, 11, 20);
if (get()->X == 0) {
get()->imm11 |= (1 << 0);
}
get()->S = BIT(imm24, 23); get()->S = BIT(imm24, 23);
U32 i1 = BIT(imm24, 22); U32 i1 = BIT(imm24, 22);
U32 i2 = BIT(imm24, 21); U32 i2 = BIT(imm24, 21);
...@@ -84,7 +87,7 @@ void T32_B32::assembler() { ...@@ -84,7 +87,7 @@ void T32_B32::assembler() {
Addr T32_B32::getImmPCOffsetTarget() { Addr T32_B32::getImmPCOffsetTarget() {
if (x == arm) { if (x == arm) {
void* base = reinterpret_cast<void *>(ALIGN((Addr)getPC(), 4)); void* base = reinterpret_cast<void *>(ALIGN((Addr) getPC(), 4));
return offset + reinterpret_cast<Addr>(base); return offset + reinterpret_cast<Addr>(base);
} else { } else {
return T32_INST_PC_REL::getImmPCOffsetTarget(); return T32_INST_PC_REL::getImmPCOffsetTarget();
...@@ -164,7 +167,9 @@ void T32_LDR_LIT::assembler() { ...@@ -164,7 +167,9 @@ void T32_LDR_LIT::assembler() {
T32_MOV_MOVT_IMM::T32_MOV_MOVT_IMM() {} T32_MOV_MOVT_IMM::T32_MOV_MOVT_IMM() {}
T32_MOV_MOVT_IMM::T32_MOV_MOVT_IMM(T32_STRUCT_MOV_MOVT_IMM *inst) : InstructionT32(inst) {} T32_MOV_MOVT_IMM::T32_MOV_MOVT_IMM(T32_STRUCT_MOV_MOVT_IMM *inst) : InstructionT32(inst) {
decode(inst);
}
T32_MOV_MOVT_IMM::T32_MOV_MOVT_IMM(T32_MOV_MOVT_IMM::OP op, RegisterA32 &rd, U16 imm16) : op(op), T32_MOV_MOVT_IMM::T32_MOV_MOVT_IMM(T32_MOV_MOVT_IMM::OP op, RegisterA32 &rd, U16 imm16) : op(op),
rd(&rd), rd(&rd),
......
...@@ -31,7 +31,7 @@ void *InlineHookArm64Android::inlineHook(void *origin, void *replace) { ...@@ -31,7 +31,7 @@ void *InlineHookArm64Android::inlineHook(void *origin, void *replace) {
__ Ldr(IP1, target_addr_label); __ Ldr(IP1, target_addr_label);
__ Br(IP1); __ Br(IP1);
__ Emit(target_addr_label); __ Emit(target_addr_label);
__ Emit(reinterpret_cast<Addr>(replace)); __ Emit((Addr) replace);
#undef __ #undef __
//build backup method //build backup method
...@@ -42,7 +42,7 @@ void *InlineHookArm64Android::inlineHook(void *origin, void *replace) { ...@@ -42,7 +42,7 @@ void *InlineHookArm64Android::inlineHook(void *origin, void *replace) {
__ Ldr(IP1, origin_addr_label); __ Ldr(IP1, origin_addr_label);
__ Br(IP1); __ Br(IP1);
__ Emit(origin_addr_label); __ Emit(origin_addr_label);
__ Emit(reinterpret_cast<Addr>(origin) + codeContainerInline->size()); __ Emit((Addr) origin + codeContainerInline->size());
__ finish(); __ finish();
#undef __ #undef __
......
...@@ -94,7 +94,7 @@ T AlignDown(T pointer, ...@@ -94,7 +94,7 @@ T AlignDown(T pointer,
return (T)(pointer_raw & ~mask); return (T)(pointer_raw & ~mask);
} }
#define ALIGN(value, align) value - value % align #define ALIGN(value, align) ((uintptr_t)value & ~((uintptr_t)align - 1))
template<typename T> template<typename T>
struct Identity { struct Identity {
......
...@@ -17,8 +17,8 @@ using namespace SandHook::AsmA64; ...@@ -17,8 +17,8 @@ using namespace SandHook::AsmA64;
int m1 = 5; int m1 = 5;
int m3 = 5; int m3 = 1036;
int m2 = 4; int m2 = 1035;
int m4 = 5; int m4 = 5;
void (*dosth3Backup)(int, int) = nullptr; void (*dosth3Backup)(int, int) = nullptr;
...@@ -57,9 +57,10 @@ void do5() { ...@@ -57,9 +57,10 @@ void do5() {
} }
void do4() { void do4() {
if (m2 > m3) { if (m3 > m2) {
return; return;
} }
do5();
int a = 1 + 1; int a = 1 + 1;
int b = a + 1; int b = a + 1;
int d = a + 1; int d = a + 1;
...@@ -97,6 +98,8 @@ void do1() { ...@@ -97,6 +98,8 @@ void do1() {
class Visitor : public InstVisitor { class Visitor : public InstVisitor {
bool visit(Unit<Base> *unit, void *pc) override { bool visit(Unit<Base> *unit, void *pc) override {
Instruction<Base>* instruction = reinterpret_cast<Instruction<Base> *>(unit);
instruction->assembler();
return true; return true;
} }
}; };
...@@ -105,52 +108,57 @@ extern "C" ...@@ -105,52 +108,57 @@ extern "C"
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_com_swift_sandhook_nativehook_NativeHook_test(JNIEnv *env, jclass jclass1) { Java_com_swift_sandhook_nativehook_NativeHook_test(JNIEnv *env, jclass jclass1) {
union { // union {
InstA64 raw = 0xF9001043; // InstA64 raw = 0xF9001043;
STRUCT_A64(STR_UIMM) str; // STRUCT_A64(STR_UIMM) str;
} test; // } test;
//
InstA64* codebl = reinterpret_cast<InstA64 *>((Addr)do1 + 8); // InstA64* codebl = reinterpret_cast<InstA64 *>((Addr)do1 + 8);
//
if (IS_OPCODE_A64(*codebl, B_BL)) { // if (IS_OPCODE_A64(*codebl, B_BL)) {
//
//decode // //decode
A64_B_BL a64bl(*reinterpret_cast<STRUCT_A64(B_BL)*>(codebl)); // A64_B_BL a64bl(*reinterpret_cast<STRUCT_A64(B_BL)*>(codebl));
Off off = a64bl.offset; // Off off = a64bl.offset;
void (*dosth2)() =reinterpret_cast<void (*)()>(a64bl.getImmPCOffsetTarget()); // void (*dosth2)() =reinterpret_cast<void (*)()>(a64bl.getImmPCOffsetTarget());
dosth2(); // dosth2();
//
//asm // //asm
memUnprotect(reinterpret_cast<Addr>(a64bl.get()), a64bl.size()); // memUnprotect(reinterpret_cast<Addr>(a64bl.get()), a64bl.size());
a64bl.assembler(); // a64bl.assembler();
Off off1 = a64bl.getImmPCOffset(); // Off off1 = a64bl.getImmPCOffset();
//
do1(); // do1();
//
} // }
//
if (IS_OPCODE_A64(test.raw, STR_UIMM)) { // if (IS_OPCODE_A64(test.raw, STR_UIMM)) {
A64_STR_UIMM str(test.str); // A64_STR_UIMM str(test.str);
str.assembler(); // str.assembler();
str.get(); // str.get();
} // }
//
Arm64Decoder arm64Decoder = Arm64Decoder(); // Arm64Decoder arm64Decoder = Arm64Decoder();
//
Visitor visitor = Visitor(); // Visitor visitor = Visitor();
//
arm64Decoder.decode(reinterpret_cast<void *>(do1), 4 * 8, visitor); // arm64Decoder.decode(reinterpret_cast<void *>(do1), 4 * 8, visitor);
//
// InlineHookArm64Android inlineHookArm64Android = InlineHookArm64Android();
//
InlineHookArm64Android inlineHookArm64Android = InlineHookArm64Android(); void* do5addr = reinterpret_cast<void *>(do5);
void* do4addr = reinterpret_cast<void *>(do4);
dosth4Backup = reinterpret_cast<void (*)()>(inlineHookArm64Android.inlineHook( dosth4Backup = reinterpret_cast<void (*)()>(InlineHook::instance->inlineHook(
reinterpret_cast<void *>(do4), reinterpret_cast<void *>(do4),
reinterpret_cast<void *>(do4replace))); reinterpret_cast<void *>(do4replace)));
dosth4Backup2 = reinterpret_cast<void (*)()>(inlineHookArm64Android.inlineHook( dosth4Backup2 = reinterpret_cast<void (*)()>(InlineHook::instance->inlineHook(
reinterpret_cast<void *>(do4), reinterpret_cast<void *>(do4),
reinterpret_cast<void *>(do4replace2))); reinterpret_cast<void *>(do4replace2)));
do4(); do4();
} }
\ No newline at end of file
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