From 386dc14a01dd7850705693be2b07e6e533c4923d Mon Sep 17 00:00:00 2001 From: xiaozhou233 Date: Sun, 9 Nov 2025 15:33:07 +0800 Subject: [PATCH] [WIP] move juiceloader --- CMakeLists.txt | 1 + includes/JuiceAgent/JuiceLoader.java | 33 ++++++ .../cn_xiaozhou233_juiceloader_JuiceLoader.h | 109 +++++++++++++++++ src/agent/juiceloader.cpp | 111 ------------------ src/agent/libagent.cpp | 11 +- src/juiceloader/CMakeLists.txt | 1 + .../NativeFunctions.cpp | 85 +++++++++++--- .../include/juiceloader.h | 1 + src/juiceloader/juiceloader.cpp | 66 +++++++++++ 9 files changed, 279 insertions(+), 139 deletions(-) create mode 100644 includes/JuiceAgent/JuiceLoader.java create mode 100644 includes/JuiceAgent/cn_xiaozhou233_juiceloader_JuiceLoader.h delete mode 100644 src/agent/juiceloader.cpp create mode 100644 src/juiceloader/CMakeLists.txt rename src/{agent => juiceloader}/NativeFunctions.cpp (82%) rename src/{agent => juiceloader}/include/juiceloader.h (98%) create mode 100644 src/juiceloader/juiceloader.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f3c5a2c..fec370c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,4 +32,5 @@ include(cmake/JuiceModule.cmake) # modules add_juice_module(agent) add_juice_module(injector) +add_juice_module(juiceloader) add_subdirectory(${CMAKE_SOURCE_DIR}/src/executable) \ No newline at end of file diff --git a/includes/JuiceAgent/JuiceLoader.java b/includes/JuiceAgent/JuiceLoader.java new file mode 100644 index 0000000..f25d706 --- /dev/null +++ b/includes/JuiceAgent/JuiceLoader.java @@ -0,0 +1,33 @@ +package cn.xiaozhou233.juiceloader; + +public class JuiceLoader { + // Init jni/jvmti, and register events + public static native boolean init(); + + // Add jar to Bootstrap ClassLoader + public static native boolean injectJar(String jarPath); + + // Redefine class + // Notice: classname is the full name of the class, e.g. "java/lang/String" + public static native boolean redefineClass(Class clazz, byte[] classBytes, int length); + public static native boolean redefineClassByName(String className, byte[] classBytes, int length); + + // Get Classes + public static native Class[] getLoadedClasses(); + + // Get ClassBytes + public static native byte[] getClassBytes(Class clazz); + public static native byte[] getClassBytesByName(String className); + + // Retransform + public static native boolean retransformClass(Class clazz, byte[] classBytes, int length); + public static native boolean retransformClassByName(String className, byte[] classBytes, int length); + + // Get Class + public static native Class getClassByName(String className); + + // Thread/Inject + public static native Thread nativeGetThreadByName(String name); + public static native ClassLoader nativeInjectJarToThread(Thread thread, String jarPath); + +} diff --git a/includes/JuiceAgent/cn_xiaozhou233_juiceloader_JuiceLoader.h b/includes/JuiceAgent/cn_xiaozhou233_juiceloader_JuiceLoader.h new file mode 100644 index 0000000..6a13d83 --- /dev/null +++ b/includes/JuiceAgent/cn_xiaozhou233_juiceloader_JuiceLoader.h @@ -0,0 +1,109 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class cn_xiaozhou233_juiceloader_JuiceLoader */ + +#ifndef _Included_cn_xiaozhou233_juiceloader_JuiceLoader +#define _Included_cn_xiaozhou233_juiceloader_JuiceLoader +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: cn_xiaozhou233_juiceloader_JuiceLoader + * Method: init + * Signature: ()Z + */ +JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_init + (JNIEnv *, jclass); + +/* + * Class: cn_xiaozhou233_juiceloader_JuiceLoader + * Method: injectJar + * Signature: (Ljava/lang/String;)Z + */ +JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_injectJar + (JNIEnv *, jclass, jstring); + +/* + * Class: cn_xiaozhou233_juiceloader_JuiceLoader + * Method: redefineClass + * Signature: (Ljava/lang/Class;[BI)Z + */ +JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_redefineClass + (JNIEnv *, jclass, jclass, jbyteArray, jint); + +/* + * Class: cn_xiaozhou233_juiceloader_JuiceLoader + * Method: redefineClassByName + * Signature: (Ljava/lang/String;[BI)Z + */ +JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_redefineClassByName + (JNIEnv *, jclass, jstring, jbyteArray, jint); + +/* + * Class: cn_xiaozhou233_juiceloader_JuiceLoader + * Method: getLoadedClasses + * Signature: ()[Ljava/lang/Class; + */ +JNIEXPORT jobjectArray JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_getLoadedClasses + (JNIEnv *, jclass); + +/* + * Class: cn_xiaozhou233_juiceloader_JuiceLoader + * Method: getClassBytes + * Signature: (Ljava/lang/Class;)[B + */ +JNIEXPORT jbyteArray JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_getClassBytes + (JNIEnv *, jclass, jclass); + +/* + * Class: cn_xiaozhou233_juiceloader_JuiceLoader + * Method: getClassBytesByName + * Signature: (Ljava/lang/String;)[B + */ +JNIEXPORT jbyteArray JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_getClassBytesByName + (JNIEnv *, jclass, jstring); + +/* + * Class: cn_xiaozhou233_juiceloader_JuiceLoader + * Method: retransformClass + * Signature: (Ljava/lang/Class;[BI)Z + */ +JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_retransformClass + (JNIEnv *, jclass, jclass, jbyteArray, jint); + +/* + * Class: cn_xiaozhou233_juiceloader_JuiceLoader + * Method: retransformClassByName + * Signature: (Ljava/lang/String;[BI)Z + */ +JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_retransformClassByName + (JNIEnv *, jclass, jstring, jbyteArray, jint); + +/* + * Class: cn_xiaozhou233_juiceloader_JuiceLoader + * Method: getClassByName + * Signature: (Ljava/lang/String;)Ljava/lang/Class; + */ +JNIEXPORT jclass JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_getClassByName + (JNIEnv *, jclass, jstring); + +/* + * Class: cn_xiaozhou233_juiceloader_JuiceLoader + * Method: nativeGetThreadByName + * Signature: (Ljava/lang/String;)Ljava/lang/Thread; + */ +JNIEXPORT jobject JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_nativeGetThreadByName + (JNIEnv *, jclass, jstring); + +/* + * Class: cn_xiaozhou233_juiceloader_JuiceLoader + * Method: nativeInjectJarToThread + * Signature: (Ljava/lang/Thread;Ljava/lang/String;)Ljava/lang/ClassLoader; + */ +JNIEXPORT jobject JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_nativeInjectJarToThread + (JNIEnv *, jclass, jobject, jstring); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/src/agent/juiceloader.cpp b/src/agent/juiceloader.cpp deleted file mode 100644 index 05120ce..0000000 --- a/src/agent/juiceloader.cpp +++ /dev/null @@ -1,111 +0,0 @@ -// JVM -#include -#include -#include -#include - -JuiceLoaderNativeType JuiceLoaderNative; - -static int RegisterNatives(JNIEnv *env) { - const char* native_class = "cn/xiaozhou233/juiceloader/JuiceLoader"; - JNINativeMethod methods[] = { - {(char*)"init", (char*)"()Z", (void *)&loader_init}, - {(char*)"injectJar", (char*)"(Ljava/lang/String;)Z", (void *)&loader_injectJar}, - {(char*)"redefineClass", (char*)"(Ljava/lang/Class;[BI)Z", (void *)&loader_redefineClass_clazz}, - {(char*)"redefineClassByName", (char*)"(Ljava/lang/String;[BI)Z", (void *)&loader_redefineClass_className}, - {(char*)"getLoadedClasses", (char*)"()[Ljava/lang/Class;", (void *)&loader_getLoadedClasses}, - {(char*)"getClassBytes", (char*)"(Ljava/lang/Class;)[B", (void *)&loader_getClassBytes}, - {(char*)"getClassBytesByName", (char*)"(Ljava/lang/String;)[B", (void *)&loader_getClassBytesByName}, - {(char*)"retransformClass", (char*)"(Ljava/lang/Class;[BI)Z", (void *)&loader_retransformClass}, - {(char*)"retransformClassByName", (char*)"(Ljava/lang/String;[BI)Z", (void *)&loader_retransformClassByName}, - {(char*)"getClassByName", (char*)"(Ljava/lang/String;)Ljava/lang/Class;", (void *)&loader_getClassByName}, - // public static native Thread nativeGetThreadByName(String var0); - {(char*)"nativeGetThreadByName", (char*)"(Ljava/lang/String;)Ljava/lang/Thread;", (void *)&loader_nativeGetThreadByName}, - // public static native ClassLoader nativeInjectJarToThread(Thread var0, String var1); - {(char*)"nativeInjectJarToThread", (char*)"(Ljava/lang/Thread;Ljava/lang/String;)Ljava/lang/ClassLoader;", (void *)&loader_nativeInjectJarToThread} - }; - - // Find Native(JNI) Class - jclass clazz = env->FindClass("cn/xiaozhou233/juiceloader/JuiceLoader"); - if (clazz == NULL) { - if (env->ExceptionCheck()) { - env->ExceptionDescribe(); - env->ExceptionClear(); - } - PLOGE.printf("[-] Failed to initialize libjuiceloader: JuiceLoader class not found"); - return -1; - } - - // Dynamicly register native methods - jint result = env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); - if (result != JNI_OK) { - PLOGE << "Failed to register native methods: " << result; - return JNI_ERR; - } - - return JNI_OK; -} - -static int ConfigureJVMTI(JNIEnv *env, _jvmtiEnv *jvmti) { - // Retransform Class Cache : to store classes that need to be retransformed - RetransformClassCache.arr = NULL; - RetransformClassCache.size = 0; - RetransformClassCache.capacity = 0; - - // Application ability - jvmtiCapabilities caps; - memset(&caps, 0, sizeof(caps)); - caps.can_get_bytecodes = 1; - caps.can_redefine_classes = 1; - caps.can_redefine_any_class = 1; - caps.can_retransform_classes = 1; - caps.can_retransform_any_class = 1; - caps.can_generate_all_class_hook_events = 1; - jint result = jvmti->AddCapabilities(&caps); - if (result != JNI_OK) { - PLOGE << "Failed to add capabilities: " << result; - return JNI_ERR; - } - - // Register callbacks - jvmtiEventCallbacks callbacks; - memset(&callbacks, 0, sizeof(callbacks)); - callbacks.ClassFileLoadHook = &ClassFileLoadHook; - jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks)); - - // Enable events - jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, NULL); - - return JNI_OK; -} - -int InitJuiceLoader(JNIEnv *env, _jvmtiEnv *jvmti) { - PLOGI.printf("[*] libjuiceloader Version %d.%d Build %d", PROJECT_VERSION_MAJOR, PROJECT_VERSION_MINOR, PROJECT_BUILD_NUMBER); - // Step 1: Check if env/jvmti is null - PLOGD.printf("env: %p, jvmti: %p", env, jvmti); - if (env == nullptr) { - PLOGE.printf("[-] Failed to initialize libjuiceloader: env is null"); - return -1; - } - if (jvmti == nullptr) { - PLOGE.printf("[-] Failed to initialize libjuiceloader: jvmti is null"); - return -1; - } - JuiceLoaderNative.jvmti = jvmti; - - // Step 2: Register Native Methods - if (RegisterNatives(env) != JNI_OK) { - PLOGE.printf("[-] Failed to initialize libjuiceloader: RegisterNatives failed"); - return -1; - } - - // Step 3: Configure JVMTI - if (ConfigureJVMTI(env, jvmti) != JNI_OK) { - PLOGE.printf("[-] Failed to initialize libjuiceloader: ConfigureJVMTI failed"); - return -1; - } - - return 0; -} - - diff --git a/src/agent/libagent.cpp b/src/agent/libagent.cpp index 0c9dd24..f145450 100644 --- a/src/agent/libagent.cpp +++ b/src/agent/libagent.cpp @@ -1,7 +1,6 @@ #include #include "Logger.hpp" #include -#include #include // For Java VM @@ -217,19 +216,13 @@ int InitJuiceAgent(const char* ConfigDir) { return 1; } - // Step 3: Invoke JuiceLoader Native Init - if (InitJuiceLoader(JuiceAgent.env, JuiceAgent.jvmti) != JNI_OK) { - PLOGE << "Failed to invoke JuiceLoader Native Init: " << result; - return 1; - } - - // Step 4: Invoke JuiceLoader Init + // Step 3: Invoke JuiceLoader Init if (InvokeJuiceLoaderInit(ConfigDir) != 0) { PLOGE << "Failed to invoke JuiceLoader Init code=" << result; return 1; } - // Step 5: Done, cleanup + // Step 4: Done, cleanup return 0; } diff --git a/src/juiceloader/CMakeLists.txt b/src/juiceloader/CMakeLists.txt new file mode 100644 index 0000000..7d826db --- /dev/null +++ b/src/juiceloader/CMakeLists.txt @@ -0,0 +1 @@ +# src/juiceloader/CMakeLists.txt diff --git a/src/agent/NativeFunctions.cpp b/src/juiceloader/NativeFunctions.cpp similarity index 82% rename from src/agent/NativeFunctions.cpp rename to src/juiceloader/NativeFunctions.cpp index 65ee3fa..af2e7b7 100644 --- a/src/agent/NativeFunctions.cpp +++ b/src/juiceloader/NativeFunctions.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -97,12 +98,40 @@ void JNICALL ClassFileLoadHook( /// ============ JNI Function ============ /// -JNIEXPORT jboolean JNICALL loader_init(JNIEnv *env, jclass loader_class) { - PLOGW << "JuiceLoaderNative.init() is deprecated, init will be done automatically."; +C_EXPORT +JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_init +(JNIEnv *env, jclass loader_class) { + PLOGI << "Invoked!"; + + // Get JavaVM + JavaVM* jvm = nullptr; + env->GetJavaVM(&jvm); + if (jvm == nullptr) { + PLOGE << "Failed to get JavaVM instance"; + return JNI_FALSE; + } + + // Get JVMTI + jvmtiEnv* jvmti = nullptr; + jvmtiError err = static_cast(jvm->GetEnv(reinterpret_cast(&jvmti), JVMTI_VERSION_1_2)); + if (err != JVMTI_ERROR_NONE || jvmti == nullptr) { + PLOGE << "Failed to get JVMTI environment, error: " << err; + return JNI_FALSE; + } + + int result = InitJuiceLoader(env, jvmti); + if (result != 0) { + PLOGE << "Failed to initialize JuiceLoader, error code: " << result; + return JNI_FALSE; + } + + PLOGI << "JuiceLoader initialized successfully"; return JNI_TRUE; } -JNIEXPORT jboolean JNICALL loader_injectJar(JNIEnv *env, jclass loader_class, jstring jarPath) { +C_EXPORT +JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_injectJar +(JNIEnv *env, jclass loader_class, jstring jarPath) { PLOGI << "Invoked!"; const char *pathStr = env->GetStringUTFChars(jarPath, NULL); if (!pathStr) { @@ -127,7 +156,10 @@ JNIEXPORT jboolean JNICALL loader_injectJar(JNIEnv *env, jclass loader_class, js PLOGI << "Jar injected: " << pathStr; return JNI_TRUE; } -JNIEXPORT jboolean JNICALL loader_redefineClass_clazz(JNIEnv *env, jclass loader_class, jclass clazz, jbyteArray classBytes, jint length) { + +C_EXPORT +JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_redefineClass +(JNIEnv *env, jclass loader_class, jclass clazz, jbyteArray classBytes, jint length) { PLOGI << "Invoked!"; if (!JuiceLoaderNative.jvmti) { PLOGE.printf("JuiceLoaderNative.jvmti is NULL! [jvmti=%p]", JuiceLoaderNative.jvmti); @@ -157,7 +189,9 @@ JNIEXPORT jboolean JNICALL loader_redefineClass_clazz(JNIEnv *env, jclass loader return JNI_TRUE; } -JNIEXPORT jboolean JNICALL loader_redefineClass_className(JNIEnv *env, jclass loader_class, jstring className, jbyteArray classBytes, jint length) { +C_EXPORT +JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_redefineClassByName +(JNIEnv *env, jclass loader_class, jstring className, jbyteArray classBytes, jint length) { PLOGI << "redefineClass_className Invoked!"; if (!JuiceLoaderNative.jvmti) { PLOGE.printf("JuiceLoaderNative.jvmti is NULL! [jvmti=%p]", JuiceLoaderNative.jvmti); @@ -177,12 +211,14 @@ JNIEXPORT jboolean JNICALL loader_redefineClass_className(JNIEnv *env, jclass lo return JNI_FALSE; } - jboolean ret = loader_redefineClass_clazz(env, loader_class, clazz, classBytes, length); + jboolean ret = Java_cn_xiaozhou233_juiceloader_JuiceLoader_redefineClass(env, loader_class, clazz, classBytes, length); env->ReleaseStringUTFChars(className, cname); return ret; } -JNIEXPORT jobjectArray JNICALL loader_getLoadedClasses(JNIEnv *env, jclass loader_class) { +C_EXPORT +JNIEXPORT jobjectArray JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_getLoadedClasses +(JNIEnv *env, jclass loader_class) { PLOGI << "loader_getLoadedClasses invoked!"; if (!JuiceLoaderNative.jvmti) { @@ -209,7 +245,9 @@ JNIEXPORT jobjectArray JNICALL loader_getLoadedClasses(JNIEnv *env, jclass loade return classArray; } -JNIEXPORT jbyteArray JNICALL loader_getClassBytes(JNIEnv *env, jclass loader_class, jclass clazz) { +C_EXPORT +JNIEXPORT jbyteArray JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_getClassBytes +(JNIEnv *env, jclass loader_class, jclass clazz) { PLOGI << "getClassBytes Invoked!"; if (!JuiceLoaderNative.jvmti) { PLOGE << "JuiceLoaderNative.jvmti is NULL!"; @@ -254,7 +292,9 @@ JNIEXPORT jbyteArray JNICALL loader_getClassBytes(JNIEnv *env, jclass loader_cla return result; } -JNIEXPORT jbyteArray JNICALL loader_getClassBytesByName(JNIEnv *env, jclass loader_class, jstring className) { +C_EXPORT +JNIEXPORT jbyteArray JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_getClassBytesByName +(JNIEnv *env, jclass loader_class, jstring className) { PLOGI << "getClassBytesByName Invoked!"; if (!JuiceLoaderNative.jvmti) { PLOGE << "JuiceLoaderNative.jvmti is NULL!"; @@ -270,9 +310,12 @@ JNIEXPORT jbyteArray JNICALL loader_getClassBytesByName(JNIEnv *env, jclass load return NULL; } - return loader_getClassBytes(env, loader_class, clazz); + return Java_cn_xiaozhou233_juiceloader_JuiceLoader_getClassBytes(env, loader_class, clazz); } -JNIEXPORT jboolean JNICALL loader_retransformClass(JNIEnv *env, jclass loader_class, jclass clazz, jbyteArray classBytes, jint length) { + +C_EXPORT +JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_retransformClass +(JNIEnv *env, jclass loader_class, jclass clazz, jbyteArray classBytes, jint length) { if (!JuiceLoaderNative.jvmti) { PLOGE << "JuiceLoaderNative.jvmti is NULL! (retransformClass)"; return JNI_FALSE; @@ -312,7 +355,9 @@ JNIEXPORT jboolean JNICALL loader_retransformClass(JNIEnv *env, jclass loader_cl return JNI_TRUE; } -JNIEXPORT jboolean JNICALL loader_retransformClassByName(JNIEnv *env, jclass loader_class, jstring className, jbyteArray classBytes, jint length) { +C_EXPORT +JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_retransformClassByName +(JNIEnv *env, jclass loader_class, jstring className, jbyteArray classBytes, jint length) { const char *name_dot = env->GetStringUTFChars(className, NULL); char *name = _strdup(name_dot); for (char *p = name; *p; ++p) if (*p == '.') *p = '/'; @@ -326,10 +371,12 @@ JNIEXPORT jboolean JNICALL loader_retransformClassByName(JNIEnv *env, jclass loa return JNI_FALSE; } - return loader_retransformClass(env, loader_class, target, classBytes, length); + return Java_cn_xiaozhou233_juiceloader_JuiceLoader_retransformClass(env, loader_class, target, classBytes, length); } -JNIEXPORT jclass JNICALL loader_getClassByName(JNIEnv *env, jclass loader_class, jstring className) { +C_EXPORT +JNIEXPORT jclass JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_getClassByName +(JNIEnv *env, jclass loader_class, jstring className) { if (className == NULL) { return NULL; @@ -354,8 +401,8 @@ JNIEXPORT jclass JNICALL loader_getClassByName(JNIEnv *env, jclass loader_class, } // static native Thread getThreadByName(String name) -JNIEXPORT jobject JNICALL -loader_nativeGetThreadByName +C_EXPORT +JNIEXPORT jobject JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_nativeGetThreadByName (JNIEnv* env, jclass, jstring jName) { // Convert jstring to std::string @@ -420,9 +467,9 @@ loader_nativeGetThreadByName // static native ClassLoader injectJarToThread(Thread thread, String jarPath) -JNIEXPORT jobject JNICALL -loader_nativeInjectJarToThread - (JNIEnv* env, jclass, jobject threadObj, jstring jJarPath) +C_EXPORT +JNIEXPORT jobject JNICALL Java_cn_xiaozhou233_juiceloader_JuiceLoader_nativeInjectJarToThread + (JNIEnv* env, jclass, jobject threadObj, jstring jJarPath) { // Convert jstring -> std::string const char* cJarPath = env->GetStringUTFChars(jJarPath, nullptr); diff --git a/src/agent/include/juiceloader.h b/src/juiceloader/include/juiceloader.h similarity index 98% rename from src/agent/include/juiceloader.h rename to src/juiceloader/include/juiceloader.h index 3201719..ddba7ae 100644 --- a/src/agent/include/juiceloader.h +++ b/src/juiceloader/include/juiceloader.h @@ -23,6 +23,7 @@ typedef struct { extern RetransformClassCacheType RetransformClassCache; +#define C_EXPORT extern "C" __declspec(dllexport) int InitJuiceLoader(JNIEnv *env, jvmtiEnv *jvmti); diff --git a/src/juiceloader/juiceloader.cpp b/src/juiceloader/juiceloader.cpp new file mode 100644 index 0000000..eb5dde3 --- /dev/null +++ b/src/juiceloader/juiceloader.cpp @@ -0,0 +1,66 @@ +// JVM +#include +#include +#include +#include + +JuiceLoaderNativeType JuiceLoaderNative; + +static int ConfigureJVMTI(JNIEnv *env, _jvmtiEnv *jvmti) { + // Retransform Class Cache : to store classes that need to be retransformed + RetransformClassCache.arr = NULL; + RetransformClassCache.size = 0; + RetransformClassCache.capacity = 0; + + // Application ability + jvmtiCapabilities caps; + memset(&caps, 0, sizeof(caps)); + caps.can_get_bytecodes = 1; + caps.can_redefine_classes = 1; + caps.can_redefine_any_class = 1; + caps.can_retransform_classes = 1; + caps.can_retransform_any_class = 1; + caps.can_generate_all_class_hook_events = 1; + jint result = jvmti->AddCapabilities(&caps); + if (result != JNI_OK) { + PLOGE << "Failed to add capabilities: " << result; + return JNI_ERR; + } + + // Register callbacks + jvmtiEventCallbacks callbacks; + memset(&callbacks, 0, sizeof(callbacks)); + callbacks.ClassFileLoadHook = &ClassFileLoadHook; + jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks)); + + // Enable events + jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, NULL); + + return JNI_OK; +} + +int InitJuiceLoader(JNIEnv *env, _jvmtiEnv *jvmti) { + PLOGI.printf("[*] libjuiceloader Version %d.%d Build %d", PROJECT_VERSION_MAJOR, PROJECT_VERSION_MINOR, PROJECT_BUILD_NUMBER); + // Step 1: Check if env/jvmti is null + PLOGD.printf("env: %p, jvmti: %p", env, jvmti); + if (env == nullptr) { + PLOGE.printf("[-] Failed to initialize libjuiceloader: env is null"); + return -1; + } + if (jvmti == nullptr) { + PLOGE.printf("[-] Failed to initialize libjuiceloader: jvmti is null"); + return -1; + } + JuiceLoaderNative.jvmti = jvmti; + + + // Step 2: Configure JVMTI + if (ConfigureJVMTI(env, jvmti) != JNI_OK) { + PLOGE.printf("[-] Failed to initialize libjuiceloader: ConfigureJVMTI failed"); + return -1; + } + + return 0; +} + +