feat: remove Remapper module and associated JNI functions

This commit is contained in:
xiaozhou233 committed 2026-04-29 17:24:40 +08:00
1 parent 2493a9243f
commit fc5d23109d
3 files changed
-320

No files matched your search

-35
View File
@@ -1,35 +0,0 @@
#include <jni_impl.hpp>
#include <global.hpp>
JNIEXPORT void JNICALL Java_cn_xiaozhou233_juiceagent_api_JuiceAgent_addRemapperWhiteList
(JNIEnv *env, jclass, jstring package_name) {
std::string pkg_name = env->GetStringUTFChars(package_name, nullptr);
if (pkg_name.empty()) {
PLOGW << "package name is empty";
return;
}
remapperWhiteList.insert(pkg_name);
PLOGI << "add remapper white list: " << pkg_name;
}
JNIEXPORT void JNICALL Java_cn_xiaozhou233_juiceagent_api_JuiceAgent_removeRemapperWhiteList
(JNIEnv *env, jclass, jstring package_name) {
std::string pkg_name = env->GetStringUTFChars(package_name, nullptr);
if (pkg_name.empty()) {
PLOGW << "package name is empty";
return;
}
remapperWhiteList.erase(pkg_name);
PLOGI << "remove remapper white list: " << pkg_name;
}
JNIEXPORT void JNICALL Java_cn_xiaozhou233_juiceagent_api_JuiceAgent_clearRemapperWhiteList
(JNIEnv *env, jclass) {
remapperWhiteList.clear();
PLOGI << "clear remapper white list";
}
-281
View File
@@ -1,281 +0,0 @@
#include <modules/ModuleRegistry.hpp>
#include <modules/ModuleBase.hpp>
#include <JuiceAgent/Logger.hpp>
#include <libagent.hpp>
#include <event/event_type.hpp>
#include <global.hpp>
#include <string_view>
#include <string>
#include <JuiceAgent/Utils.hpp>
namespace JuiceAgent::Core::Modules {
struct RemapperConfig {
bool enabled = false;
std::string JuiceRemapperJarPath;
};
static RemapperConfig config;
static JuiceAgent::Agent& agent = JuiceAgent::Agent::instance();
class RemapperModule : public ModuleBase {
using super = ModuleBase;
struct _Remapper {
jclass handler_class = nullptr;
jmethodID onClassFileLoadMethod = nullptr;
jmethodID onMethodEntryMethod = nullptr;
jmethodID onMethodExitMethod = nullptr;
} Remapper;
public:
std::string name() const override {
return "Remapper";
}
protected:
bool on_init() override {
auto& cfg = agent.get_config();
config.enabled = cfg.get<bool>(
"JuiceAgent.Modules.Remapper.Enabled",
false
);
config.JuiceRemapperJarPath = cfg.get<std::string>(
"JuiceAgent.Modules.Remapper.JuiceRemapperJarPath",
"./JuiceRemapper.jar",
true
);
if (!config.enabled) {
return true;
}
if (agent.get_jvmti()->AddToSystemClassLoaderSearch(
config.JuiceRemapperJarPath.c_str()
) != JNI_OK) {
PLOGE << "Failed to load JuiceRemapper.jar";
return false;
}
JNIEnv* env = agent.get_env();
jclass local = env->FindClass(
"cn/xiaozhou233/juiceremapper/api/Handler"
);
if (local == nullptr) {
check_and_clear_exception(env, "FindClass Handler");
PLOGE << "Failed to find Handler";
return false;
}
Remapper.handler_class =
static_cast<jclass>(env->NewGlobalRef(local));
env->DeleteLocalRef(local);
if (Remapper.handler_class == nullptr) {
return false;
}
Remapper.onClassFileLoadMethod = env->GetStaticMethodID(
Remapper.handler_class,
"onClassFileLoadHook",
"(Ljava/lang/Class;Ljava/lang/ClassLoader;Ljava/lang/String;Ljava/lang/Object;I[B)[B"
);
if (Remapper.onClassFileLoadMethod == nullptr) {
check_and_clear_exception(env, "onClassFileLoadHook");
return false;
}
Remapper.onMethodEntryMethod = env->GetStaticMethodID(
Remapper.handler_class,
"onMethodEntry",
"(Ljava/lang/Thread;Ljava/lang/reflect/Method;)V"
);
if (Remapper.onMethodEntryMethod == nullptr) {
check_and_clear_exception(env, "onMethodEntry");
return false;
}
Remapper.onMethodExitMethod = env->GetStaticMethodID(
Remapper.handler_class,
"onMethodExit",
"(Ljava/lang/Thread;Ljava/lang/reflect/Method;ZLjava/lang/Object;)V"
);
if (Remapper.onMethodExitMethod == nullptr) {
check_and_clear_exception(env, "onMethodExit");
return false;
}
return true;
}
bool on_start() override {
if (!config.enabled) {
return true;
}
auto& bus = agent.get_eventbus();
_classFileToken = bus.subscribe<EventClassFileLoadHook>(
[this](const auto& e) {
on_class_file_load_hook(e);
}
);
_methodEntryToken = bus.subscribe<EventMethodEntry>(
[this](const auto& e) {
on_method_entry_hook(e);
}
);
_methodExitToken = bus.subscribe<EventMethodExit>(
[this](const auto& e) {
on_method_exit_hook(e);
}
);
return true;
}
void on_stop() override {
if (!config.enabled) {
return;
}
auto& bus = agent.get_eventbus();
if (_classFileToken != 0) {
bus.unsubscribe<EventClassFileLoadHook>(_classFileToken);
}
if (_methodEntryToken != 0) {
bus.unsubscribe<EventMethodEntry>(_methodEntryToken);
}
if (_methodExitToken != 0) {
bus.unsubscribe<EventMethodExit>(_methodExitToken);
}
_classFileToken = 0;
_methodEntryToken = 0;
_methodExitToken = 0;
JNIEnv* env = agent.get_env();
if (env != nullptr && Remapper.handler_class != nullptr) {
env->DeleteGlobalRef(Remapper.handler_class);
Remapper.handler_class = nullptr;
}
}
private:
void on_class_file_load_hook(const EventClassFileLoadHook& event) {
std::string_view name = event.name ? event.name : "";
if (name.starts_with("cn/xiaozhou233/juiceremapper/")) {
return;
}
JNIEnv* env = event.jni_env;
std::string class_name(name);
jstring j_name = env->NewStringUTF(class_name.c_str());
if (j_name == nullptr) {
return;
}
jbyteArray input = env->NewByteArray(event.class_data_len);
if (input == nullptr) {
env->DeleteLocalRef(j_name);
return;
}
env->SetByteArrayRegion(
input,
0,
event.class_data_len,
reinterpret_cast<const jbyte*>(event.classbytes)
);
jbyteArray output = static_cast<jbyteArray>(
env->CallStaticObjectMethod(
Remapper.handler_class,
Remapper.onClassFileLoadMethod,
event.class_being_redefined,
event.loader,
j_name,
event.protection_domain,
event.class_data_len,
input
)
);
check_and_clear_exception(env, "on_class_file_load_hook");
if (output != nullptr) {
jsize len = env->GetArrayLength(output);
unsigned char* buffer = nullptr;
if (event.jvmti_env->Allocate(len, &buffer) == JVMTI_ERROR_NONE) {
env->GetByteArrayRegion(
output,
0,
len,
reinterpret_cast<jbyte*>(buffer)
);
*event.new_class_data_len = len;
*event.new_classbytes = buffer;
}
env->DeleteLocalRef(output);
}
env->DeleteLocalRef(input);
env->DeleteLocalRef(j_name);
}
void on_method_entry_hook(const EventMethodEntry& event) {
// event.jni_env->CallStaticVoidMethod(
// Remapper.handler_class,
// Remapper.onMethodEntryMethod,
// event.thread,
// event.method
// );
check_and_clear_exception(event.jni_env, "on_method_entry_hook");
}
void on_method_exit_hook(const EventMethodExit& event) {
// event.jni_env->CallStaticVoidMethod(
// Remapper.handler_class,
// Remapper.onMethodExitMethod,
// event.thread,
// event.method,
// event.was_popped_by_exception,
// event.return_value.l
// );
check_and_clear_exception(event.jni_env, "on_method_exit_hook");
}
private:
EventBus::Token _classFileToken = 0;
EventBus::Token _methodEntryToken = 0;
EventBus::Token _methodExitToken = 0;
};
}
JUICEAGENT_REGISTER_MODULE(
JuiceAgent::Core::Modules::RemapperModule,
Remapper
)