From fc5d23109d36ba75e1e57f55faaeb8b08c814137 Mon Sep 17 00:00:00 2001 From: xiaozhou233 Date: Wed, 29 Apr 2026 17:24:40 +0800 Subject: [PATCH] feat: remove Remapper module and associated JNI functions --- examples/config_empty.toml | 4 - src/libagent/jni/remapper.cpp | 35 ---- src/libagent/modules/Remapper.cpp | 281 ------------------------------ 3 files changed, 320 deletions(-) delete mode 100644 src/libagent/jni/remapper.cpp delete mode 100644 src/libagent/modules/Remapper.cpp diff --git a/examples/config_empty.toml b/examples/config_empty.toml index 73e7fdc..80869e4 100644 --- a/examples/config_empty.toml +++ b/examples/config_empty.toml @@ -13,10 +13,6 @@ JarPath = "" EntryClass = "" EntryMethod = "" -[JuiceAgent.Modules.Remapper] -Enabled = false -JuiceRemapperJarPath = "" - [JuiceAgent.Modules.Dumper] Enabled = false DumpDir = "" \ No newline at end of file diff --git a/src/libagent/jni/remapper.cpp b/src/libagent/jni/remapper.cpp deleted file mode 100644 index 0592ccc..0000000 --- a/src/libagent/jni/remapper.cpp +++ /dev/null @@ -1,35 +0,0 @@ -#include -#include - -JNIEXPORT void JNICALL Java_cn_xiaozhou233_juiceagent_api_JuiceAgent_addRemapperWhiteList - (JNIEnv *env, jclass, jstring package_name) { - std::string pkg_name = env->GetStringUTFChars(package_name, nullptr); - if (pkg_name.empty()) { - PLOGW << "package name is empty"; - return; - } - - remapperWhiteList.insert(pkg_name); - - PLOGI << "add remapper white list: " << pkg_name; - } - -JNIEXPORT void JNICALL Java_cn_xiaozhou233_juiceagent_api_JuiceAgent_removeRemapperWhiteList - (JNIEnv *env, jclass, jstring package_name) { - std::string pkg_name = env->GetStringUTFChars(package_name, nullptr); - if (pkg_name.empty()) { - PLOGW << "package name is empty"; - return; - } - - remapperWhiteList.erase(pkg_name); - - PLOGI << "remove remapper white list: " << pkg_name; - } - -JNIEXPORT void JNICALL Java_cn_xiaozhou233_juiceagent_api_JuiceAgent_clearRemapperWhiteList - (JNIEnv *env, jclass) { - remapperWhiteList.clear(); - - PLOGI << "clear remapper white list"; - } diff --git a/src/libagent/modules/Remapper.cpp b/src/libagent/modules/Remapper.cpp deleted file mode 100644 index c0b93e8..0000000 --- a/src/libagent/modules/Remapper.cpp +++ /dev/null @@ -1,281 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace JuiceAgent::Core::Modules { - -struct RemapperConfig { - bool enabled = false; - std::string JuiceRemapperJarPath; -}; - -static RemapperConfig config; -static JuiceAgent::Agent& agent = JuiceAgent::Agent::instance(); - -class RemapperModule : public ModuleBase { - using super = ModuleBase; - - struct _Remapper { - jclass handler_class = nullptr; - jmethodID onClassFileLoadMethod = nullptr; - jmethodID onMethodEntryMethod = nullptr; - jmethodID onMethodExitMethod = nullptr; - } Remapper; - -public: - std::string name() const override { - return "Remapper"; - } - -protected: - bool on_init() override { - auto& cfg = agent.get_config(); - - config.enabled = cfg.get( - "JuiceAgent.Modules.Remapper.Enabled", - false - ); - - config.JuiceRemapperJarPath = cfg.get( - "JuiceAgent.Modules.Remapper.JuiceRemapperJarPath", - "./JuiceRemapper.jar", - true - ); - - if (!config.enabled) { - return true; - } - - if (agent.get_jvmti()->AddToSystemClassLoaderSearch( - config.JuiceRemapperJarPath.c_str() - ) != JNI_OK) { - PLOGE << "Failed to load JuiceRemapper.jar"; - return false; - } - - JNIEnv* env = agent.get_env(); - - jclass local = env->FindClass( - "cn/xiaozhou233/juiceremapper/api/Handler" - ); - - if (local == nullptr) { - check_and_clear_exception(env, "FindClass Handler"); - PLOGE << "Failed to find Handler"; - return false; - } - - Remapper.handler_class = - static_cast(env->NewGlobalRef(local)); - - env->DeleteLocalRef(local); - - if (Remapper.handler_class == nullptr) { - return false; - } - - Remapper.onClassFileLoadMethod = env->GetStaticMethodID( - Remapper.handler_class, - "onClassFileLoadHook", - "(Ljava/lang/Class;Ljava/lang/ClassLoader;Ljava/lang/String;Ljava/lang/Object;I[B)[B" - ); - - if (Remapper.onClassFileLoadMethod == nullptr) { - check_and_clear_exception(env, "onClassFileLoadHook"); - return false; - } - - Remapper.onMethodEntryMethod = env->GetStaticMethodID( - Remapper.handler_class, - "onMethodEntry", - "(Ljava/lang/Thread;Ljava/lang/reflect/Method;)V" - ); - - if (Remapper.onMethodEntryMethod == nullptr) { - check_and_clear_exception(env, "onMethodEntry"); - return false; - } - - Remapper.onMethodExitMethod = env->GetStaticMethodID( - Remapper.handler_class, - "onMethodExit", - "(Ljava/lang/Thread;Ljava/lang/reflect/Method;ZLjava/lang/Object;)V" - ); - - if (Remapper.onMethodExitMethod == nullptr) { - check_and_clear_exception(env, "onMethodExit"); - return false; - } - - return true; - } - - bool on_start() override { - if (!config.enabled) { - return true; - } - - auto& bus = agent.get_eventbus(); - - _classFileToken = bus.subscribe( - [this](const auto& e) { - on_class_file_load_hook(e); - } - ); - - _methodEntryToken = bus.subscribe( - [this](const auto& e) { - on_method_entry_hook(e); - } - ); - - _methodExitToken = bus.subscribe( - [this](const auto& e) { - on_method_exit_hook(e); - } - ); - - return true; - } - - void on_stop() override { - if (!config.enabled) { - return; - } - - auto& bus = agent.get_eventbus(); - - if (_classFileToken != 0) { - bus.unsubscribe(_classFileToken); - } - - if (_methodEntryToken != 0) { - bus.unsubscribe(_methodEntryToken); - } - - if (_methodExitToken != 0) { - bus.unsubscribe(_methodExitToken); - } - - _classFileToken = 0; - _methodEntryToken = 0; - _methodExitToken = 0; - - JNIEnv* env = agent.get_env(); - - if (env != nullptr && Remapper.handler_class != nullptr) { - env->DeleteGlobalRef(Remapper.handler_class); - Remapper.handler_class = nullptr; - } - } - -private: - void on_class_file_load_hook(const EventClassFileLoadHook& event) { - std::string_view name = event.name ? event.name : ""; - - if (name.starts_with("cn/xiaozhou233/juiceremapper/")) { - return; - } - - JNIEnv* env = event.jni_env; - - std::string class_name(name); - - jstring j_name = env->NewStringUTF(class_name.c_str()); - if (j_name == nullptr) { - return; - } - - jbyteArray input = env->NewByteArray(event.class_data_len); - if (input == nullptr) { - env->DeleteLocalRef(j_name); - return; - } - - env->SetByteArrayRegion( - input, - 0, - event.class_data_len, - reinterpret_cast(event.classbytes) - ); - - jbyteArray output = static_cast( - env->CallStaticObjectMethod( - Remapper.handler_class, - Remapper.onClassFileLoadMethod, - event.class_being_redefined, - event.loader, - j_name, - event.protection_domain, - event.class_data_len, - input - ) - ); - - check_and_clear_exception(env, "on_class_file_load_hook"); - - if (output != nullptr) { - jsize len = env->GetArrayLength(output); - unsigned char* buffer = nullptr; - - if (event.jvmti_env->Allocate(len, &buffer) == JVMTI_ERROR_NONE) { - env->GetByteArrayRegion( - output, - 0, - len, - reinterpret_cast(buffer) - ); - - *event.new_class_data_len = len; - *event.new_classbytes = buffer; - } - - env->DeleteLocalRef(output); - } - - env->DeleteLocalRef(input); - env->DeleteLocalRef(j_name); - } - - void on_method_entry_hook(const EventMethodEntry& event) { - // event.jni_env->CallStaticVoidMethod( - // Remapper.handler_class, - // Remapper.onMethodEntryMethod, - // event.thread, - // event.method - // ); - - check_and_clear_exception(event.jni_env, "on_method_entry_hook"); - } - - void on_method_exit_hook(const EventMethodExit& event) { - // event.jni_env->CallStaticVoidMethod( - // Remapper.handler_class, - // Remapper.onMethodExitMethod, - // event.thread, - // event.method, - // event.was_popped_by_exception, - // event.return_value.l - // ); - - check_and_clear_exception(event.jni_env, "on_method_exit_hook"); - } - -private: - EventBus::Token _classFileToken = 0; - EventBus::Token _methodEntryToken = 0; - EventBus::Token _methodExitToken = 0; -}; - -} - -JUICEAGENT_REGISTER_MODULE( - JuiceAgent::Core::Modules::RemapperModule, - Remapper -) \ No newline at end of file