Commit dd6c7099 authored by swift_gan's avatar swift_gan

teak code

parent aefbd450
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "../includes/utils.h" #include "../includes/utils.h"
extern int SDK_INT; extern int SDK_INT;
extern bool DEBUG;
using namespace art::mirror; using namespace art::mirror;
using namespace SandHook; using namespace SandHook;
...@@ -21,9 +22,9 @@ void ArtMethod::tryDisableInline() { ...@@ -21,9 +22,9 @@ void ArtMethod::tryDisableInline() {
} }
void ArtMethod::disableInterpreterForO() { void ArtMethod::disableInterpreterForO() {
uint32_t accessFlag = getAccessFlags(); if (SDK_INT >= ANDROID_O && DEBUG) {
accessFlag |= 0x0100; setNative();
setAccessFlags(accessFlag); }
} }
void ArtMethod::disableCompilable() { void ArtMethod::disableCompilable() {
...@@ -53,7 +54,7 @@ bool ArtMethod::isStatic() { ...@@ -53,7 +54,7 @@ bool ArtMethod::isStatic() {
} }
bool ArtMethod::isCompiled() { bool ArtMethod::isCompiled() {
return getQuickCodeEntry() != SandHook::CastArtMethod::quickToInterpreterBridge; return getQuickCodeEntry() != CastArtMethod::quickToInterpreterBridge && getQuickCodeEntry() != CastArtMethod::genericJniStub;
} }
bool ArtMethod::isThumbCode() { bool ArtMethod::isThumbCode() {
...@@ -82,6 +83,13 @@ void ArtMethod::setStatic() { ...@@ -82,6 +83,13 @@ void ArtMethod::setStatic() {
setAccessFlags(accessFlag); setAccessFlags(accessFlag);
}; };
void ArtMethod::setNative() {
uint32_t accessFlag = getAccessFlags();
accessFlag |= 0x0100;
setAccessFlags(accessFlag);
}
uint32_t ArtMethod::getAccessFlags() { uint32_t ArtMethod::getAccessFlags() {
return CastArtMethod::accessFlag->get(this); return CastArtMethod::accessFlag->get(this);
} }
...@@ -128,6 +136,9 @@ void ArtMethod::setDeclaringClassPtr(void *classPtr) { ...@@ -128,6 +136,9 @@ void ArtMethod::setDeclaringClassPtr(void *classPtr) {
bool ArtMethod::compile(JNIEnv* env) { bool ArtMethod::compile(JNIEnv* env) {
if (isCompiled()) if (isCompiled())
return true; 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"); Size threadId = getAddressFromJavaByCallMethod(env, "com/swift/sandhook/SandHook", "getThreadId");
if (threadId == 0) if (threadId == 0)
return false; return false;
......
...@@ -57,6 +57,7 @@ public: ...@@ -57,6 +57,7 @@ public:
void disableInterpreterForO(); void disableInterpreterForO();
void setPrivate(); void setPrivate();
void setStatic(); void setStatic();
void setNative();
void setQuickCodeEntry(void* entry); void setQuickCodeEntry(void* entry);
void setJniCodeEntry(void* entry); void setJniCodeEntry(void* entry);
......
...@@ -2,12 +2,12 @@ ...@@ -2,12 +2,12 @@
#include "includes/cast_art_method.h" #include "includes/cast_art_method.h"
#include "includes/trampoline_manager.h" #include "includes/trampoline_manager.h"
#include "includes/hide_api.h" #include "includes/hide_api.h"
#include "includes/log.h"
#include "includes/cast_compiler_options.h" #include "includes/cast_compiler_options.h"
SandHook::TrampolineManager trampolineManager; SandHook::TrampolineManager trampolineManager;
extern "C" int SDK_INT = 0; extern "C" int SDK_INT = 0;
extern "C" bool DEBUG = false;
enum HookMode { enum HookMode {
AUTO = 0, AUTO = 0,
...@@ -19,10 +19,11 @@ HookMode gHookMode = AUTO; ...@@ -19,10 +19,11 @@ HookMode gHookMode = AUTO;
extern "C" extern "C"
JNIEXPORT jboolean JNICALL 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 // TODO
SDK_INT = sdk; SDK_INT = sdk;
DEBUG = debug;
SandHook::CastArtMethod::init(env); SandHook::CastArtMethod::init(env);
SandHook::CastCompilerOptions::init(env); SandHook::CastCompilerOptions::init(env);
trampolineManager.init(SandHook::CastArtMethod::entryPointQuickCompiled->getOffset()); trampolineManager.init(SandHook::CastArtMethod::entryPointQuickCompiled->getOffset());
...@@ -70,9 +71,7 @@ bool doHookWithReplacement(JNIEnv* env, ...@@ -70,9 +71,7 @@ bool doHookWithReplacement(JNIEnv* env,
if (SDK_INT >= ANDROID_N) { if (SDK_INT >= ANDROID_N) {
backupMethod->disableCompilable(); backupMethod->disableCompilable();
} }
if (SDK_INT >= ANDROID_O) { backupMethod->disableInterpreterForO();
backupMethod->disableInterpreterForO();
}
backupMethod->tryDisableInline(); backupMethod->tryDisableInline();
if (!backupMethod->isStatic()) { if (!backupMethod->isStatic()) {
backupMethod->setPrivate(); backupMethod->setPrivate();
...@@ -87,9 +86,7 @@ bool doHookWithReplacement(JNIEnv* env, ...@@ -87,9 +86,7 @@ bool doHookWithReplacement(JNIEnv* env,
} }
originMethod->tryDisableInline(); originMethod->tryDisableInline();
if (SDK_INT >= ANDROID_O && !originMethod->isCompiled()) { originMethod->disableInterpreterForO();
originMethod->disableInterpreterForO();
}
SandHook::HookTrampoline* hookTrampoline = trampolineManager.installReplacementTrampoline(originMethod, hookMethod, backupMethod); SandHook::HookTrampoline* hookTrampoline = trampolineManager.installReplacementTrampoline(originMethod, hookMethod, backupMethod);
if (hookTrampoline != nullptr) { if (hookTrampoline != nullptr) {
...@@ -137,9 +134,7 @@ bool doHookWithInline(JNIEnv* env, ...@@ -137,9 +134,7 @@ bool doHookWithInline(JNIEnv* env,
if (SDK_INT >= ANDROID_N) { if (SDK_INT >= ANDROID_N) {
backupMethod->disableCompilable(); backupMethod->disableCompilable();
} }
if (SDK_INT >= ANDROID_O) { backupMethod->disableInterpreterForO();
backupMethod->disableInterpreterForO();
}
backupMethod->tryDisableInline(); backupMethod->tryDisableInline();
if (!backupMethod->isStatic()) { if (!backupMethod->isStatic()) {
backupMethod->setPrivate(); backupMethod->setPrivate();
...@@ -182,6 +177,8 @@ Java_com_swift_sandhook_SandHook_hookMethod(JNIEnv *env, jclass type, jobject or ...@@ -182,6 +177,8 @@ Java_com_swift_sandhook_SandHook_hookMethod(JNIEnv *env, jclass type, jobject or
if (origin->isAbstract()) { if (origin->isAbstract()) {
isInlineHook = false; isInlineHook = false;
} else if (origin->isNative()) {
isInlineHook = SDK_INT < ANDROID_O;
} else if (gHookMode != AUTO) { } else if (gHookMode != AUTO) {
if (gHookMode == INLINE) { if (gHookMode == INLINE) {
isInlineHook = origin->compile(env); isInlineHook = origin->compile(env);
......
...@@ -76,7 +76,7 @@ namespace SandHook { ...@@ -76,7 +76,7 @@ namespace SandHook {
return true; return true;
//check size //check size
if (!method->isNative()) { if (method->isCompiled()) {
uint32_t originCodeSize = sizeOfEntryCode(method); uint32_t originCodeSize = sizeOfEntryCode(method);
if (originCodeSize < SIZE_DIRECT_JUMP_TRAMPOLINE) { if (originCodeSize < SIZE_DIRECT_JUMP_TRAMPOLINE) {
LOGW("can not inline due to origin code is too small(size is %d)", originCodeSize); LOGW("can not inline due to origin code is too small(size is %d)", originCodeSize);
......
...@@ -24,7 +24,7 @@ extern "C" { ...@@ -24,7 +24,7 @@ extern "C" {
void initHideApi(JNIEnv* env) { void initHideApi(JNIEnv* env) {
//init compile //init compile
if (SDK_INT >= 24) { if (SDK_INT >= ANDROID_N) {
void *jit_lib; void *jit_lib;
if (BYTE_POINT == 8) { if (BYTE_POINT == 8) {
jit_lib = fake_dlopen("/system/lib64/libart-compiler.so", RTLD_NOW); jit_lib = fake_dlopen("/system/lib64/libart-compiler.so", RTLD_NOW);
...@@ -51,7 +51,7 @@ extern "C" { ...@@ -51,7 +51,7 @@ extern "C" {
} else { } else {
art_lib_path = "/system/lib/libart.so"; 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); art_lib = fake_dlopen(art_lib_path, RTLD_NOW);
if (art_lib > 0) { if (art_lib > 0) {
innerSuspendVM = reinterpret_cast<void (*)()>(fake_dlsym(art_lib, innerSuspendVM = reinterpret_cast<void (*)()>(fake_dlsym(art_lib,
...@@ -70,11 +70,11 @@ extern "C" { ...@@ -70,11 +70,11 @@ extern "C" {
} }
//init for getObject & JitCompiler //init for getObject & JitCompiler
if (SDK_INT < 23) { if (SDK_INT < ANDROID_M) {
void *handle = dlopen("libart.so", RTLD_LAZY | RTLD_GLOBAL); void *handle = dlopen("libart.so", RTLD_LAZY | RTLD_GLOBAL);
addWeakGlobalRef = (jobject (*)(JavaVM *, void *, void *)) dlsym(handle, addWeakGlobalRef = (jobject (*)(JavaVM *, void *, void *)) dlsym(handle,
"_ZN3art9JavaVMExt22AddWeakGlobalReferenceEPNS_6ThreadEPNS_6mirror6ObjectE"); "_ZN3art9JavaVMExt22AddWeakGlobalReferenceEPNS_6ThreadEPNS_6mirror6ObjectE");
} else if (SDK_INT < 24) { } else if (SDK_INT < ANDROID_N) {
void *handle = dlopen("libart.so", RTLD_LAZY | RTLD_GLOBAL); void *handle = dlopen("libart.so", RTLD_LAZY | RTLD_GLOBAL);
addWeakGlobalRef = (jobject (*)(JavaVM *, void *, void *)) dlsym(handle, addWeakGlobalRef = (jobject (*)(JavaVM *, void *, void *)) dlsym(handle,
"_ZN3art9JavaVMExt16AddWeakGlobalRefEPNS_6ThreadEPNS_6mirror6ObjectE"); "_ZN3art9JavaVMExt16AddWeakGlobalRefEPNS_6ThreadEPNS_6mirror6ObjectE");
......
...@@ -54,7 +54,7 @@ public class SandHook { ...@@ -54,7 +54,7 @@ public class SandHook {
initTestOffset(); initTestOffset();
initThreadPeer(); initThreadPeer();
SandHookMethodResolver.init(); SandHookMethodResolver.init();
return initNative(Build.VERSION.SDK_INT); return initNative(Build.VERSION.SDK_INT, SandHookConfig.DEBUG);
} }
private static void initThreadPeer() { private static void initThreadPeer() {
...@@ -295,7 +295,7 @@ public class SandHook { ...@@ -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); public static native void setHookMode(int hookMode);
......
package com.swift.sandhook; package com.swift.sandhook;
import com.swift.sandhook.lib.BuildConfig;
public class SandHookConfig { public class SandHookConfig {
public volatile static String libSandHookPath; 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