|
|
|
@@ -10,7 +10,7 @@ Desc: JuiceLoader Native, Provide jni function for JuiceLoader
|
|
|
|
|
#include "javautils.h"
|
|
|
|
|
|
|
|
|
|
JuiceLoaderNativeType JuiceLoaderNative;
|
|
|
|
|
JavaHookCacheType JavaHookCache = {false, NULL, NULL};
|
|
|
|
|
RetransformClassCacheType RetransformClassCache = {NULL, 0, 0};
|
|
|
|
|
|
|
|
|
|
static const char* g_bytecodes_classname = NULL;
|
|
|
|
|
static unsigned char* g_bytecodes = NULL;
|
|
|
|
@@ -35,90 +35,54 @@ void JNICALL ClassFileLoadHook(
|
|
|
|
|
jint* new_class_data_len,
|
|
|
|
|
unsigned char** new_classbytes) {
|
|
|
|
|
|
|
|
|
|
// Retransform save class bytes
|
|
|
|
|
// ---- get class bytecode ---- //
|
|
|
|
|
if (g_bytecodes_classname != NULL && g_bytecodes == NULL) {
|
|
|
|
|
if (strcmp(name, g_bytecodes_classname) == 0) {
|
|
|
|
|
g_bytecodes_len = class_data_len;
|
|
|
|
|
g_bytecodes = (unsigned char*)malloc(class_data_len);
|
|
|
|
|
memcpy(g_bytecodes, classbytes, class_data_len);
|
|
|
|
|
|
|
|
|
|
*new_classbytes = NULL;
|
|
|
|
|
*new_class_data_len = 0;
|
|
|
|
|
|
|
|
|
|
log_trace("[Hook:get] Captured class bytes of %s, len=%d", name, class_data_len);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// ---- get class bytecode ---- //
|
|
|
|
|
|
|
|
|
|
// Java Hook Event
|
|
|
|
|
if (JavaHookCache.isReady){
|
|
|
|
|
// byte[] -> jbyteArray
|
|
|
|
|
jbyteArray jBytes = (*jni_env)->NewByteArray(jni_env, class_data_len);
|
|
|
|
|
if (jBytes == NULL) return;
|
|
|
|
|
|
|
|
|
|
// jbyteArray -> byte[]
|
|
|
|
|
(*jni_env)->SetByteArrayRegion(jni_env, jBytes, 0, class_data_len, (const jbyte*)classbytes);
|
|
|
|
|
jstring jName = (*jni_env)->NewStringUTF(jni_env, name);
|
|
|
|
|
|
|
|
|
|
// Call Java Hook
|
|
|
|
|
jbyteArray result = (jbyteArray)(*jni_env)->CallStaticObjectMethod(
|
|
|
|
|
jni_env,
|
|
|
|
|
JavaHookCache.hook_class,
|
|
|
|
|
JavaHookCache.post_method,
|
|
|
|
|
class_being_redefined,
|
|
|
|
|
loader,
|
|
|
|
|
jName,
|
|
|
|
|
protection_domain,
|
|
|
|
|
class_data_len,
|
|
|
|
|
jBytes
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// clean up local references
|
|
|
|
|
(*jni_env)->DeleteLocalRef(jni_env, jName);
|
|
|
|
|
(*jni_env)->DeleteLocalRef(jni_env, jBytes);
|
|
|
|
|
|
|
|
|
|
// check result
|
|
|
|
|
if (result == NULL) {
|
|
|
|
|
// log_trace("result == NULL -> nothing to do");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ensure result is a byte array
|
|
|
|
|
jclass byteArrCls = (*jni_env)->FindClass(jni_env, "[B");
|
|
|
|
|
jboolean isByteArr = (*jni_env)->IsInstanceOf(jni_env, result, byteArrCls);
|
|
|
|
|
(*jni_env)->DeleteLocalRef(jni_env, byteArrCls);
|
|
|
|
|
|
|
|
|
|
if (!isByteArr) {
|
|
|
|
|
log_error("result is NOT byte[]");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get result length
|
|
|
|
|
jsize newLen = (*jni_env)->GetArrayLength(jni_env, (jarray)result);
|
|
|
|
|
// log_trace("newLen = %d", (int)newLen);
|
|
|
|
|
|
|
|
|
|
// check newLen
|
|
|
|
|
if (newLen <= 0 || newLen > 64 * 1024 * 1024) {
|
|
|
|
|
log_error("invalid newLen = %d", (int)newLen);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// allocate new buffer
|
|
|
|
|
unsigned char* buf = NULL;
|
|
|
|
|
jint allocRes = (*jvmti_env)->Allocate(jvmti_env, (jlong)newLen, (unsigned char**)&buf);
|
|
|
|
|
if (allocRes != JVMTI_ERROR_NONE || buf == NULL) {
|
|
|
|
|
log_error("jvmti Allocate failed: %d", allocRes);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// copy result to new buffer
|
|
|
|
|
(*jni_env)->GetByteArrayRegion(jni_env, (jbyteArray)result, 0, newLen, (jbyte*)buf);
|
|
|
|
|
|
|
|
|
|
// assign output pointers
|
|
|
|
|
*new_classbytes = buf;
|
|
|
|
|
*new_class_data_len = (int)newLen;
|
|
|
|
|
|
|
|
|
|
// log_trace("[*] Assigned output pointers: %p, %d", *new_classbytes, *new_class_data_len);
|
|
|
|
|
|
|
|
|
|
// clean up
|
|
|
|
|
(*jni_env)->DeleteLocalRef(jni_env, result);
|
|
|
|
|
// ---- set class bytecode ---- //
|
|
|
|
|
if (RetransformClassCache.size == 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < RetransformClassCache.size; i++) {
|
|
|
|
|
ClassBytesCacheType *entry = &RetransformClassCache.arr[i];
|
|
|
|
|
if (strcmp(entry->name, name) == 0) {
|
|
|
|
|
|
|
|
|
|
unsigned char *newBytes = NULL;
|
|
|
|
|
jvmtiError err = (*jvmti_env)->Allocate(jvmti_env, entry->len, &newBytes);
|
|
|
|
|
if (err != JVMTI_ERROR_NONE || newBytes == NULL) {
|
|
|
|
|
log_error("[Hook:set] jvmti->Allocate failed for class %s, err=%d", name, err);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memcpy(newBytes, entry->bytes, entry->len);
|
|
|
|
|
*new_classbytes = newBytes;
|
|
|
|
|
*new_class_data_len = entry->len;
|
|
|
|
|
|
|
|
|
|
log_info("[Hook:set] Patched class: %s, new_len=%d", name, entry->len);
|
|
|
|
|
|
|
|
|
|
free(entry->name);
|
|
|
|
|
free(entry->bytes);
|
|
|
|
|
for (int j = i; j < RetransformClassCache.size - 1; j++) {
|
|
|
|
|
RetransformClassCache.arr[j] = RetransformClassCache.arr[j + 1];
|
|
|
|
|
}
|
|
|
|
|
RetransformClassCache.size--;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// ---- set class bytecode ---- //
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -160,7 +124,11 @@ jint init_juiceloader(JNIEnv *env, jvmtiEnv *jvmti) {
|
|
|
|
|
// public static native byte[] getClassBytes(Class<?> clazz);
|
|
|
|
|
// public static native byte[] getClassBytesByName(String className);
|
|
|
|
|
{"getClassBytes", "(Ljava/lang/Class;)[B", (void *)&loader_getClassBytes},
|
|
|
|
|
{ "getClassBytesByName", "(Ljava/lang/String;)[B", (void *)&loader_getClassBytesByName}
|
|
|
|
|
{ "getClassBytesByName", "(Ljava/lang/String;)[B", (void *)&loader_getClassBytesByName},
|
|
|
|
|
// public static native boolean retransformClass(Class<?> clazz, byte[] classBytes, int length);
|
|
|
|
|
{"retransformClass", "(Ljava/lang/Class;[BI)Z", (void *)&loader_retransformClass},
|
|
|
|
|
// public static native boolean retransformClassByName(String className, byte[] classBytes, int length);
|
|
|
|
|
{"retransformClassByName", "(Ljava/lang/String;[BI)Z", (void *)&loader_retransformClassByName}
|
|
|
|
|
};
|
|
|
|
|
jint result = (*env)->RegisterNatives(env, clazz, methods, sizeof(methods) / sizeof(methods[0]));
|
|
|
|
|
if (result != JNI_OK) {
|
|
|
|
@@ -168,6 +136,11 @@ jint init_juiceloader(JNIEnv *env, jvmtiEnv *jvmti) {
|
|
|
|
|
return JNI_ERR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Init RetransformClassCache
|
|
|
|
|
RetransformClassCache.arr = NULL;
|
|
|
|
|
RetransformClassCache.size = 0;
|
|
|
|
|
RetransformClassCache.capacity = 0;
|
|
|
|
|
|
|
|
|
|
// Application ability
|
|
|
|
|
jvmtiCapabilities caps;
|
|
|
|
|
memset(&caps, 0, sizeof(caps));
|
|
|
|
@@ -199,26 +172,8 @@ jint init_juiceloader(JNIEnv *env, jvmtiEnv *jvmti) {
|
|
|
|
|
|
|
|
|
|
// JuiceLoaderNative.init()
|
|
|
|
|
JNIEXPORT jboolean JNICALL loader_init(JNIEnv *env, jclass loader_class) {
|
|
|
|
|
log_info("init Invoked!");
|
|
|
|
|
// Get JuiceEventBus class
|
|
|
|
|
log_info("Find JuiceEventBus class...");
|
|
|
|
|
JavaHookCache.hook_class = (*env)->FindClass(env, "cn/xiaozhou233/juiceloader/JuiceEventBus");
|
|
|
|
|
if (JavaHookCache.hook_class == NULL) {
|
|
|
|
|
log_error("Cannot find JuiceEventBus class!");
|
|
|
|
|
return JNI_FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get test method
|
|
|
|
|
log_info("Find JuiceEventBus post method...");
|
|
|
|
|
JavaHookCache.post_method = (*env)->GetStaticMethodID(env, JavaHookCache.hook_class, "postClassFileLoadHook", "(Ljava/lang/Class;Ljava/lang/ClassLoader;Ljava/lang/String;Ljava/lang/Object;I[B)[B");
|
|
|
|
|
if (JavaHookCache.post_method == NULL) {
|
|
|
|
|
log_error("Cannot find JuiceEventBus hook method!");
|
|
|
|
|
return JNI_FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set Ready
|
|
|
|
|
log_info("JuiceEventBus is ready!");
|
|
|
|
|
JavaHookCache.isReady = true;
|
|
|
|
|
// Deprcated
|
|
|
|
|
log_warn("JuiceLoaderNative.init() is deprecated, init will be done automatically.");
|
|
|
|
|
return JNI_TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -381,6 +336,7 @@ JNIEXPORT jbyteArray JNICALL loader_getClassBytes(JNIEnv *env, jclass loader_cla
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// JVM 内部使用路径名(. 替换为 /)
|
|
|
|
|
for (char* p = className; *p; p++) {
|
|
|
|
|
if (*p == '.') *p = '/';
|
|
|
|
|
}
|
|
|
|
@@ -433,4 +389,65 @@ JNIEXPORT jbyteArray JNICALL loader_getClassBytesByName(JNIEnv *env, jclass load
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return loader_getClassBytes(env, loader_class, clazz);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JNIEXPORT jboolean JNICALL loader_retransformClass(JNIEnv *env, jclass loader_class, jclass clazz, jbyteArray classBytes, jint length) {
|
|
|
|
|
if (JuiceLoaderNative.jvmti == NULL) {
|
|
|
|
|
log_error("JuiceLoaderNative.jvmti is NULL! (retransformClass)");
|
|
|
|
|
return JNI_FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char *name_dot = get_class_name(env, clazz);
|
|
|
|
|
char* name = strdup(name_dot);
|
|
|
|
|
for (char* p = name; *p; ++p) {
|
|
|
|
|
if (*p == '.') *p = '/';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get class bytes
|
|
|
|
|
jbyte *bytes = (*env)->GetByteArrayElements(env, classBytes, NULL);
|
|
|
|
|
jint len = (*env)->GetArrayLength(env, classBytes);
|
|
|
|
|
|
|
|
|
|
if (RetransformClassCache.size >= RetransformClassCache.capacity) {
|
|
|
|
|
int new_capacity = RetransformClassCache.capacity == 0 ? 4 : RetransformClassCache.capacity * 2;
|
|
|
|
|
RetransformClassCache.arr = realloc(RetransformClassCache.arr, new_capacity * sizeof(ClassBytesCacheType));
|
|
|
|
|
RetransformClassCache.capacity = new_capacity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// save class bytes to cache
|
|
|
|
|
ClassBytesCacheType *entry = &RetransformClassCache.arr[RetransformClassCache.size++];
|
|
|
|
|
entry->name = _strdup(name);
|
|
|
|
|
entry->bytes = malloc(len);
|
|
|
|
|
memcpy(entry->bytes, bytes, len);
|
|
|
|
|
entry->len = len;
|
|
|
|
|
|
|
|
|
|
// clean up
|
|
|
|
|
(*env)->ReleaseByteArrayElements(env, classBytes, bytes, JNI_ABORT);
|
|
|
|
|
|
|
|
|
|
// retransform classes
|
|
|
|
|
jclass classes[1] = { clazz };
|
|
|
|
|
jvmtiError err = (*JuiceLoaderNative.jvmti)->RetransformClasses(JuiceLoaderNative.jvmti, 1, classes);
|
|
|
|
|
if (err != JVMTI_ERROR_NONE) {
|
|
|
|
|
log_error("RetransformClasses failed! [err=%d]", err);
|
|
|
|
|
return JNI_FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log_info("[retransformClass] Class retransformed: %s (len=%d)", entry->name, entry->len);
|
|
|
|
|
return JNI_TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JNIEXPORT jboolean JNICALL loader_retransformClassByName(JNIEnv *env, jclass loader_class, jstring className, jbyteArray classBytes, jint length) {
|
|
|
|
|
const char *name_dot = (*env)->GetStringUTFChars(env, className, NULL);
|
|
|
|
|
char *name = strdup(name_dot);
|
|
|
|
|
for (char *p = name; *p; ++p) if (*p == '.') *p = '/';
|
|
|
|
|
(*env)->ReleaseStringUTFChars(env, className, name_dot);
|
|
|
|
|
|
|
|
|
|
jclass target = (*env)->FindClass(env, name);
|
|
|
|
|
free(name);
|
|
|
|
|
|
|
|
|
|
if (!target) {
|
|
|
|
|
log_error("FindClass failed in retransformClassByName");
|
|
|
|
|
return JNI_FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return loader_retransformClass(env, loader_class, target, classBytes, length);
|
|
|
|
|
}
|