Commit dd6c7099 authored by swift_gan's avatar swift_gan

teak code

parent aefbd450
......@@ -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;
......
......@@ -57,6 +57,7 @@ public:
void disableInterpreterForO();
void setPrivate();
void setStatic();
void setNative();
void setQuickCodeEntry(void* entry);
void setJniCodeEntry(void* entry);
......
......@@ -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->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();
}
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->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);
......
......@@ -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);
......
......@@ -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");
......
......@@ -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);
......
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;
}
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