From 2df1e4c80f82cda9e304d141d674fd9b5fc42fbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=AD=B1=E5=91=A8XiaoZhou?= Date: Mon, 13 Oct 2025 09:22:27 +0800 Subject: [PATCH] [Refactor] Utils --- includes/JuiceAgent/InjectParameters.h | 9 +++++ src/agent/libagent.c | 47 ++++++++++++------------ src/utils/CMakeLists.txt | 11 ++++-- src/utils/GlobalUtils.h | 7 +++- src/utils/JVMUtils.c | 49 ++++++++++++++++++++++++++ src/utils/test.c | 5 --- 6 files changed, 97 insertions(+), 31 deletions(-) create mode 100644 src/utils/JVMUtils.c delete mode 100644 src/utils/test.c diff --git a/includes/JuiceAgent/InjectParameters.h b/includes/JuiceAgent/InjectParameters.h index 6ed4902..281d99e 100644 --- a/includes/JuiceAgent/InjectParameters.h +++ b/includes/JuiceAgent/InjectParameters.h @@ -7,4 +7,13 @@ typedef struct InjectParameters{ char loaderDir[MAX_PATH]; } InjectParameters; +/* +typedef struct InjectParameters{ + char *JuiceLoaderJarPath; + // Optional: + char *JuiceLoaderLibPath; + // Optional: + char *EntryJarPath; +} InjectParameters; +*/ #endif \ No newline at end of file diff --git a/src/agent/libagent.c b/src/agent/libagent.c index 8705e69..5f29c60 100644 --- a/src/agent/libagent.c +++ b/src/agent/libagent.c @@ -6,6 +6,7 @@ #include "jvmti.h" #include "InjectParameters.h" #include "log.h" +#include "GlobalUtils.h" #define WIN_X64 extern HINSTANCE hAppInstance; @@ -44,38 +45,38 @@ DWORD WINAPI ThreadProc(LPVOID lpParam) { /// ======== Check Environment ======== /// // Init vars - JavaVM *jvm; - JNIEnv *env; - - // Get JavaVM - if (JNI_GetCreatedJavaVMs(&jvm, 1, NULL) != JNI_OK || !jvm) { - log_error("[%s] JNI_GetCreatedJavaVMs failed", LOGNAME); - MessageBoxA(NULL, "JNI_GetCreatedJavaVMs failed", "Error", MB_OK); - } else { - log_trace("[%s] JNI_GetCreatedJavaVMs success", LOGNAME); - } - - // Get JNIEnv - if ((*jvm)->AttachCurrentThread(jvm, (void **)&env, NULL) != JNI_OK) { - log_error("[%s] AttachCurrentThread failed", LOGNAME); - MessageBoxA(NULL, "Failed to attach current thread to JVM!", "Error", MB_ICONERROR); + jint result = JNI_ERR; + JavaVM *jvm = NULL; + + result = GetCreatedJVM(&jvm); + if (result != JNI_OK) { + log_error("[%s] Failed to get JVM (%d)", LOGNAME, result); + MessageBoxA(NULL, "Failed to get JVM", "Error", MB_OK); return 1; - } else { - log_trace("[%s] AttachCurrentThread success", LOGNAME); } + log_info("[%s] Got JVM", LOGNAME); + + JNIEnv *env = NULL; + result = GetJNIEnv(jvm, &env); + if (result != JNI_OK) { + log_error("[%s] Failed to get JNIEnv (%d)", LOGNAME, result); + MessageBoxA(NULL, "Failed to get JNIEnv", "Error", MB_OK); + return 1; + } + log_info("[%s] Got JNIEnv", LOGNAME); + // Debug: JVM version jint version = (*env)->GetVersion(env); log_trace("[%s] JVM version: 0x%x", LOGNAME, version); // Init and enable jvmti - jvmtiEnv *jvmti; - jint res = (*jvm)->GetEnv(jvm, (void**)&jvmti, JVMTI_VERSION_1_2); - if (res != JNI_OK || jvmti == NULL) { - log_error("[%s] GetEnv failed", LOGNAME); + jvmtiEnv *jvmti = NULL; + result = GetJVMTI(jvm, &jvmti); + if (result != JNI_OK) { + log_error("[%s] Failed to get JVMTI (%d)", LOGNAME, result); + MessageBoxA(NULL, "Failed to get JVMTI", "Error", MB_OK); return 1; - } else { - log_trace("[%s] GetEnv success", LOGNAME); } log_info("[%s] enabled jvmti", LOGNAME); diff --git a/src/utils/CMakeLists.txt b/src/utils/CMakeLists.txt index 54f894e..13545df 100644 --- a/src/utils/CMakeLists.txt +++ b/src/utils/CMakeLists.txt @@ -1,6 +1,13 @@ # src/utils/CMakeLists.txt add_library(utils STATIC - test.c) + JVMUtils.c) -target_include_directories(utils PUBLIC ${CMAKE_CURRENT_LIST_DIR}) \ No newline at end of file +target_include_directories(utils PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_SOURCE_DIR}/includes + ${CMAKE_SOURCE_DIR}/includes/jvm + ${CMAKE_SOURCE_DIR}/includes/jvm/win32) + +target_link_libraries(utils PRIVATE + jvm) \ No newline at end of file diff --git a/src/utils/GlobalUtils.h b/src/utils/GlobalUtils.h index 0bcb082..b629376 100644 --- a/src/utils/GlobalUtils.h +++ b/src/utils/GlobalUtils.h @@ -1,8 +1,13 @@ #ifndef GLOBALUTILS_H #define GLOBALUTILS_H +#include "windows.h" #include "stdio.h" +#include "jni.h" +#include "jvmti.h" -void test(); +jint GetCreatedJVM(JavaVM**); +jint GetJNIEnv(JavaVM*, JNIEnv**); +jint GetJVMTI(JavaVM*, jvmtiEnv**); #endif \ No newline at end of file diff --git a/src/utils/JVMUtils.c b/src/utils/JVMUtils.c new file mode 100644 index 0000000..f054ebf --- /dev/null +++ b/src/utils/JVMUtils.c @@ -0,0 +1,49 @@ +#include "./GlobalUtils.h" + +// Get JavaVM +jint GetCreatedJVM(JavaVM **out_jvm) { + if (!out_jvm) return JNI_ERR; + *out_jvm = NULL; + + jint res = JNI_GetCreatedJavaVMs(out_jvm, 1, NULL); + if (res != JNI_OK || *out_jvm == NULL) { + MessageBoxA(NULL, "JNI_GetCreatedJavaVMs failed", "Error", MB_OK); + return res; + } + return JNI_OK; +} + +// Get JNIEnv +jint GetJNIEnv(JavaVM *jvm, JNIEnv **out_env) { + if (!jvm || !out_env) return JNI_ERR; + *out_env = NULL; + + void *env = NULL; + jint res = (*jvm)->GetEnv(jvm, &env, JNI_VERSION_1_8); + if (res == JNI_OK && env != NULL) { + *out_env = (JNIEnv *)env; + return JNI_OK; + } + + // Attach current thread + res = (*jvm)->AttachCurrentThread(jvm, &env, NULL); + if (res != JNI_OK || env == NULL) { + return res; + } + *out_env = (JNIEnv *)env; + return JNI_OK; +} + +// Get JVMTI +jint GetJVMTI(JavaVM *jvm, jvmtiEnv **out_jvmti) { + if (!jvm || !out_jvmti) return JNI_ERR; + *out_jvmti = NULL; + + jint res = (*jvm)->GetEnv(jvm, (void**)out_jvmti, JVMTI_VERSION_1_2); + if (res == JNI_OK && *out_jvmti != NULL) { + return JNI_OK; + } + + *out_jvmti = NULL; + return res; +} \ No newline at end of file diff --git a/src/utils/test.c b/src/utils/test.c deleted file mode 100644 index fdec43c..0000000 --- a/src/utils/test.c +++ /dev/null @@ -1,5 +0,0 @@ -#include "GlobalUtils.h" - -void test() { - printf("Hello World!\n"); -} \ No newline at end of file