Feat: EventBus

This commit is contained in:
xiaozhou233 committed 2026-04-04 21:42:15 +08:00
1 parent a64bbb8bb8
commit 49d2499f73
7 files changed
+176 -43

No files matched your search

+14 -43
View File
@@ -1,5 +1,5 @@
#include <jni_impl.hpp>
#include <iostream>
#include <event_type.hpp>
void JNICALL ClassFileLoadHook(
jvmtiEnv* jvmti_env,
@@ -17,46 +17,17 @@ void JNICALL ClassFileLoadHook(
return;
}
/* =========================
* 1. Capture original bytes
* ========================= */
std::lock_guard<std::mutex> 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
});
}
+59
View File
@@ -0,0 +1,59 @@
#include <bytecodes_opt.hpp>
#include <jni_impl.hpp>
#include <event_type.hpp>
namespace JuiceAgent::Bytecodes {
void register_bytecodes() {
auto& agent = JuiceAgent::Agent::instance();
agent.get_eventbus().subscribe<EventClassFileLoadHook>(capture_bytecodes);
agent.get_eventbus().subscribe<EventClassFileLoadHook>(patch_bytecodes);
}
void capture_bytecodes(const EventClassFileLoadHook& e) {
/* =========================
* 1. Capture original bytes
* ========================= */
std::lock_guard<std::mutex> 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() << ")";
}
}
}
+10
View File
@@ -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);
}
+17
View File
@@ -0,0 +1,17 @@
#pragma once
#include <jni_impl.hpp>
// 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;
};
+69
View File
@@ -0,0 +1,69 @@
#pragma once
#include <eventpp/eventdispatcher.h>
#include <unordered_map>
#include <functional>
#include <memory>
#include <mutex>
#include <typeindex>
class EventBus {
public:
template<typename Event>
using Listener = std::function<void(const Event&)>;
template<typename Event>
void subscribe(Listener<Event> listener, int priority = 0) {
std::lock_guard<std::mutex> lock(mutex_);
auto& dispatcher = getDispatcher<Event>();
dispatcher.appendListener(priority, std::move(listener));
}
template<typename Event>
void clear() {
std::lock_guard<std::mutex> lock(mutex_);
dispatchers_.erase(std::type_index(typeid(Event)));
}
template<typename Event>
void post(const Event& event) {
std::shared_ptr<void> dispatcherPtr;
{
std::lock_guard<std::mutex> 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<int, void(const Event&)>
>(dispatcherPtr);
dispatcher->dispatch(0, event);
}
private:
std::mutex mutex_;
std::unordered_map<std::type_index, std::shared_ptr<void>> dispatchers_;
template<typename Event>
eventpp::EventDispatcher<int, void(const Event&)>& getDispatcher() {
auto typeIdx = std::type_index(typeid(Event));
auto it = dispatchers_.find(typeIdx);
if(it == dispatchers_.end()) {
auto dispatcher = std::make_shared<
eventpp::EventDispatcher<int, void(const Event&)>
>();
dispatchers_[typeIdx] = dispatcher;
return *dispatcher;
}
return *std::static_pointer_cast<
eventpp::EventDispatcher<int, void(const Event&)>
>(it->second);
}
};
+3
View File
@@ -3,12 +3,14 @@
#include <JuiceAgent/Logger.hpp>
#include <jni.h>
#include <jvmti.h>
#include <eventbus.hpp>
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; }
+4
View File
@@ -1,5 +1,6 @@
#include <libagent.hpp>
#include <jni_impl.hpp>
#include <bytecodes_opt.hpp>
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;
}
}