mirror of
https://github.com/xiaozhou233/JuiceAgent.git
synced 2026-12-05 21:15:56 +08:00
Feat: Add Remapper
This commit is contained in:
1 parent
d9d44f029f
commit
1e2e5afd07
3 files changed
+369
-38
No files matched your search
@@ -14,9 +14,17 @@ class EventBus {
|
||||
public:
|
||||
using Token = std::uint64_t;
|
||||
|
||||
// === Readonly ===
|
||||
template<typename Event>
|
||||
using Listener = std::function<void(const Event&)>;
|
||||
|
||||
// === Mutable ===
|
||||
template<typename Event>
|
||||
using MutableListener = std::function<void(Event&)>;
|
||||
|
||||
// =========================
|
||||
// Subscribe (Readonly)
|
||||
// =========================
|
||||
template<typename Event>
|
||||
Token subscribe(Listener<Event> listener, int priority = 0) {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
@@ -31,40 +39,58 @@ public:
|
||||
std::move(listener)
|
||||
});
|
||||
|
||||
std::sort(vec.begin(), vec.end(),
|
||||
[](const auto& a, const auto& b) {
|
||||
return a.priority > b.priority;
|
||||
});
|
||||
|
||||
sort(vec);
|
||||
return token;
|
||||
}
|
||||
|
||||
// =========================
|
||||
// Subscribe (Mutable)
|
||||
// =========================
|
||||
template<typename Event>
|
||||
Token subscribe_mutable(MutableListener<Event> listener, int priority = 0) {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
||||
auto& vec = getMutableListeners<Event>();
|
||||
|
||||
Token token = nextToken_++;
|
||||
|
||||
vec.push_back({
|
||||
token,
|
||||
priority,
|
||||
std::move(listener)
|
||||
});
|
||||
|
||||
sort(vec);
|
||||
return token;
|
||||
}
|
||||
|
||||
// =========================
|
||||
// Unsubscribe
|
||||
// =========================
|
||||
template<typename Event>
|
||||
bool unsubscribe(Token token) {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
||||
auto type = std::type_index(typeid(Event));
|
||||
auto it = listeners_.find(type);
|
||||
if (it == listeners_.end()) {
|
||||
return false;
|
||||
bool removed = false;
|
||||
|
||||
// readonly
|
||||
if (auto it = listeners_.find(std::type_index(typeid(Event))); it != listeners_.end()) {
|
||||
auto& vec = *std::static_pointer_cast<std::vector<Node<Event>>>(it->second);
|
||||
removed |= erase(vec, token);
|
||||
}
|
||||
|
||||
auto& vec =
|
||||
*std::static_pointer_cast<std::vector<Node<Event>>>(it->second);
|
||||
// mutable
|
||||
if (auto it = mutable_listeners_.find(std::type_index(typeid(Event))); it != mutable_listeners_.end()) {
|
||||
auto& vec = *std::static_pointer_cast<std::vector<MutableNode<Event>>>(it->second);
|
||||
removed |= erase(vec, token);
|
||||
}
|
||||
|
||||
auto oldSize = vec.size();
|
||||
|
||||
vec.erase(
|
||||
std::remove_if(vec.begin(), vec.end(),
|
||||
[token](const Node<Event>& node) {
|
||||
return node.token == token;
|
||||
}),
|
||||
vec.end()
|
||||
);
|
||||
|
||||
return vec.size() != oldSize;
|
||||
return removed;
|
||||
}
|
||||
|
||||
// =========================
|
||||
// Post (Readonly)
|
||||
// =========================
|
||||
template<typename Event>
|
||||
void post(const Event& event) {
|
||||
std::vector<Node<Event>> copy;
|
||||
@@ -72,15 +98,10 @@ public:
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
||||
auto type = std::type_index(typeid(Event));
|
||||
auto it = listeners_.find(type);
|
||||
auto it = listeners_.find(std::type_index(typeid(Event)));
|
||||
if (it == listeners_.end()) return;
|
||||
|
||||
if (it == listeners_.end()) {
|
||||
return;
|
||||
}
|
||||
|
||||
copy =
|
||||
*std::static_pointer_cast<std::vector<Node<Event>>>(it->second);
|
||||
copy = *std::static_pointer_cast<std::vector<Node<Event>>>(it->second);
|
||||
}
|
||||
|
||||
for (auto& node : copy) {
|
||||
@@ -88,15 +109,41 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
// =========================
|
||||
// Post (Mutable)
|
||||
// =========================
|
||||
template<typename Event>
|
||||
void post_mutable(Event& event) {
|
||||
std::vector<MutableNode<Event>> copy;
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
||||
auto it = mutable_listeners_.find(std::type_index(typeid(Event)));
|
||||
if (it == mutable_listeners_.end()) return;
|
||||
|
||||
copy = *std::static_pointer_cast<std::vector<MutableNode<Event>>>(it->second);
|
||||
}
|
||||
|
||||
for (auto& node : copy) {
|
||||
node.callback(event);
|
||||
}
|
||||
}
|
||||
|
||||
// =========================
|
||||
// Clear
|
||||
// =========================
|
||||
template<typename Event>
|
||||
void clear() {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
listeners_.erase(std::type_index(typeid(Event)));
|
||||
mutable_listeners_.erase(std::type_index(typeid(Event)));
|
||||
}
|
||||
|
||||
void clearAll() {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
listeners_.clear();
|
||||
mutable_listeners_.clear();
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -107,22 +154,65 @@ private:
|
||||
Listener<Event> callback;
|
||||
};
|
||||
|
||||
template<typename Event>
|
||||
struct MutableNode {
|
||||
Token token;
|
||||
int priority;
|
||||
MutableListener<Event> callback;
|
||||
};
|
||||
|
||||
std::mutex mutex_;
|
||||
std::atomic<Token> nextToken_ {1};
|
||||
std::atomic<Token> nextToken_{1};
|
||||
|
||||
std::unordered_map<std::type_index, std::shared_ptr<void>> listeners_;
|
||||
std::unordered_map<std::type_index, std::shared_ptr<void>> mutable_listeners_;
|
||||
|
||||
// === helpers ===
|
||||
|
||||
template<typename T>
|
||||
static void sort(std::vector<T>& vec) {
|
||||
std::sort(vec.begin(), vec.end(),
|
||||
[](const auto& a, const auto& b) {
|
||||
return a.priority > b.priority;
|
||||
});
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static bool erase(std::vector<T>& vec, Token token) {
|
||||
auto oldSize = vec.size();
|
||||
|
||||
vec.erase(
|
||||
std::remove_if(vec.begin(), vec.end(),
|
||||
[token](const T& node) {
|
||||
return node.token == token;
|
||||
}),
|
||||
vec.end()
|
||||
);
|
||||
|
||||
return vec.size() != oldSize;
|
||||
}
|
||||
|
||||
template<typename Event>
|
||||
std::vector<Node<Event>>& getListeners() {
|
||||
auto type = std::type_index(typeid(Event));
|
||||
auto it = listeners_.find(type);
|
||||
return getContainer<Event, Node<Event>>(listeners_);
|
||||
}
|
||||
|
||||
if (it == listeners_.end()) {
|
||||
auto ptr = std::make_shared<std::vector<Node<Event>>>();
|
||||
listeners_[type] = ptr;
|
||||
template<typename Event>
|
||||
std::vector<MutableNode<Event>>& getMutableListeners() {
|
||||
return getContainer<Event, MutableNode<Event>>(mutable_listeners_);
|
||||
}
|
||||
|
||||
template<typename Event, typename NodeType>
|
||||
std::vector<NodeType>& getContainer(std::unordered_map<std::type_index, std::shared_ptr<void>>& map) {
|
||||
auto type = std::type_index(typeid(Event));
|
||||
auto it = map.find(type);
|
||||
|
||||
if (it == map.end()) {
|
||||
auto ptr = std::make_shared<std::vector<NodeType>>();
|
||||
map[type] = ptr;
|
||||
return *ptr;
|
||||
}
|
||||
|
||||
return *std::static_pointer_cast<std::vector<Node<Event>>>(it->second);
|
||||
return *std::static_pointer_cast<std::vector<NodeType>>(it->second);
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,238 @@
|
||||
#include <modules/ModuleRegistry.hpp>
|
||||
#include <modules/ModuleBase.hpp>
|
||||
#include <JuiceAgent/Logger.hpp>
|
||||
#include <libagent.hpp>
|
||||
#include <event/eventbus.hpp>
|
||||
#include <event/event_type.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <cstring>
|
||||
|
||||
namespace JuiceAgent::Core::Modules {
|
||||
|
||||
struct RemapperConfig {
|
||||
bool enabled = false;
|
||||
};
|
||||
|
||||
struct RemapperBridge {
|
||||
static jclass bridge_class;
|
||||
static jmethodID remap_method;
|
||||
};
|
||||
|
||||
jclass RemapperBridge::bridge_class = nullptr;
|
||||
jmethodID RemapperBridge::remap_method = nullptr;
|
||||
|
||||
static JuiceAgent::Agent& agent = JuiceAgent::Agent::instance();
|
||||
static RemapperConfig config;
|
||||
|
||||
// 防递归
|
||||
static thread_local bool in_remap = false;
|
||||
|
||||
class RemapperModule : public ModuleBase {
|
||||
public:
|
||||
std::string name() const override {
|
||||
return "Remapper";
|
||||
}
|
||||
|
||||
protected:
|
||||
bool on_init() override {
|
||||
auto& cfg = agent.get_config();
|
||||
|
||||
config.enabled = cfg.get<bool>(
|
||||
"JuiceAgent.Modules.Remapper.Enabled",
|
||||
false
|
||||
);
|
||||
|
||||
if (!config.enabled) {
|
||||
return true;
|
||||
}
|
||||
|
||||
JNIEnv* env = agent.get_env();
|
||||
|
||||
// === 找类 ===
|
||||
jclass local_class = env->FindClass(
|
||||
"cn/xiaozhou233/juiceremapper/bridge/JuiceAgentBridge"
|
||||
);
|
||||
|
||||
if (!local_class) {
|
||||
PLOGE << "Failed to find JuiceAgentBridge";
|
||||
return false;
|
||||
}
|
||||
|
||||
RemapperBridge::bridge_class =
|
||||
reinterpret_cast<jclass>(env->NewGlobalRef(local_class));
|
||||
env->DeleteLocalRef(local_class);
|
||||
|
||||
// === 找方法 ===
|
||||
RemapperBridge::remap_method = env->GetStaticMethodID(
|
||||
RemapperBridge::bridge_class,
|
||||
"remap",
|
||||
"(Ljava/lang/Class;Ljava/lang/Object;Ljava/lang/String;Ljava/lang/Object;I[B)[B"
|
||||
);
|
||||
|
||||
if (!RemapperBridge::remap_method) {
|
||||
PLOGE << "Failed to find remap method";
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool on_start() override {
|
||||
if (!config.enabled) {
|
||||
return true;
|
||||
}
|
||||
|
||||
auto& bus = agent.get_eventbus();
|
||||
|
||||
_classFileToken = bus.subscribe_mutable<EventClassFileLoadHook>(
|
||||
[this](auto& e) {
|
||||
on_class_file_load_hook(e);
|
||||
}
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void on_stop() override {
|
||||
if (!config.enabled) return;
|
||||
|
||||
auto& bus = agent.get_eventbus();
|
||||
|
||||
if (_classFileToken != 0) {
|
||||
bus.unsubscribe<EventClassFileLoadHook>(_classFileToken);
|
||||
_classFileToken = 0;
|
||||
}
|
||||
|
||||
// 释放 GlobalRef
|
||||
JNIEnv* env = agent.get_env();
|
||||
if (RemapperBridge::bridge_class) {
|
||||
env->DeleteGlobalRef(RemapperBridge::bridge_class);
|
||||
RemapperBridge::bridge_class = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
void on_class_file_load_hook(EventClassFileLoadHook& event) {
|
||||
// === 基础检查 ===
|
||||
if (!event.classbytes || event.class_data_len <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::string_view name = event.name ? event.name : "";
|
||||
if (name.empty()) return;
|
||||
|
||||
// === 防递归 ===
|
||||
if (in_remap) return;
|
||||
|
||||
// === 过滤系统类 ===
|
||||
if (name.starts_with("java/") ||
|
||||
name.starts_with("javax/") ||
|
||||
name.starts_with("sun/") ||
|
||||
name.starts_with("jdk/") ||
|
||||
name.starts_with("cn/xiaozhou233/juiceremapper")) {
|
||||
return;
|
||||
}
|
||||
|
||||
JNIEnv* env = event.jni_env;
|
||||
jvmtiEnv* jvmti = event.jvmti_env;
|
||||
|
||||
if (!env || !jvmti) return;
|
||||
|
||||
in_remap = true;
|
||||
|
||||
// === 构造输入 byte[] ===
|
||||
jbyteArray inputArray = env->NewByteArray(event.class_data_len);
|
||||
if (!inputArray) {
|
||||
in_remap = false;
|
||||
return;
|
||||
}
|
||||
|
||||
env->SetByteArrayRegion(
|
||||
inputArray,
|
||||
0,
|
||||
event.class_data_len,
|
||||
reinterpret_cast<const jbyte*>(event.classbytes)
|
||||
);
|
||||
|
||||
// class name
|
||||
jstring jname = env->NewStringUTF(name.data());
|
||||
|
||||
// === 调用 Java remap ===
|
||||
jobject result = env->CallStaticObjectMethod(
|
||||
RemapperBridge::bridge_class,
|
||||
RemapperBridge::remap_method,
|
||||
nullptr,
|
||||
event.loader,
|
||||
jname,
|
||||
event.protection_domain,
|
||||
0,
|
||||
inputArray
|
||||
);
|
||||
|
||||
// === 异常处理 ===
|
||||
if (env->ExceptionCheck()) {
|
||||
env->ExceptionDescribe();
|
||||
env->ExceptionClear();
|
||||
cleanup(env, inputArray, jname, nullptr);
|
||||
in_remap = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!result) {
|
||||
cleanup(env, inputArray, jname, nullptr);
|
||||
in_remap = false;
|
||||
return;
|
||||
}
|
||||
|
||||
jbyteArray outputArray = reinterpret_cast<jbyteArray>(result);
|
||||
|
||||
jsize new_len = env->GetArrayLength(outputArray);
|
||||
|
||||
jbyte* new_bytes = env->GetByteArrayElements(outputArray, nullptr);
|
||||
if (!new_bytes) {
|
||||
cleanup(env, inputArray, jname, outputArray);
|
||||
in_remap = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// === JVMTI 分配 ===
|
||||
unsigned char* new_class_data = nullptr;
|
||||
|
||||
if (jvmti->Allocate(new_len, &new_class_data) != JVMTI_ERROR_NONE) {
|
||||
env->ReleaseByteArrayElements(outputArray, new_bytes, JNI_ABORT);
|
||||
cleanup(env, inputArray, jname, outputArray);
|
||||
in_remap = false;
|
||||
return;
|
||||
}
|
||||
|
||||
std::memcpy(new_class_data, new_bytes, new_len);
|
||||
|
||||
// === 写回 ===
|
||||
*event.new_classbytes = new_class_data;
|
||||
*event.new_class_data_len = new_len;
|
||||
|
||||
// === 清理 ===
|
||||
env->ReleaseByteArrayElements(outputArray, new_bytes, JNI_ABORT);
|
||||
cleanup(env, inputArray, jname, outputArray);
|
||||
|
||||
in_remap = false;
|
||||
}
|
||||
|
||||
void cleanup(JNIEnv* env, jbyteArray in, jstring name, jbyteArray out) {
|
||||
if (in) env->DeleteLocalRef(in);
|
||||
if (name) env->DeleteLocalRef(name);
|
||||
if (out) env->DeleteLocalRef(out);
|
||||
}
|
||||
|
||||
private:
|
||||
EventBus::Token _classFileToken = 0;
|
||||
};
|
||||
|
||||
JUICEAGENT_REGISTER_MODULE(
|
||||
JuiceAgent::Core::Modules::RemapperModule,
|
||||
RemapperModule
|
||||
)
|
||||
|
||||
} // namespace
|
||||
Reference in new issue
Block a user