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
fc125fb0
Commit
fc125fb0
authored
Feb 15, 2019
by
swift_gan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
android p 32bit support inline hook
parent
d577c5a6
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
46 additions
and
16 deletions
+46
-16
trampoline.h
hooklib/src/main/cpp/includes/trampoline.h
+16
-11
arm32.S
hooklib/src/main/cpp/trampoline/arch/arm32.S
+4
-0
trampoline.cpp
hooklib/src/main/cpp/trampoline/trampoline.cpp
+4
-0
trampoline_manager.cpp
hooklib/src/main/cpp/trampoline/trampoline_manager.cpp
+22
-5
No files found.
hooklib/src/main/cpp/includes/trampoline.h
View file @
fc125fb0
...
@@ -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 * 1
6
#define SIZE_INLINE_HOOK_TRAMPOLINE 4 * 1
7
#define OFFSET_INLINE_ORIGIN_CODE 4 * 6
#define OFFSET_INLINE_ORIGIN_CODE 4 * 6
#define OFFSET_INLINE_ORIGIN_ART_METHOD 4 * 1
2
#define OFFSET_INLINE_ORIGIN_ART_METHOD 4 * 1
3
#define OFFSET_INLINE_ADDR_ORIGIN_CODE_ENTRY 4 * 1
3
#define OFFSET_INLINE_ADDR_ORIGIN_CODE_ENTRY 4 * 1
4
#define OFFSET_INLINE_HOOK_ART_METHOD 4 * 1
4
#define OFFSET_INLINE_HOOK_ART_METHOD 4 * 1
5
#define OFFSET_INLINE_ADDR_HOOK_CODE_ENTRY 4 * 1
5
#define OFFSET_INLINE_ADDR_HOOK_CODE_ENTRY 4 * 1
6
#define OFFSET_INLINE_OP_ORIGIN_OFFSET_CODE 4 * 1
0
#define OFFSET_INLINE_OP_ORIGIN_OFFSET_CODE 4 * 1
1
#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
{
u
nsigned
char
op1
;
u
int32_t
op1
:
8
;
u
nsigned
char
op2
;
u
int32_t
op2
:
8
;
u
nsigned
char
op3
;
u
int32_t
op3
:
8
;
u
nsigned
char
op4
;
u
int32_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
.
op
4
=
imm
;
code32Bit
.
op
.
op
2
=
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
...
...
hooklib/src/main/cpp/trampoline/arch/arm32.S
View file @
fc125fb0
...
@@ -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
...
...
hooklib/src/main/cpp/trampoline/trampoline.cpp
View file @
fc125fb0
...
@@ -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
);
}
}
...
...
hooklib/src/main/cpp/trampoline/trampoline_manager.cpp
View file @
fc125fb0
...
@@ -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
;
u
nsigned
char
offsetOP
=
callOriginTrampoline
->
isBigEnd
()
?
offset32
.
op
.
op2
:
offset32
.
op
.
op1
;
u
int8_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
...
...
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