Perf & Fix: reduce ClassFileLoadHook overhead and fix retransform patch path

- Skip mutable event dispatch when no mutable listeners exist (hot path)
- Wire retransformClass bytecode into pendingRetransform so patches are applied
- Guard pendingRetransform with its mutex; check jvmti Allocate result
- Store global refs in captured ClassFileData and release on re-capture
- Add null/exception checks in defineClass/getLoadedClasses
- Extract shared find_class_by_internal_name helper
- Use CONFIGURE_DEPENDS for CMake globs; guard InitLogger; delete leaked jArgs
This commit is contained in:
xiaozhou233 committed 2026-08-14 17:07:36 +08:00
1 parent 8cf2b8330c
commit bb5b215a33
10 files changed
+198 -130

No files matched your search

+36
View File
@@ -1,6 +1,7 @@
#pragma once
#include <JuiceAgent.hpp>
#include <cstring>
inline bool check_env(JuiceAgent::Agent& agent) {
if (agent.get_jvm() == nullptr) {
@@ -14,6 +15,41 @@ inline bool check_env(JuiceAgent::Agent& agent) {
return true;
}
// Search all loaded classes for one whose internal name (e.g. "a/b/C") matches.
// Returns a jclass reference owned by JVMTI (must not be deleted); nullptr if not found.
inline jclass find_class_by_internal_name(jvmtiEnv* jvmti, const std::string& internal_name) {
if (!jvmti || internal_name.empty()) return nullptr;
jint count = 0;
jclass* classes = nullptr;
if (jvmti->GetLoadedClasses(&count, &classes) != JVMTI_ERROR_NONE || count == 0) {
return nullptr;
}
jclass result = nullptr;
for (jint i = 0; i < count; i++) {
char* signature = nullptr;
if (jvmti->GetClassSignature(classes[i], &signature, nullptr) != JVMTI_ERROR_NONE || !signature) {
continue;
}
size_t len = strlen(signature);
// Signature format: "La/b/C;"
if (len > 2 && signature[0] == 'L' && signature[len - 1] == ';' &&
internal_name.size() == len - 2 &&
memcmp(signature + 1, internal_name.data(), len - 2) == 0) {
result = classes[i];
jvmti->Deallocate(reinterpret_cast<unsigned char*>(signature));
break;
}
jvmti->Deallocate(reinterpret_cast<unsigned char*>(signature));
}
jvmti->Deallocate(reinterpret_cast<unsigned char*>(classes));
return result;
}
inline std::string get_class_name(JNIEnv* env, jclass clazz) {
if (!env || !clazz) return "";