From 49d2499f737ecbe1ec35965e807f574a9c9b87f6 Mon Sep 17 00:00:00 2001 From: xiaozhou233 Date: Sat, 4 Apr 2026 21:42:15 +0800 Subject: [PATCH] Feat: EventBus --- src/libagent/callback/ClassFileLoadHook.cpp | 57 +++++------------ src/libagent/core/bytecodes_opt.cpp | 59 ++++++++++++++++++ src/libagent/includes/bytecodes_opt.hpp | 10 +++ src/libagent/includes/event_type.hpp | 17 +++++ src/libagent/includes/eventbus.hpp | 69 +++++++++++++++++++++ src/libagent/includes/libagent.hpp | 3 + src/libagent/libagent.cpp | 4 ++ 7 files changed, 176 insertions(+), 43 deletions(-) create mode 100644 src/libagent/core/bytecodes_opt.cpp create mode 100644 src/libagent/includes/bytecodes_opt.hpp create mode 100644 src/libagent/includes/event_type.hpp create mode 100644 src/libagent/includes/eventbus.hpp diff --git a/src/libagent/callback/ClassFileLoadHook.cpp b/src/libagent/callback/ClassFileLoadHook.cpp index 03e6f14..839ee39 100644 --- a/src/libagent/callback/ClassFileLoadHook.cpp +++ b/src/libagent/callback/ClassFileLoadHook.cpp @@ -1,5 +1,5 @@ #include -#include +#include void JNICALL ClassFileLoadHook( jvmtiEnv* jvmti_env, @@ -17,46 +17,17 @@ void JNICALL ClassFileLoadHook( return; } - /* ========================= - * 1. Capture original bytes - * ========================= */ - std::lock_guard lock(classDataMutex); - if (classToCapture.contains(name)) { - if (!classFileDataMap.contains(name)) { - ClassFileData data; - - data.classname = name; - data.bytecode.assign(classbytes, classbytes + class_data_len); - // Experimental: class/classloader/protection_domain references (to be used in future features, e.g. dynamic patching) - data.clazz = class_being_redefined; - data.classloader = loader; - data.protection_domain = protection_domain; - - - classFileDataMap[name] = std::move(data); - - classToCapture.erase(name); - - PLOGI << "Captured class: " << name << " (length: " << class_data_len << ")"; - } - } - - /* ========================= - * 2. Apply patches - * ========================= */ - if (pendingRetransform.contains(name)) { - auto& bytes = pendingRetransform[name]; - - unsigned char* new_buf = nullptr; - jvmti_env->Allocate(bytes.size(), &new_buf); - - memcpy(new_buf, bytes.data(), bytes.size()); - - *new_class_data_len = (jint)bytes.size(); - *new_classbytes = new_buf; - - pendingRetransform.erase(name); - - PLOGI << "Retransformed: " << name << " (new length: " << bytes.size() << ")"; - } + auto& agent = JuiceAgent::Agent::instance(); + agent.get_eventbus().post(EventClassFileLoadHook{ + .jvmti_env = jvmti_env, + .jni_env = jni_env, + .class_being_redefined = class_being_redefined, + .loader = loader, + .name = name, + .protection_domain = protection_domain, + .class_data_len = class_data_len, + .classbytes = classbytes, + .new_class_data_len = new_class_data_len, + .new_classbytes = new_classbytes + }); } \ No newline at end of file diff --git a/src/libagent/core/bytecodes_opt.cpp b/src/libagent/core/bytecodes_opt.cpp new file mode 100644 index 0000000..0a7cd38 --- /dev/null +++ b/src/libagent/core/bytecodes_opt.cpp @@ -0,0 +1,59 @@ +#include +#include +#include + +namespace JuiceAgent::Bytecodes { + void register_bytecodes() { + auto& agent = JuiceAgent::Agent::instance(); + + agent.get_eventbus().subscribe(capture_bytecodes); + agent.get_eventbus().subscribe(patch_bytecodes); + } + + void capture_bytecodes(const EventClassFileLoadHook& e) { + /* ========================= + * 1. Capture original bytes + * ========================= */ + std::lock_guard lock(classDataMutex); + if (classToCapture.contains(e.name)) { + if (!classFileDataMap.contains(e.name)) { + ClassFileData data; + + data.classname = e.name; + data.bytecode.assign(e.classbytes, e.classbytes + e.class_data_len); + // Experimental: class/classloader/protection_domain references (to be used in future features, e.g. dynamic patching) + data.clazz = e.class_being_redefined; + data.classloader = e.loader; + data.protection_domain = e.protection_domain; + + + classFileDataMap[e.name] = std::move(data); + + classToCapture.erase(e.name); + + PLOGI << "Captured class: " << e.name << " (length: " << e.class_data_len << ")"; + } + } + } + + void patch_bytecodes(const EventClassFileLoadHook& e) { + /* ========================= + * 2. Apply patches + * ========================= */ + if (pendingRetransform.contains(e.name)) { + auto& bytes = pendingRetransform[e.name]; + + unsigned char* new_buf = nullptr; + e.jvmti_env->Allocate(bytes.size(), &new_buf); + + memcpy(new_buf, bytes.data(), bytes.size()); + + *e.new_class_data_len = (jint)bytes.size(); + *e.new_classbytes = new_buf; + + pendingRetransform.erase(e.name); + + PLOGI << "Retransformed: " << e.name << " (new length: " << bytes.size() << ")"; + } + } +} \ No newline at end of file diff --git a/src/libagent/includes/bytecodes_opt.hpp b/src/libagent/includes/bytecodes_opt.hpp new file mode 100644 index 0000000..e422fd3 --- /dev/null +++ b/src/libagent/includes/bytecodes_opt.hpp @@ -0,0 +1,10 @@ +#pragma once + +#include "event_type.hpp" + +namespace JuiceAgent::Bytecodes { + void register_bytecodes(); + + void capture_bytecodes(const EventClassFileLoadHook& e); + void patch_bytecodes(const EventClassFileLoadHook& e); +} \ No newline at end of file diff --git a/src/libagent/includes/event_type.hpp b/src/libagent/includes/event_type.hpp new file mode 100644 index 0000000..f077d99 --- /dev/null +++ b/src/libagent/includes/event_type.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include + +// ClassFileLoadHook +struct EventClassFileLoadHook { + jvmtiEnv* jvmti_env; + JNIEnv* jni_env; + jclass class_being_redefined; + jobject loader; + const char* name; + jobject protection_domain; + jint class_data_len; + const unsigned char* classbytes; + jint* new_class_data_len; + unsigned char** new_classbytes; +}; \ No newline at end of file diff --git a/src/libagent/includes/eventbus.hpp b/src/libagent/includes/eventbus.hpp new file mode 100644 index 0000000..3e93c37 --- /dev/null +++ b/src/libagent/includes/eventbus.hpp @@ -0,0 +1,69 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +class EventBus { +public: + template + using Listener = std::function; + + template + void subscribe(Listener listener, int priority = 0) { + std::lock_guard lock(mutex_); + auto& dispatcher = getDispatcher(); + dispatcher.appendListener(priority, std::move(listener)); + } + + template + void clear() { + std::lock_guard lock(mutex_); + dispatchers_.erase(std::type_index(typeid(Event))); + } + + template + void post(const Event& event) { + std::shared_ptr dispatcherPtr; + + { + std::lock_guard lock(mutex_); + auto it = dispatchers_.find(std::type_index(typeid(Event))); + if(it == dispatchers_.end()) return; + dispatcherPtr = it->second; + } + + auto dispatcher = std::static_pointer_cast< + eventpp::EventDispatcher + >(dispatcherPtr); + + dispatcher->dispatch(0, event); + } + +private: + std::mutex mutex_; + + std::unordered_map> dispatchers_; + + template + eventpp::EventDispatcher& getDispatcher() { + auto typeIdx = std::type_index(typeid(Event)); + auto it = dispatchers_.find(typeIdx); + + if(it == dispatchers_.end()) { + auto dispatcher = std::make_shared< + eventpp::EventDispatcher + >(); + + dispatchers_[typeIdx] = dispatcher; + return *dispatcher; + } + + return *std::static_pointer_cast< + eventpp::EventDispatcher + >(it->second); + } +}; \ No newline at end of file diff --git a/src/libagent/includes/libagent.hpp b/src/libagent/includes/libagent.hpp index 6cce749..3cc41e0 100644 --- a/src/libagent/includes/libagent.hpp +++ b/src/libagent/includes/libagent.hpp @@ -3,12 +3,14 @@ #include #include #include +#include namespace JuiceAgent { class Agent { private: JavaVM* jvm; jvmtiEnv* jvmti; + EventBus eventbus; private: Agent() = default; @@ -27,6 +29,7 @@ namespace JuiceAgent { // getters JavaVM* get_jvm() const { return jvm; } jvmtiEnv* get_jvmti() const { return jvmti; } + EventBus& get_eventbus() { return eventbus; } // setters void set_jvm(JavaVM* jvm) { this->jvm = jvm; } diff --git a/src/libagent/libagent.cpp b/src/libagent/libagent.cpp index e363f83..a82a56f 100644 --- a/src/libagent/libagent.cpp +++ b/src/libagent/libagent.cpp @@ -1,5 +1,6 @@ #include #include +#include namespace JuiceAgent { // ===== Singleton ===== @@ -60,6 +61,9 @@ namespace JuiceAgent { PLOGE << "Failed to enable event notification: " << result; } + // register bytecodes_opt + JuiceAgent::Bytecodes::register_bytecodes(); + return true; } } \ No newline at end of file