diff --git a/src/libloader/common/libloader.cpp b/src/libloader/common/libloader.cpp index 0d9d394..06240a5 100644 --- a/src/libloader/common/libloader.cpp +++ b/src/libloader/common/libloader.cpp @@ -5,30 +5,49 @@ namespace libloader { - void entrypoint(const char* runtime_dir) { + void entrypoint(const char* runtime_dir) + { + if (runtime_dir == nullptr) { + PLOGE << "runtime_dir is null, fallback may fail"; + } - // Load Config + // Load Config (allow fallback) config::Config cfg(runtime_dir); if (!cfg.is_valid()) { - PLOGE << "Invalid config"; + PLOGW << "Invalid config, using default values"; } InjectionInfo info = cfg.get_injection_info(); config::print_injection_info(info); - // Attach to JVM - jvm::Jvm jvm; - - if (!jvm.attach()) { - PLOGE << "Attach failed"; + // Validate injection path early + if (info.JuiceAgentAPIJarPath.empty()) { + PLOGE << "JuiceAgentAPIJarPath is empty, abort injection"; return; } - // Inject JuiceLoaderAPI.jar - jint status = jvm.get_jvmti()->AddToSystemClassLoaderSearch(info.JuiceAgentAPIJarPath.c_str()); - if (status != JNI_OK) { - PLOGE << "AddToSystemClassLoaderSearch failed: " << status; + // Attach to JVM + jvm::Jvm jvm; + if (!jvm.attach()) { + PLOGE << "Attach to JVM failed"; + return; } + auto* jvmti = jvm.get_jvmti(); + if (jvmti == nullptr) { + PLOGE << "JVMTI is null"; + return; + } + + // Inject JuiceAgentAPI.jar + const char* jar_path = info.JuiceAgentAPIJarPath.c_str(); + jint status = jvmti->AddToSystemClassLoaderSearch(jar_path); + + if (status != JNI_OK) { + PLOGE << "AddToSystemClassLoaderSearch failed: " << status; + return; + } + + PLOGI << "Jar injected successfully: " << jar_path; } -} // namespace libloader +} // namespace libloader \ No newline at end of file