From 92ad21003fd758b82896337a65ba9e09b7329121 Mon Sep 17 00:00:00 2001 From: xiaozhou233 Date: Wed, 14 Oct 2026 19:23:41 +0800 Subject: [PATCH] Feat: support built-in and custom jar loading modes - Split jar loading into load_builtin (extract and register the bundled JuiceAgent API jar) and load_custom (register a user-supplied jar) in a new JarLoader.cpp - Check JuiceAgent.Version against CONFIG_VERSION and dispatch on JarLoader.Mode (BuiltIn / CustomJar) during preload - Pass the parsed Config into Loader::initialize and add CONFIG_VERSION in JuiceAgent.hpp - Restore Config::Utils loader-config helpers on top of the new getPath API --- includes/JuiceAgent/Config.hpp | 58 ++++++++++++++++++- includes/JuiceAgent/JuiceAgent.hpp | 3 + src/libloader/core/JarLoader.cpp | 55 ++++++++++++++++++ src/libloader/core/Loader.cpp | 92 ++++++++++++------------------ src/libloader/includes/Loader.hpp | 6 +- 5 files changed, 158 insertions(+), 56 deletions(-) create mode 100644 includes/JuiceAgent/JuiceAgent.hpp create mode 100644 src/libloader/core/JarLoader.cpp diff --git a/includes/JuiceAgent/Config.hpp b/includes/JuiceAgent/Config.hpp index 3c59cf2..8e2172e 100644 --- a/includes/JuiceAgent/Config.hpp +++ b/includes/JuiceAgent/Config.hpp @@ -8,6 +8,8 @@ #include #include #include +#include +#include namespace JuiceAgent::Config { @@ -89,7 +91,31 @@ public: } std::string getPath(const std::string& key) const { - const std::filesystem::path value(get(key)); + const std::string value = get(key); + + if (value.empty()) { + return {}; + } + + return getPath(key, value); + } + + std::string getPath( + const std::string& key, + const std::string& defaultValue + ) const { + std::string rawValue = get(key, defaultValue); + + // Use the default value when the configuration value is empty. + if (rawValue.empty()) { + rawValue = defaultValue; + } + + if (rawValue.empty()) { + return {}; + } + + const std::filesystem::path value(rawValue); if (value.is_absolute()) { return value.lexically_normal().string(); @@ -207,4 +233,34 @@ public: } }; +namespace Utils { + inline LoaderConfig get_loader_config(const Config& config) + { + LoaderConfig loader_config; + loader_config.JuiceAgentNativeLibraryPath = config.getPath( + "JuiceAgent.Loader.JuiceAgentNativeLibraryPath", + (config.getRuntimeDir() / "libagent.dll").string() + ); + loader_config.RuntimeDir = config.getRuntimeDir().string(); + + return loader_config; + } + + inline void print_loader_config(const LoaderConfig& config) + { + spdlog::info("JuiceAgentNativeLibraryPath: {}", config.JuiceAgentNativeLibraryPath); + spdlog::info("RuntimeDir: {}", config.RuntimeDir); + } + + inline std::string serialize_loader_config(const Config& config, const LoaderConfig& loader_config) + { + JuiceAgent::Utils::Serializer ser; + ser.add_kv("Version", config.get("JuiceAgent.Version", -1)); + ser.add_kv("JuiceAgentNativeLibraryPath", loader_config.JuiceAgentNativeLibraryPath); + ser.add_kv("RuntimeDir", loader_config.RuntimeDir); + + return ser.serialize(); + } +} + } // namespace JuiceAgent::Config \ No newline at end of file diff --git a/includes/JuiceAgent/JuiceAgent.hpp b/includes/JuiceAgent/JuiceAgent.hpp new file mode 100644 index 0000000..7fb82b3 --- /dev/null +++ b/includes/JuiceAgent/JuiceAgent.hpp @@ -0,0 +1,3 @@ +#pragma once + +const int CONFIG_VERSION = 2; \ No newline at end of file diff --git a/src/libloader/core/JarLoader.cpp b/src/libloader/core/JarLoader.cpp new file mode 100644 index 0000000..2ec4e08 --- /dev/null +++ b/src/libloader/core/JarLoader.cpp @@ -0,0 +1,55 @@ +#include +#include +#include +#include + +#include +#include +#include + +int JuiceAgent::Loader::load_builtin(jvmtiEnv* jvmti) { + // Extract JuiceAgent API to temp file + std::string hash = + JuiceAgent::Resource::juiceagent_api_bytes_sha256; + + std::string file_name = + hash.substr(0, 8) + "_" + + JuiceAgent::Resource::juiceagent_api_bytes_name; + + std::string juiceagent_api_path = + JuiceAgent::Utils::File::write_to_tempfile( + JuiceAgent::Resource::juiceagent_api_bytes, + JuiceAgent::Resource::juiceagent_api_bytes_len, + file_name + ); + + // Debug + spdlog::debug("JuiceAgent API path: {}", juiceagent_api_path); + spdlog::debug("JuiceAgent API size: {}", JuiceAgent::Resource::juiceagent_api_bytes_len); + spdlog::debug("JuiceAgent API sha256: {}", JuiceAgent::Resource::juiceagent_api_bytes_sha256); + + // Load JuiceAgent API + jvmtiError status = jvmti->AddToSystemClassLoaderSearch(juiceagent_api_path.c_str()); + if (status != JVMTI_ERROR_NONE) { + spdlog::error("Failed to add JuiceAgent API to system classloader! code: {}", static_cast(status)); + return false; + } + + spdlog::info("JuiceAgent API loaded to system classloader!"); + + return true; +} + +int JuiceAgent::Loader::load_custom(jvmtiEnv* jvmti, const char* jar_path) { + spdlog::info("Loading custom jar: {}", jar_path); + + jvmtiError status = jvmti->AddToSystemClassLoaderSearch(jar_path); + if (status != JVMTI_ERROR_NONE) { + spdlog::error("Failed to add custom jar to system classloader! code: {}", static_cast(status)); + return false; + } + + spdlog::info("Custom jar loaded to system classloader!"); + + return true; +} \ No newline at end of file diff --git a/src/libloader/core/Loader.cpp b/src/libloader/core/Loader.cpp index b0ab0a8..2da701e 100644 --- a/src/libloader/core/Loader.cpp +++ b/src/libloader/core/Loader.cpp @@ -2,10 +2,10 @@ #include #include #include -#include #include #include -#include + +#include // Preload section // Check parameters and load the runtime @@ -25,64 +25,48 @@ void JuiceAgent::Loader::preload(const char* runtime_dir, JNIEnv* env, jvmtiEnv* spdlog::info("[PreLoad] Runtime directory: {}", runtime_dir_new); - // TODO: Advanced options - // Extract JuiceAgent API to temp file - std::string hash = - JuiceAgent::Resource::juiceagent_api_bytes_sha256; - - std::string file_name = - hash.substr(0, 8) + "_" + - JuiceAgent::Resource::juiceagent_api_bytes_name; - - std::string juiceagent_api_path = - JuiceAgent::Utils::File::write_to_tempfile( - JuiceAgent::Resource::juiceagent_api_bytes, - JuiceAgent::Resource::juiceagent_api_bytes_len, - file_name - ); - - // Debug - spdlog::debug("JuiceAgent API path: {}", juiceagent_api_path); - spdlog::debug("JuiceAgent API size: {}", JuiceAgent::Resource::juiceagent_api_bytes_len); - spdlog::debug("JuiceAgent API sha256: {}", JuiceAgent::Resource::juiceagent_api_bytes_sha256); - - // Load JuiceAgent API - jvmtiError status = jvmti->AddToSystemClassLoaderSearch(juiceagent_api_path.c_str()); - if (status != JVMTI_ERROR_NONE) { - spdlog::error("Failed to add JuiceAgent API to system classloader! code: {}", static_cast(status)); + spdlog::trace("[PreLoad] Loading config..."); + JuiceAgent::Config::Config cfg(runtime_dir ? runtime_dir : ""); + if(!cfg.isValid()) { + spdlog::error("[PreLoad] Failed to load config!"); return; } - spdlog::info("JuiceAgent API loaded to system classloader!"); + // Check config version + int version = cfg.get("JuiceAgent.Version"); + if(version != CONFIG_VERSION) { + spdlog::error("[PreLoad] Config version mismatch! Expected: {}, Got: {}, Please update your config!", CONFIG_VERSION, version); + return; + } - // Init JuiceAgent via JuiceAgent API - JuiceAgent::Loader::initialize(runtime_dir_new.c_str(), env, jvmti); + // JarLoader mode check and load jars + std::string jar_loader_mode = cfg.get("JarLoader.Mode"); + if(jar_loader_mode == "BuiltIn") { + spdlog::info("[PreLoad] JarLoader mode: BuiltIn"); + + if(!load_builtin(jvmti)) { + spdlog::error("[PreLoad] Failed to load built-in jars!"); + } + } else if(jar_loader_mode == "CustomJar") { + spdlog::info("[PreLoad] JarLoader mode: CustomJar"); + + std::string jar_path = cfg.get("JarLoader.JarPath"); + + if(!load_custom(jvmti, jar_path.c_str())) { + spdlog::error("[PreLoad] Failed to load custom jar: {}", jar_path); + } + } else { + spdlog::error("[PreLoad] Invalid JarLoader mode: {}", jar_loader_mode); + return; + } + + JuiceAgent::Loader::initialize(runtime_dir_new.c_str(), env, jvmti, cfg); } -void JuiceAgent::Loader::initialize(const char* runtime_dir, JNIEnv* env, jvmtiEnv* jvmti) { - const char* bootstrap_class = "cn/xiaozhou233/juiceagent/api/JuiceAgentBootstrap"; - const char* method_name = "start"; +void JuiceAgent::Loader::initialize(const char* runtime_dir, JNIEnv* env, jvmtiEnv* jvmti, JuiceAgent::Config::Config& cfg) { + std::string jar_loader_mode = cfg.get("JarLoader.Mode"); - // Read JuiceAgent config - JuiceAgent::Config::Config cfg(runtime_dir ? runtime_dir : ""); - if (!cfg.is_valid()) { - spdlog::error("JuiceAgent configuration is invalid!"); - return; - } + // TODO: Implement this - // TODO: Refactor config - LoaderConfig config = JuiceAgent::Config::Utils::get_loader_config(cfg); - JuiceAgent::Config::Utils::print_loader_config(config); - - // Serialize InjectionInfo to string - std::string args = JuiceAgent::Config::Utils::serialize_loader_config(cfg, config); - spdlog::debug("Serialized config: {}", args); - - bool result = JuiceAgent::Utils::call_java_impl(env, bootstrap_class, method_name, args.c_str()); - if (!result) { - spdlog::error("Failed to initialize JuiceAgent!"); - return; - } - - spdlog::info("JuiceAgent initialized!"); + // spdlog::info("JuiceAgent initialized!"); } \ No newline at end of file diff --git a/src/libloader/includes/Loader.hpp b/src/libloader/includes/Loader.hpp index 8ed1758..947d614 100644 --- a/src/libloader/includes/Loader.hpp +++ b/src/libloader/includes/Loader.hpp @@ -2,9 +2,13 @@ #include #include +#include namespace JuiceAgent::Loader { void entrypoint(const char* runtime_dir); void preload(const char* runtime_dir, JNIEnv* env, jvmtiEnv* jvmti); - void initialize(const char* runtime_dir, JNIEnv* env, jvmtiEnv* jvmti); + void initialize(const char* runtime_dir, JNIEnv* env, jvmtiEnv* jvmti, JuiceAgent::Config::Config& cfg); + + int load_builtin(jvmtiEnv* jvmti); + int load_custom(jvmtiEnv* jvmti, const char* jar_path); } // namespace JuiceAgent::Loader