From 34cb16d3858363cdb985a2047c9f9453f2f7424b Mon Sep 17 00:00:00 2001 From: xiaozhou233 Date: Wed, 1 Apr 2026 10:19:40 +0800 Subject: [PATCH] Refactor & Impl: JuiceAgent and init function and AddToClassLoader --- src/libagent/core/init.cpp | 33 +++++++++++++++++++ src/libagent/includes/jni_impl.hpp | 10 ++++++ src/libagent/includes/libagent.hpp | 35 ++++++++++++++++++++ src/libagent/includes/utils.hpp | 15 +++++++++ src/libagent/jni/classloader.cpp | 52 ++++++++++++++++++++++++++++++ src/libagent/libagent.cpp | 50 ++++++++++++++++++++++++++++ 6 files changed, 195 insertions(+) create mode 100644 src/libagent/core/init.cpp create mode 100644 src/libagent/includes/jni_impl.hpp create mode 100644 src/libagent/includes/libagent.hpp create mode 100644 src/libagent/includes/utils.hpp create mode 100644 src/libagent/jni/classloader.cpp diff --git a/src/libagent/core/init.cpp b/src/libagent/core/init.cpp new file mode 100644 index 0000000..f5b52eb --- /dev/null +++ b/src/libagent/core/init.cpp @@ -0,0 +1,33 @@ +#include +#include + +// This function is called by Java to initialize the agent. +JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceagent_api_JuiceAgent_init + (JNIEnv *env, jclass loader_class) { + InitLogger(); + + PLOGI << "Initializing JuiceAgent..."; + + JavaVM* jvm = nullptr; + jvmtiEnv* jvmti = nullptr; + + // Get Java VM + env->GetJavaVM(&jvm); + if (jvm == nullptr) { + PLOGE << "Failed to get JavaVM instance"; + return JNI_FALSE; + } + + // Get JVMTI + 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; + } + + // Invoke EntryPoint + JuiceAgent::Agent& agent = JuiceAgent::Agent::instance(); + agent.init(jvm, env, jvmti); + + return JNI_TRUE; +} diff --git a/src/libagent/includes/jni_impl.hpp b/src/libagent/includes/jni_impl.hpp new file mode 100644 index 0000000..c182a6d --- /dev/null +++ b/src/libagent/includes/jni_impl.hpp @@ -0,0 +1,10 @@ +#pragma once + +#include +#include +#include +#include +#include + +// Generated by JuiceAgent-API +#include diff --git a/src/libagent/includes/libagent.hpp b/src/libagent/includes/libagent.hpp new file mode 100644 index 0000000..6cce749 --- /dev/null +++ b/src/libagent/includes/libagent.hpp @@ -0,0 +1,35 @@ +#pragma once + +#include +#include +#include + +namespace JuiceAgent { + class Agent { + private: + JavaVM* jvm; + jvmtiEnv* jvmti; + + private: + Agent() = default; + ~Agent() = default; + + public: + // ===== Singleton ===== + static Agent& instance(); + + Agent(const Agent&) = delete; + Agent& operator=(const Agent&) = delete; + + // EntryPoint + bool init(JavaVM* jvm, JNIEnv* env, jvmtiEnv* jvmti); + + // getters + JavaVM* get_jvm() const { return jvm; } + jvmtiEnv* get_jvmti() const { return jvmti; } + + // setters + void set_jvm(JavaVM* jvm) { this->jvm = jvm; } + void set_jvmti(jvmtiEnv* jvmti) { this->jvmti = jvmti; } + }; +} \ No newline at end of file diff --git a/src/libagent/includes/utils.hpp b/src/libagent/includes/utils.hpp new file mode 100644 index 0000000..48d18a3 --- /dev/null +++ b/src/libagent/includes/utils.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include + +inline bool check_env(JuiceAgent::Agent& agent) { + if (agent.get_jvm() == nullptr) { + PLOGE << "Failed to get JavaVM instance"; + return false; + } + if (agent.get_jvmti() == nullptr) { + PLOGE << "Failed to get JVMTI environment"; + return false; + } + return true; +} \ No newline at end of file diff --git a/src/libagent/jni/classloader.cpp b/src/libagent/jni/classloader.cpp new file mode 100644 index 0000000..547945f --- /dev/null +++ b/src/libagent/jni/classloader.cpp @@ -0,0 +1,52 @@ +#include + +// Add jar to bootstrap classloader search path +JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceagent_api_JuiceAgent_addToBootstrapClassLoaderSearch + (JNIEnv *env, jclass, jstring jar_path) { + auto& agent = JuiceAgent::Agent::instance(); + + if (!check_env(agent)) + return JNI_FALSE; + + const char* j_jar_path = env->GetStringUTFChars(jar_path, nullptr); + if (!j_jar_path) { + PLOGE << "Path is NULL!"; + return JNI_FALSE; + } + + jvmtiError result = agent.get_jvmti()->AddToBootstrapClassLoaderSearch(j_jar_path); + if (result != JVMTI_ERROR_NONE) { + PLOGE.printf("Cannot AddToBootstrapClassLoaderSearch [result=%d]", result); + env->ReleaseStringUTFChars(jar_path, j_jar_path); + return JNI_FALSE; + } + PLOGI << "AddToBootstrapClassLoaderSearch: " << j_jar_path; + + env->ReleaseStringUTFChars(jar_path, j_jar_path); + return JNI_TRUE; + } + +// Add jar to system classloader search path +JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceagent_api_JuiceAgent_addToSystemClassLoaderSearch + (JNIEnv *env, jclass, jstring jar_path) { + auto& agent = JuiceAgent::Agent::instance(); + if (!check_env(agent)) + return JNI_FALSE; + + const char* j_jar_path = env->GetStringUTFChars(jar_path, nullptr); + if (!j_jar_path) { + PLOGE << "Path is NULL!"; + return JNI_FALSE; + } + + jvmtiError result = agent.get_jvmti()->AddToSystemClassLoaderSearch(j_jar_path); + if (result != JVMTI_ERROR_NONE) { + PLOGE.printf("Cannot AddToSystemClassLoaderSearch [result=%d]", result); + env->ReleaseStringUTFChars(jar_path, j_jar_path); + return JNI_FALSE; + } + PLOGI << "AddToSystemClassLoaderSearch: " << j_jar_path; + + env->ReleaseStringUTFChars(jar_path, j_jar_path); + return JNI_TRUE; + } \ No newline at end of file diff --git a/src/libagent/libagent.cpp b/src/libagent/libagent.cpp index e69de29..bab114c 100644 --- a/src/libagent/libagent.cpp +++ b/src/libagent/libagent.cpp @@ -0,0 +1,50 @@ +#include + +namespace JuiceAgent { + // ===== Singleton ===== + Agent& Agent::instance() { + static Agent instance; + return instance; + } + + bool Agent::init(JavaVM* jvm, JNIEnv* env, jvmtiEnv* jvmti) { + PLOGI << "JuiceAgent Native EntryPoint Invoked!"; + + // ======== neccessary environment check ======== + if (jvm == nullptr) { + PLOGE << "Failed to initialize JuiceAgent: jvm is null"; + return false; + } + if (env == nullptr) { + PLOGE << "Failed to initialize JuiceAgent: env is null"; + return false; + } + if (jvmti == nullptr) { + PLOGE << "Failed to initialize JuiceAgent: jvmti is null"; + return false; + } + + set_jvm(jvm); + set_jvmti(jvmti); + + PLOGD << "env: " << env << ", jvmti: " << jvmti; + + // ======== Application ability ======== + jvmtiCapabilities 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 false; + } + + PLOGI << "Abilities added successfully"; + + return true; + } +} \ No newline at end of file