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
90da813a
Commit
90da813a
authored
Mar 14, 2019
by
swift_gan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix get jni trampoline
parent
83470325
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
72 additions
and
28 deletions
+72
-28
art_method.cpp
hooklib/src/main/cpp/art/art_method.cpp
+9
-3
cast_art_method.cpp
hooklib/src/main/cpp/casts/cast_art_method.cpp
+30
-7
cast_art_method.h
hooklib/src/main/cpp/includes/cast_art_method.h
+2
-1
elf_util.h
hooklib/src/main/cpp/includes/elf_util.h
+4
-1
sandhook.cpp
hooklib/src/main/cpp/sandhook.cpp
+8
-1
elf_util.cpp
hooklib/src/main/cpp/utils/elf_util.cpp
+13
-3
hide_api.cpp
hooklib/src/main/cpp/utils/hide_api.cpp
+5
-12
ClassNeverCall.java
hooklib/src/main/java/com/swift/sandhook/ClassNeverCall.java
+1
-0
No files found.
hooklib/src/main/cpp/art/art_method.cpp
View file @
90da813a
...
...
@@ -148,10 +148,16 @@ bool ArtMethod::compile(JNIEnv* env) {
bool
ArtMethod
::
deCompile
()
{
if
(
!
isCompiled
())
return
true
;
if
(
CastArtMethod
::
beAot
)
if
((
isNative
()
&&
CastArtMethod
::
canGetJniBridge
)
||
(
!
isNative
()
&&
CastArtMethod
::
canGetInterpreterBridge
))
{
setQuickCodeEntry
(
isNative
()
?
CastArtMethod
::
genericJniStub
:
CastArtMethod
::
quickToInterpreterBridge
);
if
(
SDK_INT
<
ANDROID_N
)
{
//TODO SetEntryPointFromInterpreterCode
}
flushCache
();
return
true
;
}
else
{
return
false
;
setQuickCodeEntry
(
isNative
()
?
CastArtMethod
::
genericJniStub
:
CastArtMethod
::
quickToInterpreterBridge
);
flushCache
();
}
}
void
ArtMethod
::
flushCache
()
{
...
...
hooklib/src/main/cpp/casts/cast_art_method.cpp
View file @
90da813a
...
...
@@ -180,23 +180,45 @@ namespace SandHook {
declaringClass
->
init
(
env
,
m1
,
size
);
jclass
neverCallTestClass
=
env
->
FindClass
(
"com/swift/sandhook/ClassNeverCall"
);
art
::
mirror
::
ArtMethod
*
neverCall
=
reinterpret_cast
<
art
::
mirror
::
ArtMethod
*>
(
env
->
GetMethodID
(
neverCallTestClass
,
"neverCall"
,
"()V"
));
art
::
mirror
::
ArtMethod
*
neverCall2
=
reinterpret_cast
<
art
::
mirror
::
ArtMethod
*>
(
env
->
GetMethodID
(
neverCallTestClass
,
"neverCall2"
,
"()V"
));
beAot
=
entryPointQuickCompiled
->
get
(
neverCall
)
!=
entryPointQuickCompiled
->
get
(
neverCall2
);
bool
beAot
=
entryPointQuickCompiled
->
get
(
neverCall
)
!=
entryPointQuickCompiled
->
get
(
neverCall2
);
if
(
beAot
)
{
quickToInterpreterBridge
=
getInterpreterBridge
(
false
);
if
(
quickToInterpreterBridge
==
nullptr
||
quickToInterpreterBridge
<=
0
)
{
quickToInterpreterBridge
=
entryPointQuickCompiled
->
get
(
neverCall
);
canGetInterpreterBridge
=
false
;
}
}
else
{
quickToInterpreterBridge
=
entryPointQuickCompiled
->
get
(
neverCall
);
}
quickToInterpreterBridge
=
entryPointQuickCompiled
->
get
(
neverCall
);
art
::
mirror
::
ArtMethod
*
neverCallNative
=
reinterpret_cast
<
art
::
mirror
::
ArtMethod
*>
(
env
->
GetMethodID
(
neverCallTestClass
,
"neverCallNative"
,
"()V"
));
art
::
mirror
::
ArtMethod
*
neverCallNative2
=
reinterpret_cast
<
art
::
mirror
::
ArtMethod
*>
(
env
->
GetMethodID
(
neverCallTestClass
,
"neverCallNative2"
,
"()V"
));
beAot
=
entryPointQuickCompiled
->
get
(
neverCallNative
)
!=
entryPointQuickCompiled
->
get
(
neverCallNative2
);
if
(
beAot
)
{
genericJniStub
=
getInterpreterBridge
(
true
);
if
(
genericJniStub
==
nullptr
||
genericJniStub
<=
0
)
{
genericJniStub
=
entryPointQuickCompiled
->
get
(
neverCallNative
);
canGetJniBridge
=
false
;
}
}
else
{
genericJniStub
=
entryPointQuickCompiled
->
get
(
neverCallNative
);
}
art
::
mirror
::
ArtMethod
*
neverCallStatic
=
reinterpret_cast
<
art
::
mirror
::
ArtMethod
*>
(
env
->
GetStaticMethodID
(
neverCallTestClass
,
"neverCallStatic"
,
"()V"
));
staticResolveStub
=
entryPointQuickCompiled
->
get
(
neverCallStatic
);
art
::
mirror
::
ArtMethod
*
neverCallNative
=
reinterpret_cast
<
art
::
mirror
::
ArtMethod
*>
(
env
->
GetMethodID
(
neverCallTestClass
,
"neverCallNative"
,
"()V"
));
genericJniStub
=
entryPointQuickCompiled
->
get
(
neverCallNative
);
}
void
CastArtMethod
::
copy
(
art
::
mirror
::
ArtMethod
*
from
,
art
::
mirror
::
ArtMethod
*
to
)
{
...
...
@@ -213,6 +235,7 @@ namespace SandHook {
void
*
CastArtMethod
::
quickToInterpreterBridge
=
nullptr
;
void
*
CastArtMethod
::
genericJniStub
=
nullptr
;
void
*
CastArtMethod
::
staticResolveStub
=
nullptr
;
bool
CastArtMethod
::
beAot
=
false
;
bool
CastArtMethod
::
canGetInterpreterBridge
=
true
;
bool
CastArtMethod
::
canGetJniBridge
=
true
;
}
\ No newline at end of file
hooklib/src/main/cpp/includes/cast_art_method.h
View file @
90da813a
...
...
@@ -22,7 +22,8 @@ namespace SandHook {
static
void
*
quickToInterpreterBridge
;
static
void
*
genericJniStub
;
static
void
*
staticResolveStub
;
static
bool
beAot
;
static
bool
canGetJniBridge
;
static
bool
canGetInterpreterBridge
;
static
void
init
(
JNIEnv
*
env
);
static
void
copy
(
art
::
mirror
::
ArtMethod
*
from
,
art
::
mirror
::
ArtMethod
*
to
);
...
...
hooklib/src/main/cpp/includes/elf_util.h
View file @
90da813a
//
// Created by Swift Gan on 2019/3/14.
//
#ifndef SANDHOOK_ELF_UTIL_H
#define SANDHOOK_ELF_UTIL_H
...
...
@@ -45,7 +48,7 @@ namespace SandHook {
void
*
base
=
nullptr
;
char
*
buffer
=
nullptr
;
off_t
size
=
0
;
off_t
bias
=
0
;
off_t
bias
=
-
4396
;
Elf_Ehdr
*
header
=
nullptr
;
Elf_Shdr
*
section_header
=
nullptr
;
Elf_Shdr
*
symtab
=
nullptr
;
...
...
hooklib/src/main/cpp/sandhook.cpp
View file @
90da813a
...
...
@@ -24,10 +24,10 @@ Java_com_swift_sandhook_SandHook_initNative(JNIEnv *env, jclass type, jint sdk,
// TODO
SDK_INT
=
sdk
;
DEBUG
=
debug
;
initHideApi
(
env
);
SandHook
::
CastArtMethod
::
init
(
env
);
SandHook
::
CastCompilerOptions
::
init
(
env
);
trampolineManager
.
init
(
SandHook
::
CastArtMethod
::
entryPointQuickCompiled
->
getOffset
());
initHideApi
(
env
);
return
JNI_TRUE
;
}
...
...
@@ -327,4 +327,11 @@ Java_com_swift_sandhook_ClassNeverCall_neverCallNative(JNIEnv *env, jobject inst
int
a
=
1
+
1
;
int
b
=
a
+
1
;
}
extern
"C"
JNIEXPORT
void
JNICALL
Java_com_swift_sandhook_ClassNeverCall_neverCallNative2
(
JNIEnv
*
env
,
jobject
instance
)
{
int
a
=
4
+
3
;
int
b
=
9
+
6
;
}
\ No newline at end of file
hooklib/src/main/cpp/utils/elf_util.cpp
View file @
90da813a
//
// Created by Swift Gan on 2019/3/14.
//
#include <malloc.h>
#include <string.h>
#include <sys/mman.h>
...
...
@@ -12,10 +15,15 @@ ElfImg::ElfImg(const char *elf) {
this
->
elf
=
elf
;
//load elf
int
fd
=
open
(
elf
,
O_RDONLY
);
if
(
fd
<
0
)
LOGE
(
"failed to open %s"
,
elf
);
if
(
fd
<
0
)
{
LOGE
(
"failed to open %s"
,
elf
);
return
;
}
size
=
lseek
(
fd
,
0
,
SEEK_END
);
if
(
size
<=
0
)
LOGE
(
"lseek() failed for %s"
,
elf
);
if
(
size
<=
0
)
{
LOGE
(
"lseek() failed for %s"
,
elf
);
}
header
=
reinterpret_cast
<
Elf_Ehdr
*>
(
mmap
(
0
,
size
,
PROT_READ
,
MAP_SHARED
,
fd
,
0
));
...
...
@@ -56,7 +64,7 @@ ElfImg::ElfImg(const char *elf) {
}
break
;
case
SHT_PROGBITS
:
if
(
has_dynsym
&&
has_strtab
&&
bias
==
0
)
{
if
(
has_dynsym
&&
has_strtab
&&
bias
==
-
4396
)
{
bias
=
(
off_t
)
section_h
->
sh_addr
-
(
off_t
)
section_h
->
sh_offset
;
}
break
;
...
...
@@ -74,10 +82,12 @@ ElfImg::ElfImg(const char *elf) {
}
ElfImg
::~
ElfImg
()
{
//open elf file local
if
(
buffer
)
{
free
(
buffer
);
buffer
=
nullptr
;
}
//use mmap
if
(
header
)
{
munmap
(
header
,
size
);
}
...
...
hooklib/src/main/cpp/utils/hide_api.cpp
View file @
90da813a
...
...
@@ -23,15 +23,13 @@ extern "C" {
void
*
(
*
get
)(
bool
*
)
=
nullptr
;
void
*
(
*
getQuickToInterpreterBridge
)(
void
*
)
=
nullptr
;
void
*
(
*
getQuickGenericJniStub
)(
void
*
)
=
nullptr
;
const
char
*
art_lib_path
;
const
char
*
jit_lib_path
;
void
initHideApi
(
JNIEnv
*
env
)
{
const
char
*
art_lib_path
;
const
char
*
jit_lib_path
;
if
(
BYTE_POINT
==
8
)
{
art_lib_path
=
"/system/lib64/libart.so"
;
jit_lib_path
=
"/system/lib64/libart-compiler.so"
;
...
...
@@ -81,9 +79,6 @@ extern "C" {
if
(
SDK_INT
>=
ANDROID_N
)
{
globalJitCompileHandlerAddr
=
reinterpret_cast
<
art
::
jit
::
JitCompiler
**>
(
getSymCompat
(
art_lib_path
,
"_ZN3art3jit3Jit20jit_compiler_handle_E"
));
getQuickGenericJniStub
=
reinterpret_cast
<
void
*
(
*
)(
void
*
)
>
(
getSymCompat
(
art_lib_path
,
"_ZNK3art11ClassLinker29GetRuntimeQuickGenericJniStubEv"
));
getQuickToInterpreterBridge
=
reinterpret_cast
<
void
*
(
*
)(
void
*
)
>
(
getSymCompat
(
art_lib_path
,
"_ZNK3art9OatHeader27GetQuickToInterpreterBridgeEv"
));
SandHook
::
ElfImg
elfImg
(
art_lib_path
);
void
*
sym
=
reinterpret_cast
<
void
*>
(
elfImg
.
getSymbAddress
(
"art_quick_to_interpreter_bridge"
));
...
...
@@ -166,13 +161,11 @@ extern "C" {
}
void
*
getInterpreterBridge
(
bool
isNative
)
{
SandHook
::
ElfImg
libart
(
art_lib_path
);
if
(
isNative
)
{
if
(
getQuickGenericJniStub
==
nullptr
||
getQuickGenericJniStub
<=
0
)
return
nullptr
;
return
getQuickGenericJniStub
(
nullptr
);
return
reinterpret_cast
<
void
*>
(
libart
.
getSymbAddress
(
"art_quick_generic_jni_trampoline"
));
}
else
{
//no implement
return
nullptr
;
return
reinterpret_cast
<
void
*>
(
libart
.
getSymbAddress
(
"art_quick_to_interpreter_bridge"
));
}
}
...
...
hooklib/src/main/java/com/swift/sandhook/ClassNeverCall.java
View file @
90da813a
...
...
@@ -9,4 +9,5 @@ public class ClassNeverCall {
}
private
static
void
neverCallStatic
()
{}
private
native
void
neverCallNative
();
private
native
void
neverCallNative2
();
}
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