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
feeab481
Commit
feeab481
authored
Feb 17, 2019
by
swift_gan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
stop the world when hooking
parent
237274b7
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
54 additions
and
4 deletions
+54
-4
hide_api.h
hooklib/src/main/cpp/includes/hide_api.h
+4
-1
sandhook.cpp
hooklib/src/main/cpp/sandhook.cpp
+9
-1
hide_api.cpp
hooklib/src/main/cpp/utils/hide_api.cpp
+34
-2
lock.h
hooklib/src/main/cpp/utils/lock.h
+7
-0
No files found.
hooklib/src/main/cpp/includes/hide_api.h
View file @
feeab481
...
@@ -11,9 +11,12 @@
...
@@ -11,9 +11,12 @@
extern
"C"
{
extern
"C"
{
void
initHideApi
(
JNIEnv
*
env
,
int
SDK_VERSION
);
void
initHideApi
(
JNIEnv
*
env
);
bool
compileMethod
(
void
*
artMethod
,
void
*
thread
);
bool
compileMethod
(
void
*
artMethod
,
void
*
thread
);
void
suspendVM
();
void
resumeVM
();
}
}
#endif //SANDHOOK_HIDE_API_H
#endif //SANDHOOK_HIDE_API_H
hooklib/src/main/cpp/sandhook.cpp
View file @
feeab481
...
@@ -24,7 +24,7 @@ Java_com_swift_sandhook_SandHook_initNative(JNIEnv *env, jclass type, jint sdk)
...
@@ -24,7 +24,7 @@ Java_com_swift_sandhook_SandHook_initNative(JNIEnv *env, jclass type, jint sdk)
SDK_INT
=
sdk
;
SDK_INT
=
sdk
;
SandHook
::
CastArtMethod
::
init
(
env
);
SandHook
::
CastArtMethod
::
init
(
env
);
trampolineManager
.
init
(
SandHook
::
CastArtMethod
::
entryPointQuickCompiled
->
getOffset
());
trampolineManager
.
init
(
SandHook
::
CastArtMethod
::
entryPointQuickCompiled
->
getOffset
());
initHideApi
(
env
,
sdk
);
initHideApi
(
env
);
return
JNI_TRUE
;
return
JNI_TRUE
;
}
}
...
@@ -32,6 +32,9 @@ Java_com_swift_sandhook_SandHook_initNative(JNIEnv *env, jclass type, jint sdk)
...
@@ -32,6 +32,9 @@ Java_com_swift_sandhook_SandHook_initNative(JNIEnv *env, jclass type, jint sdk)
void
ensureMethodCached
(
art
::
mirror
::
ArtMethod
*
hookMethod
,
art
::
mirror
::
ArtMethod
*
backupMethod
)
{
void
ensureMethodCached
(
art
::
mirror
::
ArtMethod
*
hookMethod
,
art
::
mirror
::
ArtMethod
*
backupMethod
)
{
if
(
SDK_INT
>=
ANDROID_P
)
if
(
SDK_INT
>=
ANDROID_P
)
return
;
return
;
SandHook
::
StopTheWorld
stopTheWorld
;
uint32_t
index
=
backupMethod
->
getDexMethodIndex
();
uint32_t
index
=
backupMethod
->
getDexMethodIndex
();
if
(
SDK_INT
<
ANDROID_O2
)
{
if
(
SDK_INT
<
ANDROID_O2
)
{
hookMethod
->
setDexCacheResolveItem
(
index
,
backupMethod
);
hookMethod
->
setDexCacheResolveItem
(
index
,
backupMethod
);
...
@@ -61,6 +64,8 @@ void ensureMethodDeclaringClass(art::mirror::ArtMethod *origin, art::mirror::Art
...
@@ -61,6 +64,8 @@ void ensureMethodDeclaringClass(art::mirror::ArtMethod *origin, art::mirror::Art
if
(
trampoline
==
nullptr
)
if
(
trampoline
==
nullptr
)
return
;
return
;
SandHook
::
StopTheWorld
stopTheWorld
;
if
(
trampoline
->
inlineJump
!=
nullptr
)
{
if
(
trampoline
->
inlineJump
!=
nullptr
)
{
origin
->
backup
(
backup
);
origin
->
backup
(
backup
);
backup
->
setQuickCodeEntry
(
trampoline
->
callOrigin
->
getCode
());
backup
->
setQuickCodeEntry
(
trampoline
->
callOrigin
->
getCode
());
...
@@ -197,6 +202,9 @@ Java_com_swift_sandhook_SandHook_hookMethod(JNIEnv *env, jclass type, jobject or
...
@@ -197,6 +202,9 @@ Java_com_swift_sandhook_SandHook_hookMethod(JNIEnv *env, jclass type, jobject or
int
mode
=
reinterpret_cast
<
int
>
(
hookMode
);
int
mode
=
reinterpret_cast
<
int
>
(
hookMode
);
//suspend other threads
SandHook
::
StopTheWorld
stopTheWorld
;
if
(
mode
==
INLINE
)
{
if
(
mode
==
INLINE
)
{
if
(
!
origin
->
isCompiled
())
{
if
(
!
origin
->
isCompiled
())
{
if
(
SDK_INT
>=
ANDROID_N
)
{
if
(
SDK_INT
>=
ANDROID_N
)
{
...
...
hooklib/src/main/cpp/utils/hide_api.cpp
View file @
feeab481
...
@@ -3,6 +3,8 @@
...
@@ -3,6 +3,8 @@
//
//
#include "../includes/hide_api.h"
#include "../includes/hide_api.h"
extern
int
SDK_INT
;
extern
"C"
{
extern
"C"
{
...
@@ -10,8 +12,13 @@ extern "C" {
...
@@ -10,8 +12,13 @@ extern "C" {
void
*
jitCompilerHandle
=
nullptr
;
void
*
jitCompilerHandle
=
nullptr
;
bool
(
*
jitCompileMethod
)(
void
*
,
void
*
,
void
*
,
bool
)
=
nullptr
;
bool
(
*
jitCompileMethod
)(
void
*
,
void
*
,
void
*
,
bool
)
=
nullptr
;
void
initHideApi
(
JNIEnv
*
env
,
int
SDK_VERSION
)
{
void
(
*
innerSuspendVM
)()
=
nullptr
;
if
(
SDK_VERSION
>=
24
)
{
void
(
*
innerResumeVM
)()
=
nullptr
;
void
initHideApi
(
JNIEnv
*
env
)
{
//init compile
if
(
SDK_INT
>=
24
)
{
void
*
jit_lib
;
void
*
jit_lib
;
if
(
sizeof
(
void
*
)
==
8
)
{
if
(
sizeof
(
void
*
)
==
8
)
{
jit_lib
=
fake_dlopen
(
"/system/lib64/libart-compiler.so"
,
RTLD_NOW
);
jit_lib
=
fake_dlopen
(
"/system/lib64/libart-compiler.so"
,
RTLD_NOW
);
...
@@ -23,6 +30,19 @@ extern "C" {
...
@@ -23,6 +30,19 @@ extern "C" {
bool
generate_debug_info
=
false
;
bool
generate_debug_info
=
false
;
jitCompilerHandle
=
(
jitLoad
)(
&
generate_debug_info
);
jitCompilerHandle
=
(
jitLoad
)(
&
generate_debug_info
);
}
}
//init suspend
void
*
art_lib
;
const
char
*
art_lib_path
;
if
(
sizeof
(
void
*
)
==
8
)
{
art_lib_path
=
"/system/lib64/libart.so"
;
}
else
{
art_lib_path
=
"/system/lib/libart.so"
;
}
if
(
SDK_INT
>=
24
)
{
art_lib
=
fake_dlopen
(
art_lib_path
,
RTLD_NOW
);
innerSuspendVM
=
reinterpret_cast
<
void
(
*
)()
>
(
fake_dlsym
(
art_lib
,
"_ZN3art3Dbg9SuspendVMEv"
));
innerResumeVM
=
reinterpret_cast
<
void
(
*
)()
>
(
fake_dlsym
(
art_lib
,
"_ZN3art3Dbg8ResumeVMEv"
));
}
}
}
bool
compileMethod
(
void
*
artMethod
,
void
*
thread
)
{
bool
compileMethod
(
void
*
artMethod
,
void
*
thread
)
{
...
@@ -32,5 +52,17 @@ extern "C" {
...
@@ -32,5 +52,17 @@ extern "C" {
return
jitCompileMethod
(
jitCompilerHandle
,
artMethod
,
thread
,
false
);
return
jitCompileMethod
(
jitCompilerHandle
,
artMethod
,
thread
,
false
);
}
}
void
suspendVM
()
{
if
(
innerSuspendVM
==
nullptr
||
innerResumeVM
==
nullptr
)
return
;
innerSuspendVM
();
}
void
resumeVM
()
{
if
(
innerSuspendVM
==
nullptr
||
innerResumeVM
==
nullptr
)
return
;
innerResumeVM
();
}
}
}
hooklib/src/main/cpp/utils/lock.h
View file @
feeab481
...
@@ -6,6 +6,7 @@
...
@@ -6,6 +6,7 @@
#define SANDHOOK_LOCK_H
#define SANDHOOK_LOCK_H
#include "mutex"
#include "mutex"
#include "../includes/hide_api.h"
namespace
SandHook
{
namespace
SandHook
{
...
@@ -18,6 +19,12 @@ namespace SandHook {
...
@@ -18,6 +19,12 @@ namespace SandHook {
std
::
mutex
&
mLock
;
std
::
mutex
&
mLock
;
};
};
class
StopTheWorld
{
public
:
inline
StopTheWorld
()
{
suspendVM
();
}
inline
~
StopTheWorld
()
{
resumeVM
();
}
};
}
}
#endif //SANDHOOK_LOCK_H
#endif //SANDHOOK_LOCK_H
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