mirror of
https://github.com/xiaozhou233/JuiceAgent.git
synced 2026-12-05 13:05:53 +08:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
16a36ef22a | ||
|
|
4305000bf7 | ||
|
|
65824934d1 | ||
|
|
992fbd9f8d | ||
|
|
882c19ad8f | ||
|
|
637640b938 | ||
|
|
490e4ce2ac | ||
|
|
30ecc82ca2 | ||
|
|
bf4ebd0e5b | ||
|
|
ec7d221b08 | ||
|
|
e5b054fcfc | ||
|
|
b3ec5b6eb5 | ||
|
|
d5a49a5ce7 | ||
|
|
90689f306b | ||
|
|
1e2e5afd07 | ||
|
|
d9d44f029f | ||
|
|
cc1c2bff30 | ||
|
|
7352f8d872 | ||
|
|
4be6324cf3 | ||
|
|
0169e5e133 | ||
|
|
fc5d23109d | ||
|
|
2493a9243f | ||
|
|
28804c0af3 | ||
|
|
9d0fdecf92 | ||
|
|
dfc5886812 |
No files matched your search
@@ -4,10 +4,13 @@ JuiceAgent is an advanced JVMTI-based injection framework designed for runtime J
|
||||
|
||||
**Warning**: This project is still experimental and is not recommended for production use. Use it at your own risk.
|
||||
|
||||
**Note**: This project is designed for a regular JVM. Custom JVMs may not be able to run it.
|
||||
|
||||

|
||||

|
||||

|
||||

|
||||

|
||||
|
||||

|
||||

