Commit c3e36683 authored by swift_gan's avatar swift_gan

add compiler switcher

parent b1fa5c26
...@@ -28,6 +28,8 @@ void ArtMethod::disableInterpreterForO() { ...@@ -28,6 +28,8 @@ void ArtMethod::disableInterpreterForO() {
} }
void ArtMethod::disableCompilable() { void ArtMethod::disableCompilable() {
if (SDK_INT < ANDROID_N)
return;
uint32_t accessFlag = getAccessFlags(); uint32_t accessFlag = getAccessFlags();
if (SDK_INT >= ANDROID_O2) { if (SDK_INT >= ANDROID_O2) {
accessFlag |= 0x02000000; accessFlag |= 0x02000000;
......
...@@ -8,6 +8,7 @@ SandHook::TrampolineManager trampolineManager; ...@@ -8,6 +8,7 @@ SandHook::TrampolineManager trampolineManager;
extern "C" int SDK_INT = 0; extern "C" int SDK_INT = 0;
extern "C" bool DEBUG = false; extern "C" bool DEBUG = false;
extern "C" volatile bool COMPILER = true;
enum HookMode { enum HookMode {
AUTO = 0, AUTO = 0,
...@@ -64,13 +65,13 @@ bool doHookWithReplacement(JNIEnv* env, ...@@ -64,13 +65,13 @@ bool doHookWithReplacement(JNIEnv* env,
art::mirror::ArtMethod *hookMethod, art::mirror::ArtMethod *hookMethod,
art::mirror::ArtMethod *backupMethod) { art::mirror::ArtMethod *backupMethod) {
hookMethod->compile(env); if (!hookMethod->compile(env)) {
hookMethod->disableCompilable();
}
if (backupMethod != nullptr) { if (backupMethod != nullptr) {
originMethod->backup(backupMethod); originMethod->backup(backupMethod);
if (SDK_INT >= ANDROID_N) {
backupMethod->disableCompilable(); backupMethod->disableCompilable();
}
backupMethod->disableInterpreterForO(); backupMethod->disableInterpreterForO();
backupMethod->tryDisableInline(); backupMethod->tryDisableInline();
if (!backupMethod->isStatic()) { if (!backupMethod->isStatic()) {
...@@ -79,13 +80,11 @@ bool doHookWithReplacement(JNIEnv* env, ...@@ -79,13 +80,11 @@ bool doHookWithReplacement(JNIEnv* env,
backupMethod->flushCache(); backupMethod->flushCache();
} }
if (SDK_INT >= ANDROID_N) {
originMethod->disableCompilable(); originMethod->disableCompilable();
hookMethod->disableCompilable(); hookMethod->disableCompilable();
hookMethod->flushCache(); hookMethod->flushCache();
}
originMethod->tryDisableInline();
originMethod->tryDisableInline();
originMethod->disableInterpreterForO(); originMethod->disableInterpreterForO();
SandHook::HookTrampoline* hookTrampoline = trampolineManager.installReplacementTrampoline(originMethod, hookMethod, backupMethod); SandHook::HookTrampoline* hookTrampoline = trampolineManager.installReplacementTrampoline(originMethod, hookMethod, backupMethod);
...@@ -112,7 +111,9 @@ bool doHookWithInline(JNIEnv* env, ...@@ -112,7 +111,9 @@ bool doHookWithInline(JNIEnv* env,
art::mirror::ArtMethod *backupMethod) { art::mirror::ArtMethod *backupMethod) {
//fix >= 8.1 //fix >= 8.1
hookMethod->compile(env); if (!hookMethod->compile(env)) {
hookMethod->disableCompilable();
}
if (SDK_INT >= ANDROID_N) { if (SDK_INT >= ANDROID_N) {
originMethod->disableCompilable(); originMethod->disableCompilable();
...@@ -317,6 +318,12 @@ Java_com_swift_sandhook_SandHook_disableVMInline(JNIEnv *env, jclass type) { ...@@ -317,6 +318,12 @@ Java_com_swift_sandhook_SandHook_disableVMInline(JNIEnv *env, jclass type) {
return static_cast<jboolean>(disableJitInline(compilerOptions)); return static_cast<jboolean>(disableJitInline(compilerOptions));
} }
extern "C"
JNIEXPORT void JNICALL
Java_com_swift_sandhook_SandHook_enableCompiler(JNIEnv *env, jclass type, jboolean enable) {
COMPILER = enable;
}
extern "C" extern "C"
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_com_swift_sandhook_test_TestClass_jni_1test(JNIEnv *env, jobject instance) { Java_com_swift_sandhook_test_TestClass_jni_1test(JNIEnv *env, jobject instance) {
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "../includes/log.h" #include "../includes/log.h"
extern int SDK_INT; extern int SDK_INT;
extern bool COMPILER;
extern "C" { extern "C" {
...@@ -97,6 +98,7 @@ extern "C" { ...@@ -97,6 +98,7 @@ extern "C" {
} }
bool compileMethod(void* artMethod, void* thread) { bool compileMethod(void* artMethod, void* thread) {
if (!COMPILER) return false;
if (SDK_INT >= ANDROID_Q) { if (SDK_INT >= ANDROID_Q) {
if (jitCompileMethodQ == nullptr) { if (jitCompileMethodQ == nullptr) {
return false; return false;
......
...@@ -52,6 +52,7 @@ public class SandHook { ...@@ -52,6 +52,7 @@ public class SandHook {
initTestOffset(); initTestOffset();
initThreadPeer(); initThreadPeer();
SandHookMethodResolver.init(); SandHookMethodResolver.init();
enableCompiler(SandHookConfig.compiler);
return initNative(SandHookConfig.SDK_INT, SandHookConfig.DEBUG); return initNative(SandHookConfig.SDK_INT, SandHookConfig.DEBUG);
} }
...@@ -319,6 +320,9 @@ public class SandHook { ...@@ -319,6 +320,9 @@ public class SandHook {
public static native boolean disableVMInline(); public static native boolean disableVMInline();
//compile error when in zygote process
public static native void enableCompiler(boolean enable);
@FunctionalInterface @FunctionalInterface
public interface HookModeCallBack { public interface HookModeCallBack {
int hookMode(Member originMethod); int hookMode(Member originMethod);
......
...@@ -8,4 +8,5 @@ public class SandHookConfig { ...@@ -8,4 +8,5 @@ public class SandHookConfig {
public volatile static int SDK_INT = Build.VERSION.SDK_INT; public volatile static int SDK_INT = Build.VERSION.SDK_INT;
public volatile static String libSandHookPath; public volatile static String libSandHookPath;
public volatile static boolean DEBUG = BuildConfig.DEBUG; public volatile static boolean DEBUG = BuildConfig.DEBUG;
public volatile static boolean compiler = true;
} }
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