mirror of
https://github.com/xiaozhou233/JuiceAgent.git
synced 2026-12-05 13:05:53 +08:00
Impl: getClassBytes/getClassBytesByName
This commit is contained in:
1 parent
9aa77848d7
commit
34dbd82a59
3 files changed
+111
-2
No files matched your search
@@ -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;
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
|
||||
// Data Structures
|
||||
struct ClassFileData {
|
||||
std::string className;
|
||||
std::string classname;
|
||||
std::vector<unsigned char> bytecode;
|
||||
|
||||
// ======== Global references to prevent GC ========
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
#include <jni_impl.hpp>
|
||||
#include <algorithm>
|
||||
|
||||
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
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user