From ff216aabeec5da64a4c4893efe457ba0082602cb Mon Sep 17 00:00:00 2001 From: xiaozhou233 Date: Tue, 18 Aug 2026 16:51:21 +0800 Subject: [PATCH] Fix: harden remapper filters and agent init guard - skip empty include/exclude entries in Remapper::shouldRemap to avoid calling back() on an empty std::string (undefined behavior) - null-check new_class_data_len/new_classbytes in JuiceRemapperService before writing remapped bytes back to the JVMTI callback - add Agent::initialized_ guard so init() is a no-op on second call, preventing duplicate service instantiation and listeners --- src/libagent/JuiceAgent.cpp | 5 +++++ src/libagent/includes/JuiceAgent.hpp | 1 + src/libagent/includes/global.hpp | 2 ++ src/libagent/services/JuiceRemapperService.cpp | 3 ++- 4 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/libagent/JuiceAgent.cpp b/src/libagent/JuiceAgent.cpp index ed5a63f..a3f3180 100644 --- a/src/libagent/JuiceAgent.cpp +++ b/src/libagent/JuiceAgent.cpp @@ -69,6 +69,11 @@ namespace JuiceAgent { spdlog::error("[JuiceAgent] Agent not loaded! Please call preload() first!"); return false; } + if(initialized_) { + spdlog::warn("[JuiceAgent] Agent already initialized! Skipping..."); + return true; + } + initialized_ = true; // Add Abilities jvmtiCapabilities caps{}; diff --git a/src/libagent/includes/JuiceAgent.hpp b/src/libagent/includes/JuiceAgent.hpp index 57f1400..a3f630a 100644 --- a/src/libagent/includes/JuiceAgent.hpp +++ b/src/libagent/includes/JuiceAgent.hpp @@ -13,6 +13,7 @@ namespace JuiceAgent { JuiceAgent::Config::Config config; bool loaded = false; + bool initialized_ = false; public: // Singleton static Agent& getInstance(); diff --git a/src/libagent/includes/global.hpp b/src/libagent/includes/global.hpp index dce21a6..6b1e90f 100644 --- a/src/libagent/includes/global.hpp +++ b/src/libagent/includes/global.hpp @@ -198,6 +198,7 @@ public: std::lock_guard lock(filterMutex_); for (const auto& exc : excludes_) { + if (exc.empty()) continue; if (name == exc) return false; if (exc.back() == '/' && name.rfind(exc, 0) == 0) return false; if (name.size() > exc.size() && @@ -205,6 +206,7 @@ public: } for (const auto& inc : includes_) { + if (inc.empty()) continue; if (name == inc) return true; if (inc.back() == '/' && name.rfind(inc, 0) == 0) return true; if (name.size() > inc.size() && diff --git a/src/libagent/services/JuiceRemapperService.cpp b/src/libagent/services/JuiceRemapperService.cpp index 6d8ac35..8f9fbcf 100644 --- a/src/libagent/services/JuiceRemapperService.cpp +++ b/src/libagent/services/JuiceRemapperService.cpp @@ -53,7 +53,8 @@ private: void onClassFileLoadHook(const EventClassFileLoadHook& e) { if (!e.name || !e.jni_env || !e.jvmti_env || - !e.classbytes || e.class_data_len <= 0) { + !e.classbytes || e.class_data_len <= 0 || + !e.new_class_data_len || !e.new_classbytes) { return; }