Refactor: simplify injector/libinject/loader code

- libinject: drop debug cruft, dedup JNI overloads into jni_inject, const-correct inject()
- loader: reuse Utils::call_java_impl for JNI bootstrap
- JvmAttach: collapse acquire_jvm/env/jvmti retry loops into one template
- injector: remove redundant copy_arg wrapper
- DllMain: remove no-op DLL_THREAD cases, merge attach logs
- libagent: guard add_library so empty source dir does not break configure
This commit is contained in:
xiaozhou233 committed 2026-08-15 12:55:27 +08:00
1 parent a251b8996e
commit bcc1ecbf0e
6 files changed
+82 -181

No files matched your search

+2 -30
View File
@@ -11,40 +11,12 @@ namespace JuiceAgent::Loader {
static bool invoke_juiceagent_init(JNIEnv* env, const LoaderConfig& config) {
const char* bootstrap_class = "cn/xiaozhou233/juiceagent/api/JuiceAgentBootstrap";
const char* method_name = "start";
const char* method_desc = "(Ljava/lang/String;)V";
using JuiceAgent::Utils::check_and_clear_exception;
// Find Java class
JuiceAgent::Utils::LocalRef<jclass> cls(env, env->FindClass(bootstrap_class));
if (!cls.get() || check_and_clear_exception(env, "FindClass")) {
spdlog::error("Failed to find class: {}", bootstrap_class);
return false;
}
// Find static method
jmethodID method_id = env->GetStaticMethodID(cls, method_name, method_desc);
if (!method_id || check_and_clear_exception(env, "GetStaticMethodID")) {
spdlog::error("Failed to find method: {}", method_name);
return false;
}
// Serialize InjectionInfo to string
std::string args = JuiceAgent::Config::Utils::serialize_loader_config(config);
spdlog::debug("Invoke args: {}", args);
// Create Java string
jstring jArgs = env->NewStringUTF(args.c_str());
if (!jArgs || check_and_clear_exception(env, "NewStringUTF")) {
spdlog::error("Failed to create jstring");
return false;
}
// Call the static method
env->CallStaticVoidMethod(cls, method_id, jArgs);
// Check for exceptions
if (check_and_clear_exception(env, "CallStaticVoidMethod")) {
if (!JuiceAgent::Utils::call_java_impl(env, bootstrap_class, method_name, args.c_str())) {
spdlog::error("Failed to init JuiceAgent!");
return false;
}
+23 -29
View File
@@ -61,53 +61,47 @@ private:
std::this_thread::sleep_for(std::chrono::milliseconds(retry_delay_ms_));
}
bool acquire_jvm() {
template<typename Fn>
bool acquire(const char* what, Fn&& attempt) {
for (int i = 1; i <= max_try_; i++) {
jint status = JNI_GetCreatedJavaVMs(&jvm_, 1, nullptr);
if (status == JNI_OK && jvm_ != nullptr) {
spdlog::debug("Got JavaVM");
if (attempt()) {
return true;
}
retry("JavaVM", i);
retry(what, i);
}
return false;
}
bool acquire_jvm() {
return acquire("JavaVM", [this] {
jvm_ = nullptr;
return JNI_GetCreatedJavaVMs(&jvm_, 1, nullptr) == JNI_OK && jvm_ != nullptr;
});
}
bool acquire_env() {
for (int i = 1; i <= max_try_; i++) {
jint status = jvm_->GetEnv(reinterpret_cast<void**>(&env_), JNI_VERSION_1_6);
if (status == JNI_OK && env_ != nullptr) {
spdlog::debug("Got JNIEnv (already attached)");
return acquire("JNIEnv", [this] {
env_ = nullptr;
if (jvm_->GetEnv(reinterpret_cast<void**>(&env_), JNI_VERSION_1_6) == JNI_OK && env_ != nullptr) {
return true;
}
status = jvm_->AttachCurrentThread(reinterpret_cast<void**>(&env_), nullptr);
if (status == JNI_OK && env_ != nullptr) {
env_ = nullptr;
if (jvm_->AttachCurrentThread(reinterpret_cast<void**>(&env_), nullptr) == JNI_OK && env_ != nullptr) {
attached_ = true;
spdlog::debug("Attached current thread");
return true;
}
retry("JNIEnv attach", i);
}
return false;
env_ = nullptr;
return false;
});
}
bool acquire_jvmti() {
for (int i = 1; i <= max_try_; i++) {
jint status = jvm_->GetEnv(reinterpret_cast<void**>(&jvmti_), JVMTI_VERSION_1_2);
if (status == JNI_OK && jvmti_ != nullptr) {
spdlog::debug("Got JVMTI");
return true;
}
retry("JVMTI", i);
}
return false;
return acquire("JVMTI", [this] {
jvmti_ = nullptr;
return jvm_->GetEnv(reinterpret_cast<void**>(&jvmti_), JVMTI_VERSION_1_2) == JNI_OK && jvmti_ != nullptr;
});
}
};
+3 -14
View File
@@ -9,22 +9,11 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved) {
DisableThreadLibraryCalls(hinstDLL);
Logger::init("libloader.log");
spdlog::info("DllMain: DLL_PROCESS_ATTACH");
// Get the runtime directory from the lpReserved parameter
const char* runtime_dir = (const char*)lpReserved;
spdlog::info("DllMain: runtime_dir: {}", runtime_dir);
// lpReserved carries the runtime directory from the reflective loader
const char* runtime_dir = (const char*)lpReserved;
spdlog::info("DllMain: DLL_PROCESS_ATTACH, runtime_dir: {}", runtime_dir);
JuiceAgent::Loader::entrypoint(runtime_dir);
break;
}
case DLL_THREAD_ATTACH: {
break;
}
case DLL_THREAD_DETACH: {
break;
}