From 34dbd82a599fa597d7a71ad0a038ff941f708642 Mon Sep 17 00:00:00 2001 From: xiaozhou233 Date: Thu, 2 Apr 2026 13:19:07 +0800 Subject: [PATCH] Impl: getClassBytes/getClassBytesByName --- src/libagent/callback/ClassFileLoadHook.cpp | 2 +- src/libagent/includes/global.hpp | 2 +- src/libagent/jni/bytecode.cpp | 109 ++++++++++++++++++++ 3 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 src/libagent/jni/bytecode.cpp diff --git a/src/libagent/callback/ClassFileLoadHook.cpp b/src/libagent/callback/ClassFileLoadHook.cpp index a2ec4d8..cd7f570 100644 --- a/src/libagent/callback/ClassFileLoadHook.cpp +++ b/src/libagent/callback/ClassFileLoadHook.cpp @@ -27,7 +27,7 @@ void JNICALL ClassFileLoadHook( if (!classFileDataMap.contains(name)) { ClassFileData data; - data.className = name; + 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; diff --git a/src/libagent/includes/global.hpp b/src/libagent/includes/global.hpp index 527e97d..8b1b052 100644 --- a/src/libagent/includes/global.hpp +++ b/src/libagent/includes/global.hpp @@ -10,7 +10,7 @@ // Data Structures struct ClassFileData { - std::string className; + std::string classname; std::vector bytecode; // ======== Global references to prevent GC ======== diff --git a/src/libagent/jni/bytecode.cpp b/src/libagent/jni/bytecode.cpp new file mode 100644 index 0000000..a88969e --- /dev/null +++ b/src/libagent/jni/bytecode.cpp @@ -0,0 +1,109 @@ +#include +#include + +JNIEXPORT jbyteArray JNICALL Java_cn_xiaozhou233_juiceagent_api_JuiceAgent_getClassBytes + (JNIEnv *env, jclass, jclass clazz) { + auto& agent = JuiceAgent::Agent::instance(); + if (!check_env(agent)) + return nullptr; + + char* signature = nullptr; + if (agent.get_jvmti()->GetClassSignature(clazz, &signature, nullptr) != JVMTI_ERROR_NONE || !signature) { + return nullptr; + } + + // Lxxx; -> xxx + std::string internalName(signature + 1, strlen(signature) - 2); + agent.get_jvmti()->Deallocate((unsigned char*)signature); + + classToCapture.insert(internalName); // Add to capture list + + jvmtiError err = agent.get_jvmti()->RetransformClasses(1, &clazz); + if (err != JVMTI_ERROR_NONE) { + PLOGE << "Retransform class failed: " << err; + return nullptr; + } + + ClassFileData data = classFileDataMap[internalName]; + if (data.classname.empty()) { + PLOGE << "Failed to capture class [classname null]: " << internalName; + } + if (data.bytecode.empty()) { + PLOGE << "Failed to capture bytecode for class [byetcode null]: " << internalName; + return nullptr; + } + + jbyteArray out = env->NewByteArray(data.bytecode.size()); + env->SetByteArrayRegion(out, 0, data.bytecode.size(), (jbyte*)data.bytecode.data()); + + return out; + } + +JNIEXPORT jbyteArray JNICALL Java_cn_xiaozhou233_juiceagent_api_JuiceAgent_getClassBytesByName + (JNIEnv *env, jclass loader_class, jstring name) { + auto& agent = JuiceAgent::Agent::instance(); + if (!check_env(agent) || !name) + return nullptr; + + // ========================= + // 1. jstring -> internal name + // ========================= + const char* utf = env->GetStringUTFChars(name, nullptr); + if (!utf) + return nullptr; + + std::string internalName(utf); + env->ReleaseStringUTFChars(name, utf); + + // a.b.C -> a/b/C + std::replace(internalName.begin(), internalName.end(), '.', '/'); + + // ========================= + // 2. Find loaded class via JVMTI + // ========================= + jint count = 0; + jclass* classes = nullptr; + + jvmtiEnv* jvmti = agent.get_jvmti(); + + if (jvmti->GetLoadedClasses(&count, &classes) != JVMTI_ERROR_NONE || count == 0) { + return nullptr; + } + + jclass targetClass = nullptr; + + for (jint i = 0; i < count; i++) { + char* signature = nullptr; + + if (jvmti->GetClassSignature(classes[i], &signature, nullptr) == JVMTI_ERROR_NONE && signature) { + size_t len = strlen(signature); + + // Lxxx; -> xxx + if (len > 2 && signature[0] == 'L' && signature[len - 1] == ';') { + if (internalName == std::string(signature + 1, len - 2)) { + targetClass = classes[i]; + jvmti->Deallocate((unsigned char*)signature); + break; + } + } + + jvmti->Deallocate((unsigned char*)signature); + } + } + + if (classes) { + jvmti->Deallocate((unsigned char*)classes); + } + + if (!targetClass) { + PLOGE << "Class not loaded: " << internalName; + return nullptr; + } + + // ========================= + // 3. Reuse existing logic + // ========================= + return Java_cn_xiaozhou233_juiceagent_api_JuiceAgent_getClassBytes( + env, loader_class, targetClass + ); +} \ No newline at end of file