Commit 6cb22ff5 authored by swift_gan's avatar swift_gan

trampoline arm64 rewrite

parent 14200bef
...@@ -67,20 +67,19 @@ Stub 中有使用 ...@@ -67,20 +67,19 @@ Stub 中有使用
#if defined(__aarch64__) #if defined(__aarch64__)
#define Reg0 x16 #define Reg0 x17
#define Reg1 x17 #define Reg1 x16
#define RegMethod x0 #define RegMethod x0
FUNCTION_START(REPLACEMENT_HOOK_TRAMPOLINE) FUNCTION_START(REPLACEMENT_HOOK_TRAMPOLINE)
ldr RegMethod, addr_art_method ldr RegMethod, addr_art_method
ldr Reg0, offset_entry_code ldr Reg0, addr_code_entry
add Reg0, RegMethod, Reg0
ldr Reg0, [Reg0] ldr Reg0, [Reg0]
br Reg0 br Reg0
addr_art_method: addr_art_method:
.long 0 .long 0
.long 0 .long 0
offset_entry_code: addr_code_entry:
.long 0 .long 0
.long 0 .long 0
FUNCTION_END(REPLACEMENT_HOOK_TRAMPOLINE) FUNCTION_END(REPLACEMENT_HOOK_TRAMPOLINE)
...@@ -95,44 +94,44 @@ addr_target: ...@@ -95,44 +94,44 @@ addr_target:
FUNCTION_END(DIRECT_JUMP_TRAMPOLINE) FUNCTION_END(DIRECT_JUMP_TRAMPOLINE)
FUNCTION_START(INLINE_HOOK_TRAMPOLINE) FUNCTION_START(INLINE_HOOK_TRAMPOLINE)
ldr Reg1, addr_origin_art_method ldr Reg0, origin_art_method
cmp RegMethod, Reg1 cmp RegMethod, Reg0
bne origin_code bne origin_code
ldr RegMethod, addr_hook_art_method ldr RegMethod, hook_art_method
ldr Reg1, offset_entry_code2 ldr Reg0, addr_hook_code_entry
add Reg1, RegMethod, Reg1 ldr Reg0, [Reg0]
ldr Reg1, [Reg1] br Reg0
br Reg1
origin_code: origin_code:
.long 0 .long 0
.long 0 .long 0
.long 0 .long 0
.long 0 .long 0
ldr Reg0, addr_origin_art_method ldr Reg0, addr_origin_code_entry
ldr Reg1, offset_entry_code2 ldr Reg0, [Reg0]
add Reg1, Reg0, Reg1 add Reg0, Reg0, SIZE_JUMP
ldr Reg1, [Reg1] br Reg0
add Reg1, Reg1, SIZE_JUMP origin_art_method:
br Reg1 .long 0
addr_origin_art_method: .long 0
addr_origin_code_entry:
.long 0 .long 0
.long 0 .long 0
offset_entry_code2: hook_art_method:
.long 0 .long 0
.long 0 .long 0
addr_hook_art_method: addr_hook_code_entry:
.long 0 .long 0
.long 0 .long 0
FUNCTION_END(INLINE_HOOK_TRAMPOLINE) FUNCTION_END(INLINE_HOOK_TRAMPOLINE)
FUNCTION_START(CALL_ORIGIN_TRAMPOLINE) FUNCTION_START(CALL_ORIGIN_TRAMPOLINE)
ldr RegMethod, origin_art_method ldr RegMethod, call_origin_art_method
ldr Reg0, addr_origin ldr Reg0, addr_call_origin_code
br Reg0 br Reg0
origin_art_method: call_origin_art_method:
.long 0 .long 0
.long 0 .long 0
addr_origin: addr_call_origin_code:
.long 0 .long 0
.long 0 .long 0
FUNCTION_END(CALL_ORIGIN_TRAMPOLINE) FUNCTION_END(CALL_ORIGIN_TRAMPOLINE)
......
...@@ -40,11 +40,9 @@ namespace SandHook { ...@@ -40,11 +40,9 @@ namespace SandHook {
public: public:
void setHookMethod(Code hookMethod) { void setHookMethod(Code hookMethod) {
codeCopy(reinterpret_cast<Code>(&hookMethod), OFFSET_REPLACEMENT_ADDR_ART_METHOD, BYTE_POINT); codeCopy(reinterpret_cast<Code>(&hookMethod), OFFSET_REPLACEMENT_ART_METHOD, BYTE_POINT);
} void* codeEntry = getEntryCodeAddr(hookMethod);
codeCopy(reinterpret_cast<Code>(&codeEntry), OFFSET_REPLACEMENT_OFFSET_CODE_ENTRY, BYTE_POINT);
void setEntryCodeOffset(Size offSet) {
codeCopy(reinterpret_cast<Code>(&offSet), OFFSET_REPLACEMENT_OFFSET_ENTRY_CODE, BYTE_POINT);
} }
protected: protected:
...@@ -61,22 +59,26 @@ namespace SandHook { ...@@ -61,22 +59,26 @@ namespace SandHook {
public: public:
void setOriginMethod(Code originMethod) { void setOriginMethod(Code originMethod) {
codeCopy(reinterpret_cast<Code>(&originMethod), OFFSET_INLINE_ADDR_ORIGIN_METHOD, BYTE_POINT); codeCopy(reinterpret_cast<Code>(&originMethod), OFFSET_INLINE_ORIGIN_ART_METHOD, BYTE_POINT);
void* codeEntry = getEntryCodeAddr(originMethod);
codeCopy(reinterpret_cast<Code>(&codeEntry), OFFSET_INLINE_ADDR_ORIGIN_CODE_ENTRY, BYTE_POINT);
} }
void setHookMethod(Code hookMethod) { void setHookMethod(Code hookMethod) {
codeCopy(reinterpret_cast<Code>(&hookMethod), OFFSET_INLINE_ADDR_HOOK_METHOD, BYTE_POINT); codeCopy(reinterpret_cast<Code>(&hookMethod), OFFSET_INLINE_HOOK_ART_METHOD, BYTE_POINT);
} void* codeEntry = getEntryCodeAddr(hookMethod);
codeCopy(reinterpret_cast<Code>(&codeEntry), OFFSET_INLINE_ADDR_HOOK_CODE_ENTRY, BYTE_POINT);
void setEntryCodeOffset(Size offSet) { }
codeCopy(reinterpret_cast<Code>(&offSet), OFFSET_INLINE_OFFSET_ENTRY_CODE, BYTE_POINT);
#if defined(__arm__) // void setEntryCodeOffset(Size offSet) {
Code32Bit offset32; // codeCopy(reinterpret_cast<Code>(&offSet), OFFSET_INLINE_OFFSET_ENTRY_CODE, BYTE_POINT);
offset32.code = offSet; // #if defined(__arm__)
unsigned char offsetOP = isBigEnd() ? offset32.op.op2 : offset32.op.op1; // Code32Bit offset32;
tweakOpImm(OFFSET_INLINE_OP_OFFSET_CODE, offsetOP); // offset32.code = offSet;
#endif // unsigned char offsetOP = isBigEnd() ? offset32.op.op2 : offset32.op.op1;
} // tweakOpImm(OFFSET_INLINE_OP_OFFSET_CODE, offsetOP);
// #endif
// }
void setOriginCode(Code originCode) { void setOriginCode(Code originCode) {
codeCopy(originCode, OFFSET_INLINE_ORIGIN_CODE, SIZE_DIRECT_JUMP_TRAMPOLINE); codeCopy(originCode, OFFSET_INLINE_ORIGIN_CODE, SIZE_DIRECT_JUMP_TRAMPOLINE);
......
...@@ -60,18 +60,19 @@ ...@@ -60,18 +60,19 @@
#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
#elif defined(__aarch64__) #elif defined(__aarch64__)
#define SIZE_REPLACEMENT_HOOK_TRAMPOLINE 4 * 9 #define SIZE_REPLACEMENT_HOOK_TRAMPOLINE 4 * 8
#define OFFSET_REPLACEMENT_ADDR_ART_METHOD 4 * 5 #define OFFSET_REPLACEMENT_ART_METHOD 4 * 4
#define OFFSET_REPLACEMENT_OFFSET_ENTRY_CODE 4 * 7 #define OFFSET_REPLACEMENT_OFFSET_CODE_ENTRY 4 * 6
#define SIZE_DIRECT_JUMP_TRAMPOLINE 4 * 4 #define SIZE_DIRECT_JUMP_TRAMPOLINE 4 * 4
#define OFFSET_JUMP_ADDR_TARGET 4 * 2 #define OFFSET_JUMP_ADDR_TARGET 4 * 2
#define SIZE_INLINE_HOOK_TRAMPOLINE 4 * 24 #define SIZE_INLINE_HOOK_TRAMPOLINE 4 * 23
#define OFFSET_INLINE_ORIGIN_CODE 4 * 8 #define OFFSET_INLINE_ORIGIN_CODE 4 * 7
#define OFFSET_INLINE_ADDR_ORIGIN_METHOD 4 * 18 #define OFFSET_INLINE_ORIGIN_ART_METHOD 4 * 15
#define OFFSET_INLINE_OFFSET_ENTRY_CODE 4 * 20 #define OFFSET_INLINE_ADDR_ORIGIN_CODE_ENTRY 4 * 17
#define OFFSET_INLINE_ADDR_HOOK_METHOD 4 * 22 #define OFFSET_INLINE_HOOK_ART_METHOD 4 * 19
#define OFFSET_INLINE_ADDR_HOOK_CODE_ENTRY 4 * 21
#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
...@@ -128,6 +129,11 @@ namespace SandHook { ...@@ -128,6 +129,11 @@ namespace SandHook {
memcpy(code, tempCode, codeLen); memcpy(code, tempCode, codeLen);
flushCache(reinterpret_cast<Size>(code), codeLen); flushCache(reinterpret_cast<Size>(code), codeLen);
} }
void setEntryCodeOffset(Size offSet) {
this->codeEntryOffSet = offSet;
}
void codeCopy(Code src, Size targetOffset, Size len) { void codeCopy(Code src, Size targetOffset, Size len) {
memcpy(reinterpret_cast<void*>((Size)code + targetOffset), src, len); memcpy(reinterpret_cast<void*>((Size)code + targetOffset), src, len);
flushCache((Size)code + targetOffset, len); flushCache((Size)code + targetOffset, len);
...@@ -194,12 +200,22 @@ namespace SandHook { ...@@ -194,12 +200,22 @@ namespace SandHook {
return reinterpret_cast<Code>(addr + 1); return reinterpret_cast<Code>(addr + 1);
} }
void* getEntryCodeAddr(void* method) {
return reinterpret_cast<void*>((Size)method + codeEntryOffSet);
}
Code getEntryCode(void* method) {
Code entryCode = *reinterpret_cast<Code*>((Size)method + codeEntryOffSet);
return entryCode;
}
protected: protected:
virtual Size codeLength() = 0; virtual Size codeLength() = 0;
virtual Code templateCode() = 0; virtual Code templateCode() = 0;
private: private:
Code tempCode; Code tempCode;
Size codeLen; Size codeLen;
Size codeEntryOffSet;
bool isThumb = false; bool isThumb = false;
}; };
......
...@@ -88,9 +88,9 @@ namespace SandHook { ...@@ -88,9 +88,9 @@ namespace SandHook {
if (inlineHookTrampolineSpace == 0) if (inlineHookTrampolineSpace == 0)
goto label_error; goto label_error;
inlineHookTrampoline->setExecuteSpace(inlineHookTrampolineSpace); inlineHookTrampoline->setExecuteSpace(inlineHookTrampolineSpace);
inlineHookTrampoline->setEntryCodeOffset(quickCompileOffset);
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);
if (inlineHookTrampoline->isThumbCode()) { if (inlineHookTrampoline->isThumbCode()) {
inlineHookTrampoline->setOriginCode(inlineHookTrampoline->getThumbCodeAddress(getEntryCode(originMethod))); inlineHookTrampoline->setOriginCode(inlineHookTrampoline->getThumbCodeAddress(getEntryCode(originMethod)));
} else { } else {
......
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