Commit 6e652fbf authored by swift_gan's avatar swift_gan

tweak compile switcher

parent 2de02441
......@@ -20,6 +20,8 @@ extern "C" {
jint getIntFromJava(JNIEnv *env, const char *className, const char *fieldName);
bool getBooleanFromJava(JNIEnv *env, const char *className, const char *fieldName);
bool munprotect(size_t addr, size_t len);
bool flushCacheExt(Size addr, Size len);
......
......@@ -8,7 +8,6 @@ SandHook::TrampolineManager trampolineManager;
extern "C" int SDK_INT = 0;
extern "C" bool DEBUG = false;
extern "C" volatile bool COMPILER = true;
enum HookMode {
AUTO = 0,
......@@ -318,12 +317,6 @@ 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) {
......
......@@ -5,9 +5,9 @@
#include "../includes/arch.h"
#include "../includes/elf_util.h"
#include "../includes/log.h"
#include "../includes/utils.h"
extern int SDK_INT;
extern bool COMPILER;
extern "C" {
......@@ -30,10 +30,12 @@ extern "C" {
const char* art_lib_path;
const char* jit_lib_path;
JavaVM* jvm;
void initHideApi(JNIEnv* env) {
env->GetJavaVM(&jvm);
if (BYTE_POINT == 8) {
art_lib_path = "/system/lib64/libart.so";
jit_lib_path = "/system/lib64/libart-compiler.so";
......@@ -97,8 +99,15 @@ extern "C" {
}
bool canCompile() {
JNIEnv *env;
jvm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6);
return getBooleanFromJava(env, "com/swift/sandhook/SandHookConfig",
"compiler");
}
bool compileMethod(void* artMethod, void* thread) {
if (!COMPILER) return false;
if (!canCompile()) return false;
if (SDK_INT >= ANDROID_Q) {
if (jitCompileMethodQ == nullptr) {
return false;
......@@ -133,11 +142,7 @@ extern "C" {
if (addWeakGlobalRef == nullptr)
return NULL;
JavaVM *vm;
env->GetJavaVM(&vm);
jobject object = addWeakGlobalRef(vm, thread, address);
jobject object = addWeakGlobalRef(jvm, thread, address);
if (object == NULL)
return NULL;
......
......@@ -48,6 +48,20 @@ extern "C" {
return env->GetStaticIntField(clazz, id);
}
bool getBooleanFromJava(JNIEnv *env, const char *className, const char *fieldName) {
jclass clazz = env->FindClass(className);
if (clazz == NULL) {
printf("find class error !");
return false;
}
jfieldID id = env->GetStaticFieldID(clazz, fieldName, "Z");
if (id == NULL) {
printf("find field error !");
return false;
}
return env->GetStaticBooleanField(clazz, id);
}
bool munprotect(size_t addr, size_t len) {
long pagesize = sysconf(_SC_PAGESIZE);
unsigned alignment = (unsigned) ((unsigned long long) addr % pagesize);
......
......@@ -52,7 +52,6 @@ public class SandHook {
initTestOffset();
initThreadPeer();
SandHookMethodResolver.init();
enableCompiler(SandHookConfig.compiler);
return initNative(SandHookConfig.SDK_INT, SandHookConfig.DEBUG);
}
......@@ -320,9 +319,6 @@ 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);
......
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