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
dd6c7099
Commit
dd6c7099
authored
Mar 12, 2019
by
swift_gan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
teak code
parent
aefbd450
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
34 additions
and
22 deletions
+34
-22
art_method.cpp
hooklib/src/main/cpp/art/art_method.cpp
+15
-4
art_method.h
hooklib/src/main/cpp/includes/art_method.h
+1
-0
sandhook.cpp
hooklib/src/main/cpp/sandhook.cpp
+8
-11
trampoline_manager.cpp
hooklib/src/main/cpp/trampoline/trampoline_manager.cpp
+1
-1
hide_api.cpp
hooklib/src/main/cpp/utils/hide_api.cpp
+4
-4
SandHook.java
hooklib/src/main/java/com/swift/sandhook/SandHook.java
+2
-2
SandHookConfig.java
hooklib/src/main/java/com/swift/sandhook/SandHookConfig.java
+3
-0
No files found.
hooklib/src/main/cpp/art/art_method.cpp
View file @
dd6c7099
...
...
@@ -8,6 +8,7 @@
#include "../includes/utils.h"
extern
int
SDK_INT
;
extern
bool
DEBUG
;
using
namespace
art
::
mirror
;
using
namespace
SandHook
;
...
...
@@ -21,9 +22,9 @@ void ArtMethod::tryDisableInline() {
}
void
ArtMethod
::
disableInterpreterForO
()
{
uint32_t
accessFlag
=
getAccessFlags
();
accessFlag
|=
0x0100
;
setAccessFlags
(
accessFlag
);
if
(
SDK_INT
>=
ANDROID_O
&&
DEBUG
)
{
setNative
()
;
}
}
void
ArtMethod
::
disableCompilable
()
{
...
...
@@ -53,7 +54,7 @@ bool ArtMethod::isStatic() {
}
bool
ArtMethod
::
isCompiled
()
{
return
getQuickCodeEntry
()
!=
SandHook
::
CastArtMethod
::
quickToInterpreterBridge
;
return
getQuickCodeEntry
()
!=
CastArtMethod
::
quickToInterpreterBridge
&&
getQuickCodeEntry
()
!=
CastArtMethod
::
genericJniStub
;
}
bool
ArtMethod
::
isThumbCode
()
{
...
...
@@ -82,6 +83,13 @@ void ArtMethod::setStatic() {
setAccessFlags
(
accessFlag
);
};
void
ArtMethod
::
setNative
()
{
uint32_t
accessFlag
=
getAccessFlags
();
accessFlag
|=
0x0100
;
setAccessFlags
(
accessFlag
);
}
uint32_t
ArtMethod
::
getAccessFlags
()
{
return
CastArtMethod
::
accessFlag
->
get
(
this
);
}
...
...
@@ -128,6 +136,9 @@ void ArtMethod::setDeclaringClassPtr(void *classPtr) {
bool
ArtMethod
::
compile
(
JNIEnv
*
env
)
{
if
(
isCompiled
())
return
true
;
//some unknown error when trigger jit for jni method manually
if
(
isNative
())
return
false
;
Size
threadId
=
getAddressFromJavaByCallMethod
(
env
,
"com/swift/sandhook/SandHook"
,
"getThreadId"
);
if
(
threadId
==
0
)
return
false
;
...
...
hooklib/src/main/cpp/includes/art_method.h
View file @
dd6c7099
...
...
@@ -57,6 +57,7 @@ public:
void
disableInterpreterForO
();
void
setPrivate
();
void
setStatic
();
void
setNative
();
void
setQuickCodeEntry
(
void
*
entry
);
void
setJniCodeEntry
(
void
*
entry
);
...
...
hooklib/src/main/cpp/sandhook.cpp
View file @
dd6c7099
...
...
@@ -2,12 +2,12 @@
#include "includes/cast_art_method.h"
#include "includes/trampoline_manager.h"
#include "includes/hide_api.h"
#include "includes/log.h"
#include "includes/cast_compiler_options.h"
SandHook
::
TrampolineManager
trampolineManager
;
extern
"C"
int
SDK_INT
=
0
;
extern
"C"
bool
DEBUG
=
false
;
enum
HookMode
{
AUTO
=
0
,
...
...
@@ -19,10 +19,11 @@ HookMode gHookMode = AUTO;
extern
"C"
JNIEXPORT
jboolean
JNICALL
Java_com_swift_sandhook_SandHook_initNative
(
JNIEnv
*
env
,
jclass
type
,
jint
sdk
)
{
Java_com_swift_sandhook_SandHook_initNative
(
JNIEnv
*
env
,
jclass
type
,
jint
sdk
,
jboolean
debug
)
{
// TODO
SDK_INT
=
sdk
;
DEBUG
=
debug
;
SandHook
::
CastArtMethod
::
init
(
env
);
SandHook
::
CastCompilerOptions
::
init
(
env
);
trampolineManager
.
init
(
SandHook
::
CastArtMethod
::
entryPointQuickCompiled
->
getOffset
());
...
...
@@ -70,9 +71,7 @@ bool doHookWithReplacement(JNIEnv* env,
if
(
SDK_INT
>=
ANDROID_N
)
{
backupMethod
->
disableCompilable
();
}
if
(
SDK_INT
>=
ANDROID_O
)
{
backupMethod
->
disableInterpreterForO
();
}
backupMethod
->
tryDisableInline
();
if
(
!
backupMethod
->
isStatic
())
{
backupMethod
->
setPrivate
();
...
...
@@ -87,9 +86,7 @@ bool doHookWithReplacement(JNIEnv* env,
}
originMethod
->
tryDisableInline
();
if
(
SDK_INT
>=
ANDROID_O
&&
!
originMethod
->
isCompiled
())
{
originMethod
->
disableInterpreterForO
();
}
SandHook
::
HookTrampoline
*
hookTrampoline
=
trampolineManager
.
installReplacementTrampoline
(
originMethod
,
hookMethod
,
backupMethod
);
if
(
hookTrampoline
!=
nullptr
)
{
...
...
@@ -137,9 +134,7 @@ bool doHookWithInline(JNIEnv* env,
if
(
SDK_INT
>=
ANDROID_N
)
{
backupMethod
->
disableCompilable
();
}
if
(
SDK_INT
>=
ANDROID_O
)
{
backupMethod
->
disableInterpreterForO
();
}
backupMethod
->
tryDisableInline
();
if
(
!
backupMethod
->
isStatic
())
{
backupMethod
->
setPrivate
();
...
...
@@ -182,6 +177,8 @@ Java_com_swift_sandhook_SandHook_hookMethod(JNIEnv *env, jclass type, jobject or
if
(
origin
->
isAbstract
())
{
isInlineHook
=
false
;
}
else
if
(
origin
->
isNative
())
{
isInlineHook
=
SDK_INT
<
ANDROID_O
;
}
else
if
(
gHookMode
!=
AUTO
)
{
if
(
gHookMode
==
INLINE
)
{
isInlineHook
=
origin
->
compile
(
env
);
...
...
hooklib/src/main/cpp/trampoline/trampoline_manager.cpp
View file @
dd6c7099
...
...
@@ -76,7 +76,7 @@ namespace SandHook {
return
true
;
//check size
if
(
!
method
->
isNative
())
{
if
(
method
->
isCompiled
())
{
uint32_t
originCodeSize
=
sizeOfEntryCode
(
method
);
if
(
originCodeSize
<
SIZE_DIRECT_JUMP_TRAMPOLINE
)
{
LOGW
(
"can not inline due to origin code is too small(size is %d)"
,
originCodeSize
);
...
...
hooklib/src/main/cpp/utils/hide_api.cpp
View file @
dd6c7099
...
...
@@ -24,7 +24,7 @@ extern "C" {
void
initHideApi
(
JNIEnv
*
env
)
{
//init compile
if
(
SDK_INT
>=
24
)
{
if
(
SDK_INT
>=
ANDROID_N
)
{
void
*
jit_lib
;
if
(
BYTE_POINT
==
8
)
{
jit_lib
=
fake_dlopen
(
"/system/lib64/libart-compiler.so"
,
RTLD_NOW
);
...
...
@@ -51,7 +51,7 @@ extern "C" {
}
else
{
art_lib_path
=
"/system/lib/libart.so"
;
}
if
(
SDK_INT
>=
24
)
{
if
(
SDK_INT
>=
ANDROID_N
)
{
art_lib
=
fake_dlopen
(
art_lib_path
,
RTLD_NOW
);
if
(
art_lib
>
0
)
{
innerSuspendVM
=
reinterpret_cast
<
void
(
*
)()
>
(
fake_dlsym
(
art_lib
,
...
...
@@ -70,11 +70,11 @@ extern "C" {
}
//init for getObject & JitCompiler
if
(
SDK_INT
<
23
)
{
if
(
SDK_INT
<
ANDROID_M
)
{
void
*
handle
=
dlopen
(
"libart.so"
,
RTLD_LAZY
|
RTLD_GLOBAL
);
addWeakGlobalRef
=
(
jobject
(
*
)(
JavaVM
*
,
void
*
,
void
*
))
dlsym
(
handle
,
"_ZN3art9JavaVMExt22AddWeakGlobalReferenceEPNS_6ThreadEPNS_6mirror6ObjectE"
);
}
else
if
(
SDK_INT
<
24
)
{
}
else
if
(
SDK_INT
<
ANDROID_N
)
{
void
*
handle
=
dlopen
(
"libart.so"
,
RTLD_LAZY
|
RTLD_GLOBAL
);
addWeakGlobalRef
=
(
jobject
(
*
)(
JavaVM
*
,
void
*
,
void
*
))
dlsym
(
handle
,
"_ZN3art9JavaVMExt16AddWeakGlobalRefEPNS_6ThreadEPNS_6mirror6ObjectE"
);
...
...
hooklib/src/main/java/com/swift/sandhook/SandHook.java
View file @
dd6c7099
...
...
@@ -54,7 +54,7 @@ public class SandHook {
initTestOffset
();
initThreadPeer
();
SandHookMethodResolver
.
init
();
return
initNative
(
Build
.
VERSION
.
SDK_INT
);
return
initNative
(
Build
.
VERSION
.
SDK_INT
,
SandHookConfig
.
DEBUG
);
}
private
static
void
initThreadPeer
()
{
...
...
@@ -295,7 +295,7 @@ public class SandHook {
}
}
private
static
native
boolean
initNative
(
int
sdk
);
private
static
native
boolean
initNative
(
int
sdk
,
boolean
debug
);
public
static
native
void
setHookMode
(
int
hookMode
);
...
...
hooklib/src/main/java/com/swift/sandhook/SandHookConfig.java
View file @
dd6c7099
package
com
.
swift
.
sandhook
;
import
com.swift.sandhook.lib.BuildConfig
;
public
class
SandHookConfig
{
public
volatile
static
String
libSandHookPath
;
public
volatile
static
boolean
DEBUG
=
BuildConfig
.
DEBUG
;
}
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