Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
S
SandHook
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
SandHook
Commits
6cb22ff5
Commit
6cb22ff5
authored
Jan 26, 2019
by
swift_gan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
trampoline arm64 rewrite
parent
14200bef
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
69 additions
and
52 deletions
+69
-52
arm64.S
hooklib/src/main/cpp/trampoline/arch/arm64.S
+24
-25
trampoline.cpp
hooklib/src/main/cpp/trampoline/trampoline.cpp
+20
-18
trampoline.h
hooklib/src/main/cpp/trampoline/trampoline.h
+24
-8
trampoline_manager.cpp
hooklib/src/main/cpp/trampoline/trampoline_manager.cpp
+1
-1
No files found.
hooklib/src/main/cpp/trampoline/arch/arm64.S
View file @
6cb22ff5
...
@@ -67,20 +67,19 @@ Stub 中有使用
...
@@ -67,20 +67,19 @@ Stub 中有使用
#if defined(__aarch64__)
#if defined(__aarch64__)
#define Reg0 x1
6
#define Reg0 x1
7
#define Reg1 x1
7
#define Reg1 x1
6
#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 Reg
1, addr_
origin_art_method
ldr Reg
0,
origin_art_method
cmp RegMethod, Reg
1
cmp RegMethod, Reg
0
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)
...
...
hooklib/src/main/cpp/trampoline/trampoline.cpp
View file @
6cb22ff5
...
@@ -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
);
...
...
hooklib/src/main/cpp/trampoline/trampoline.h
View file @
6cb22ff5
...
@@ -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_A
DDR_ART_METHOD 4 * 5
#define OFFSET_REPLACEMENT_A
RT_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
;
};
};
...
...
hooklib/src/main/cpp/trampoline/trampoline_manager.cpp
View file @
6cb22ff5
...
@@ -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
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment