Skip to content

Commit 331f913

Browse files
author
swift_gan
committedMar 13, 2019
tweak code
1 parent 891bd37 commit 331f913

File tree

11 files changed

+121
-51
lines changed

11 files changed

+121
-51
lines changed
 

‎hooklib/src/main/cpp/art/art_method.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,15 @@ bool ArtMethod::compile(JNIEnv* env) {
145145
return compileMethod(this, reinterpret_cast<void *>(threadId)) && isCompiled();
146146
}
147147

148+
bool ArtMethod::deCompile() {
149+
if (!isCompiled())
150+
return true;
151+
if (CastArtMethod::beAot)
152+
return false;
153+
setQuickCodeEntry(isNative() ? CastArtMethod::genericJniStub : CastArtMethod::quickToInterpreterBridge);
154+
flushCache();
155+
}
156+
148157
void ArtMethod::flushCache() {
149158
flushCacheExt(reinterpret_cast<Size>(this), size());
150159
}

‎hooklib/src/main/cpp/casts/cast_art_method.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,11 @@ namespace SandHook {
182182
jclass neverCallTestClass = env->FindClass("com/swift/sandhook/ClassNeverCall");
183183
art::mirror::ArtMethod *neverCall = reinterpret_cast<art::mirror::ArtMethod *>(env->GetMethodID(
184184
neverCallTestClass, "neverCall", "()V"));
185+
art::mirror::ArtMethod *neverCall2 = reinterpret_cast<art::mirror::ArtMethod *>(env->GetMethodID(
186+
neverCallTestClass, "neverCall2", "()V"));
187+
188+
beAot = entryPointQuickCompiled->get(neverCall) != entryPointQuickCompiled->get(neverCall2);
189+
185190
quickToInterpreterBridge = entryPointQuickCompiled->get(neverCall);
186191

187192
art::mirror::ArtMethod *neverCallStatic = reinterpret_cast<art::mirror::ArtMethod *>(env->GetStaticMethodID(
@@ -208,5 +213,6 @@ namespace SandHook {
208213
void *CastArtMethod::quickToInterpreterBridge = nullptr;
209214
void *CastArtMethod::genericJniStub = nullptr;
210215
void *CastArtMethod::staticResolveStub = nullptr;
216+
bool CastArtMethod::beAot = false;
211217

212218
}

‎hooklib/src/main/cpp/includes/art_method.h

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class ArtMethod {
7373
void* getDeclaringClassPtr();
7474

7575
bool compile(JNIEnv* env);
76+
bool deCompile();
7677
void flushCache();
7778
void backup(ArtMethod* backup);
7879

‎hooklib/src/main/cpp/includes/cast_art_method.h

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ namespace SandHook {
2222
static void* quickToInterpreterBridge;
2323
static void* genericJniStub;
2424
static void* staticResolveStub;
25+
static bool beAot;
2526

2627
static void init(JNIEnv *env);
2728
static void copy(art::mirror::ArtMethod* from, art::mirror::ArtMethod* to);

‎hooklib/src/main/cpp/includes/dlfcn_nougat.h

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ void *fake_dlopen(const char *filename, int flags);
1010
void *fake_dlsym(void *handle, const char *name);
1111

1212
const char *fake_dlerror();
13+
14+
void *getSymCompat(const char *filename, const char *name);
1315
}
1416

1517
#endif //DLFCN_NOUGAT_H

‎hooklib/src/main/cpp/includes/hide_api.h

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ extern "C" {
3131

3232
bool disableJitInline(art::CompilerOptions* compilerOptions);
3333

34+
void* getInterpreterBridge(bool isNative);
35+
3436
}
3537

3638
#endif //SANDHOOK_HIDE_API_H

‎hooklib/src/main/cpp/sandhook.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,29 @@ Java_com_swift_sandhook_SandHook_compileMethod(JNIEnv *env, jclass type, jobject
242242

243243
}
244244

245+
extern "C"
246+
JNIEXPORT jboolean JNICALL
247+
Java_com_swift_sandhook_SandHook_deCompileMethod(JNIEnv *env, jclass type, jobject member) {
248+
249+
if (member == NULL)
250+
return JNI_FALSE;
251+
art::mirror::ArtMethod* method = reinterpret_cast<art::mirror::ArtMethod *>(env->FromReflectedMethod(member));
252+
253+
if (method == nullptr)
254+
return JNI_FALSE;
255+
256+
if (method->isCompiled()) {
257+
SandHook::StopTheWorld stopTheWorld;
258+
if (SDK_INT >= ANDROID_N) {
259+
method->disableCompilable();
260+
}
261+
return static_cast<jboolean>(method->deCompile());
262+
} else {
263+
return JNI_TRUE;
264+
}
265+
266+
}
267+
245268
extern "C"
246269
JNIEXPORT jobject JNICALL
247270
Java_com_swift_sandhook_SandHook_getObjectNative(JNIEnv *env, jclass type, jlong thread,

‎hooklib/src/main/cpp/utils/dlfcn_nougat.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
#include <sys/mman.h>
3131
#include <elf.h>
3232
#include <android/log.h>
33+
#include <dlfcn.h>
34+
#include "../includes/arch.h"
3335

3436
#define TAG_NAME "nougat_dlfcn"
3537

@@ -52,6 +54,8 @@
5254
#define Elf_Sym Elf32_Sym
5355
#endif
5456

57+
extern int SDK_INT;
58+
5559

5660
struct ctx {
5761
void *load_addr;
@@ -237,4 +241,20 @@ void *fake_dlsym(void *handle, const char *name) {
237241
const char *fake_dlerror() {
238242
return NULL;
239243
}
244+
245+
246+
void *getSymCompat(const char *filename, const char *name) {
247+
if (SDK_INT >= ANDROID_N) {
248+
void* handle = fake_dlopen(filename, RTLD_NOW);
249+
if (handle) {
250+
return fake_dlsym(handle, name);
251+
}
252+
} else {
253+
void* handle = dlopen(filename, RTLD_LAZY | RTLD_GLOBAL);
254+
if (handle) {
255+
return dlsym(handle, name);
256+
}
257+
}
258+
}
259+
240260
}

‎hooklib/src/main/cpp/utils/hide_api.cpp

+51-51
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,30 @@ extern "C" {
2020

2121
art::jit::JitCompiler** globalJitCompileHandlerAddr = nullptr;
2222

23+
void* (*get)(bool*) = nullptr;
24+
25+
void* (*getQuickToInterpreterBridge)(void*) = nullptr;
26+
void* (*getQuickGenericJniStub)(void*) = nullptr;
27+
2328

2429

2530
void initHideApi(JNIEnv* env) {
31+
32+
const char* art_lib_path;
33+
const char* jit_lib_path;
34+
if (BYTE_POINT == 8) {
35+
art_lib_path = "/system/lib64/libart.so";
36+
jit_lib_path = "/system/lib64/libart-compiler.so";
37+
} else {
38+
art_lib_path = "/system/lib/libart.so";
39+
jit_lib_path = "/system/lib/libart-compiler.so";
40+
}
41+
2642
//init compile
2743
if (SDK_INT >= ANDROID_N) {
28-
void *jit_lib;
29-
if (BYTE_POINT == 8) {
30-
jit_lib = fake_dlopen("/system/lib64/libart-compiler.so", RTLD_NOW);
31-
} else {
32-
jit_lib = fake_dlopen("/system/lib/libart-compiler.so", RTLD_NOW);
33-
}
34-
jitCompileMethod = (bool (*)(void *, void *, void *, bool)) fake_dlsym(jit_lib, "jit_compile_method");
35-
jitLoad = reinterpret_cast<void* (*)(bool*)>(fake_dlsym(jit_lib, "jit_load"));
44+
jitCompileMethod = reinterpret_cast<bool (*)(void *, void *, void *,
45+
bool)>(getSymCompat(jit_lib_path, "jit_compile_method"));
46+
jitLoad = reinterpret_cast<void* (*)(bool*)>(getSymCompat(jit_lib_path, "jit_load"));
3647
bool generate_debug_info = false;
3748
jitCompilerHandle = (jitLoad)(&generate_debug_info);
3849

@@ -43,56 +54,34 @@ extern "C" {
4354
}
4455

4556
}
57+
58+
4659
//init suspend
47-
void* art_lib;
48-
const char* art_lib_path;
49-
if (BYTE_POINT == 8) {
50-
art_lib_path = "/system/lib64/libart.so";
51-
} else {
52-
art_lib_path = "/system/lib/libart.so";
53-
}
54-
if (SDK_INT >= ANDROID_N) {
55-
art_lib = fake_dlopen(art_lib_path, RTLD_NOW);
56-
if (art_lib > 0) {
57-
innerSuspendVM = reinterpret_cast<void (*)()>(fake_dlsym(art_lib,
58-
"_ZN3art3Dbg9SuspendVMEv"));
59-
innerResumeVM = reinterpret_cast<void (*)()>(fake_dlsym(art_lib,
60-
"_ZN3art3Dbg8ResumeVMEv"));
61-
}
62-
} else {
63-
art_lib = dlopen(art_lib_path, RTLD_NOW);
64-
if (art_lib > 0) {
65-
innerSuspendVM = reinterpret_cast<void (*)()>(dlsym(art_lib,
60+
innerSuspendVM = reinterpret_cast<void (*)()>(getSymCompat(art_lib_path,
6661
"_ZN3art3Dbg9SuspendVMEv"));
67-
innerResumeVM = reinterpret_cast<void (*)()>(dlsym(art_lib,
62+
innerResumeVM = reinterpret_cast<void (*)()>(getSymCompat(art_lib_path,
6863
"_ZN3art3Dbg8ResumeVMEv"));
69-
}
70-
}
64+
7165

7266
//init for getObject & JitCompiler
67+
const char* add_weak_ref_sym;
7368
if (SDK_INT < ANDROID_M) {
74-
void *handle = dlopen("libart.so", RTLD_LAZY | RTLD_GLOBAL);
75-
addWeakGlobalRef = (jobject (*)(JavaVM *, void *, void *)) dlsym(handle,
76-
"_ZN3art9JavaVMExt22AddWeakGlobalReferenceEPNS_6ThreadEPNS_6mirror6ObjectE");
69+
add_weak_ref_sym = "_ZN3art9JavaVMExt22AddWeakGlobalReferenceEPNS_6ThreadEPNS_6mirror6ObjectE";
7770
} else if (SDK_INT < ANDROID_N) {
78-
void *handle = dlopen("libart.so", RTLD_LAZY | RTLD_GLOBAL);
79-
addWeakGlobalRef = (jobject (*)(JavaVM *, void *, void *)) dlsym(handle,
80-
"_ZN3art9JavaVMExt16AddWeakGlobalRefEPNS_6ThreadEPNS_6mirror6ObjectE");
81-
} else {
82-
void *handle;
83-
if (BYTE_POINT == 8) {
84-
handle = fake_dlopen("/system/lib64/libart.so", RTLD_NOW);
85-
} else {
86-
handle = fake_dlopen("/system/lib/libart.so", RTLD_NOW);
87-
}
88-
const char *addWeakGloablReferenceSymbol = SDK_INT <= 25
89-
? "_ZN3art9JavaVMExt16AddWeakGlobalRefEPNS_6ThreadEPNS_6mirror6ObjectE"
90-
: "_ZN3art9JavaVMExt16AddWeakGlobalRefEPNS_6ThreadENS_6ObjPtrINS_6mirror6ObjectEEE";
91-
addWeakGlobalRef = (jobject (*)(JavaVM *, void *, void *)) fake_dlsym(handle,
92-
addWeakGloablReferenceSymbol);
93-
94-
//try disable inline !
95-
globalJitCompileHandlerAddr = reinterpret_cast<art::jit::JitCompiler **>(fake_dlsym(handle, "_ZN3art3jit3Jit20jit_compiler_handle_E"));
71+
add_weak_ref_sym = "_ZN3art9JavaVMExt16AddWeakGlobalRefEPNS_6ThreadEPNS_6mirror6ObjectE";
72+
} else {
73+
add_weak_ref_sym = SDK_INT <= ANDROID_N2
74+
? "_ZN3art9JavaVMExt16AddWeakGlobalRefEPNS_6ThreadEPNS_6mirror6ObjectE"
75+
: "_ZN3art9JavaVMExt16AddWeakGlobalRefEPNS_6ThreadENS_6ObjPtrINS_6mirror6ObjectEEE";
76+
}
77+
78+
addWeakGlobalRef = reinterpret_cast<jobject (*)(JavaVM *, void *,
79+
void *)>(getSymCompat(art_lib_path, add_weak_ref_sym));
80+
81+
if (SDK_INT >= ANDROID_N) {
82+
globalJitCompileHandlerAddr = reinterpret_cast<art::jit::JitCompiler **>(getSymCompat(art_lib_path, "_ZN3art3jit3Jit20jit_compiler_handle_E"));
83+
getQuickGenericJniStub = reinterpret_cast<void *(*)(void *)>(getSymCompat(art_lib_path, "_ZNK3art11ClassLinker29GetRuntimeQuickGenericJniStubEv"));
84+
getQuickToInterpreterBridge = reinterpret_cast<void *(*)(void *)>(getSymCompat(art_lib_path, "_ZNK3art9OatHeader27GetQuickToInterpreterBridgeEv"));
9685
}
9786

9887
}
@@ -170,5 +159,16 @@ extern "C" {
170159
}
171160
}
172161

162+
void* getInterpreterBridge(bool isNative) {
163+
if (isNative) {
164+
if (getQuickGenericJniStub == nullptr || getQuickGenericJniStub <= 0)
165+
return nullptr;
166+
return getQuickGenericJniStub(nullptr);
167+
} else {
168+
//no implement
169+
return nullptr;
170+
}
171+
}
172+
173173
}
174174

Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package com.swift.sandhook;
22

3+
import android.util.Log;
4+
35
public class ClassNeverCall {
46
private void neverCall() {}
7+
private void neverCall2() {
8+
Log.e("ClassNeverCall", "ClassNeverCall2");
9+
}
510
private static void neverCallStatic() {}
611
private native void neverCallNative();
712
}

‎hooklib/src/main/java/com/swift/sandhook/SandHook.java

+1
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ public static long getThreadId() {
308308
public static native void ensureMethodCached(Method hook, Method backup);
309309

310310
public static native boolean compileMethod(Member member);
311+
public static native boolean deCompileMethod(Member member);
311312

312313
public static native boolean canGetObject();
313314
public static native Object getObjectNative(long thread, long address);

0 commit comments

Comments
 (0)
Please sign in to comment.