Commit fc125fb0 authored by swift_gan's avatar swift_gan

android p 32bit support inline hook

parent d577c5a6
...@@ -48,17 +48,19 @@ ...@@ -48,17 +48,19 @@
#define SIZE_DIRECT_JUMP_TRAMPOLINE 4 * 2 #define SIZE_DIRECT_JUMP_TRAMPOLINE 4 * 2
#define OFFSET_JUMP_ADDR_TARGET 4 * 1 #define OFFSET_JUMP_ADDR_TARGET 4 * 1
#define SIZE_INLINE_HOOK_TRAMPOLINE 4 * 16 #define SIZE_INLINE_HOOK_TRAMPOLINE 4 * 17
#define OFFSET_INLINE_ORIGIN_CODE 4 * 6 #define OFFSET_INLINE_ORIGIN_CODE 4 * 6
#define OFFSET_INLINE_ORIGIN_ART_METHOD 4 * 12 #define OFFSET_INLINE_ORIGIN_ART_METHOD 4 * 13
#define OFFSET_INLINE_ADDR_ORIGIN_CODE_ENTRY 4 * 13 #define OFFSET_INLINE_ADDR_ORIGIN_CODE_ENTRY 4 * 14
#define OFFSET_INLINE_HOOK_ART_METHOD 4 * 14 #define OFFSET_INLINE_HOOK_ART_METHOD 4 * 15
#define OFFSET_INLINE_ADDR_HOOK_CODE_ENTRY 4 * 15 #define OFFSET_INLINE_ADDR_HOOK_CODE_ENTRY 4 * 16
#define OFFSET_INLINE_OP_ORIGIN_OFFSET_CODE 4 * 10 #define OFFSET_INLINE_OP_ORIGIN_OFFSET_CODE 4 * 11
#define SIZE_CALL_ORIGIN_TRAMPOLINE 4 * 4 #define SIZE_CALL_ORIGIN_TRAMPOLINE 4 * 4
#define OFFSET_CALL_ORIGIN_ART_METHOD 4 * 2 #define OFFSET_CALL_ORIGIN_ART_METHOD 4 * 2
#define OFFSET_CALL_ORIGIN_JUMP_ADDR 4 * 3 #define OFFSET_CALL_ORIGIN_JUMP_ADDR 4 * 3
#define SIZE_ORIGIN_PLACE_HOLDER 4 * 3
#elif defined(__aarch64__) #elif defined(__aarch64__)
#define SIZE_REPLACEMENT_HOOK_TRAMPOLINE 4 * 8 #define SIZE_REPLACEMENT_HOOK_TRAMPOLINE 4 * 8
#define OFFSET_REPLACEMENT_ART_METHOD 4 * 4 #define OFFSET_REPLACEMENT_ART_METHOD 4 * 4
...@@ -77,6 +79,8 @@ ...@@ -77,6 +79,8 @@
#define SIZE_CALL_ORIGIN_TRAMPOLINE 4 * 7 #define SIZE_CALL_ORIGIN_TRAMPOLINE 4 * 7
#define OFFSET_CALL_ORIGIN_ART_METHOD 4 * 3 #define OFFSET_CALL_ORIGIN_ART_METHOD 4 * 3
#define OFFSET_CALL_ORIGIN_JUMP_ADDR 4 * 5 #define OFFSET_CALL_ORIGIN_JUMP_ADDR 4 * 5
#define SIZE_ORIGIN_PLACE_HOLDER 4 * 4
#else #else
#endif #endif
...@@ -98,10 +102,10 @@ namespace SandHook { ...@@ -98,10 +102,10 @@ namespace SandHook {
union Code32Bit { union Code32Bit {
uint32_t code; uint32_t code;
struct { struct {
unsigned char op1; uint32_t op1:8;
unsigned char op2; uint32_t op2:8;
unsigned char op3; uint32_t op3:8;
unsigned char op4; uint32_t op4:8;
} op; } op;
}; };
...@@ -182,11 +186,12 @@ namespace SandHook { ...@@ -182,11 +186,12 @@ namespace SandHook {
Code32Bit code32Bit; Code32Bit code32Bit;
code32Bit.code = *reinterpret_cast<uint32_t*>(((Size)code + codeOffset)); code32Bit.code = *reinterpret_cast<uint32_t*>(((Size)code + codeOffset));
if (isBigEnd()) { if (isBigEnd()) {
code32Bit.op.op4 = imm; code32Bit.op.op2 = imm;
} else { } else {
code32Bit.op.op3 = imm; code32Bit.op.op3 = imm;
} }
codeCopy(reinterpret_cast<Code>(&code32Bit.code), codeOffset, 4); codeCopy(reinterpret_cast<Code>(&code32Bit.code), codeOffset, 4);
flushCache((Size)code + codeOffset, 4);
} }
//work for thumb //work for thumb
......
...@@ -37,6 +37,7 @@ FUNCTION_START(INLINE_HOOK_TRAMPOLINE) ...@@ -37,6 +37,7 @@ FUNCTION_START(INLINE_HOOK_TRAMPOLINE)
origin_code: origin_code:
.long 0 .long 0
.long 0 .long 0
nop
ldr Reg0, addr_origin_code_entry ldr Reg0, addr_origin_code_entry
ldr Reg0, [Reg0] ldr Reg0, [Reg0]
add Reg0, Reg0, SIZE_JUMP add Reg0, Reg0, SIZE_JUMP
...@@ -89,6 +90,9 @@ origin_code_t: ...@@ -89,6 +90,9 @@ origin_code_t:
.long 0 .long 0
//4 byte //4 byte
.long 0 .long 0
//4byte
nop
nop
//4 byte //4 byte
ldr RegT, addr_origin_code_entry_t ldr RegT, addr_origin_code_entry_t
//4 byte //4 byte
......
...@@ -84,6 +84,10 @@ namespace SandHook { ...@@ -84,6 +84,10 @@ namespace SandHook {
codeCopy(originCode, OFFSET_INLINE_ORIGIN_CODE, SIZE_DIRECT_JUMP_TRAMPOLINE); codeCopy(originCode, OFFSET_INLINE_ORIGIN_CODE, SIZE_DIRECT_JUMP_TRAMPOLINE);
} }
void setOriginCode(Code originCode, Size codeLen) {
codeCopy(originCode, OFFSET_INLINE_ORIGIN_CODE, codeLen);
}
Code getCallOriginCode() { Code getCallOriginCode() {
return reinterpret_cast<Code>((Size)getCode() + OFFSET_INLINE_ORIGIN_CODE); return reinterpret_cast<Code>((Size)getCode() + OFFSET_INLINE_ORIGIN_CODE);
} }
......
...@@ -40,7 +40,7 @@ namespace SandHook { ...@@ -40,7 +40,7 @@ namespace SandHook {
return false; return false;
} }
if (instSize > SIZE_DIRECT_JUMP_TRAMPOLINE) { if (instSize > SIZE_ORIGIN_PLACE_HOLDER) {
canSafeBackup = false; canSafeBackup = false;
} }
...@@ -48,6 +48,17 @@ namespace SandHook { ...@@ -48,6 +48,17 @@ namespace SandHook {
} }
}; };
class InstSizeNeedBackupVisitor : public InstVisitor {
public:
Size instSize = 0;
bool visit(Inst *inst, Size offset, Size length) override {
instSize += inst->instLen();
return true;
}
};
bool TrampolineManager::canSafeInline(mirror::ArtMethod *method) { bool TrampolineManager::canSafeInline(mirror::ArtMethod *method) {
//check size //check size
if (!method->isNative()) { if (!method->isNative()) {
...@@ -87,6 +98,7 @@ namespace SandHook { ...@@ -87,6 +98,7 @@ namespace SandHook {
if (mmapRes == MAP_FAILED) { if (mmapRes == MAP_FAILED) {
return 0; return 0;
} }
memset(mmapRes, 0, EXE_BLOCK_SIZE);
exeSpace = static_cast<Code>(mmapRes); exeSpace = static_cast<Code>(mmapRes);
executeSpaceList.push_back(exeSpace); executeSpaceList.push_back(exeSpace);
executePageOffset = size; executePageOffset = size;
...@@ -161,6 +173,11 @@ namespace SandHook { ...@@ -161,6 +173,11 @@ namespace SandHook {
Code inlineHookTrampolineSpace; Code inlineHookTrampolineSpace;
Code callOriginTrampolineSpace; Code callOriginTrampolineSpace;
Code originEntry; Code originEntry;
Size sizeNeedBackup = SIZE_DIRECT_JUMP_TRAMPOLINE;
InstSizeNeedBackupVisitor instVisitor;
InstDecode::decode(originMethod->getQuickCodeEntry(), SIZE_DIRECT_JUMP_TRAMPOLINE, &instVisitor);
sizeNeedBackup = instVisitor.instSize;
//生成二段跳板 //生成二段跳板
inlineHookTrampoline = new InlineHookTrampoline(); inlineHookTrampoline = new InlineHookTrampoline();
...@@ -176,9 +193,9 @@ namespace SandHook { ...@@ -176,9 +193,9 @@ 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));
if (inlineHookTrampoline->isThumbCode()) { if (inlineHookTrampoline->isThumbCode()) {
inlineHookTrampoline->setOriginCode(inlineHookTrampoline->getThumbCodeAddress(getEntryCode(originMethod))); inlineHookTrampoline->setOriginCode(inlineHookTrampoline->getThumbCodeAddress(getEntryCode(originMethod)), sizeNeedBackup);
} else { } else {
inlineHookTrampoline->setOriginCode(getEntryCode(originMethod)); inlineHookTrampoline->setOriginCode(getEntryCode(originMethod), sizeNeedBackup);
} }
hookTrampoline->inlineSecondory = inlineHookTrampoline; hookTrampoline->inlineSecondory = inlineHookTrampoline;
...@@ -216,12 +233,12 @@ namespace SandHook { ...@@ -216,12 +233,12 @@ namespace SandHook {
if (callOriginTrampoline->isThumbCode()) { 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 + sizeNeedBackup);
Size offset = originRemCode - getEntryCode(originMethod); Size offset = originRemCode - getEntryCode(originMethod);
if (offset != directJumpTrampoline->getCodeLen()) { if (offset != directJumpTrampoline->getCodeLen()) {
Code32Bit offset32; Code32Bit offset32;
offset32.code = offset; offset32.code = offset;
unsigned char offsetOP = callOriginTrampoline->isBigEnd() ? offset32.op.op2 : offset32.op.op1; uint8_t offsetOP = callOriginTrampoline->isBigEnd() ? offset32.op.op4 : offset32.op.op1;
inlineHookTrampoline->tweakOpImm(OFFSET_INLINE_OP_ORIGIN_OFFSET_CODE, offsetOP); inlineHookTrampoline->tweakOpImm(OFFSET_INLINE_OP_ORIGIN_OFFSET_CODE, offsetOP);
} }
#endif #endif
......
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