mirror of
https://github.com/xiaozhou233/JuiceAgent.git
synced 2026-12-05 21:15:56 +08:00
Refactor: Move bytecodes_opt -> BytecodeService
This commit is contained in:
1 parent
637640b938
commit
882c19ad8f
5 files changed
+23
-25
No files matched your search
@@ -0,0 +1,61 @@
|
||||
#include <services.hpp>
|
||||
#include <jni_impl.hpp>
|
||||
|
||||
namespace JuiceAgent::services::Bytecode {
|
||||
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() << ")";
|
||||
}
|
||||
}
|
||||
|
||||
void init() {
|
||||
agent.get_eventbus().subscribe<EventClassFileLoadHook>(capture_bytecodes);
|
||||
agent.get_eventbus().subscribe<EventClassFileLoadHook>(patch_bytecodes);
|
||||
}
|
||||
|
||||
void start() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -19,12 +19,17 @@ namespace JuiceAgent::services::Manager {
|
||||
// JarLoader
|
||||
JuiceAgent::services::JarLoader::init();
|
||||
|
||||
// Bytecode
|
||||
JuiceAgent::services::Bytecode::init();
|
||||
}
|
||||
|
||||
void start(const EventLoaded& event) {
|
||||
PLOGI << "Starting services";
|
||||
// JarLoader
|
||||
JuiceAgent::services::JarLoader::start();
|
||||
|
||||
// Bytecode
|
||||
JuiceAgent::services::Bytecode::start();
|
||||
}
|
||||
|
||||
void stop() {
|
||||
|
||||
Reference in new issue
Block a user