Skip to content

Commit

Permalink
Fix: Native lib maybe produce crash.
Browse files Browse the repository at this point in the history
  • Loading branch information
asLody committed Aug 18, 2016
1 parent e88e9e3 commit f8f8e93
Show file tree
Hide file tree
Showing 12 changed files with 190 additions and 203 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ public synchronized void start() {
fixBoundApp(mBoundApplication, VirtualCore.getHostBindData());
Application app = data.info.makeApplication(false, null);
mInitialApplication = app;
Reflect.on(VirtualCore.mainThread()).set("mInitialApplication", app);
ContextFixer.fixContext(app);
List<ProviderInfo> providers = data.providers;
if (providers != null) {
Expand Down
2 changes: 1 addition & 1 deletion VirtualApp/lib/src/main/jni/MSHook/hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ int elfHook(const char *soname, const char *symbol, void *replace_func,

int elfHookDirect(unsigned int addr, void *replace_func, void **old_func) {
if (addr == 0) {
MS_LOGW("hook direct addr:%p error!", (void *) addr);
MS_LOGW("replaceImplementation direct addr:%p error!", (void *) addr);
return -1;
}
Cydia::MSHookFunction((void *) addr, replace_func, old_func);
Expand Down
19 changes: 7 additions & 12 deletions VirtualApp/lib/src/main/jni/core.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Created by Xfast on 2016/7/21.
// VirtualApp Native Project
//
#include "core.h"

Expand All @@ -14,12 +14,11 @@ void hook_native(JNIEnv *env, jclass jclazz, jobject javaMethod, jboolean isArt)
if (hasHooked) {
return;
}
HOOK_NATIVE::hook(javaMethod, isArt);
hookNative(javaMethod, isArt);
hasHooked = true;
}



void hook_io(JNIEnv *env, jclass jclazz, jint apiLevel) {
static bool hasHooked = false;
if (hasHooked) {
Expand Down Expand Up @@ -62,33 +61,29 @@ static JNINativeMethod gMethods[] = {

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
JNIEnv *env;
if (vm->GetEnv((void **) &env, JNI_VERSION_1_4) != JNI_OK) {
LOGE("GetEnv() FAILED!!!");
if (vm->GetEnv((void **) &env, JNI_VERSION_1_6) != JNI_OK) {
return JNI_ERR;
}
jclass javaClass = env->FindClass(JAVA_CLASS);
if (javaClass == NULL) {
LOGE("unable to find class: %s", JAVA_CLASS);
LOGE("Ops: Unable to find hook class.");
return JNI_ERR;
}
env->UnregisterNatives(javaClass);
if (env->RegisterNatives(javaClass, gMethods, NELEM(gMethods)) < 0) {
LOGE("register methods FAILED!!!");
LOGE("Ops: Unable to register the native methods.");
return JNI_ERR;
}
g_vm = vm;
g_jclass = (jclass) env->NewGlobalRef(javaClass);
env->DeleteLocalRef(javaClass);
LOGI("JavaVM::GetEnv() SUCCESS!");
return JNI_VERSION_1_4;
return JNI_VERSION_1_6;
}



JNIEXPORT void JNICALL JNI_OnUnload(JavaVM* vm, void* reserved) {
JNIEnv *env;
if (vm->GetEnv((void **) &env, JNI_VERSION_1_4) != JNI_OK) {
LOGE("JNI_OnUnload GetEnv() FAILED!!!");
if (vm->GetEnv((void **) &env, JNI_VERSION_1_6) != JNI_OK) {
return;
}
env->DeleteGlobalRef((jobject)g_vm);
Expand Down
2 changes: 1 addition & 1 deletion VirtualApp/lib/src/main/jni/core.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Created by Xfast on 2016/7/22.
// VirtualApp Native Project
//

#ifndef NDK_CORE_H
Expand Down
3 changes: 2 additions & 1 deletion VirtualApp/lib/src/main/jni/helper/helper.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Created by Xfast on 2016/7/21.
// VirtualApp Native Project
//

#ifndef NDK_LOG_H
Expand All @@ -10,6 +10,7 @@
#define TAG "VA-IO"

#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__)
#define LOGDT(T, ...) __android_log_print(ANDROID_LOG_DEBUG, T, __VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__)
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN, TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__)
Expand Down
17 changes: 4 additions & 13 deletions VirtualApp/lib/src/main/jni/hook/Hook.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
//
// Created by Xfast on 2016/7/21.
// VirtualApp Native Project
//
#include "Hook.h"

static std::map<std::string/*orig_path*/, std::string/*new_path*/> IORedirectMap;
static std::map<std::string/*orig_path*/, std::string/*new_path*/> RootIORedirectMap;

static inline void hook_template(const char *lib_so, const char *symbol, void *new_func, void **old_func) {
LOGI("hook symbol=%s, new_func=%p, old_func=%p", symbol, new_func, *old_func);
void *handle = dlopen(lib_so, RTLD_GLOBAL | RTLD_LAZY);
if (handle == NULL) {
LOGW("can't hook %s in %s: 'dlopen' %s FAILED!!!", symbol, lib_so, lib_so);
LOGW("Ops: unable to find the so : %s.", lib_so);
return;
}
void *addr = dlsym(handle, symbol);
if (addr == NULL) {
LOGW("can't hook %s in %s: 'dlsym' %s func FAILED!!!", symbol, lib_so, symbol);
LOGW("Ops: unable to find the symbol : %s.", symbol);
return;
}
elfHookDirect((unsigned int) (addr), new_func, old_func);
LOGI("Hook %s in %s SUCCESS!", symbol, lib_so);
dlclose(handle);
}

Expand Down Expand Up @@ -77,7 +75,7 @@ const char *match_redirected_path(const char *_path) {


void HOOK::redirect(const char *org_path, const char *new_path) {
LOGI("native add redirect: from %s to %s", org_path, new_path);
LOGI("Start redirect : from %s to %s", org_path, new_path);
add_pair(org_path, new_path);
}

Expand All @@ -96,7 +94,6 @@ const char *HOOK::restore(const char *path) {



// we hook system call
__BEGIN_DECLS

// dlopen //TODO
Expand Down Expand Up @@ -471,7 +468,6 @@ __END_DECLS


void HOOK::hook(int api_level) {
LOGI("Begin IO hooks...");

//通用型
HOOK_IO(__getcwd);
Expand All @@ -491,9 +487,6 @@ void HOOK::hook(int api_level) {
// HOOK_IO(vfork);

if (api_level < ANDROID_L) {
//xxx型
// HOOK_IO(fchmod);
// HOOK_IO(fstat);
HOOK_IO(link);
HOOK_IO(symlink);
HOOK_IO(readlink);
Expand All @@ -512,7 +505,6 @@ void HOOK::hook(int api_level) {
}

if (api_level >= ANDROID_L) {
///xxxat型
HOOK_IO(linkat);
HOOK_IO(symlinkat);
HOOK_IO(readlinkat);
Expand All @@ -528,5 +520,4 @@ void HOOK::hook(int api_level) {
HOOK_IO(faccessat);
}

LOGI("End IO hooks SUCCESS!");
}
2 changes: 1 addition & 1 deletion VirtualApp/lib/src/main/jni/hook/Hook.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Created by Xfast on 2016/7/21.
// VirtualApp Native Project
//

#ifndef NDK_HOOK_H
Expand Down
Loading

0 comments on commit f8f8e93

Please sign in to comment.