Fix: avoid running agent/loader logic on plain System.load

This commit is contained in:
xiaozhou233 committed 2026-08-16 15:32:23 +08:00
1 parent c5acf2aba0
commit 092110672a
6 files changed
+22 -9

No files matched your search

+4 -1
View File
@@ -6,7 +6,10 @@
namespace JuiceAgent::services
{
static JuiceAgent::Agent& agent = JuiceAgent::Agent::instance();
inline JuiceAgent::Agent& agent() {
static JuiceAgent::Agent& instance = JuiceAgent::Agent::instance();
return instance;
}
namespace Manager {
void register_events();
+2 -2
View File
@@ -53,8 +53,8 @@ namespace JuiceAgent::services::Bytecode {
}
void init() {
agent.get_eventbus().subscribe<EventClassFileLoadHook>(capture_bytecodes);
agent.get_eventbus().subscribe<EventClassFileLoadHook>(patch_bytecodes);
agent().get_eventbus().subscribe<EventClassFileLoadHook>(capture_bytecodes);
agent().get_eventbus().subscribe<EventClassFileLoadHook>(patch_bytecodes);
}
void start() {
+2 -2
View File
@@ -7,7 +7,7 @@ namespace JuiceAgent::services::JarLoader {
static const char* MODULE_METHOD = "loadJar";
void init() {
auto& cfg = agent.get_config();
auto& cfg = agent().get_config();
config.enabled = cfg.get<bool>(
"JuiceAgent.Modules.JarLoader.Enabled",
@@ -56,7 +56,7 @@ namespace JuiceAgent::services::JarLoader {
auto data = ser.serialize();
JuiceAgent::Utils::call_java_impl(
agent.get_env(),
agent().get_env(),
MODULE_CLASS,
MODULE_METHOD,
data.c_str()
+2 -2
View File
@@ -4,10 +4,10 @@
namespace JuiceAgent::services::Manager {
void register_events() {
// PreLoad
agent.get_eventbus().subscribe<EventPreLoad>(init);
agent().get_eventbus().subscribe<EventPreLoad>(init);
// Loaded
agent.get_eventbus().subscribe<EventLoaded>(start);
agent().get_eventbus().subscribe<EventLoaded>(start);
// TODO: Stopped
+2
View File
@@ -122,6 +122,8 @@ static jboolean jni_inject(JNIEnv* env, jint pid, jstring path, const char* conf
strncpy(params, config_dir, MAX_PATH - 1);
params[MAX_PATH - 1] = '\0';
inject_params = params;
} else {
inject_params = params;
}
int ret = inject(pid, dll_path, inject_params);
+10 -2
View File
@@ -3,13 +3,19 @@
#include "JuiceAgent/Logger.hpp"
#include "Loader.hpp"
static bool g_loaded_via_injection = false;
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved) {
switch (fdwReason) {
case DLL_PROCESS_ATTACH: {
if (lpReserved == NULL) {
return TRUE;
}
g_loaded_via_injection = true;
DisableThreadLibraryCalls(hinstDLL);
Logger::init("libloader.log");
// 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);
@@ -18,7 +24,9 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved) {
}
case DLL_PROCESS_DETACH: {
spdlog::info("DllMain: DLL_PROCESS_DETACH");
if (g_loaded_via_injection) {
spdlog::info("DllMain: DLL_PROCESS_DETACH");
}
break;
}
}