|
||||
@@ -28,6 +31,7 @@ With JuiceAgent, developers can:
|
||||
- Invoke a specified entry class and method after injection
|
||||
- Redefine already loaded classes at runtime
|
||||
- Retransform bytecode for instrumentation or modification
|
||||
- Dump classes to disk by -agentpath:libagent.dll flags
|
||||
- And more...
|
||||
|
||||
**If you have any questions or suggestions, please feel free to open an issue or submit a pull request. =D**
|
||||
@@ -65,13 +69,10 @@ This project is the native implementation of JuiceAgent-API. See [JuiceAgent.jav
|
||||
|
||||
## Quick Start
|
||||
### 1. Download Release
|
||||
Download `libagent.dll` `libinject.dll` `libloader.dll` and `injector.exe` from the [releases](https://github.com/xiaozhou233/JuiceAgent/releases) page.
|
||||
Download `JuiceAgent_vx.x.x_x64.zip` from the [releases](https://github.com/xiaozhou233/JuiceAgent/releases) page.
|
||||
|
||||
Download `JuiceAgent-API-x.x.x+build.x.jar` from the [JuiceAgent-API](https://github.com/xiaozhou233/JuiceAgent-API/releases) page.
|
||||
|
||||
Put the downloaded files into the same directory.
|
||||
Extract the ZIP file to a directory.
|
||||
- YourDir
|
||||
- JuiceAgent-API-x.x.x+build.x.jar
|
||||
- libagent.dll
|
||||
- libinject.dll
|
||||
- libloader.dll
|
||||
@@ -79,7 +80,6 @@ Put the downloaded files into the same directory.
|
||||
|
||||
### 2. Copy your custom JAR/Dependencies to Directory
|
||||
- YourDir
|
||||
- JuiceAgent-API-x.x.x+build.x.jar
|
||||
- libagent.dll
|
||||
- libinject.dll
|
||||
- libloader.dll
|
||||
@@ -112,6 +112,7 @@ JarPath = "./MyCustomJar.jar"
|
||||
# Entry class to be executed after loading the JAR
|
||||
EntryClass = "Example.Main"
|
||||
# Entry method to be executed after loading the JAR
|
||||
# Method: public static void run()
|
||||
EntryMethod = "run"
|
||||
```
|
||||
|
||||
@@ -119,7 +120,6 @@ Save the config file as `config.toml`.
|
||||
|
||||
- YourDir
|
||||
- **config.toml**
|
||||
- JuiceAgent-API-x.x.x+build.x.jar
|
||||
- libagent.dll
|
||||
- libinject.dll
|
||||
- libloader.dll
|
||||
|
||||
+1
-1
@@ -1 +1 @@
|
||||
VERSION = Version 3.3.1 Build 1
|
||||
VERSION = Version 3.4.0 Build 1
|
||||
@@ -13,6 +13,10 @@ JarPath = ""
|
||||
EntryClass = ""
|
||||
EntryMethod = ""
|
||||
|
||||
[JuiceAgent.Modules.Dumper]
|
||||
Enabled = false
|
||||
DumpDir = ""
|
||||
|
||||
[JuiceAgent.Modules.Remapper]
|
||||
Enabled = false
|
||||
JuiceRemapperJarPath = ""
|
||||
JuiceRemapperJarPath = ""
|
||||
@@ -94,7 +94,7 @@ public:
|
||||
|
||||
if constexpr (std::is_same_v<T, std::string>) {
|
||||
if (value.empty()) {
|
||||
return default_value;
|
||||
value = default_value;
|
||||
}
|
||||
|
||||
// Handle relative paths
|
||||
@@ -127,10 +127,11 @@ public:
|
||||
|
||||
namespace Utils {
|
||||
inline LoaderConfig get_loader_config(const Config& config) {
|
||||
auto cfg = config.get_config().at("JuiceAgent").at("Loader");
|
||||
|
||||
LoaderConfig loader_config = LoaderConfig();
|
||||
loader_config.JuiceAgentNativeLibraryPath = cfg.at("JuiceAgentNativeLibraryPath").as_string();
|
||||
loader_config.JuiceAgentNativeLibraryPath = config.get<std::string>("JuiceAgent.Loader.JuiceAgentNativeLibraryPath",
|
||||
(config.runtime_dir()/"libagent.dll").string(),
|
||||
true);
|
||||
loader_config.RuntimeDir = config.runtime_dir().string();
|
||||
|
||||
return loader_config;
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
#include <libagent.hpp>
|
||||
#include <JuiceAgent.hpp>
|
||||
#include <jni_impl.hpp>
|
||||
#include <bytecodes_opt.hpp>
|
||||
#include <event/event_type.hpp>
|
||||
#include <event/eventbus.hpp>
|
||||
#include <JuiceAgent/Config.hpp>
|
||||
#include <services.hpp>
|
||||
|
||||
namespace JuiceAgent {
|
||||
// ===== Singleton =====
|
||||
@@ -41,12 +41,8 @@ namespace JuiceAgent {
|
||||
set_config(cfg);
|
||||
PLOGD << "env: " << env << ", jvmti: " << jvmti;
|
||||
|
||||
// Load and Initialize Modules
|
||||
module_manager.load_all();
|
||||
module_manager.init_all();
|
||||
|
||||
// Initialize Modules
|
||||
module_manager.init_all();
|
||||
// Register events for services
|
||||
JuiceAgent::services::Manager::register_events();
|
||||
|
||||
// PreLoad Event
|
||||
eventbus.post(EventPreLoad{
|
||||
@@ -85,13 +81,6 @@ namespace JuiceAgent {
|
||||
PLOGE << "Failed to enable event notification: " << result;
|
||||
}
|
||||
|
||||
// register bytecodes_opt
|
||||
JuiceAgent::Bytecodes::register_bytecodes();
|
||||
|
||||
// Start Modules
|
||||
module_manager.start_all();
|
||||
|
||||
|
||||
// Loaded Event
|
||||
eventbus.post(EventLoaded{
|
||||
.jvm = jvm,
|
||||
@@ -18,7 +18,8 @@ void JNICALL ClassFileLoadHook(
|
||||
}
|
||||
|
||||
auto& agent = JuiceAgent::Agent::instance();
|
||||
agent.get_eventbus().post(EventClassFileLoadHook{
|
||||
|
||||
EventClassFileLoadHook event{
|
||||
.jvmti_env = jvmti_env,
|
||||
.jni_env = jni_env,
|
||||
.class_being_redefined = class_being_redefined,
|
||||
@@ -29,5 +30,9 @@ void JNICALL ClassFileLoadHook(
|
||||
.classbytes = classbytes,
|
||||
.new_class_data_len = new_class_data_len,
|
||||
.new_classbytes = new_classbytes
|
||||
});
|
||||
};
|
||||
|
||||
agent.get_eventbus().post(event);
|
||||
|
||||
agent.get_eventbus().post_mutable(event);
|
||||
}
|
||||
@@ -6,5 +6,3 @@ std::mutex classDataMutex;
|
||||
|
||||
std::unordered_map<std::string, std::vector<unsigned char>> pendingRetransform;
|
||||
std::mutex pendingRetransformMutex;
|
||||
|
||||
std::unordered_set<std::string> remapperWhiteList;
|
||||
@@ -0,0 +1,119 @@
|
||||
#include <jni.h>
|
||||
#include <jvmti.h>
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
#include <jni_impl.hpp>
|
||||
#include <JuiceAgent/JuiceAgent-API-1.4.1+build.1.hpp>
|
||||
|
||||
// Global state
|
||||
static JavaVM* g_vm = nullptr;
|
||||
static jvmtiEnv* g_jvmti = nullptr;
|
||||
|
||||
// Forward declaration
|
||||
void JNICALL OnVMInit(jvmtiEnv* jvmti, JNIEnv* env, jthread thread);
|
||||
|
||||
// Utility: check JVMTI error
|
||||
static bool CheckJvmti(jvmtiError err, const char* action) {
|
||||
if (err != JVMTI_ERROR_NONE) {
|
||||
PLOGE << action << " failed, code=" << err;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// VMInit callback
|
||||
void JNICALL OnVMInit(jvmtiEnv* jvmti, JNIEnv* env, jthread thread) {
|
||||
PLOGI << "JVMTI VMInit triggered";
|
||||
|
||||
std::string runtime_dir = std::filesystem::current_path().string();
|
||||
|
||||
if (runtime_dir.empty()) {
|
||||
PLOGE << "runtime_dir is empty";
|
||||
return;
|
||||
}
|
||||
|
||||
PLOGI << "Runtime directory: " << runtime_dir;
|
||||
|
||||
// Inject JuiceAgent-API
|
||||
// Save bytes to file
|
||||
std::string hash =
|
||||
JuiceAgent::Resource::juiceagent_api_bytes_sha256;
|
||||
|
||||
std::string file_name =
|
||||
hash.substr(0, 8) + "_" +
|
||||
JuiceAgent::Resource::juiceagent_api_bytes_name;
|
||||
|
||||
std::string juiceagent_api_path =
|
||||
JuiceAgent::Utils::File::write_to_tempfile(
|
||||
JuiceAgent::Resource::juiceagent_api_bytes,
|
||||
JuiceAgent::Resource::juiceagent_api_bytes_len,
|
||||
file_name
|
||||
);
|
||||
|
||||
PLOGD << "Saved JuiceAgent-API to: " << juiceagent_api_path;
|
||||
PLOGD << "JuiceAgent-API size: " << JuiceAgent::Resource::juiceagent_api_bytes_len;
|
||||
PLOGD << "JuiceAgent-API SHA256: " << JuiceAgent::Resource::juiceagent_api_bytes_sha256;
|
||||
|
||||
// Inject JuiceAgent jar into system class loader
|
||||
jint status = jvmti->AddToSystemClassLoaderSearch(juiceagent_api_path.c_str());
|
||||
if (status != JNI_OK) {
|
||||
PLOGE << "AddToSystemClassLoaderSearch failed: " << status;
|
||||
return;
|
||||
}
|
||||
|
||||
PLOGI << "Jar injected successfully: " << juiceagent_api_path;
|
||||
|
||||
JuiceAgent::Agent& agent = JuiceAgent::Agent::instance();
|
||||
agent.init(g_vm, env, g_jvmti, runtime_dir);
|
||||
|
||||
PLOGI << "JuiceAgent initialized successfully";
|
||||
}
|
||||
|
||||
// Agent entry
|
||||
JNIEXPORT jint JNICALL
|
||||
Agent_OnLoad(JavaVM* vm, char* options, void* reserved) {
|
||||
InitLogger();
|
||||
|
||||
PLOGI << "Agent_OnLoad";
|
||||
|
||||
g_vm = vm;
|
||||
|
||||
// Get JVMTI first
|
||||
jint result = vm->GetEnv(reinterpret_cast<void**>(&g_jvmti), JVMTI_VERSION_1_2);
|
||||
if (result != JNI_OK || g_jvmti == nullptr) {
|
||||
PLOGE << "Failed to get JVMTI, code=" << result;
|
||||
return JNI_ERR;
|
||||
}
|
||||
|
||||
// Optional capabilities
|
||||
jvmtiCapabilities caps{};
|
||||
CheckJvmti(g_jvmti->AddCapabilities(&caps), "AddCapabilities");
|
||||
|
||||
// Register callbacks
|
||||
jvmtiEventCallbacks callbacks{};
|
||||
callbacks.VMInit = &OnVMInit;
|
||||
|
||||
if (!CheckJvmti(
|
||||
g_jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks)),
|
||||
"SetEventCallbacks")) {
|
||||
return JNI_ERR;
|
||||
}
|
||||
|
||||
if (!CheckJvmti(
|
||||
g_jvmti->SetEventNotificationMode(
|
||||
JVMTI_ENABLE,
|
||||
JVMTI_EVENT_VM_INIT,
|
||||
nullptr),
|
||||
"Enable VM_INIT")) {
|
||||
return JNI_ERR;
|
||||
}
|
||||
|
||||
PLOGI << "Agent loaded, waiting for VMInit...";
|
||||
return JNI_OK;
|
||||
}
|
||||
|
||||
// Optional unload
|
||||
JNIEXPORT void JNICALL
|
||||
Agent_OnUnload(JavaVM* vm) {
|
||||
PLOGI << "Agent_OnUnload";
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
#include <jni_impl.hpp>
|
||||
#include <libagent.hpp>
|
||||
#include <JuiceAgent.hpp>
|
||||
|
||||
// This function is called by Java to initialize the agent.
|
||||
JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceagent_api_JuiceAgent_init
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
#include <jvmti.h>
|
||||
#include <event/eventbus.hpp>
|
||||
#include <JuiceAgent/Config.hpp>
|
||||
#include <modules/ModuleManager.hpp>
|
||||
|
||||
namespace JuiceAgent {
|
||||
class Agent {
|
||||
@@ -15,8 +14,7 @@ namespace JuiceAgent {
|
||||
JNIEnv* env;
|
||||
EventBus eventbus;
|
||||
JuiceAgent::Config::Config config;
|
||||
JuiceAgent::Core::Modules::ModuleManager module_manager;
|
||||
|
||||
|
||||
private:
|
||||
Agent() = default;
|
||||
~Agent() = default;
|
||||
@@ -1,10 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "event/event_type.hpp"
|
||||
|
||||
namespace JuiceAgent::Bytecodes {
|
||||
void register_bytecodes();
|
||||
|
||||
void capture_bytecodes(const EventClassFileLoadHook& e);
|
||||
void patch_bytecodes(const EventClassFileLoadHook& e);
|
||||
}
|
||||
@@ -3,7 +3,7 @@
|
||||
#include <jni_impl.hpp>
|
||||
|
||||
// ClassFileLoadHook
|
||||
struct EventClassFileLoadHook {
|
||||
struct EventClassFileLoadHook {
|
||||
jvmtiEnv* jvmti_env;
|
||||
JNIEnv* jni_env;
|
||||
jclass class_being_redefined;
|
||||
|
||||
@@ -14,9 +14,17 @@ class EventBus {
|
||||
public:
|
||||
using Token = std::uint64_t;
|
||||
|
||||
// === Readonly ===
|
||||
template<typename Event>
|
||||
using Listener = std::function<void(const Event&)>;
|
||||
|
||||
// === Mutable ===
|
||||
template<typename Event>
|
||||
using MutableListener = std::function<void(Event&)>;
|
||||
|
||||
// =========================
|
||||
// Subscribe (Readonly)
|
||||
// =========================
|
||||
template<typename Event>
|
||||
Token subscribe(Listener<Event> listener, int priority = 0) {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
@@ -31,40 +39,58 @@ public:
|
||||
std::move(listener)
|
||||
});
|
||||
|
||||
std::sort(vec.begin(), vec.end(),
|
||||
[](const auto& a, const auto& b) {
|
||||
return a.priority > b.priority;
|
||||
});
|
||||
|
||||
sort(vec);
|
||||
return token;
|
||||
}
|
||||
|
||||
// =========================
|
||||
// Subscribe (Mutable)
|
||||
// =========================
|
||||
template<typename Event>
|
||||
Token subscribe_mutable(MutableListener<Event> listener, int priority = 0) {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
||||
auto& vec = getMutableListeners<Event>();
|
||||
|
||||
Token token = nextToken_++;
|
||||
|
||||
vec.push_back({
|
||||
token,
|
||||
priority,
|
||||
std::move(listener)
|
||||
});
|
||||
|
||||
sort(vec);
|
||||
return token;
|
||||
}
|
||||
|
||||
// =========================
|
||||
// Unsubscribe
|
||||
// =========================
|
||||
template<typename Event>
|
||||
bool unsubscribe(Token token) {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
||||
auto type = std::type_index(typeid(Event));
|
||||
auto it = listeners_.find(type);
|
||||
if (it == listeners_.end()) {
|
||||
return false;
|
||||
bool removed = false;
|
||||
|
||||
// readonly
|
||||
if (auto it = listeners_.find(std::type_index(typeid(Event))); it != listeners_.end()) {
|
||||
auto& vec = *std::static_pointer_cast<std::vector<Node<Event>>>(it->second);
|
||||
removed |= erase(vec, token);
|
||||
}
|
||||
|
||||
auto& vec =
|
||||
*std::static_pointer_cast<std::vector<Node<Event>>>(it->second);
|
||||
// mutable
|
||||
if (auto it = mutable_listeners_.find(std::type_index(typeid(Event))); it != mutable_listeners_.end()) {
|
||||
auto& vec = *std::static_pointer_cast<std::vector<MutableNode<Event>>>(it->second);
|
||||
removed |= erase(vec, token);
|
||||
}
|
||||
|
||||
auto oldSize = vec.size();
|
||||
|
||||
vec.erase(
|
||||
std::remove_if(vec.begin(), vec.end(),
|
||||
[token](const Node<Event>& node) {
|
||||
return node.token == token;
|
||||
}),
|
||||
vec.end()
|
||||
);
|
||||
|
||||
return vec.size() != oldSize;
|
||||
return removed;
|
||||
}
|
||||
|
||||
// =========================
|
||||
// Post (Readonly)
|
||||
// =========================
|
||||
template<typename Event>
|
||||
void post(const Event& event) {
|
||||
std::vector<Node<Event>> copy;
|
||||
@@ -72,15 +98,10 @@ public:
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
||||
auto type = std::type_index(typeid(Event));
|
||||
auto it = listeners_.find(type);
|
||||
auto it = listeners_.find(std::type_index(typeid(Event)));
|
||||
if (it == listeners_.end()) return;
|
||||
|
||||
if (it == listeners_.end()) {
|
||||
return;
|
||||
}
|
||||
|
||||
copy =
|
||||
*std::static_pointer_cast<std::vector<Node<Event>>>(it->second);
|
||||
copy = *std::static_pointer_cast<std::vector<Node<Event>>>(it->second);
|
||||
}
|
||||
|
||||
for (auto& node : copy) {
|
||||
@@ -88,15 +109,41 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
// =========================
|
||||
// Post (Mutable)
|
||||
// =========================
|
||||
template<typename Event>
|
||||
void post_mutable(Event& event) {
|
||||
std::vector<MutableNode<Event>> copy;
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
||||
auto it = mutable_listeners_.find(std::type_index(typeid(Event)));
|
||||
if (it == mutable_listeners_.end()) return;
|
||||
|
||||
copy = *std::static_pointer_cast<std::vector<MutableNode<Event>>>(it->second);
|
||||
}
|
||||
|
||||
for (auto& node : copy) {
|
||||
node.callback(event);
|
||||
}
|
||||
}
|
||||
|
||||
// =========================
|
||||
// Clear
|
||||
// =========================
|
||||
template<typename Event>
|
||||
void clear() {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
listeners_.erase(std::type_index(typeid(Event)));
|
||||
mutable_listeners_.erase(std::type_index(typeid(Event)));
|
||||
}
|
||||
|
||||
void clearAll() {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
listeners_.clear();
|
||||
mutable_listeners_.clear();
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -107,22 +154,65 @@ private:
|
||||
Listener<Event> callback;
|
||||
};
|
||||
|
||||
template<typename Event>
|
||||
struct MutableNode {
|
||||
Token token;
|
||||
int priority;
|
||||
MutableListener<Event> callback;
|
||||
};
|
||||
|
||||
std::mutex mutex_;
|
||||
std::atomic<Token> nextToken_ {1};
|
||||
std::atomic<Token> nextToken_{1};
|
||||
|
||||
std::unordered_map<std::type_index, std::shared_ptr<void>> listeners_;
|
||||
std::unordered_map<std::type_index, std::shared_ptr<void>> mutable_listeners_;
|
||||
|
||||
// === helpers ===
|
||||
|
||||
template<typename T>
|
||||
static void sort(std::vector<T>& vec) {
|
||||
std::sort(vec.begin(), vec.end(),
|
||||
[](const auto& a, const auto& b) {
|
||||
return a.priority > b.priority;
|
||||
});
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static bool erase(std::vector<T>& vec, Token token) {
|
||||
auto oldSize = vec.size();
|
||||
|
||||
vec.erase(
|
||||
std::remove_if(vec.begin(), vec.end(),
|
||||
[token](const T& node) {
|
||||
return node.token == token;
|
||||
}),
|
||||
vec.end()
|
||||
);
|
||||
|
||||
return vec.size() != oldSize;
|
||||
}
|
||||
|
||||
template<typename Event>
|
||||
std::vector<Node<Event>>& getListeners() {
|
||||
auto type = std::type_index(typeid(Event));
|
||||
auto it = listeners_.find(type);
|
||||
return getContainer<Event, Node<Event>>(listeners_);
|
||||
}
|
||||
|
||||
if (it == listeners_.end()) {
|
||||
auto ptr = std::make_shared<std::vector<Node<Event>>>();
|
||||
listeners_[type] = ptr;
|
||||
template<typename Event>
|
||||
std::vector<MutableNode<Event>>& getMutableListeners() {
|
||||
return getContainer<Event, MutableNode<Event>>(mutable_listeners_);
|
||||
}
|
||||
|
||||
template<typename Event, typename NodeType>
|
||||
std::vector<NodeType>& getContainer(std::unordered_map<std::type_index, std::shared_ptr<void>>& map) {
|
||||
auto type = std::type_index(typeid(Event));
|
||||
auto it = map.find(type);
|
||||
|
||||
if (it == map.end()) {
|
||||
auto ptr = std::make_shared<std::vector<NodeType>>();
|
||||
map[type] = ptr;
|
||||
return *ptr;
|
||||
}
|
||||
|
||||
return *std::static_pointer_cast<std::vector<Node<Event>>>(it->second);
|
||||
return *std::static_pointer_cast<std::vector<NodeType>>(it->second);
|
||||
}
|
||||
};
|
||||
@@ -36,6 +36,3 @@ extern std::mutex classDataMutex;
|
||||
|
||||
extern std::unordered_map<std::string, std::vector<unsigned char>> pendingRetransform;
|
||||
extern std::mutex pendingRetransformMutex;
|
||||
|
||||
// Remapper WhiteList
|
||||
extern std::unordered_set<std::string> remapperWhiteList;
|
||||
@@ -3,7 +3,7 @@
|
||||
#include <jni.h>
|
||||
#include <jvmti.h>
|
||||
#include <JuiceAgent/Logger.hpp>
|
||||
#include <libagent.hpp>
|
||||
#include <JuiceAgent.hpp>
|
||||
#include <utils.hpp>
|
||||
#include <global.hpp>
|
||||
#include <algorithm>
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
|
||||
namespace JuiceAgent::Core::Modules {
|
||||
|
||||
class IModule {
|
||||
public:
|
||||
virtual ~IModule() = default;
|
||||
|
||||
virtual std::string name() const = 0;
|
||||
|
||||
virtual bool init() = 0;
|
||||
virtual bool start() = 0;
|
||||
virtual void stop() = 0;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <modules/IModule.hpp>
|
||||
|
||||
namespace JuiceAgent::Core::Modules {
|
||||
|
||||
class ModuleManager {
|
||||
public:
|
||||
ModuleManager() = default;
|
||||
~ModuleManager() = default;
|
||||
|
||||
void load_all();
|
||||
|
||||
bool init_all();
|
||||
bool start_all();
|
||||
void stop_all();
|
||||
|
||||
IModule* find_module(const std::string& name);
|
||||
|
||||
private:
|
||||
std::vector<std::unique_ptr<IModule>> _modules;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <modules/IModule.hpp>
|
||||
|
||||
namespace JuiceAgent::Core::Modules {
|
||||
|
||||
class ModuleRegistry {
|
||||
public:
|
||||
using Factory = std::function<std::unique_ptr<IModule>()>;
|
||||
|
||||
struct Entry {
|
||||
std::string name;
|
||||
Factory factory;
|
||||
};
|
||||
|
||||
static ModuleRegistry& instance() {
|
||||
static ModuleRegistry inst;
|
||||
return inst;
|
||||
}
|
||||
|
||||
bool add(const std::string& name, Factory factory) {
|
||||
for (const auto& item : _entries) {
|
||||
if (item.name == name) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
_entries.push_back({name, std::move(factory)});
|
||||
return true;
|
||||
}
|
||||
|
||||
const std::vector<Entry>& entries() const {
|
||||
return _entries;
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<Entry> _entries;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#define JUICEAGENT_REGISTER_MODULE(TYPE, NAME) \
|
||||
namespace { \
|
||||
const bool registered_##NAME = \
|
||||
JuiceAgent::Core::Modules::ModuleRegistry::instance().add( \
|
||||
#NAME, \
|
||||
[]() -> std::unique_ptr<JuiceAgent::Core::Modules::IModule> { \
|
||||
return std::make_unique<TYPE>(); \
|
||||
} \
|
||||
); \
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <JuiceAgent.hpp>
|
||||
#include <event/event_type.hpp>
|
||||
|
||||
namespace JuiceAgent::services
|
||||
{
|
||||
static JuiceAgent::Agent& agent = JuiceAgent::Agent::instance();
|
||||
|
||||
namespace Manager {
|
||||
void register_events();
|
||||
void init(const EventPreLoad&);
|
||||
void start(const EventLoaded&);
|
||||
void stop();
|
||||
}
|
||||
|
||||
namespace JarLoader
|
||||
{
|
||||
struct JarLoaderConfig {
|
||||
bool enabled = false;
|
||||
std::string injection_dir;
|
||||
std::string jar_path;
|
||||
std::string entry_class;
|
||||
std::string entry_method;
|
||||
};
|
||||
|
||||
void init();
|
||||
void start();
|
||||
} // namespace JarLoader
|
||||
|
||||
namespace Bytecode
|
||||
{
|
||||
void init();
|
||||
void start();
|
||||
}
|
||||
} // namespace JuiceAgent::services
|
||||
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <libagent.hpp>
|
||||
#include <JuiceAgent.hpp>
|
||||
|
||||
inline bool check_env(JuiceAgent::Agent& agent) {
|
||||
if (agent.get_jvm() == nullptr) {
|
||||
|
||||
@@ -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";
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
#include <modules/ModuleManager.hpp>
|
||||
#include <modules/ModuleRegistry.hpp>
|
||||
#include <JuiceAgent/Logger.hpp>
|
||||
|
||||
namespace JuiceAgent::Core::Modules {
|
||||
|
||||
void ModuleManager::load_all() {
|
||||
for (const auto& entry : ModuleRegistry::instance().entries()) {
|
||||
auto module = entry.factory();
|
||||
|
||||
if (!module) {
|
||||
PLOGE << "Failed to create module: " << entry.name;
|
||||
continue;
|
||||
}
|
||||
|
||||
PLOGI << "Register module: " << module->name();
|
||||
_modules.push_back(std::move(module));
|
||||
}
|
||||
}
|
||||
|
||||
bool ModuleManager::init_all() {
|
||||
for (auto& module : _modules) {
|
||||
PLOGI << "Init module: " << module->name();
|
||||
|
||||
if (!module->init()) {
|
||||
PLOGE << "Failed to init module: " << module->name();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ModuleManager::start_all() {
|
||||
for (auto& module : _modules) {
|
||||
PLOGI << "Start module: " << module->name();
|
||||
|
||||
if (!module->start()) {
|
||||
PLOGE << "Failed to start module: " << module->name();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ModuleManager::stop_all() {
|
||||
for (auto it = _modules.rbegin(); it != _modules.rend(); ++it) {
|
||||
PLOGI << "Stop module: " << (*it)->name();
|
||||
(*it)->stop();
|
||||
}
|
||||
}
|
||||
|
||||
IModule* ModuleManager::find_module(const std::string& name) {
|
||||
for (auto& module : _modules) {
|
||||
if (module->name() == name) {
|
||||
return module.get();
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,281 +0,0 @@
|
||||
#include <modules/ModuleRegistry.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 IModule {
|
||||
struct _Remapper {
|
||||
jclass handler_class = nullptr;
|
||||
jmethodID onClassFileLoadMethod = nullptr;
|
||||
jmethodID onMethodEntryMethod = nullptr;
|
||||
jmethodID onMethodExitMethod = nullptr;
|
||||
} Remapper;
|
||||
|
||||
public:
|
||||
std::string name() const override {
|
||||
return "Remapper";
|
||||
}
|
||||
|
||||
bool init() override {
|
||||
if (_initialized) {
|
||||
return true;
|
||||
}
|
||||
|
||||
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) {
|
||||
_initialized = true;
|
||||
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;
|
||||
}
|
||||
|
||||
_initialized = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool start() override {
|
||||
if (!_initialized) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_running) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!config.enabled) {
|
||||
_running = true;
|
||||
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);
|
||||
}
|
||||
);
|
||||
|
||||
_running = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void stop() override {
|
||||
if (!_running) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto& bus = agent.get_eventbus();
|
||||
|
||||
bus.unsubscribe<EventClassFileLoadHook>(_classFileToken);
|
||||
bus.unsubscribe<EventMethodEntry>(_methodEntryToken);
|
||||
bus.unsubscribe<EventMethodExit>(_methodExitToken);
|
||||
|
||||
_classFileToken = 0;
|
||||
_methodEntryToken = 0;
|
||||
_methodExitToken = 0;
|
||||
|
||||
_running = false;
|
||||
}
|
||||
|
||||
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 className(name);
|
||||
jstring jName = env->NewStringUTF(className.c_str());
|
||||
if (jName == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
jbyteArray input = env->NewByteArray(event.class_data_len);
|
||||
if (input == nullptr) {
|
||||
env->DeleteLocalRef(jName);
|
||||
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,
|
||||
jName,
|
||||
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(jName);
|
||||
}
|
||||
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:
|
||||
bool _initialized = false;
|
||||
bool _running = false;
|
||||
|
||||
EventBus::Token _classFileToken = 0;
|
||||
EventBus::Token _methodEntryToken = 0;
|
||||
EventBus::Token _methodExitToken = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
JUICEAGENT_REGISTER_MODULE(
|
||||
JuiceAgent::Core::Modules::RemapperModule,
|
||||
Remapper
|
||||
)
|
||||
@@ -1,58 +0,0 @@
|
||||
#include <modules/ModuleRegistry.hpp>
|
||||
#include <JuiceAgent/Logger.hpp>
|
||||
|
||||
namespace JuiceAgent::Core::Modules {
|
||||
|
||||
class TemplateModule : public IModule {
|
||||
public:
|
||||
std::string name() const override {
|
||||
return "Template";
|
||||
}
|
||||
|
||||
bool init() override {
|
||||
if (_initialized) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// load config / prepare resource
|
||||
|
||||
_initialized = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool start() override {
|
||||
if (!_initialized) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_running) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// start logic
|
||||
|
||||
_running = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void stop() override {
|
||||
if (!_running) {
|
||||
return;
|
||||
}
|
||||
|
||||
// cleanup logic
|
||||
|
||||
_running = false;
|
||||
}
|
||||
|
||||
private:
|
||||
bool _initialized = false;
|
||||
bool _running = false;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
JUICEAGENT_REGISTER_MODULE(
|
||||
JuiceAgent::Core::Modules::TemplateModule,
|
||||
Template
|
||||
)
|
||||
@@ -1,16 +1,8 @@
|
||||
#include <bytecodes_opt.hpp>
|
||||
#include <services.hpp>
|
||||
#include <jni_impl.hpp>
|
||||
#include <event/event_type.hpp>
|
||||
|
||||
namespace JuiceAgent::Bytecodes {
|
||||
void register_bytecodes() {
|
||||
auto& agent = JuiceAgent::Agent::instance();
|
||||
|
||||
agent.get_eventbus().subscribe<EventClassFileLoadHook>(capture_bytecodes);
|
||||
agent.get_eventbus().subscribe<EventClassFileLoadHook>(patch_bytecodes);
|
||||
}
|
||||
|
||||
void capture_bytecodes(const EventClassFileLoadHook& e) {
|
||||
namespace JuiceAgent::services::Bytecode {
|
||||
void capture_bytecodes(const EventClassFileLoadHook& e) {
|
||||
/* =========================
|
||||
* 1. Capture original bytes
|
||||
* ========================= */
|
||||
@@ -56,4 +48,14 @@ namespace JuiceAgent::Bytecodes {
|
||||
PLOGI << "Retransformed: " << e.name << " (new length: " << bytes.size() << ")";
|
||||
}
|
||||
}
|
||||
|
||||
void init() {
|
||||
agent.get_eventbus().subscribe<EventClassFileLoadHook>(capture_bytecodes);
|
||||
agent.get_eventbus().subscribe<EventClassFileLoadHook>(patch_bytecodes);
|
||||
}
|
||||
|
||||
void start() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,35 +1,12 @@
|
||||
#include <modules/ModuleRegistry.hpp>
|
||||
#include <JuiceAgent/Logger.hpp>
|
||||
#include <libagent.hpp>
|
||||
#include <JuiceAgent/Utils.hpp>
|
||||
#include <services.hpp>
|
||||
|
||||
namespace JuiceAgent::Core::Modules {
|
||||
namespace JuiceAgent::services::JarLoader {
|
||||
static JarLoaderConfig config;
|
||||
|
||||
struct JarLoaderConfig {
|
||||
bool enabled = true;
|
||||
std::string injection_dir;
|
||||
std::string jar_path;
|
||||
std::string entry_class;
|
||||
std::string entry_method;
|
||||
};
|
||||
|
||||
static JarLoaderConfig config;
|
||||
static JuiceAgent::Agent& agent = JuiceAgent::Agent::instance();
|
||||
|
||||
static const char* MODULE_CLASS = "cn/xiaozhou233/juiceagent/api/modules/JarLoader";
|
||||
static const char* MODULE_METHOD = "loadJar";
|
||||
|
||||
class JarLoaderModule : public IModule {
|
||||
public:
|
||||
std::string name() const override {
|
||||
return "JarLoader";
|
||||
}
|
||||
|
||||
bool init() override {
|
||||
if (_initialized) {
|
||||
return true;
|
||||
}
|
||||
static const char* MODULE_CLASS = "cn/xiaozhou233/juiceagent/api/modules/JarLoader";
|
||||
static const char* MODULE_METHOD = "loadJar";
|
||||
|
||||
void init() {
|
||||
auto& cfg = agent.get_config();
|
||||
|
||||
config.enabled = cfg.get<bool>(
|
||||
@@ -61,21 +38,16 @@ public:
|
||||
false
|
||||
);
|
||||
|
||||
PLOGI << "JarLoader init";
|
||||
_initialized = true;
|
||||
return true;
|
||||
PLOGI << "JarLoaderService initialized";
|
||||
}
|
||||
|
||||
bool start() override {
|
||||
if (!_initialized) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_running || !config.enabled) {
|
||||
return true;
|
||||
void start() {
|
||||
if (!config.enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
JuiceAgent::Utils::Serializer ser;
|
||||
|
||||
ser.add_kv("InjectionDir", config.injection_dir);
|
||||
ser.add_kv("JarPath", config.jar_path);
|
||||
ser.add_kv("EntryClass", config.entry_class);
|
||||
@@ -91,27 +63,6 @@ public:
|
||||
);
|
||||
|
||||
PLOGI << "JarLoader start";
|
||||
_running = true;
|
||||
return true;
|
||||
return;
|
||||
}
|
||||
|
||||
void stop() override {
|
||||
if (!_running) {
|
||||
return;
|
||||
}
|
||||
|
||||
PLOGI << "JarLoader stop";
|
||||
_running = false;
|
||||
}
|
||||
|
||||
private:
|
||||
bool _initialized = false;
|
||||
bool _running = false;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
JUICEAGENT_REGISTER_MODULE(
|
||||
JuiceAgent::Core::Modules::JarLoaderModule,
|
||||
JarLoader
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
#include <services.hpp>
|
||||
#include <event/event_type.hpp>
|
||||
|
||||
namespace JuiceAgent::services::Manager {
|
||||
void register_events() {
|
||||
// PreLoad
|
||||
agent.get_eventbus().subscribe<EventPreLoad>(init);
|
||||
|
||||
// Loaded
|
||||
agent.get_eventbus().subscribe<EventLoaded>(start);
|
||||
|
||||
// TODO: Stopped
|
||||
|
||||
PLOGI << "Services Manager registered events";
|
||||
}
|
||||
|
||||
void init(const EventPreLoad& event) {
|
||||
PLOGI << "Initializing services";
|
||||
// JarLoader
|
||||
JuiceAgent::services::JarLoader::init();
|
||||
|
||||
// Bytecode
|
||||
JuiceAgent::services::Bytecode::init();
|
||||
}
|
||||
|
||||
void start(const EventLoaded& event) {
|
||||
PLOGI << "Starting services";
|
||||
// JarLoader
|
||||
JuiceAgent::services::JarLoader::start();
|
||||
|
||||
// Bytecode
|
||||
JuiceAgent::services::Bytecode::start();
|
||||
}
|
||||
|
||||
void stop() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -50,9 +50,8 @@ bool invoke_juiceagent_init(JNIEnv* env, const LoaderConfig& config) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Entrypoint function for loader
|
||||
void entrypoint(const char* runtime_dir) {
|
||||
if (!runtime_dir) {
|
||||
static void init(const char* runtime_dir, JNIEnv * env, jvmtiEnv* jvmti) {
|
||||
if (!runtime_dir) {
|
||||
PLOGW << "runtime_dir is null, using empty path";
|
||||
}
|
||||
|
||||
@@ -89,21 +88,6 @@ void entrypoint(const char* runtime_dir) {
|
||||
PLOGD << "JuiceAgent-API size: " << JuiceAgent::Resource::juiceagent_api_bytes_len;
|
||||
PLOGD << "JuiceAgent-API SHA256: " << JuiceAgent::Resource::juiceagent_api_bytes_sha256;
|
||||
|
||||
// Attach to JVM
|
||||
JuiceAgent::Loader::JvmManager::Jvm jvm;
|
||||
if (!jvm.attach()) {
|
||||
PLOGE << "Attach to JVM failed";
|
||||
return;
|
||||
}
|
||||
|
||||
JNIEnv* env = jvm.get_env();
|
||||
auto* jvmti = jvm.get_jvmti();
|
||||
|
||||
if (!env || !jvmti) {
|
||||
PLOGE << "JNIEnv or JVMTI is null";
|
||||
return;
|
||||
}
|
||||
|
||||
// Inject JuiceAgent jar into system class loader
|
||||
jint status = jvmti->AddToSystemClassLoaderSearch(juiceagent_api_path.c_str());
|
||||
if (status != JNI_OK) {
|
||||
@@ -119,4 +103,50 @@ void entrypoint(const char* runtime_dir) {
|
||||
}
|
||||
}
|
||||
|
||||
// Entrypoint function for loader
|
||||
void entrypoint(const char* runtime_dir) {
|
||||
// Attach to JVM
|
||||
JuiceAgent::Loader::JvmManager::Jvm jvm;
|
||||
if (!jvm.attach()) {
|
||||
PLOGE << "Attach to JVM failed";
|
||||
return;
|
||||
}
|
||||
|
||||
JNIEnv* env = jvm.get_env();
|
||||
auto* jvmti = jvm.get_jvmti();
|
||||
|
||||
if (!env || !jvmti) {
|
||||
PLOGE << "JNIEnv or JVMTI is null";
|
||||
return;
|
||||
}
|
||||
|
||||
// Init JuiceAgent
|
||||
init(runtime_dir, env, jvmti);
|
||||
}
|
||||
|
||||
void entrypoint_with_env(const char* runtime_dir, JNIEnv* env) {
|
||||
JavaVM* jvm = nullptr;
|
||||
jvmtiEnv* jvmti = nullptr;
|
||||
|
||||
env->GetJavaVM(&jvm);
|
||||
|
||||
if (!jvm) {
|
||||
PLOGE << "Failed to get JavaVM";
|
||||
return;
|
||||
}
|
||||
|
||||
jint result = jvm->GetEnv(
|
||||
reinterpret_cast<void**>(&jvmti),
|
||||
JVMTI_VERSION_1_2
|
||||
);
|
||||
|
||||
if (result != JNI_OK || !jvmti) {
|
||||
PLOGE << "Failed to get JVMTI environment";
|
||||
return;
|
||||
}
|
||||
|
||||
PLOGI << "Successfully acquired JVMTI";
|
||||
|
||||
init(runtime_dir, env, jvmti);
|
||||
}
|
||||
} // namespace JuiceAgent::Loader
|
||||
@@ -9,6 +9,7 @@
|
||||
namespace JuiceAgent::Loader
|
||||
{
|
||||
void entrypoint(const char* runtime_dir);
|
||||
void entrypoint_with_env(const char* runtime_dir, JNIEnv* env);
|
||||
|
||||
bool invoke_juiceagent_init(JNIEnv* env, const LoaderConfig& info);
|
||||
} // namespace JuiceAgent::Loader
|
||||
|
||||
Reference in new issue
Block a user