Feat: add JNI bytecode API and BytecodeStore

- introduce thread-safe BytecodeStore singleton for pending captures,
  captured bytecode cache, and pending patches; replaces ad-hoc global
  mutex/map pairs
- implement BytecodeService capture/patch listeners that operate on
  the store; keep JVMTI Allocate outside of store locks and put failed
  patches back so they can be retried
- add JNI bindings: getClassBytes(ByName), redefineClass(ByName),
  retransformClass(ByName); use JVMTI GetLoadedClasses for name-based
  lookup instead of JNI FindClass, validate array length vs declared
  length in redefineClass
This commit is contained in:
xiaozhou233 committed 2026-08-17 19:19:26 +08:00
1 parent 8e1b8b38e4
commit f38743e863
4 files changed
+440 -6

No files matched your search

+38 -6
View File
@@ -2,6 +2,9 @@
#include <services/service_manager.hpp>
#include <event/event_type.hpp>
#include <JuiceAgent/Logger.hpp>
#include <global.hpp>
#include <cstring>
namespace JuiceAgent {
namespace Services {
@@ -31,15 +34,44 @@ public:
private:
void captureBytecodes(const EventClassFileLoadHook& e) {
// TODO: Capture original bytes
spdlog::trace("[BytecodeService] Capture original bytes: {}",
e.name ? e.name : "<null>");
if (!e.name) return;
const std::size_t len = BytecodeStore::getInstance()
.tryCapture(e.name, e.classbytes, e.class_data_len);
if (len > 0) {
spdlog::info("[BytecodeService] Captured class: {} (length: {})",
e.name, len);
}
}
void patchBytecodes(const EventClassFileLoadHook& e) {
// TODO: Apply patches
spdlog::trace("[BytecodeService] Apply patches: {}",
e.name ? e.name : "<null>");
if (!e.name || !e.jvmti_env ||
!e.new_class_data_len || !e.new_classbytes) {
return;
}
std::vector<unsigned char> bytes;
if (!BytecodeStore::getInstance().takePatch(e.name, bytes) || bytes.empty()) {
return;
}
const jlong new_len = static_cast<jlong>(bytes.size());
unsigned char* new_buf = nullptr;
if (e.jvmti_env->Allocate(new_len, &new_buf) != JVMTI_ERROR_NONE) {
// Put bytes back so a future hook can retry instead of losing the patch.
BytecodeStore::getInstance().putBackPatch(e.name, std::move(bytes));
spdlog::error("[BytecodeService] Failed to allocate buffer for: {}", e.name);
return;
}
std::memcpy(new_buf, bytes.data(), bytes.size());
*e.new_class_data_len = static_cast<jint>(new_len);
*e.new_classbytes = new_buf;
spdlog::info("[BytecodeService] Patched class: {} (new length: {})",
e.name, new_len);
}
};