Commit e75ab9c9 authored by swift_gan's avatar swift_gan

add thumb code

parent 22ec6bcb
......@@ -125,10 +125,10 @@ Java_com_swift_sandhook_SandHook_hookMethod(JNIEnv *env, jclass type, jobject or
bool isInterpreter = SandHook::CastArtMethod::entryPointQuickCompiled->get(origin) == SandHook::CastArtMethod::quickToInterpreterBridge;
#if defined(__arm__)
doHookWithReplacement(origin, hook, backup);
return JNI_TRUE;
#endif
// #if defined(__arm__)
// doHookWithReplacement(origin, hook, backup);
// return JNI_TRUE;
// #endif
if (isInterpreter) {
if (SDK_INT >= ANDROID_N) {
......
......@@ -4,7 +4,7 @@
#define Reg0 ip
//need restore
#define Reg1 r11
#define RegT ip
#define RegMethod r0
FUNCTION_START(REPLACEMENT_HOOK_TRAMPOLINE)
......@@ -42,7 +42,8 @@ origin_code:
ldr Reg0, addr_origin_art_method
add Reg0, Reg0, #8
ldr Reg0, [Reg0]
add pc, Reg0, SIZE_JUMP
add Reg0, Reg0, SIZE_JUMP
mov pc, Reg0
addr_origin_art_method:
.long 0
offset_entry_code2:
......@@ -52,7 +53,6 @@ addr_hook_art_method:
FUNCTION_END(INLINE_HOOK_TRAMPOLINE)
FUNCTION_START(CALL_ORIGIN_TRAMPOLINE)
ldr RegMethod, origin_art_method
ldr pc, addr_origin
......@@ -62,4 +62,45 @@ addr_origin:
.long 0
FUNCTION_END(CALL_ORIGIN_TRAMPOLINE)
//thumb-2
FUNCTION_START_T(DIRECT_JUMP_TRAMPOLINE_T)
ldr pc, addr_target_t
addr_target_t:
.long 0
FUNCTION_END(DIRECT_JUMP_TRAMPOLINE_T)
FUNCTION_START_T(INLINE_HOOK_TRAMPOLINE_T)
ldr RegT, addr_origin_art_method_t
cmp RegMethod, RegT
bne origin_code_t
ldr RegMethod, addr_hook_art_method_t
ldr RegT, offset_entry_code2_t
add RegT, RegMethod, RegT
ldr pc, [RegT]
origin_code_t:
.long 0
.long 0
ldr RegT, addr_origin_art_method_t
add RegT, RegT, #8
ldr RegT, [RegT]
add RegT, RegT, SIZE_JUMP
mov pc, RegT
addr_origin_art_method_t:
.long 0
offset_entry_code2_t:
.long 0
addr_hook_art_method_t:
.long 0
FUNCTION_END(INLINE_HOOK_TRAMPOLINE_T)
FUNCTION_START_T(CALL_ORIGIN_TRAMPOLINE_T)
ldr RegMethod, origin_art_method_t
ldr pc, addr_origin_t
origin_art_method_t:
.long 0
addr_origin_t:
.long 0
FUNCTION_END(CALL_ORIGIN_TRAMPOLINE_T)
#endif
\ No newline at end of file
......@@ -11,6 +11,14 @@
.global x; \
x: \
#define FUNCTION_START_T(x) \
.syntax unified; \
.text; \
.align 4; \
.thumb_func; \
.global x; \
x: \
#define FUNCTION_END(x) .size x, .-x
#define REPLACEMENT_HOOK_TRAMPOLINE replacement_hook_trampoline
......@@ -18,4 +26,8 @@ x: \
#define DIRECT_JUMP_TRAMPOLINE direct_jump_trampoline
#define CALL_ORIGIN_TRAMPOLINE call_origin_trampoline
#define INLINE_HOOK_TRAMPOLINE_T inline_hook_trampoline_t
#define DIRECT_JUMP_TRAMPOLINE_T direct_jump_trampoline_t
#define CALL_ORIGIN_TRAMPOLINE_T call_origin_trampoline_t
#endif //SANDHOOK_BASE_H
......@@ -24,7 +24,15 @@ namespace SandHook {
}
Code templateCode() override {
#if defined(__arm__)
if (isThumCode()) {
return reinterpret_cast<Code>(DIRECT_JUMP_TRAMPOLINE_T);
} else {
return reinterpret_cast<Code>(DIRECT_JUMP_TRAMPOLINE);
}
#else
return reinterpret_cast<Code>(DIRECT_JUMP_TRAMPOLINE);
#endif
}
};
......@@ -84,7 +92,15 @@ namespace SandHook {
}
Code templateCode() override {
#if defined(__arm__)
if (isThumCode()) {
return reinterpret_cast<Code>(INLINE_HOOK_TRAMPOLINE_T);
} else {
return reinterpret_cast<Code>(INLINE_HOOK_TRAMPOLINE);
}
#else
return reinterpret_cast<Code>(INLINE_HOOK_TRAMPOLINE);
#endif
}
};
......@@ -105,7 +121,15 @@ namespace SandHook {
}
Code templateCode() override {
#if defined(__arm__)
if (isThumCode()) {
return reinterpret_cast<Code>(CALL_ORIGIN_TRAMPOLINE_T);
} else {
return reinterpret_cast<Code>(CALL_ORIGIN_TRAMPOLINE);
}
#else
return reinterpret_cast<Code>(CALL_ORIGIN_TRAMPOLINE);
#endif
}
};
......
......@@ -83,6 +83,13 @@ extern "C" void INLINE_HOOK_TRAMPOLINE();
extern "C" void REPLACEMENT_HOOK_TRAMPOLINE();
extern "C" void CALL_ORIGIN_TRAMPOLINE();
#if defined(__arm__)
extern "C" void DIRECT_JUMP_TRAMPOLINE_T();
extern "C" void INLINE_HOOK_TRAMPOLINE_T();
extern "C" void CALL_ORIGIN_TRAMPOLINE_T();
#endif
namespace SandHook {
//deal with little or big edn
......@@ -105,6 +112,15 @@ namespace SandHook {
codeLen = codeLength();
tempCode = templateCode();
}
void setThumb(bool thumb) {
isThumb = thumb;
}
bool isThumCode() {
return isThumb;
}
void setExecuteSpace(Code start) {
code = start;
memcpy(code, tempCode, codeLen);
......@@ -133,9 +149,11 @@ namespace SandHook {
void clone(Code dest) {
memcpy(dest, code, codeLen);
}
Code getCode() {
return code;
}
Size getCodeLen() {
return codeLen;
}
......@@ -166,6 +184,7 @@ namespace SandHook {
Code code;
Code tempCode;
Size codeLen;
bool isThumb = false;
};
}
......
......@@ -80,7 +80,10 @@ namespace SandHook {
Code originEntry;
//生成二段跳板
inlineHookTrampoline = new InlineHookTrampoline;
inlineHookTrampoline = new InlineHookTrampoline();
#if defined(__arm__)
checkThumbCode(inlineHookTrampoline, getEntryCode(originMethod));
#endif
inlineHookTrampoline->init();
inlineHookTrampolineSpace = allocExecuteSpace(inlineHookTrampoline->getCodeLen());
if (inlineHookTrampolineSpace == 0)
......@@ -94,6 +97,9 @@ namespace SandHook {
//注入 EntryCode
directJumpTrampoline = new DirectJumpTrampoline();
#if defined(__arm__)
checkThumbCode(directJumpTrampoline, getEntryCode(originMethod));
#endif
directJumpTrampoline->init();
originEntry = getEntryCode(originMethod);
if (!memUnprotect(reinterpret_cast<Size>(originEntry), directJumpTrampoline->getCodeLen())) {
......@@ -106,6 +112,9 @@ namespace SandHook {
//备份原始方法
if (backupMethod != nullptr) {
callOriginTrampoline = new CallOriginTrampoline();
#if defined(__arm__)
checkThumbCode(callOriginTrampoline, getEntryCode(originMethod));
#endif
callOriginTrampoline->init();
callOriginTrampolineSpace = allocExecuteSpace(callOriginTrampoline->getCodeLen());
if (callOriginTrampolineSpace == 0)
......
......@@ -63,6 +63,16 @@ namespace SandHook {
return entryCode;
}
bool isThumbCode(Size codeAddr) {
return (codeAddr & (Size)1) == (Size)1;
}
void checkThumbCode(Trampoline* trampoline, Code code) {
#if defined(__arm__)
trampoline->setThumb(isThumbCode(reinterpret_cast<Size>(code)));
#endif
}
private:
......
......@@ -56,10 +56,10 @@ public class SandHook {
}
private static void resolveBackupMethod(Method method) {
//ignore result, just call to trigger resolve
try {
method.invoke(null, null);
} catch (Exception e) {
e.printStackTrace();
}
}
......
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