diff --git a/src/libagent/includes/global.hpp b/src/libagent/includes/global.hpp index 4da7aa5..dce21a6 100644 --- a/src/libagent/includes/global.hpp +++ b/src/libagent/includes/global.hpp @@ -150,12 +150,79 @@ public: void setRemapClass(jclass clazz) { remapClass_ = clazz; } void setRemapMethodId(jmethodID methodId) { remapMethodId_ = methodId; } + // ---- Include/Exclude filters ---- + + void addInclude(std::string name) { + std::lock_guard lock(filterMutex_); + includes_.insert(std::move(name)); + } + + void removeInclude(const std::string& name) { + std::lock_guard lock(filterMutex_); + includes_.erase(name); + } + + void clearIncludes() { + std::lock_guard lock(filterMutex_); + includes_.clear(); + } + + void addExclude(std::string name) { + std::lock_guard lock(filterMutex_); + excludes_.insert(std::move(name)); + } + + void removeExclude(const std::string& name) { + std::lock_guard lock(filterMutex_); + excludes_.erase(name); + } + + void clearExcludes() { + std::lock_guard lock(filterMutex_); + excludes_.clear(); + } + + // A class should be remapped when: + // 1. its name is not excluded, and + // 2. its name matches an include filter. + // Remapping is OFF by default: an empty include set means nothing is + // remapped. To enable remapping for a package, add it as an include + // (e.g. "com/example/" matches the whole package). + // + // Matching rules (applied to both include and exclude): + // - exact internal name match + // - package prefix (ends with '/', e.g. "java/lang/") + // - class prefix + nested classes (e.g. "java/lang/Shutdown" + // also excludes "java/lang/Shutdown$Lock") + bool shouldRemap(const std::string& name) const { + std::lock_guard lock(filterMutex_); + + for (const auto& exc : excludes_) { + if (name == exc) return false; + if (exc.back() == '/' && name.rfind(exc, 0) == 0) return false; + if (name.size() > exc.size() && + name.rfind(exc, 0) == 0 && name[exc.size()] == '$') return false; + } + + for (const auto& inc : includes_) { + if (name == inc) return true; + if (inc.back() == '/' && name.rfind(inc, 0) == 0) return true; + if (name.size() > inc.size() && + name.rfind(inc, 0) == 0 && name[inc.size()] == '$') return true; + } + return false; + } + private: Remapper() = default; bool initialized_ = false; jclass remapClass_ = nullptr; // global ref jmethodID remapMethodId_ = nullptr; // static method id + + mutable std::mutex filterMutex_; + std::unordered_set includes_; + std::unordered_set excludes_; }; } diff --git a/src/libagent/jni/juiceremapper.cpp b/src/libagent/jni/juiceremapper.cpp index 9b9680f..41325d5 100644 --- a/src/libagent/jni/juiceremapper.cpp +++ b/src/libagent/jni/juiceremapper.cpp @@ -72,45 +72,69 @@ JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceremapper_JuiceRemapper_initN } JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceremapper_JuiceRemapper_addInclude - (JNIEnv *, jclass, jstring) { - // TODO: implement - spdlog::warn("JuiceRemapper::addInclude"); + (JNIEnv *env, jclass, jstring name) { + if (!name) return JNI_FALSE; + const char* utf = env->GetStringUTFChars(name, nullptr); + if (!utf) return JNI_FALSE; + std::string s(utf); + env->ReleaseStringUTFChars(name, utf); + + JuiceAgent::Services::Remapper::getInstance().addInclude(std::move(s)); + spdlog::debug("[JuiceRemapper JNI] addInclude"); return JNI_TRUE; } JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceremapper_JuiceRemapper_addExclude - (JNIEnv *, jclass, jstring) { - // TODO: implement - spdlog::warn("JuiceRemapper::addExclude"); + (JNIEnv *env, jclass, jstring name) { + if (!name) return JNI_FALSE; + const char* utf = env->GetStringUTFChars(name, nullptr); + if (!utf) return JNI_FALSE; + std::string s(utf); + env->ReleaseStringUTFChars(name, utf); + + JuiceAgent::Services::Remapper::getInstance().addExclude(std::move(s)); + spdlog::debug("[JuiceRemapper JNI] addExclude"); return JNI_TRUE; } JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceremapper_JuiceRemapper_removeInclude - (JNIEnv *, jclass, jstring) { - // TODO: implement - spdlog::warn("JuiceRemapper::removeInclude"); + (JNIEnv *env, jclass, jstring name) { + if (!name) return JNI_FALSE; + const char* utf = env->GetStringUTFChars(name, nullptr); + if (!utf) return JNI_FALSE; + std::string s(utf); + env->ReleaseStringUTFChars(name, utf); + + JuiceAgent::Services::Remapper::getInstance().removeInclude(s); + spdlog::debug("[JuiceRemapper JNI] removeInclude"); return JNI_TRUE; } JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceremapper_JuiceRemapper_removeExclude - (JNIEnv *, jclass, jstring){ - // TODO: implement - spdlog::warn("JuiceRemapper::removeExclude"); + (JNIEnv *env, jclass, jstring name){ + if (!name) return JNI_FALSE; + const char* utf = env->GetStringUTFChars(name, nullptr); + if (!utf) return JNI_FALSE; + std::string s(utf); + env->ReleaseStringUTFChars(name, utf); + + JuiceAgent::Services::Remapper::getInstance().removeExclude(s); + spdlog::debug("[JuiceRemapper JNI] removeExclude"); return JNI_TRUE; } JNIEXPORT void JNICALL Java_cn_xiaozhou233_juiceremapper_JuiceRemapper_clearIncludes (JNIEnv *, jclass) { - // TODO: implement - spdlog::warn("JuiceRemapper::clearIncludes"); + JuiceAgent::Services::Remapper::getInstance().clearIncludes(); + spdlog::debug("[JuiceRemapper JNI] clearIncludes"); } JNIEXPORT void JNICALL Java_cn_xiaozhou233_juiceremapper_JuiceRemapper_clearExcludes (JNIEnv *, jclass) { - // TODO: implement - spdlog::warn("JuiceRemapper::clearExcludes"); + JuiceAgent::Services::Remapper::getInstance().clearExcludes(); + spdlog::debug("[JuiceRemapper JNI] clearExcludes"); } \ No newline at end of file diff --git a/src/libagent/services/JuiceRemapperService.cpp b/src/libagent/services/JuiceRemapperService.cpp index e45bf95..6d8ac35 100644 --- a/src/libagent/services/JuiceRemapperService.cpp +++ b/src/libagent/services/JuiceRemapperService.cpp @@ -48,7 +48,7 @@ private: return; } - spdlog::debug("[JuiceRemapperService] Enabled"); + spdlog::debug("[JuiceRemapperService] Enabled. Waiting for addInclude to start remapping"); } void onClassFileLoadHook(const EventClassFileLoadHook& e) { @@ -64,7 +64,11 @@ private: if (std::string_view(e.name).starts_with("cn/xiaozhou233/juiceremapper/")) return; - // TODO: Include/Exclude check + // Include/Exclude filter check + if (!remapper.shouldRemap(e.name)) { + spdlog::trace("[JuiceRemapperService] Skipped by filter: {}", e.name); + return; + } jclass remapClass = remapper.getRemapClass(); jmethodID remapMethod = remapper.getRemapMethodId();