BugFix: contain exceptions at the loader entry point

- loader: wrap entrypoint() in try/catch; it runs from DllMain inside the target JVM process, so an uncaught exception (e.g. a missing required key in config.toml) would take the target process down
This commit is contained in:
xiaozhou233 committed 2026-10-16 12:50:06 +08:00
1 parent e6da1d6fff
commit 124c656400
1 file changed
+31 -20
+31 -20
View File
@@ -1,29 +1,40 @@
#include <exception>
#include <Loader.hpp>
#include <JvmAttach.hpp>
#include <JuiceAgent/Logger.hpp>
void JuiceAgent::Loader::entrypoint(const char* path) {
// Attach to the JVM
JuiceAgent::Loader::JvmAttach jvmAttach;
// This runs from DllMain inside the target JVM process, so a C++ exception
// escaping here would take the target process down. Contain everything as a
// last line of defence; a bad config.toml is reported and logged instead.
try {
// Attach to the JVM
JuiceAgent::Loader::JvmAttach jvmAttach;
if(!jvmAttach.attach()) {
spdlog::error("Failed to attach to JVM!");
return;
}
if(!jvmAttach.attach()) {
spdlog::error("Failed to attach to JVM!");
return;
}
// Get JNIEnv
JNIEnv* env = jvmAttach.get_env();
if(!env) {
spdlog::error("Failed to get JNIEnv!");
return;
}
// Get JNIEnv
JNIEnv* env = jvmAttach.get_env();
if(!env) {
spdlog::error("Failed to get JNIEnv!");
return;
}
// Get JVMTI environment
jvmtiEnv* jvmti = jvmAttach.get_jvmti();
if(!jvmti) {
spdlog::error("Failed to get JVMTI environment!");
return;
// Get JVMTI environment
jvmtiEnv* jvmti = jvmAttach.get_jvmti();
if(!jvmti) {
spdlog::error("Failed to get JVMTI environment!");
return;
}
JuiceAgent::Loader::preload(path, env, jvmti);
} catch (const std::exception& e) {
spdlog::error("[Entry] Unhandled exception in entrypoint: {}", e.what());
} catch (...) {
spdlog::error("[Entry] Unhandled unknown exception in entrypoint");
}
JuiceAgent::Loader::preload(path, env, jvmti);
}
}