Commit c3e36683 authored by swift_gan's avatar swift_gan

add compiler switcher

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