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

[Reconstitution]tweak code

parent 612980be
...@@ -10,44 +10,44 @@ using namespace SandHook::RegistersA32; ...@@ -10,44 +10,44 @@ using namespace SandHook::RegistersA32;
using namespace SandHook::AsmA32; using namespace SandHook::AsmA32;
AssemblerA32::AssemblerA32(CodeBuffer* codeBuffer) { AssemblerA32::AssemblerA32(CodeBuffer* codeBuffer) {
codeContainer.setCodeBuffer(codeBuffer); code_container.setCodeBuffer(codeBuffer);
} }
void *AssemblerA32::getPC() { void *AssemblerA32::GetPC() {
return reinterpret_cast<void *>(codeContainer.curPc); return reinterpret_cast<void *>(code_container.curPc);
} }
void *AssemblerA32::getStartPC() { void *AssemblerA32::GetStartPC() {
return reinterpret_cast<void *>(codeContainer.startPc); return reinterpret_cast<void *>(code_container.startPc);
} }
void AssemblerA32::allocBufferFirst(U32 size) { void AssemblerA32::AllocBufferFirst(U32 size) {
codeContainer.allocBufferFirst(size); code_container.allocBufferFirst(size);
} }
void *AssemblerA32::finish() { void *AssemblerA32::Finish() {
codeContainer.commit(); code_container.commit();
return reinterpret_cast<void *>(codeContainer.startPc); return reinterpret_cast<void *>(code_container.startPc);
} }
void AssemblerA32::Emit(U32 data32) { void AssemblerA32::Emit(U32 data32) {
Emit(reinterpret_cast<Unit<Base>*>(new Data32(data32))); Emit(reinterpret_cast<BaseUnit*>(new Data32(data32)));
} }
void AssemblerA32::Emit(U16 data16) { void AssemblerA32::Emit(U16 data16) {
Emit(reinterpret_cast<Unit<Base>*>(new Data16(data16))); Emit(reinterpret_cast<BaseUnit*>(new Data16(data16)));
} }
void AssemblerA32::Emit(Unit<Base> *unit) { void AssemblerA32::Emit(BaseUnit *unit) {
codeContainer.append(unit); code_container.append(unit);
} }
void AssemblerA32::Mov(RegisterA32 &rd, U16 imm16) { void AssemblerA32::Mov(RegisterA32 &rd, U16 imm16) {
Emit(reinterpret_cast<Unit<Base>*>(new INST_T32(MOV_MOVT_IMM)(INST_T32(MOV_MOVT_IMM)::MOV, rd, imm16))); Emit(reinterpret_cast<BaseUnit*>(new INST_T32(MOV_MOVT_IMM)(INST_T32(MOV_MOVT_IMM)::MOV, rd, imm16)));
} }
void AssemblerA32::Movt(RegisterA32 &rd, U16 imm16) { void AssemblerA32::Movt(RegisterA32 &rd, U16 imm16) {
Emit(reinterpret_cast<Unit<Base>*>(new INST_T32(MOV_MOVT_IMM)(INST_T32(MOV_MOVT_IMM)::MOVT, rd, imm16))); Emit(reinterpret_cast<BaseUnit*>(new INST_T32(MOV_MOVT_IMM)(INST_T32(MOV_MOVT_IMM)::MOVT, rd, imm16)));
} }
void AssemblerA32::Mov(RegisterA32 &rd, U32 imm32) { void AssemblerA32::Mov(RegisterA32 &rd, U32 imm32) {
...@@ -58,104 +58,104 @@ void AssemblerA32::Mov(RegisterA32 &rd, U32 imm32) { ...@@ -58,104 +58,104 @@ void AssemblerA32::Mov(RegisterA32 &rd, U32 imm32) {
} }
void AssemblerA32::Ldr(RegisterA32 &rt, Off offset) { void AssemblerA32::Ldr(RegisterA32 &rt, Off offset) {
Emit(reinterpret_cast<Unit<Base>*>(new INST_T32(LDR_LIT)(INST_T32(LDR_LIT)::LDR, INST_T32(LDR_LIT)::UnSign, rt, offset))); Emit(reinterpret_cast<BaseUnit*>(new INST_T32(LDR_LIT)(INST_T32(LDR_LIT)::LDR, INST_T32(LDR_LIT)::UnSign, rt, offset)));
} }
void AssemblerA32::Ldr(RegisterA32 &rt, Label *label) { void AssemblerA32::Ldr(RegisterA32 &rt, Label *label) {
Emit(reinterpret_cast<Unit<Base>*>(new INST_T32(LDR_LIT)(INST_T32(LDR_LIT)::LDR, INST_T32(LDR_LIT)::UnSign, rt, label))); Emit(reinterpret_cast<BaseUnit*>(new INST_T32(LDR_LIT)(INST_T32(LDR_LIT)::LDR, INST_T32(LDR_LIT)::UnSign, rt, label)));
} }
void AssemblerA32::Ldrb(RegisterA32 &rt, Label *label) { void AssemblerA32::Ldrb(RegisterA32 &rt, Label *label) {
Emit(reinterpret_cast<Unit<Base>*>(new INST_T32(LDR_LIT)(INST_T32(LDR_LIT)::LDRB, INST_T32(LDR_LIT)::UnSign,rt, label))); Emit(reinterpret_cast<BaseUnit*>(new INST_T32(LDR_LIT)(INST_T32(LDR_LIT)::LDRB, INST_T32(LDR_LIT)::UnSign,rt, label)));
} }
void AssemblerA32::Ldrh(RegisterA32 &rt, Label *label) { void AssemblerA32::Ldrh(RegisterA32 &rt, Label *label) {
Emit(reinterpret_cast<Unit<Base>*>(new INST_T32(LDR_LIT)(INST_T32(LDR_LIT)::LDRH, INST_T32(LDR_LIT)::UnSign,rt, label))); Emit(reinterpret_cast<BaseUnit*>(new INST_T32(LDR_LIT)(INST_T32(LDR_LIT)::LDRH, INST_T32(LDR_LIT)::UnSign,rt, label)));
} }
void AssemblerA32::Ldrsb(RegisterA32 &rt, Label *label) { void AssemblerA32::Ldrsb(RegisterA32 &rt, Label *label) {
Emit(reinterpret_cast<Unit<Base>*>(new INST_T32(LDR_LIT)(INST_T32(LDR_LIT)::LDRB, INST_T32(LDR_LIT)::Sign,rt, label))); Emit(reinterpret_cast<BaseUnit*>(new INST_T32(LDR_LIT)(INST_T32(LDR_LIT)::LDRB, INST_T32(LDR_LIT)::Sign,rt, label)));
} }
void AssemblerA32::Ldrsh(RegisterA32 &rt, Label *label) { void AssemblerA32::Ldrsh(RegisterA32 &rt, Label *label) {
Emit(reinterpret_cast<Unit<Base>*>(new INST_T32(LDR_LIT)(INST_T32(LDR_LIT)::LDRH, INST_T32(LDR_LIT)::Sign,rt, label))); Emit(reinterpret_cast<BaseUnit*>(new INST_T32(LDR_LIT)(INST_T32(LDR_LIT)::LDRH, INST_T32(LDR_LIT)::Sign,rt, label)));
} }
void AssemblerA32::Ldr(RegisterA32 &rt, const MemOperand &operand) { void AssemblerA32::Ldr(RegisterA32 &rt, const MemOperand &operand) {
if (operand.addr_mode == Offset && operand.offset >= 0) { if (operand.addr_mode == Offset && operand.offset >= 0) {
Emit(reinterpret_cast<Unit<Base>*>(new INST_T32(LDR_UIMM)(rt, *operand.rn, operand.addr_mode))); Emit(reinterpret_cast<BaseUnit*>(new INST_T32(LDR_UIMM)(rt, *operand.rn, operand.addr_mode)));
} else { } else {
Emit(reinterpret_cast<Unit<Base>*>(new INST_T32(LDR_IMM)(INST_T32(LDR_IMM)::LDR, rt, operand))); Emit(reinterpret_cast<BaseUnit*>(new INST_T32(LDR_IMM)(INST_T32(LDR_IMM)::LDR, rt, operand)));
} }
} }
void AssemblerA32::Ldrb(RegisterA32 &rt, const MemOperand &operand) { void AssemblerA32::Ldrb(RegisterA32 &rt, const MemOperand &operand) {
Emit(reinterpret_cast<Unit<Base>*>(new INST_T32(LDR_IMM)(INST_T32(LDR_IMM)::LDRB, rt, operand))); Emit(reinterpret_cast<BaseUnit*>(new INST_T32(LDR_IMM)(INST_T32(LDR_IMM)::LDRB, rt, operand)));
} }
void AssemblerA32::Ldrh(RegisterA32 &rt, const MemOperand &operand) { void AssemblerA32::Ldrh(RegisterA32 &rt, const MemOperand &operand) {
Emit(reinterpret_cast<Unit<Base>*>(new INST_T32(LDR_IMM)(INST_T32(LDR_IMM)::LDRH, rt, operand))); Emit(reinterpret_cast<BaseUnit*>(new INST_T32(LDR_IMM)(INST_T32(LDR_IMM)::LDRH, rt, operand)));
} }
void AssemblerA32::Ldrsb(RegisterA32 &rt, const MemOperand &operand) { void AssemblerA32::Ldrsb(RegisterA32 &rt, const MemOperand &operand) {
Emit(reinterpret_cast<Unit<Base>*>(new INST_T32(LDR_IMM)(INST_T32(LDR_IMM)::LDRSB, rt, operand))); Emit(reinterpret_cast<BaseUnit*>(new INST_T32(LDR_IMM)(INST_T32(LDR_IMM)::LDRSB, rt, operand)));
} }
void AssemblerA32::Ldrsh(RegisterA32 &rt, const MemOperand &operand) { void AssemblerA32::Ldrsh(RegisterA32 &rt, const MemOperand &operand) {
Emit(reinterpret_cast<Unit<Base>*>(new INST_T32(LDR_IMM)(INST_T32(LDR_IMM)::LDRSH, rt, operand))); Emit(reinterpret_cast<BaseUnit*>(new INST_T32(LDR_IMM)(INST_T32(LDR_IMM)::LDRSH, rt, operand)));
} }
void AssemblerA32::B(Label *label) { void AssemblerA32::B(Label *label) {
Emit(reinterpret_cast<Unit<Base>*>(new INST_T16(B)(label))); Emit(reinterpret_cast<BaseUnit*>(new INST_T16(B)(label)));
} }
void AssemblerA32::Bl(Label *label) { void AssemblerA32::Bl(Label *label) {
Emit(reinterpret_cast<Unit<Base>*>(new INST_T32(B32)(INST_T32(B32)::BL, INST_T32(B32)::arm, label))); Emit(reinterpret_cast<BaseUnit*>(new INST_T32(B32)(INST_T32(B32)::BL, INST_T32(B32)::arm, label)));
} }
void AssemblerA32::Blx(Label *label) { void AssemblerA32::Blx(Label *label) {
Emit(reinterpret_cast<Unit<Base>*>(new INST_T32(B32)(INST_T32(B32)::BL, INST_T32(B32)::thumb, label))); Emit(reinterpret_cast<BaseUnit*>(new INST_T32(B32)(INST_T32(B32)::BL, INST_T32(B32)::thumb, label)));
} }
void AssemblerA32::Bx(Label *label) { void AssemblerA32::Bx(Label *label) {
Emit(reinterpret_cast<Unit<Base>*>(new INST_T32(B32)(INST_T32(B32)::B, INST_T32(B32)::thumb, label))); Emit(reinterpret_cast<BaseUnit*>(new INST_T32(B32)(INST_T32(B32)::B, INST_T32(B32)::thumb, label)));
} }
void AssemblerA32::Mov(RegisterA32 &rd, RegisterA32 &rm) { void AssemblerA32::Mov(RegisterA32 &rd, RegisterA32 &rm) {
Emit(reinterpret_cast<Unit<Base>*>(new INST_T16(MOV_REG)(rd, rm))); Emit(reinterpret_cast<BaseUnit*>(new INST_T16(MOV_REG)(rd, rm)));
} }
void AssemblerA32::Bx(RegisterA32 &rm) { void AssemblerA32::Bx(RegisterA32 &rm) {
Emit(reinterpret_cast<Unit<Base>*>(new INST_T16(BX_BLX)(INST_T16(BX_BLX)::BX, rm))); Emit(reinterpret_cast<BaseUnit*>(new INST_T16(BX_BLX)(INST_T16(BX_BLX)::BX, rm)));
} }
void AssemblerA32::Blx(RegisterA32 &rm) { void AssemblerA32::Blx(RegisterA32 &rm) {
Emit(reinterpret_cast<Unit<Base>*>(new INST_T16(BX_BLX)(INST_T16(BX_BLX)::BLX, rm))); Emit(reinterpret_cast<BaseUnit*>(new INST_T16(BX_BLX)(INST_T16(BX_BLX)::BLX, rm)));
} }
void AssemblerA32::B(Condition condition, Label* label) { void AssemblerA32::B(Condition condition, Label* label) {
Emit(reinterpret_cast<Unit<Base>*>(new INST_T16(B_COND)(condition, label))); Emit(reinterpret_cast<BaseUnit*>(new INST_T16(B_COND)(condition, label)));
} }
void AssemblerA32::Add(RegisterA32 &rdn, U8 imm8) { void AssemblerA32::Add(RegisterA32 &rdn, U8 imm8) {
Emit(reinterpret_cast<Unit<Base>*>(new INST_T16(ADD_IMM_RDN)(&rdn, imm8))); Emit(reinterpret_cast<BaseUnit*>(new INST_T16(ADD_IMM_RDN)(&rdn, imm8)));
} }
void AssemblerA32::Add(RegisterA32 &rd, RegisterA32 &rn, RegisterA32 &rm) { void AssemblerA32::Add(RegisterA32 &rd, RegisterA32 &rn, RegisterA32 &rm) {
Emit(reinterpret_cast<Unit<Base>*>(new INST_T16(ADD_REG)(&rd, &rn, &rm))); Emit(reinterpret_cast<BaseUnit*>(new INST_T16(ADD_REG)(&rd, &rn, &rm)));
} }
void AssemblerA32::Cmp(RegisterA32 &rd, RegisterA32 &rn) { void AssemblerA32::Cmp(RegisterA32 &rd, RegisterA32 &rn) {
if (rd.Code() < 8 && rn.Code() < 8) { if (rd.Code() < 8 && rn.Code() < 8) {
Emit(reinterpret_cast<Unit<Base>*>(new INST_T16(CMP_REG)(rd, rn))); Emit(reinterpret_cast<BaseUnit*>(new INST_T16(CMP_REG)(rd, rn)));
} else { } else {
Emit(reinterpret_cast<Unit<Base>*>(new INST_T16(CMP_REG_EXT)(rd, rn))); Emit(reinterpret_cast<BaseUnit*>(new INST_T16(CMP_REG_EXT)(rd, rn)));
} }
} }
void AssemblerA32::Pop(RegisterA32 &rt) { void AssemblerA32::Pop(RegisterA32 &rt) {
if (rt.Code() < 8 || rt == PC) { if (rt.Code() < 8 || rt == PC) {
Emit(reinterpret_cast<Unit<Base>*>(new INST_T16(POP)(RegisterList(rt)))); Emit(reinterpret_cast<BaseUnit*>(new INST_T16(POP)(RegisterList(rt))));
} else { } else {
throw ErrorCodeException("error pop inst"); throw ErrorCodeException("error pop inst");
} }
...@@ -163,14 +163,14 @@ void AssemblerA32::Pop(RegisterA32 &rt) { ...@@ -163,14 +163,14 @@ void AssemblerA32::Pop(RegisterA32 &rt) {
void AssemblerA32::Push(RegisterA32 &rt) { void AssemblerA32::Push(RegisterA32 &rt) {
if (rt.Code() < 8 || rt == PC) { if (rt.Code() < 8 || rt == PC) {
Emit(reinterpret_cast<Unit<Base>*>(new INST_T16(PUSH)(RegisterList(rt)))); Emit(reinterpret_cast<BaseUnit*>(new INST_T16(PUSH)(RegisterList(rt))));
} else { } else {
throw ErrorCodeException("error pop inst"); throw ErrorCodeException("error pop inst");
} }
} }
void AssemblerA32::Adr(RegisterA32 &rd, Label *label) { void AssemblerA32::Adr(RegisterA32 &rd, Label *label) {
Emit(reinterpret_cast<Unit<Base>*>(new INST_T16(ADR)(rd, label))); Emit(reinterpret_cast<BaseUnit*>(new INST_T16(ADR)(rd, label)));
} }
void AssemblerA32::Nop16() { void AssemblerA32::Nop16() {
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
#include "inst_t32.h" #include "inst_t32.h"
#define ALIGN_FOR_LDR \ #define ALIGN_FOR_LDR \
if ((Addr) __ getPC() % 4 != 0) { \ if ((Addr) __ GetPC() % 4 != 0) { \
__ Nop16(); \ __ Nop16(); \
} }
...@@ -24,10 +24,10 @@ namespace SandHook { ...@@ -24,10 +24,10 @@ namespace SandHook {
public: public:
AssemblerA32(CodeBuffer* codeBuffer); AssemblerA32(CodeBuffer* codeBuffer);
void allocBufferFirst(U32 size); void AllocBufferFirst(U32 size);
void* getStartPC(); void* GetStartPC();
void* getPC(); void* GetPC();
void* finish(); void* Finish();
void Emit(U32 data32); void Emit(U32 data32);
void Emit(U16 data16); void Emit(U16 data16);
...@@ -78,7 +78,7 @@ namespace SandHook { ...@@ -78,7 +78,7 @@ namespace SandHook {
void Nop16(); void Nop16();
public: public:
CodeContainer codeContainer = CodeContainer(nullptr); CodeContainer code_container = CodeContainer(nullptr);
}; };
} }
} }
......
...@@ -20,9 +20,9 @@ using namespace SandHook::RegistersA32; ...@@ -20,9 +20,9 @@ using namespace SandHook::RegistersA32;
void *InlineHookArm32Android::Hook(void *origin, void *replace) { void *InlineHookArm32Android::Hook(void *origin, void *replace) {
AutoLock lock(hookLock); AutoLock lock(hookLock);
void* originCode; void* origin_code;
if (IsThumbCode((Addr) origin)) { if (IsThumbCode((Addr) origin)) {
originCode = GetThumbCodeAddress(origin); origin_code = GetThumbCodeAddress(origin);
} else { } else {
LOGE("hook %d error!, only support thumb2 now!", origin); LOGE("hook %d error!, only support thumb2 now!", origin);
return nullptr; return nullptr;
...@@ -31,14 +31,14 @@ void *InlineHookArm32Android::Hook(void *origin, void *replace) { ...@@ -31,14 +31,14 @@ void *InlineHookArm32Android::Hook(void *origin, void *replace) {
bool changeMode = IsThumbCode((Addr) origin) != IsThumbCode((Addr) replace); bool changeMode = IsThumbCode((Addr) origin) != IsThumbCode((Addr) replace);
void* backup = nullptr; void* backup = nullptr;
AssemblerA32 assemblerBackup(backupBuffer); AssemblerA32 assembler_backup(backup_buffer);
StaticCodeBuffer inlineBuffer = StaticCodeBuffer(reinterpret_cast<Addr>(originCode)); StaticCodeBuffer inline_buffer = StaticCodeBuffer(reinterpret_cast<Addr>(origin_code));
AssemblerA32 assemblerInline(&inlineBuffer); AssemblerA32 assembler_inline(&inline_buffer);
CodeContainer* codeContainerInline = &assemblerInline.codeContainer; CodeContainer* code_container_inline = &assembler_inline.code_container;
//build inline trampoline //build inline trampoline
#define __ assemblerInline. #define __ assembler_inline.
if (!changeMode) { if (!changeMode) {
Label *target_addr_label = new Label(); Label *target_addr_label = new Label();
ALIGN_FOR_LDR ALIGN_FOR_LDR
...@@ -53,20 +53,20 @@ void *InlineHookArm32Android::Hook(void *origin, void *replace) { ...@@ -53,20 +53,20 @@ void *InlineHookArm32Android::Hook(void *origin, void *replace) {
#undef __ #undef __
//build backup method //build backup method
CodeRelocateA32 relocate = CodeRelocateA32(assemblerBackup); CodeRelocateA32 relocate = CodeRelocateA32(assembler_backup);
backup = relocate.relocate(origin, codeContainerInline->size(), nullptr); backup = relocate.Relocate(origin, code_container_inline->size(), nullptr);
#define __ assemblerBackup. #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( __ Emit((Addr) GetThumbPC(
reinterpret_cast<void *>(reinterpret_cast<Addr>(originCode) + relocate.curOffset))); reinterpret_cast<void *>(reinterpret_cast<Addr>(origin_code) + relocate.cur_offset)));
__ finish(); __ Finish();
#undef __ #undef __
//commit inline trampoline //commit inline trampoline
assemblerInline.finish(); assembler_inline.Finish();
return GetThumbPC(backup); return GetThumbPC(backup);
} }
...@@ -77,43 +77,42 @@ IMPORT_LABEL(origin_addr_s, Addr) ...@@ -77,43 +77,42 @@ IMPORT_LABEL(origin_addr_s, Addr)
bool InlineHookArm32Android::BreakPoint(void *origin, void (*callback)(REG *)) { bool InlineHookArm32Android::BreakPoint(void *origin, void (*callback)(REG *)) {
AutoLock lock(hookLock); AutoLock lock(hookLock);
void* originCode; void* origin_code;
if (IsThumbCode((Addr) origin)) { if (IsThumbCode((Addr) origin)) {
originCode = GetThumbCodeAddress(origin); origin_code = GetThumbCodeAddress(origin);
} else { } else {
LOGE("hook %d error!, only support thumb2 now!", origin); LOGE("hook %d error!, only support thumb2 now!", origin);
return false; return false;
} }
bool changeMode = IsThumbCode((Addr) origin) != IsThumbCode((Addr) callback); bool change_mode = IsThumbCode((Addr) origin) != IsThumbCode((Addr) callback);
void* backup = nullptr; void* backup = nullptr;
AssemblerA32 assemblerBackup(backupBuffer); AssemblerA32 assembler_backup(backup_buffer);
StaticCodeBuffer inlineBuffer = StaticCodeBuffer(reinterpret_cast<Addr>(originCode)); StaticCodeBuffer inline_buffer = StaticCodeBuffer(reinterpret_cast<Addr>(origin_code));
AssemblerA32 assemblerInline(&inlineBuffer); AssemblerA32 assembler_inline(&inline_buffer);
//build backup method //build backup method
CodeRelocateA32 relocate = CodeRelocateA32(assemblerBackup); CodeRelocateA32 relocate = CodeRelocateA32(assembler_backup);
backup = relocate.relocate(origin, changeMode ? (4 * 2 + 2) : (4 * 2), nullptr); backup = relocate.Relocate(origin, change_mode ? (4 * 2 + 2) : (4 * 2), nullptr);
#define __ assemblerBackup. #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( __ Emit((Addr) GetThumbPC(reinterpret_cast<void *>(reinterpret_cast<Addr>(origin_code) + relocate.cur_offset)));
reinterpret_cast<void *>(reinterpret_cast<Addr>(originCode) + relocate.curOffset))); __ Finish();
__ finish();
#undef __ #undef __
//build trampoline //build trampoline
origin_addr_s = (Addr) GetThumbPC(backup); origin_addr_s = (Addr) GetThumbPC(backup);
callback_addr_s = (Addr) callback; callback_addr_s = (Addr) callback;
void* trampoline = backupBuffer->copy((void*)BP_SHELLCODE, SHELLCODE_LEN(BP_SHELLCODE)); void* trampoline = backup_buffer->copy((void*)BP_SHELLCODE, SHELLCODE_LEN(BP_SHELLCODE));
//build inline trampoline //build inline trampoline
#define __ assemblerInline. #define __ assembler_inline.
if (!changeMode) { if (!change_mode) {
Label *target_addr_label = new Label(); Label *target_addr_label = new Label();
ALIGN_FOR_LDR ALIGN_FOR_LDR
__ Ldr(PC, target_addr_label); __ Ldr(PC, target_addr_label);
...@@ -123,7 +122,7 @@ bool InlineHookArm32Android::BreakPoint(void *origin, void (*callback)(REG *)) { ...@@ -123,7 +122,7 @@ bool InlineHookArm32Android::BreakPoint(void *origin, void (*callback)(REG *)) {
__ Mov(IP, (Addr) trampoline); __ Mov(IP, (Addr) trampoline);
__ Bx(IP); __ Bx(IP);
} }
__ finish(); __ Finish();
#undef __ #undef __
return true; return true;
......
...@@ -20,42 +20,42 @@ case ENUM_VALUE(InstCode##T, InstCode##T::X): \ ...@@ -20,42 +20,42 @@ 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), toPc); \
break; break;
CodeRelocateA32::CodeRelocateA32(AssemblerA32 &assembler) : CodeRelocate(assembler.codeContainer) { CodeRelocateA32::CodeRelocateA32(AssemblerA32 &assembler) : CodeRelocate(assembler.code_container) {
this->assemblerA32 = &assembler; this->assemblerA32 = &assembler;
} }
bool CodeRelocateA32::Visit(BaseUnit *unit, void *pc) { bool CodeRelocateA32::Visit(BaseUnit *unit, void *pc) {
relocate(reinterpret_cast<BaseInst *>(unit), __ getPC()); Relocate(reinterpret_cast<BaseInst *>(unit), __ GetPC());
curOffset += unit->Size(); cur_offset += unit->Size();
if (unit->RefCount() == 0) { if (unit->RefCount() == 0) {
delete unit; delete unit;
} }
return true; return true;
} }
void* CodeRelocateA32::relocate(void *startPc, Addr len, void *toPc = nullptr) throw(ErrorCodeException) { void* CodeRelocateA32::Relocate(void *startPc, Addr len, void *toPc = nullptr) throw(ErrorCodeException) {
AutoLock autoLock(relocateLock); AutoLock autoLock(relocate_lock);
startAddr = reinterpret_cast<Addr>(startPc); start_addr = reinterpret_cast<Addr>(startPc);
if (IsThumbCode(startAddr)) { if (IsThumbCode(start_addr)) {
startAddr = reinterpret_cast<Addr>(GetThumbCodeAddress(startPc)); start_addr = reinterpret_cast<Addr>(GetThumbCodeAddress(startPc));
} }
length = len; length = len;
curOffset = 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 (toPc == nullptr) {
Disassembler::get()->Disassembler(startPc, len, *this, true); Disassembler::Get()->Disassembler(startPc, len, *this, true);
} else { } else {
//TODO //TODO
} }
return curPc; return curPc;
} }
void* CodeRelocateA32::relocate(BaseInst *instruction, void *toPc) throw(ErrorCodeException) { void* CodeRelocateA32::Relocate(BaseInst *instruction, void *toPc) throw(ErrorCodeException) {
void* curPc = __ getPC(); void* curPc = __ GetPC();
//insert later AddBind labels //insert later AddBind labels
__ Emit(getLaterBindLabel(curOffset)); __ Emit(GetLaterBindLabel(cur_offset));
if (!instruction->PcRelate()) { if (!instruction->PcRelate()) {
__ Emit(instruction); __ Emit(instruction);
...@@ -94,8 +94,8 @@ void* CodeRelocateA32::relocate(BaseInst *instruction, void *toPc) throw(ErrorCo ...@@ -94,8 +94,8 @@ void* CodeRelocateA32::relocate(BaseInst *instruction, void *toPc) throw(ErrorCo
IMPL_RELOCATE(T16, B_COND) { IMPL_RELOCATE(T16, B_COND) {
if (inRelocateRange(CODE_OFFSET(inst), sizeof(InstT16))) { if (InRelocateRange(CODE_OFFSET(inst), sizeof(InstT16))) {
__ B(inst->condition, getLaterBindLabel(CODE_OFFSET(inst) + curOffset)); __ B(inst->condition, GetLaterBindLabel(CODE_OFFSET(inst) + cur_offset));
return; return;
} }
...@@ -125,8 +125,8 @@ IMPL_RELOCATE(T16, B_COND) { ...@@ -125,8 +125,8 @@ IMPL_RELOCATE(T16, B_COND) {
IMPL_RELOCATE(T16, B) { IMPL_RELOCATE(T16, B) {
if (inRelocateRange(CODE_OFFSET(inst), sizeof(InstT16))) { if (InRelocateRange(CODE_OFFSET(inst), sizeof(InstT16))) {
__ B(getLaterBindLabel(CODE_OFFSET(inst) + curOffset)); __ B(GetLaterBindLabel(CODE_OFFSET(inst) + cur_offset));
return; return;
} }
...@@ -149,8 +149,8 @@ IMPL_RELOCATE(T16, CBZ_CBNZ) { ...@@ -149,8 +149,8 @@ IMPL_RELOCATE(T16, CBZ_CBNZ) {
inst->Ref(); inst->Ref();
if (inRelocateRange(CODE_OFFSET(inst), sizeof(InstT16))) { if (InRelocateRange(CODE_OFFSET(inst), sizeof(InstT16))) {
inst->BindLabel(getLaterBindLabel(CODE_OFFSET(inst) + curOffset)); inst->BindLabel(GetLaterBindLabel(CODE_OFFSET(inst) + cur_offset));
__ Emit(reinterpret_cast<BaseInst*>(inst)); __ Emit(reinterpret_cast<BaseInst*>(inst));
return; return;
} }
...@@ -175,9 +175,9 @@ IMPL_RELOCATE(T16, CBZ_CBNZ) { ...@@ -175,9 +175,9 @@ IMPL_RELOCATE(T16, CBZ_CBNZ) {
IMPL_RELOCATE(T16, LDR_LIT) { IMPL_RELOCATE(T16, LDR_LIT) {
if (inRelocateRange(CODE_OFFSET(inst), inst->rt->Wide())) { if (InRelocateRange(CODE_OFFSET(inst), inst->rt->Wide())) {
inst->Ref(); inst->Ref();
inst->BindLabel(getLaterBindLabel(CODE_OFFSET(inst) + curOffset)); inst->BindLabel(GetLaterBindLabel(CODE_OFFSET(inst) + cur_offset));
__ Emit(reinterpret_cast<BaseInst*>(inst)); __ Emit(reinterpret_cast<BaseInst*>(inst));
return; return;
} }
...@@ -190,9 +190,9 @@ IMPL_RELOCATE(T16, LDR_LIT) { ...@@ -190,9 +190,9 @@ IMPL_RELOCATE(T16, LDR_LIT) {
IMPL_RELOCATE(T16, ADR) { IMPL_RELOCATE(T16, ADR) {
if (inRelocateRange(CODE_OFFSET(inst), inst->rd->Wide())) { if (InRelocateRange(CODE_OFFSET(inst), inst->rd->Wide())) {
inst->Ref(); inst->Ref();
inst->BindLabel(getLaterBindLabel(CODE_OFFSET(inst) + curOffset)); inst->BindLabel(GetLaterBindLabel(CODE_OFFSET(inst) + cur_offset));
__ Emit(reinterpret_cast<BaseInst*>(inst)); __ Emit(reinterpret_cast<BaseInst*>(inst));
return; return;
} }
...@@ -222,9 +222,9 @@ IMPL_RELOCATE(T16, ADD_REG_RDN) { ...@@ -222,9 +222,9 @@ IMPL_RELOCATE(T16, ADD_REG_RDN) {
IMPL_RELOCATE(T32, B32) { IMPL_RELOCATE(T32, B32) {
if (inRelocateRange(CODE_OFFSET(inst), sizeof(InstT16))) { if (InRelocateRange(CODE_OFFSET(inst), sizeof(InstT16))) {
inst->Ref(); inst->Ref();
inst->BindLabel(getLaterBindLabel(CODE_OFFSET(inst) + curOffset)); inst->BindLabel(GetLaterBindLabel(CODE_OFFSET(inst) + cur_offset));
__ Emit(reinterpret_cast<BaseInst *>(inst)); __ Emit(reinterpret_cast<BaseInst *>(inst));
return; return;
} }
...@@ -247,9 +247,9 @@ IMPL_RELOCATE(T32, B32) { ...@@ -247,9 +247,9 @@ IMPL_RELOCATE(T32, B32) {
IMPL_RELOCATE(T32, LDR_LIT) { IMPL_RELOCATE(T32, LDR_LIT) {
if (inRelocateRange(CODE_OFFSET(inst), sizeof(Addr))) { if (InRelocateRange(CODE_OFFSET(inst), sizeof(Addr))) {
inst->Ref(); inst->Ref();
inst->BindLabel(getLaterBindLabel(CODE_OFFSET(inst) + curOffset)); inst->BindLabel(GetLaterBindLabel(CODE_OFFSET(inst) + cur_offset));
__ Emit(reinterpret_cast<BaseInst*>(inst)); __ Emit(reinterpret_cast<BaseInst*>(inst));
return; return;
} }
......
...@@ -22,9 +22,9 @@ namespace SandHook { ...@@ -22,9 +22,9 @@ namespace SandHook {
public: public:
CodeRelocateA32(AssemblerA32 &assembler); CodeRelocateA32(AssemblerA32 &assembler);
void* relocate(BaseInst *instruction, void *toPc) throw(ErrorCodeException) override; void* Relocate(BaseInst *instruction, void *toPc) 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;
bool Visit(BaseUnit *unit, void *pc) override; bool Visit(BaseUnit *unit, void *pc) override;
......
...@@ -84,7 +84,7 @@ namespace SandHook { ...@@ -84,7 +84,7 @@ namespace SandHook {
void Subs(RegisterA64& rd, const Operand& operand); void Subs(RegisterA64& rd, const Operand& operand);
void Msr(SystemRegister &sysReg, RegisterA64& rt); void Msr(SystemRegister &sysReg, RegisterA64& rt);
void Mrs(SystemRegister &sysReg, RegisterA64& rt); void Mrs(SystemRegister &sys_reg, RegisterA64& rt);
void Mov(RegisterA64& rd, RegisterA64& rt); void Mov(RegisterA64& rd, RegisterA64& rt);
...@@ -94,7 +94,7 @@ namespace SandHook { ...@@ -94,7 +94,7 @@ namespace SandHook {
public: public:
CodeContainer codeContainer = CodeContainer(nullptr); CodeContainer code_container = CodeContainer(nullptr);
}; };
} }
......
...@@ -19,9 +19,9 @@ Arm64Decoder* Arm64Decoder::instant = new Arm64Decoder(); ...@@ -19,9 +19,9 @@ Arm64Decoder* Arm64Decoder::instant = new Arm64Decoder();
void Arm64Decoder::Disassembler(void *codeStart, Addr codeLen, InstVisitor &visitor, void Arm64Decoder::Disassembler(void *codeStart, Addr codeLen, InstVisitor &visitor,
bool onlyPcRelInst) { bool onlyPcRelInst) {
InstA64 *pc = reinterpret_cast<InstA64 *>(codeStart); InstA64 *pc = reinterpret_cast<InstA64 *>(codeStart);
Addr endAddr = (Addr) codeStart + codeLen; Addr end_addr = (Addr) codeStart + codeLen;
Unit<Base>* unit = nullptr; Unit<Base>* unit = nullptr;
while((Addr) pc < endAddr) { while((Addr) pc < end_addr) {
// pc relate insts // pc relate insts
CASE(B_BL) CASE(B_BL)
CASE(B_COND) CASE(B_COND)
......
...@@ -16,17 +16,17 @@ using namespace SandHook::Utils; ...@@ -16,17 +16,17 @@ using namespace SandHook::Utils;
#include "code_relocate_arm64.h" #include "code_relocate_arm64.h"
using namespace SandHook::RegistersA64; using namespace SandHook::RegistersA64;
void *InlineHookArm64Android::Hook(void *origin, void *replace) { void *InlineHookArm64Android::Hook(void *origin, void *replace) {
AutoLock lock(hookLock); AutoLock lock(hook_lock);
void* backup = nullptr; void* backup = nullptr;
AssemblerA64 assemblerBackup(backupBuffer); AssemblerA64 assembler_backup(backup_buffer);
StaticCodeBuffer inlineBuffer = StaticCodeBuffer(reinterpret_cast<Addr>(origin)); StaticCodeBuffer inline_buffer = StaticCodeBuffer(reinterpret_cast<Addr>(origin));
AssemblerA64 assemblerInline(&inlineBuffer); AssemblerA64 assembler_inline(&inline_buffer);
CodeContainer* codeContainerInline = &assemblerInline.codeContainer; CodeContainer* code_container_inline = &assembler_inline.code_container;
//build inline trampoline //build inline trampoline
#define __ assemblerInline. #define __ assembler_inline.
Label* target_addr_label = new Label(); Label* target_addr_label = new Label();
__ Ldr(IP1, target_addr_label); __ Ldr(IP1, target_addr_label);
__ Br(IP1); __ Br(IP1);
...@@ -35,37 +35,37 @@ void *InlineHookArm64Android::Hook(void *origin, void *replace) { ...@@ -35,37 +35,37 @@ void *InlineHookArm64Android::Hook(void *origin, void *replace) {
#undef __ #undef __
//build backup method //build backup method
CodeRelocateA64 relocate = CodeRelocateA64(assemblerBackup); CodeRelocateA64 relocate = CodeRelocateA64(assembler_backup);
backup = relocate.relocate(origin, codeContainerInline->size(), nullptr); backup = relocate.Relocate(origin, code_container_inline->size(), nullptr);
#define __ assemblerBackup. #define __ assembler_backup.
Label* origin_addr_label = new Label(); Label* origin_addr_label = new Label();
__ Ldr(IP1, origin_addr_label); __ Ldr(IP1, origin_addr_label);
__ Br(IP1); __ Br(IP1);
__ Emit(origin_addr_label); __ Emit(origin_addr_label);
__ Emit((Addr) origin + codeContainerInline->size()); __ Emit((Addr) origin + code_container_inline->size());
__ Finish(); __ Finish();
#undef __ #undef __
//commit inline trampoline //commit inline trampoline
assemblerInline.Finish(); assembler_inline.Finish();
return backup; return backup;
} }
bool InlineHookArm64Android::BreakPoint(void *point, void (*callback)(REG regs[])) { bool InlineHookArm64Android::BreakPoint(void *point, void (*callback)(REG regs[])) {
AutoLock lock(hookLock); AutoLock lock(hook_lock);
void* backup = nullptr; void* backup = nullptr;
AssemblerA64 assemblerBackup(backupBuffer); AssemblerA64 assembler_backup(backup_buffer);
AssemblerA64 assemblerTrampoline(backupBuffer); AssemblerA64 assembler_trampoline(backup_buffer);
StaticCodeBuffer inlineBuffer = StaticCodeBuffer(reinterpret_cast<Addr>(point)); StaticCodeBuffer inline_buffer = StaticCodeBuffer(reinterpret_cast<Addr>(point));
AssemblerA64 assemblerInline(&inlineBuffer); AssemblerA64 assembler_inline(&inline_buffer);
//build backup inst //build backup inst
CodeRelocateA64 relocate = CodeRelocateA64(assemblerBackup); CodeRelocateA64 relocate = CodeRelocateA64(assembler_backup);
backup = relocate.relocate(point, 4 * 4, nullptr); backup = relocate.Relocate(point, 4 * 4, nullptr);
#define __ assemblerBackup. #define __ assembler_backup.
Label* origin_addr_label = new Label(); Label* origin_addr_label = new Label();
__ Ldr(IP1, origin_addr_label); __ Ldr(IP1, origin_addr_label);
__ Br(IP1); __ Br(IP1);
...@@ -76,7 +76,7 @@ bool InlineHookArm64Android::BreakPoint(void *point, void (*callback)(REG regs[] ...@@ -76,7 +76,7 @@ bool InlineHookArm64Android::BreakPoint(void *point, void (*callback)(REG regs[]
//build shell code //build shell code
#define __ assemblerTrampoline. #define __ assembler_trampoline.
//backup NZCV //backup NZCV
__ Sub(SP, Operand(&SP, 0x20)); __ Sub(SP, Operand(&SP, 0x20));
...@@ -118,14 +118,14 @@ bool InlineHookArm64Android::BreakPoint(void *point, void (*callback)(REG regs[] ...@@ -118,14 +118,14 @@ bool InlineHookArm64Android::BreakPoint(void *point, void (*callback)(REG regs[]
#undef __ #undef __
void* secondTrampoline = assemblerTrampoline.GetStartPC(); void* second_trampoline = assembler_trampoline.GetStartPC();
//build inline trampoline //build inline trampoline
#define __ assemblerInline. #define __ assembler_inline.
Label* target_addr_label = new Label(); Label* target_addr_label = new Label();
__ Ldr(IP1, target_addr_label); __ Ldr(IP1, target_addr_label);
__ Br(IP1); __ Br(IP1);
__ Emit(target_addr_label); __ Emit(target_addr_label);
__ Emit((Addr) secondTrampoline); __ Emit((Addr) second_trampoline);
__ Finish(); __ Finish();
#undef __ #undef __
......
...@@ -20,39 +20,39 @@ case ENUM_VALUE(InstCodeA64, InstCodeA64::X): \ ...@@ -20,39 +20,39 @@ case ENUM_VALUE(InstCodeA64, InstCodeA64::X): \
relocate_##X(reinterpret_cast<INST_A64(X)*>(instruction), toPc); \ relocate_##X(reinterpret_cast<INST_A64(X)*>(instruction), toPc); \
break; break;
CodeRelocateA64::CodeRelocateA64(AssemblerA64 &assembler) : CodeRelocate(assembler.codeContainer) { CodeRelocateA64::CodeRelocateA64(AssemblerA64 &assembler) : CodeRelocate(assembler.code_container) {
assemblerA64 = &assembler; assemblerA64 = &assembler;
} }
bool CodeRelocateA64::Visit(Unit<Base> *unit, void *pc) { bool CodeRelocateA64::Visit(Unit<Base> *unit, void *pc) {
relocate(reinterpret_cast<BaseInst *>(unit), __ GetPC()); Relocate(reinterpret_cast<BaseInst *>(unit), __ GetPC());
curOffset += unit->Size(); cur_offset += unit->Size();
if (unit->RefCount() == 0) { if (unit->RefCount() == 0) {
delete unit; delete unit;
} }
return true; return true;
} }
void* CodeRelocateA64::relocate(void *startPc, Addr len, void *toPc = nullptr) throw(ErrorCodeException) { void* CodeRelocateA64::Relocate(void *startPc, Addr len, void *toPc = nullptr) throw(ErrorCodeException) {
AutoLock autoLock(relocateLock); AutoLock autoLock(relocate_lock);
startAddr = reinterpret_cast<Addr>(startPc); start_addr = reinterpret_cast<Addr>(startPc);
length = len; length = len;
curOffset = 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 (toPc == nullptr) {
Disassembler::get()->Disassembler(startPc, len, *this, true); Disassembler::Get()->Disassembler(startPc, len, *this, true);
} else { } else {
//TODO //TODO
} }
return curPc; return curPc;
} }
void* CodeRelocateA64::relocate(BaseInst *instruction, void *toPc) throw(ErrorCodeException) { void* CodeRelocateA64::Relocate(BaseInst *instruction, void *toPc) throw(ErrorCodeException) {
void* curPc = __ GetPC(); void* curPc = __ GetPC();
//insert later AddBind labels //insert later AddBind labels
__ Emit(getLaterBindLabel(curOffset)); __ Emit(GetLaterBindLabel(cur_offset));
if (!instruction->PcRelate()) { if (!instruction->PcRelate()) {
__ Emit(instruction); __ Emit(instruction);
...@@ -75,9 +75,9 @@ void* CodeRelocateA64::relocate(BaseInst *instruction, void *toPc) throw(ErrorCo ...@@ -75,9 +75,9 @@ void* CodeRelocateA64::relocate(BaseInst *instruction, void *toPc) throw(ErrorCo
IMPL_RELOCATE(B_BL) { IMPL_RELOCATE(B_BL) {
if (inRelocateRange(inst->offset, sizeof(InstA64))) { if (InRelocateRange(inst->offset, sizeof(InstA64))) {
inst->Ref(); inst->Ref();
inst->BindLabel(getLaterBindLabel(inst->offset + curOffset)); inst->BindLabel(GetLaterBindLabel(inst->offset + cur_offset));
__ Emit(reinterpret_cast<BaseInst*>(inst)); __ Emit(reinterpret_cast<BaseInst*>(inst));
return; return;
} }
...@@ -97,9 +97,9 @@ IMPL_RELOCATE(B_BL) { ...@@ -97,9 +97,9 @@ IMPL_RELOCATE(B_BL) {
IMPL_RELOCATE(B_COND) { IMPL_RELOCATE(B_COND) {
if (inRelocateRange(inst->offset, sizeof(InstA64))) { if (InRelocateRange(inst->offset, sizeof(InstA64))) {
inst->Ref(); inst->Ref();
inst->BindLabel(getLaterBindLabel(inst->offset + curOffset)); inst->BindLabel(GetLaterBindLabel(inst->offset + cur_offset));
__ Emit(reinterpret_cast<BaseInst*>(inst)); __ Emit(reinterpret_cast<BaseInst*>(inst));
return; return;
} }
...@@ -121,9 +121,9 @@ IMPL_RELOCATE(B_COND) { ...@@ -121,9 +121,9 @@ IMPL_RELOCATE(B_COND) {
IMPL_RELOCATE(TBZ_TBNZ) { IMPL_RELOCATE(TBZ_TBNZ) {
if (inRelocateRange(inst->offset, sizeof(InstA64))) { if (InRelocateRange(inst->offset, sizeof(InstA64))) {
inst->Ref(); inst->Ref();
inst->BindLabel(getLaterBindLabel(inst->offset + curOffset)); inst->BindLabel(GetLaterBindLabel(inst->offset + cur_offset));
__ Emit(reinterpret_cast<BaseInst*>(inst)); __ Emit(reinterpret_cast<BaseInst*>(inst));
return; return;
} }
...@@ -149,9 +149,9 @@ IMPL_RELOCATE(TBZ_TBNZ) { ...@@ -149,9 +149,9 @@ IMPL_RELOCATE(TBZ_TBNZ) {
IMPL_RELOCATE(CBZ_CBNZ) { IMPL_RELOCATE(CBZ_CBNZ) {
if (inRelocateRange(inst->offset, sizeof(InstA64))) { if (InRelocateRange(inst->offset, sizeof(InstA64))) {
inst->Ref(); inst->Ref();
inst->BindLabel(getLaterBindLabel(inst->offset + curOffset)); inst->BindLabel(GetLaterBindLabel(inst->offset + cur_offset));
__ Emit(reinterpret_cast<BaseInst*>(inst)); __ Emit(reinterpret_cast<BaseInst*>(inst));
return; return;
} }
...@@ -181,9 +181,9 @@ IMPL_RELOCATE(LDR_LIT) { ...@@ -181,9 +181,9 @@ IMPL_RELOCATE(LDR_LIT) {
XRegister* rtX = XReg(inst->rt->Code()); XRegister* rtX = XReg(inst->rt->Code());
WRegister* rtW = WReg(inst->rt->Code()); WRegister* rtW = WReg(inst->rt->Code());
if (inRelocateRange(inst->offset, sizeof(Addr))) { if (InRelocateRange(inst->offset, sizeof(Addr))) {
inst->Ref(); inst->Ref();
inst->BindLabel(getLaterBindLabel(inst->offset + curOffset)); inst->BindLabel(GetLaterBindLabel(inst->offset + cur_offset));
__ Emit(reinterpret_cast<BaseInst*>(inst)); __ Emit(reinterpret_cast<BaseInst*>(inst));
return; return;
} }
......
...@@ -22,9 +22,9 @@ namespace SandHook { ...@@ -22,9 +22,9 @@ namespace SandHook {
public: public:
CodeRelocateA64(AssemblerA64 &assembler); CodeRelocateA64(AssemblerA64 &assembler);
void* relocate(BaseInst *instruction, void *toPc) throw(ErrorCodeException) override; void* Relocate(BaseInst *instruction, void *toPc) 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;
bool Visit(Unit<Base> *unit, void *pc) override; bool Visit(Unit<Base> *unit, void *pc) override;
......
...@@ -31,9 +31,9 @@ void CodeContainer::commit() { ...@@ -31,9 +31,9 @@ void CodeContainer::commit() {
void* bufferStart; void* bufferStart;
if (startPc > 0) { if (startPc > 0) {
bufferStart = reinterpret_cast<void *>(startPc); bufferStart = reinterpret_cast<void *>(startPc);
codeBuffer->resetLastBufferSize(bufferSize); codeBuffer->ResetLastBufferSize(bufferSize);
} else { } else {
bufferStart = codeBuffer->getBuffer(bufferSize); bufferStart = codeBuffer->GetBuffer(bufferSize);
} }
Addr pcNow = reinterpret_cast<Addr>(bufferStart); Addr pcNow = reinterpret_cast<Addr>(bufferStart);
...@@ -58,7 +58,7 @@ void CodeContainer::commit() { ...@@ -58,7 +58,7 @@ void CodeContainer::commit() {
} }
//flush I cache //flush I cache
flushCache(reinterpret_cast<Addr>(bufferStart), pcNow - reinterpret_cast<Addr>(bufferStart)); FlushCache(reinterpret_cast<Addr>(bufferStart), pcNow - reinterpret_cast<Addr>(bufferStart));
//Set pc //Set pc
startPc = reinterpret_cast<Addr>(bufferStart); startPc = reinterpret_cast<Addr>(bufferStart);
...@@ -67,7 +67,7 @@ void CodeContainer::commit() { ...@@ -67,7 +67,7 @@ void CodeContainer::commit() {
} }
void CodeContainer::allocBufferFirst(U32 size) { void CodeContainer::allocBufferFirst(U32 size) {
startPc = reinterpret_cast<Addr>(codeBuffer->getBuffer(size)); startPc = reinterpret_cast<Addr>(codeBuffer->GetBuffer(size));
curPc = startPc; curPc = startPc;
} }
......
...@@ -11,31 +11,31 @@ ...@@ -11,31 +11,31 @@
using namespace SandHook::Assembler; using namespace SandHook::Assembler;
using namespace SandHook::Utils; using namespace SandHook::Utils;
void *AndroidCodeBuffer::getBuffer(U32 size) { void *AndroidCodeBuffer::GetBuffer(U32 size) {
AutoLock autoLock(allocSpaceLock); AutoLock autoLock(alloc_space_lock);
void* mmapRes; void* mmapRes;
Addr exeSpace = 0; Addr exeSpace = 0;
if (executeSpaceList.size() == 0) { if (execute_space_list.size() == 0) {
goto label_alloc_new_space; goto label_alloc_new_space;
} else if (executePageOffset + size + 4> currentExecutePageSize) { } else if (execute_page_offset + size + 4> current_execute_page_size) {
goto label_alloc_new_space; goto label_alloc_new_space;
} else { } else {
exeSpace = reinterpret_cast<Addr>(executeSpaceList.back()); exeSpace = reinterpret_cast<Addr>(execute_space_list.back());
//4 字节对齐 //4 字节对齐
Addr retSpace = RoundUp(exeSpace + executePageOffset, 4); Addr retSpace = RoundUp(exeSpace + execute_page_offset, 4);
executePageOffset = retSpace + size - exeSpace; execute_page_offset = retSpace + size - exeSpace;
return reinterpret_cast<void *>(retSpace); return reinterpret_cast<void *>(retSpace);
} }
label_alloc_new_space: label_alloc_new_space:
currentExecutePageSize = static_cast<U32>(FIT(size, P_SIZE)); current_execute_page_size = static_cast<U32>(FIT(size, P_SIZE));
mmapRes = mmap(NULL, currentExecutePageSize, PROT_READ | PROT_WRITE | PROT_EXEC, mmapRes = mmap(NULL, current_execute_page_size, PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_ANON | MAP_PRIVATE, -1, 0); MAP_ANON | MAP_PRIVATE, -1, 0);
if (mmapRes == MAP_FAILED) { if (mmapRes == MAP_FAILED) {
return 0; return 0;
} }
memset(mmapRes, 0, currentExecutePageSize); memset(mmapRes, 0, current_execute_page_size);
executeSpaceList.push_back(mmapRes); execute_space_list.push_back(mmapRes);
executePageOffset = size; execute_page_offset = size;
return mmapRes; return mmapRes;
} }
...@@ -43,24 +43,24 @@ AndroidCodeBuffer::AndroidCodeBuffer() {} ...@@ -43,24 +43,24 @@ AndroidCodeBuffer::AndroidCodeBuffer() {}
StaticCodeBuffer::StaticCodeBuffer(Addr pc) : pc(pc) {} StaticCodeBuffer::StaticCodeBuffer(Addr pc) : pc(pc) {}
void *StaticCodeBuffer::getBuffer(U32 bufferSize) { void *StaticCodeBuffer::GetBuffer(U32 bufferSize) {
if (!memUnprotect(pc, bufferSize)) { if (!MemUnprotect(pc, bufferSize)) {
LOGE("error memUnprotect!"); LOGE("error MemUnprotect!");
} }
return reinterpret_cast<void *>(pc); return reinterpret_cast<void *>(pc);
} }
void AndroidRellocBufferUnsafe::resetLastBufferSize(U32 size) { void AndroidReSizableBufferUnsafe::ResetLastBufferSize(U32 size) {
if (executePageOffset + (size - lastAllocSize) <= currentExecutePageSize) { if (execute_page_offset + (size - last_alloc_size) <= current_execute_page_size) {
executePageOffset += size - lastAllocSize; execute_page_offset += size - last_alloc_size;
lastAllocSize = size; last_alloc_size = size;
} }
} }
void *AndroidRellocBufferUnsafe::getBuffer(U32 bufferSize) { void *AndroidReSizableBufferUnsafe::GetBuffer(U32 bufferSize) {
void* res = AndroidCodeBuffer::getBuffer(bufferSize); void* res = AndroidCodeBuffer::GetBuffer(bufferSize);
if (res) { if (res) {
lastAllocSize = bufferSize; last_alloc_size = bufferSize;
} }
return res; return res;
} }
...@@ -14,25 +14,25 @@ namespace SandHook { ...@@ -14,25 +14,25 @@ namespace SandHook {
AndroidCodeBuffer(); AndroidCodeBuffer();
void *getBuffer(U32 bufferSize) override; void *GetBuffer(U32 bufferSize) override;
protected: protected:
std::list<void*> executeSpaceList = std::list<void*>(); std::list<void*> execute_space_list = std::list<void*>();
std::mutex allocSpaceLock; std::mutex alloc_space_lock;
Addr executePageOffset = 0; Addr execute_page_offset = 0;
U32 currentExecutePageSize = 0; U32 current_execute_page_size = 0;
}; };
//thread unsafe //thread unsafe
class AndroidRellocBufferUnsafe : public AndroidCodeBuffer { class AndroidReSizableBufferUnsafe : public AndroidCodeBuffer {
public: public:
public: public:
void resetLastBufferSize(U32 size) override; void ResetLastBufferSize(U32 size) override;
void *getBuffer(U32 bufferSize) override; void *GetBuffer(U32 bufferSize) override;
private: private:
U32 lastAllocSize; U32 last_alloc_size;
}; };
class StaticCodeBuffer : public CodeBuffer { class StaticCodeBuffer : public CodeBuffer {
...@@ -40,7 +40,7 @@ namespace SandHook { ...@@ -40,7 +40,7 @@ namespace SandHook {
StaticCodeBuffer(Addr pc); StaticCodeBuffer(Addr pc);
void *getBuffer(U32 bufferSize) override; void *GetBuffer(U32 bufferSize) override;
private: private:
Addr pc; Addr pc;
......
...@@ -20,18 +20,18 @@ bool DefaultVisitor::Visit(Unit<Base> *unit, void *pc) { ...@@ -20,18 +20,18 @@ bool DefaultVisitor::Visit(Unit<Base> *unit, void *pc) {
DefaultVisitor::DefaultVisitor(bool (*visitor)(Unit<Base> *, void *)) : visitor(visitor) {} DefaultVisitor::DefaultVisitor(bool (*visitor)(Unit<Base> *, void *)) : visitor(visitor) {}
//do not support now //do not support now
InstDecoder* Disassembler::get(Arch arch) { InstDecoder* Disassembler::Get(Arch arch) {
switch (arch) { switch (arch) {
case arm32: case arm32:
return get(); return Get();
case arm64: case arm64:
return get(); return Get();
default: default:
return nullptr; return nullptr;
} }
} }
InstDecoder *Disassembler::get() { InstDecoder *Disassembler::Get() {
#if defined(__arm__) #if defined(__arm__)
return Arm32Decoder::instant; return Arm32Decoder::instant;
#elif defined(__aarch64__) #elif defined(__aarch64__)
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
using namespace SandHook::Hook; using namespace SandHook::Hook;
CodeBuffer* InlineHook::backupBuffer = new AndroidCodeBuffer(); CodeBuffer* InlineHook::backup_buffer = new AndroidCodeBuffer();
#if defined(__arm__) #if defined(__arm__)
InlineHook* InlineHook::instance = new InlineHookArm32Android(); InlineHook* InlineHook::instance = new InlineHookArm32Android();
......
...@@ -21,14 +21,14 @@ namespace SandHook { ...@@ -21,14 +21,14 @@ namespace SandHook {
class CodeBuffer { class CodeBuffer {
public: public:
virtual void* getBuffer(U32 size) = 0; virtual void* GetBuffer(U32 size) = 0;
virtual void resetLastBufferSize(U32 size){}; virtual void ResetLastBufferSize(U32 size){};
virtual void* copy(void* start, U32 size) { virtual void* copy(void* start, U32 size) {
void* bufferStart = getBuffer(size); void* bufferStart = GetBuffer(size);
if (bufferStart == nullptr) if (bufferStart == nullptr)
return nullptr; return nullptr;
memcpy(bufferStart, start, size); memcpy(bufferStart, start, size);
flushCache((Addr)bufferStart, size); FlushCache((Addr) bufferStart, size);
return bufferStart; return bufferStart;
}; };
}; };
...@@ -40,7 +40,7 @@ namespace SandHook { ...@@ -40,7 +40,7 @@ namespace SandHook {
void setCodeBuffer(CodeBuffer *codeBuffer); void setCodeBuffer(CodeBuffer *codeBuffer);
//allow code relocate to Get new pc first //allow code Relocate to Get new pc first
void allocBufferFirst(U32 size); void allocBufferFirst(U32 size);
void append(Unit<Base>* unit); void append(Unit<Base>* unit);
void commit(); void commit();
......
...@@ -20,27 +20,27 @@ namespace SandHook { ...@@ -20,27 +20,27 @@ namespace SandHook {
class CodeRelocate : public InstVisitor { class CodeRelocate : public InstVisitor {
public: public:
CodeRelocate(CodeContainer &codeContainer) : codeContainer(&codeContainer) {} CodeRelocate(CodeContainer &codeContainer) : code_container(&codeContainer) {}
virtual void* relocate(BaseInst *instruction, void* toPc) throw(ErrorCodeException) = 0; virtual void* Relocate(BaseInst *instruction, void *toPc) throw(ErrorCodeException) = 0;
virtual void* relocate(void *startPc, Addr len, void *toPc) throw(ErrorCodeException) = 0; virtual void* Relocate(void *startPc, Addr len, void *toPc) throw(ErrorCodeException) = 0;
bool inRelocateRange(Off targetOffset, Addr targetLen); bool InRelocateRange(Off targetOffset, Addr targetLen);
Label* getLaterBindLabel(Addr offset); Label* GetLaterBindLabel(Addr offset);
virtual ~CodeRelocate() { virtual ~CodeRelocate() {
delete relocateLock; delete relocate_lock;
delete laterBindlabels; delete later_bind_labels;
} }
public: public:
CodeContainer* codeContainer; CodeContainer* code_container;
std::mutex* relocateLock = new std::mutex(); std::mutex* relocate_lock = new std::mutex();
std::map<Addr, Label*>* laterBindlabels = new std::map<Addr, Label*>(); std::map<Addr, Label*>* later_bind_labels = new std::map<Addr, Label*>();
Addr startAddr = 0; Addr start_addr = 0;
Addr length = 0; Addr length = 0;
Addr curOffset = 0; Addr cur_offset = 0;
}; };
} }
......
...@@ -32,19 +32,19 @@ namespace SandHook { ...@@ -32,19 +32,19 @@ namespace SandHook {
class InstDecoder { class InstDecoder {
public: public:
virtual void Disassembler(void *codeStart, Addr codeLen, InstVisitor &visitor, virtual void Disassembler(void *code_start, Addr code_len, InstVisitor &visitor,
bool onlyPcRelInst = false) = 0; bool only_pc_rel = false) = 0;
inline void Disassembler(void* codeStart, Addr codeLen, bool (*visitor)(Unit<Base>*, void*), bool onlyPcRelInst = false) { inline void Disassembler(void* codeStart, Addr codeLen, bool (*visitor)(Unit<Base>*, void*), bool only_pc_rel = false) {
InstVisitor vis = DefaultVisitor(visitor); InstVisitor vis = DefaultVisitor(visitor);
Disassembler(codeStart, codeLen, vis, onlyPcRelInst); Disassembler(codeStart, codeLen, vis, only_pc_rel);
}; };
}; };
class Disassembler { class Disassembler {
public: public:
static InstDecoder* get(Arch arch); static InstDecoder* Get(Arch arch);
static InstDecoder* get(); static InstDecoder* Get();
}; };
} }
} }
// //
// Created by swift on 2019/5/10. // Created by swift on 2019/5/10.
// //
#pragma once
#include "exception" #include "exception"
#ifndef SANDHOOK_NH_EXCEPTION_H
#define SANDHOOK_NH_EXCEPTION_H
namespace SandHook { namespace SandHook {
namespace Asm { namespace Asm {
...@@ -25,5 +24,3 @@ namespace SandHook { ...@@ -25,5 +24,3 @@ namespace SandHook {
} }
} }
\ No newline at end of file
#endif //SANDHOOK_NH_EXCEPTION_H
...@@ -2,8 +2,7 @@ ...@@ -2,8 +2,7 @@
// Created by swift on 2019/5/14. // Created by swift on 2019/5/14.
// //
#ifndef SANDHOOK_NH_HOOK_H #pragma once
#define SANDHOOK_NH_HOOK_H
#include <mutex> #include <mutex>
...@@ -25,13 +24,11 @@ namespace SandHook { ...@@ -25,13 +24,11 @@ namespace SandHook {
return false; return false;
}; };
protected: protected:
static CodeBuffer* backupBuffer; static CodeBuffer* backup_buffer;
std::mutex hookLock; std::mutex hook_lock;
public: public:
static InlineHook* instance; static InlineHook* instance;
}; };
} }
} }
\ No newline at end of file
#endif //SANDHOOK_NH_HOOK_H
...@@ -7,21 +7,21 @@ ...@@ -7,21 +7,21 @@
using namespace SandHook::Asm; using namespace SandHook::Asm;
//in range of Copy //in range of Copy
bool CodeRelocate::inRelocateRange(Off targetOffset, Addr targetLen) { bool CodeRelocate::InRelocateRange(Off targetOffset, Addr targetLen) {
Off startP = curOffset + targetOffset; Off startP = cur_offset + targetOffset;
Off endP = startP + targetLen; Off endP = startP + targetLen;
return startP >= 0 && endP <= length; return startP >= 0 && endP <= length;
} }
Label *CodeRelocate::getLaterBindLabel(Addr offset) { Label *CodeRelocate::GetLaterBindLabel(Addr offset) {
Label* label_per_unit = nullptr; Label* label_per_unit = nullptr;
std::map<Addr,Label*>::iterator it = laterBindlabels->find(offset); std::map<Addr,Label*>::iterator it = later_bind_labels->find(offset);
if (it != laterBindlabels->end()) { if (it != later_bind_labels->end()) {
label_per_unit = it->second; label_per_unit = it->second;
} }
if (label_per_unit == nullptr) { if (label_per_unit == nullptr) {
label_per_unit = new Label(); label_per_unit = new Label();
laterBindlabels->insert(std::map<Addr, Label*>::value_type(offset, label_per_unit)); later_bind_labels->insert(std::map<Addr, Label*>::value_type(offset, label_per_unit));
} }
return label_per_unit; return label_per_unit;
} }
\ No newline at end of file
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
#include <sys/mman.h> #include <sys/mman.h>
#include "platform.h" #include "platform.h"
bool flushCache(Addr addr, Off len) { bool FlushCache(Addr addr, Off len) {
#if defined(__arm__) #if defined(__arm__)
int i = cacheflush(addr, addr + len, 0); int i = cacheflush(addr, addr + len, 0);
if (i == -1) { if (i == -1) {
...@@ -21,7 +21,7 @@ bool flushCache(Addr addr, Off len) { ...@@ -21,7 +21,7 @@ bool flushCache(Addr addr, Off len) {
} }
extern "C" bool memUnprotect(Addr addr, Addr len) { extern "C" bool MemUnprotect(Addr addr, Addr len) {
long pagesize = P_SIZE; long pagesize = P_SIZE;
unsigned alignment = (unsigned)((unsigned long long)addr % pagesize); unsigned alignment = (unsigned)((unsigned long long)addr % pagesize);
int i = mprotect((void *) (addr - alignment), (size_t) (alignment + len), int i = mprotect((void *) (addr - alignment), (size_t) (alignment + len),
......
...@@ -6,6 +6,6 @@ ...@@ -6,6 +6,6 @@
#include "base.h" #include "base.h"
extern "C" bool flushCache(Addr addr, Off len); extern "C" bool FlushCache(Addr addr, Off len);
extern "C" bool memUnprotect(Addr addr, Addr len); extern "C" bool MemUnprotect(Addr addr, Addr len);
\ No newline at end of file \ 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