mirror of
https://github.com/xiaozhou233/JuiceAgent.git
synced 2026-12-05 13:05:53 +08:00
Fix: harden libloader config handling
- Guard Config against NULL runtime_dir - Read JuiceAgent.Version from config instead of hardcoding 2 - Move LoaderConfig to ConfigStruct.hpp; remove obsolete JuiceAgent.hpp
This commit is contained in:
1 parent
b34377d447
commit
3b180e7199
3 files changed
+39
-14
No files matched your search
@@ -4,7 +4,7 @@
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
|
||||
#include <JuiceAgent/JuiceAgent.hpp>
|
||||
#include <JuiceAgent/ConfigStruct.hpp>
|
||||
#include <JuiceAgent/Logger.hpp>
|
||||
#include <JuiceAgent/Utils.hpp>
|
||||
#include <toml.hpp>
|
||||
@@ -142,12 +142,12 @@ namespace Utils {
|
||||
spdlog::info("RuntimeDir: {}", config.RuntimeDir);
|
||||
}
|
||||
|
||||
inline std::string serialize_loader_config(const LoaderConfig& config)
|
||||
inline std::string serialize_loader_config(const Config& config, const LoaderConfig& loader_config)
|
||||
{
|
||||
JuiceAgent::Utils::Serializer ser;
|
||||
ser.add_kv("Version", 2);
|
||||
ser.add_kv("JuiceAgentNativeLibraryPath", config.JuiceAgentNativeLibraryPath);
|
||||
ser.add_kv("RuntimeDir", config.RuntimeDir);
|
||||
ser.add_kv("Version", config.get<int>("JuiceAgent.Version", 1));
|
||||
ser.add_kv("JuiceAgentNativeLibraryPath", loader_config.JuiceAgentNativeLibraryPath);
|
||||
ser.add_kv("RuntimeDir", loader_config.RuntimeDir);
|
||||
|
||||
return ser.serialize();
|
||||
}
|
||||
|
||||
@@ -2,11 +2,8 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace JuiceAgent {
|
||||
|
||||
struct LoaderConfig {
|
||||
std::string JuiceAgentNativeLibraryPath;
|
||||
std::string RuntimeDir;
|
||||
};
|
||||
|
||||
} // namespace JuiceAgent
|
||||
std::string RuntimeDir;
|
||||
};
|
||||
@@ -1,18 +1,31 @@
|
||||
#include <jvm/jni.h>
|
||||
#include <Loader.hpp>
|
||||
#include <filesystem>
|
||||
#include <JuiceAgent/Logger.hpp>
|
||||
#include <JuiceAgent/Utils.hpp>
|
||||
#include <JuiceAgent/Config.hpp>
|
||||
#include <JuiceAgent/ConfigStruct.hpp>
|
||||
#include <JuiceAgent/JuiceAgent-API-1.4.1+build.1.hpp>
|
||||
|
||||
// Preload section
|
||||
// Check parameters and load the runtime
|
||||
void JuiceAgent::Loader::preload(const char* runtime_dir, JNIEnv* env, jvmtiEnv* jvmti) {
|
||||
std::string runtime_dir_new;
|
||||
|
||||
// Check parameters
|
||||
if(!runtime_dir) {
|
||||
spdlog::warn("[PreLoad] Runtime directory is not specified!");
|
||||
}
|
||||
spdlog::info("[PreLoad] Runtime directory: {}", runtime_dir ? runtime_dir : "NULL");
|
||||
|
||||
runtime_dir_new = std::filesystem::current_path().string();
|
||||
|
||||
spdlog::warn( "[PreLoad] Using default runtime directory: {}", runtime_dir_new);
|
||||
} else {
|
||||
runtime_dir_new = runtime_dir;
|
||||
}
|
||||
|
||||
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;
|
||||
@@ -43,14 +56,29 @@ void JuiceAgent::Loader::preload(const char* runtime_dir, JNIEnv* env, jvmtiEnv*
|
||||
spdlog::info("JuiceAgent API loaded to system classloader!");
|
||||
|
||||
// Init JuiceAgent via JuiceAgent API
|
||||
JuiceAgent::Loader::initialize(runtime_dir, env, jvmti);
|
||||
JuiceAgent::Loader::initialize(runtime_dir_new.c_str(), env, jvmti);
|
||||
}
|
||||
|
||||
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";
|
||||
|
||||
bool result = JuiceAgent::Utils::call_java_impl(env, bootstrap_class, method_name, runtime_dir);
|
||||
// Read JuiceAgent config
|
||||
JuiceAgent::Config::Config cfg(runtime_dir ? runtime_dir : "");
|
||||
if (!cfg.is_valid()) {
|
||||
spdlog::error("JuiceAgent configuration is invalid!");
|
||||
return;
|
||||
}
|
||||
|
||||
// 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;
|
||||
|
||||
Reference in new issue
Block a user