Commit feeab481 authored by swift_gan's avatar swift_gan

stop the world when hooking

parent 237274b7
...@@ -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
...@@ -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) {
......
...@@ -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();
}
} }
...@@ -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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment