mirror of
https://github.com/xiaozhou233/JuiceAgent.git
synced 2026-12-05 13:05:53 +08:00
Compare commits
11
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
382bfb7fed | ||
|
|
560215c0ae | ||
|
|
55303b7dc9 | ||
|
|
41049f1852 | ||
|
|
396ec1a96f | ||
|
|
10ec330480 | ||
|
|
0ddcfcd7c8 | ||
|
|
87a91376c5 | ||
|
|
dbae5e5258 | ||
|
|
ff216aabee | ||
|
|
18a923373b |
No files matched your search
@@ -1,3 +1,4 @@
|
|||||||
build/
|
build/
|
||||||
.cache/
|
.cache/
|
||||||
*.log
|
*.log
|
||||||
|
dist/
|
||||||
@@ -170,25 +170,7 @@ You can also pass arguments directly: `injector.exe <pid> <libloader.dll path> [
|
|||||||
|
|
||||||
#### Method B: Use JNI to call `inject`
|
#### Method B: Use JNI to call `inject`
|
||||||
|
|
||||||
Create `cn/xiaozhou233/juiceagent/injector/InjectorNative.java` in your project:
|
See [Inject](docs/Inject.md)
|
||||||
|
|
||||||
```java
|
|
||||||
package cn.xiaozhou233.juiceagent.injector;
|
|
||||||
|
|
||||||
public class InjectorNative {
|
|
||||||
public native boolean inject(int pid, String path);
|
|
||||||
public native boolean inject(int pid, String path, String configDir);
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Example:
|
|
||||||
|
|
||||||
```java
|
|
||||||
System.load("<path-to-libinject.dll>");
|
|
||||||
InjectorNative injectorNative = new InjectorNative();
|
|
||||||
|
|
||||||
injectorNative.inject(<pid>, "<path-to-libloader.dll>", "<path-to-config-directory>");
|
|
||||||
```
|
|
||||||
|
|
||||||
### 5. Done
|
### 5. Done
|
||||||
|
|
||||||
|
|||||||
+1
-19
@@ -171,25 +171,7 @@ Input PID:
|
|||||||
|
|
||||||
#### 方法 B:通过 JNI 调用 `inject`
|
#### 方法 B:通过 JNI 调用 `inject`
|
||||||
|
|
||||||
在你的项目中创建 `cn/xiaozhou233/juiceagent/injector/InjectorNative.java`:
|
看 [Inject](docs/Inject.md)
|
||||||
|
|
||||||
```java
|
|
||||||
package cn.xiaozhou233.juiceagent.injector;
|
|
||||||
|
|
||||||
public class InjectorNative {
|
|
||||||
public native boolean inject(int pid, String path);
|
|
||||||
public native boolean inject(int pid, String path, String configDir);
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
示例:
|
|
||||||
|
|
||||||
```java
|
|
||||||
System.load("<libinject.dll 的路径>");
|
|
||||||
InjectorNative injectorNative = new InjectorNative();
|
|
||||||
|
|
||||||
injectorNative.inject(<pid>, "<libloader.dll 的路径>", "<配置文件所在目录>");
|
|
||||||
```
|
|
||||||
|
|
||||||
### 5. 完成
|
### 5. 完成
|
||||||
|
|
||||||
|
|||||||
+3
-1
@@ -1 +1,3 @@
|
|||||||
Version 4.1.0
|
Major=4
|
||||||
|
Minor=1
|
||||||
|
Patch=2
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
# Inject Method
|
||||||
|
|
||||||
|
## Method A: Use `injector.exe`
|
||||||
|
Run `injector.exe` from `YourDir`.
|
||||||
|
|
||||||
|
```text
|
||||||
|
<jps output>
|
||||||
|
Input PID:
|
||||||
|
```
|
||||||
|
|
||||||
|
Enter the PID of the target JVM process and press Enter.
|
||||||
|
|
||||||
|
## Method B: Use JNI to call `inject`
|
||||||
|
Download the JuiceAgent-JavaInjector-x.x-xxxx.jar from [here](https://github.com/xiaozhou233/JuiceAgent-JavaInjector/releases).
|
||||||
|
|
||||||
|
Add JuiceAgent-JavaInjector-x.x-xxxx.jar to your classpath.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
```java
|
||||||
|
import cn.xiaozhou233.juiceagent.injector.Injector;
|
||||||
|
|
||||||
|
System.load("<path-to-libinject>");
|
||||||
|
|
||||||
|
Injector.inject(<pid>, "<path-to-libloader>", "<path-to-config-directory>");
|
||||||
|
```
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
[JuiceAgent]
|
|
||||||
Version = 1
|
|
||||||
|
|
||||||
[JuiceAgent.Loader]
|
|
||||||
# Path to JuiceAgent-API jar, leave empty to use the built-in API
|
|
||||||
JuiceAgentAPIJarPath = ""
|
|
||||||
# Path to libagent.dll, default is "./libagent.dll"
|
|
||||||
JuiceAgentNativeLibraryPath = ""
|
|
||||||
|
|
||||||
[JuiceAgent.Modules]
|
|
||||||
|
|
||||||
[JuiceAgent.Modules.JuiceRemapper]
|
|
||||||
Enabled = true
|
|
||||||
@@ -15,9 +15,13 @@ class Config {
|
|||||||
public:
|
public:
|
||||||
explicit Config(const std::filesystem::path& runtime_dir = {})
|
explicit Config(const std::filesystem::path& runtime_dir = {})
|
||||||
{
|
{
|
||||||
_runtime_dir = runtime_dir.empty()
|
_runtime_dir = runtime_dir;
|
||||||
? std::filesystem::current_path()
|
|
||||||
: runtime_dir;
|
// No runtime directory specified: do not auto-read a config file
|
||||||
|
// from the current working directory.
|
||||||
|
if (runtime_dir.empty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const auto result = toml::try_parse(_runtime_dir / "config.toml");
|
const auto result = toml::try_parse(_runtime_dir / "config.toml");
|
||||||
|
|
||||||
|
|||||||
@@ -111,30 +111,6 @@ JNIEXPORT jbyteArray JNICALL Java_cn_xiaozhou233_juiceagent_api_JuiceAgent_getCl
|
|||||||
JNIEXPORT jbyteArray JNICALL Java_cn_xiaozhou233_juiceagent_api_JuiceAgent_getClassBytesByName
|
JNIEXPORT jbyteArray JNICALL Java_cn_xiaozhou233_juiceagent_api_JuiceAgent_getClassBytesByName
|
||||||
(JNIEnv *, jclass, jstring);
|
(JNIEnv *, jclass, jstring);
|
||||||
|
|
||||||
/*
|
|
||||||
* Class: cn_xiaozhou233_juiceagent_api_JuiceAgent
|
|
||||||
* Method: addRemapperWhiteList
|
|
||||||
* Signature: (Ljava/lang/String;)V
|
|
||||||
*/
|
|
||||||
JNIEXPORT void JNICALL Java_cn_xiaozhou233_juiceagent_api_JuiceAgent_addRemapperWhiteList
|
|
||||||
(JNIEnv *, jclass, jstring);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Class: cn_xiaozhou233_juiceagent_api_JuiceAgent
|
|
||||||
* Method: removeRemapperWhiteList
|
|
||||||
* Signature: (Ljava/lang/String;)V
|
|
||||||
*/
|
|
||||||
JNIEXPORT void JNICALL Java_cn_xiaozhou233_juiceagent_api_JuiceAgent_removeRemapperWhiteList
|
|
||||||
(JNIEnv *, jclass, jstring);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Class: cn_xiaozhou233_juiceagent_api_JuiceAgent
|
|
||||||
* Method: clearRemapperWhiteList
|
|
||||||
* Signature: ()V
|
|
||||||
*/
|
|
||||||
JNIEXPORT void JNICALL Java_cn_xiaozhou233_juiceagent_api_JuiceAgent_clearRemapperWhiteList
|
|
||||||
(JNIEnv *, jclass);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,69 +0,0 @@
|
|||||||
/* DO NOT EDIT THIS FILE - it is machine generated */
|
|
||||||
#include <jni.h>
|
|
||||||
/* Header for class cn_xiaozhou233_juiceremapper_JuiceRemapper */
|
|
||||||
|
|
||||||
#ifndef _Included_cn_xiaozhou233_juiceremapper_JuiceRemapper
|
|
||||||
#define _Included_cn_xiaozhou233_juiceremapper_JuiceRemapper
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
/*
|
|
||||||
* Class: cn_xiaozhou233_juiceremapper_JuiceRemapper
|
|
||||||
* Method: initNative
|
|
||||||
* Signature: ()Z
|
|
||||||
*/
|
|
||||||
JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceremapper_JuiceRemapper_initNative
|
|
||||||
(JNIEnv *, jclass);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Class: cn_xiaozhou233_juiceremapper_JuiceRemapper
|
|
||||||
* Method: addInclude
|
|
||||||
* Signature: (Ljava/lang/String;)Z
|
|
||||||
*/
|
|
||||||
JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceremapper_JuiceRemapper_addInclude
|
|
||||||
(JNIEnv *, jclass, jstring);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Class: cn_xiaozhou233_juiceremapper_JuiceRemapper
|
|
||||||
* Method: addExclude
|
|
||||||
* Signature: (Ljava/lang/String;)Z
|
|
||||||
*/
|
|
||||||
JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceremapper_JuiceRemapper_addExclude
|
|
||||||
(JNIEnv *, jclass, jstring);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Class: cn_xiaozhou233_juiceremapper_JuiceRemapper
|
|
||||||
* Method: removeInclude
|
|
||||||
* Signature: (Ljava/lang/String;)Z
|
|
||||||
*/
|
|
||||||
JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceremapper_JuiceRemapper_removeInclude
|
|
||||||
(JNIEnv *, jclass, jstring);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Class: cn_xiaozhou233_juiceremapper_JuiceRemapper
|
|
||||||
* Method: removeExclude
|
|
||||||
* Signature: (Ljava/lang/String;)Z
|
|
||||||
*/
|
|
||||||
JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceremapper_JuiceRemapper_removeExclude
|
|
||||||
(JNIEnv *, jclass, jstring);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Class: cn_xiaozhou233_juiceremapper_JuiceRemapper
|
|
||||||
* Method: clearIncludes
|
|
||||||
* Signature: ()V
|
|
||||||
*/
|
|
||||||
JNIEXPORT void JNICALL Java_cn_xiaozhou233_juiceremapper_JuiceRemapper_clearIncludes
|
|
||||||
(JNIEnv *, jclass);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Class: cn_xiaozhou233_juiceremapper_JuiceRemapper
|
|
||||||
* Method: clearExcludes
|
|
||||||
* Signature: ()V
|
|
||||||
*/
|
|
||||||
JNIEXPORT void JNICALL Java_cn_xiaozhou233_juiceremapper_JuiceRemapper_clearExcludes
|
|
||||||
(JNIEnv *, jclass);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
|
||||||
|
@echo off
|
||||||
|
setlocal
|
||||||
|
|
||||||
|
set "ROOT_DIR=%~dp0"
|
||||||
|
|
||||||
|
cmake -P "%ROOT_DIR%package.cmake"
|
||||||
|
|
||||||
|
if errorlevel 1 (
|
||||||
|
echo [ERROR] Packaging failed.
|
||||||
|
exit /b 1
|
||||||
|
)
|
||||||
|
|
||||||
|
echo [SUCCESS] Packaging completed.
|
||||||
|
|
||||||
|
pause
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
|
||||||
|
# Resolve project directory
|
||||||
|
get_filename_component(PROJECT_DIR "${CMAKE_CURRENT_LIST_DIR}/.." ABSOLUTE)
|
||||||
|
|
||||||
|
set(VERSION_FILE "${PROJECT_DIR}/Version.txt")
|
||||||
|
set(BIN_DIR "${PROJECT_DIR}/build/bin")
|
||||||
|
set(OUTPUT_DIR "${PROJECT_DIR}/dist")
|
||||||
|
file(MAKE_DIRECTORY "${OUTPUT_DIR}")
|
||||||
|
|
||||||
|
# Read version file
|
||||||
|
if(NOT EXISTS "${VERSION_FILE}")
|
||||||
|
message(FATAL_ERROR "Version.txt not found: ${VERSION_FILE}")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
file(STRINGS "${VERSION_FILE}" VERSION_LINES)
|
||||||
|
|
||||||
|
foreach(LINE IN LISTS VERSION_LINES)
|
||||||
|
if(LINE MATCHES "^[ \t]*Major[ \t]*=[ \t]*([0-9]+)[ \t]*$")
|
||||||
|
set(VERSION_MAJOR "${CMAKE_MATCH_1}")
|
||||||
|
elseif(LINE MATCHES "^[ \t]*Minor[ \t]*=[ \t]*([0-9]+)[ \t]*$")
|
||||||
|
set(VERSION_MINOR "${CMAKE_MATCH_1}")
|
||||||
|
elseif(LINE MATCHES "^[ \t]*Patch[ \t]*=[ \t]*([0-9]+)[ \t]*$")
|
||||||
|
set(VERSION_PATCH "${CMAKE_MATCH_1}")
|
||||||
|
endif()
|
||||||
|
endforeach()
|
||||||
|
|
||||||
|
if(NOT DEFINED VERSION_MAJOR OR
|
||||||
|
NOT DEFINED VERSION_MINOR OR
|
||||||
|
NOT DEFINED VERSION_PATCH)
|
||||||
|
message(FATAL_ERROR "Invalid Version.txt: Major, Minor and Patch are required")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
|
||||||
|
# Define package files
|
||||||
|
set(PACKAGE_FILES
|
||||||
|
injector.exe
|
||||||
|
libinject.dll
|
||||||
|
libloader.dll
|
||||||
|
libagent.dll
|
||||||
|
)
|
||||||
|
|
||||||
|
foreach(FILE_NAME IN LISTS PACKAGE_FILES)
|
||||||
|
if(NOT EXISTS "${BIN_DIR}/${FILE_NAME}")
|
||||||
|
message(FATAL_ERROR "Required file not found: ${BIN_DIR}/${FILE_NAME}")
|
||||||
|
endif()
|
||||||
|
endforeach()
|
||||||
|
|
||||||
|
# Create package
|
||||||
|
set(OUTPUT_FILE
|
||||||
|
"${OUTPUT_DIR}/JuiceAgent_v${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}_x64.zip"
|
||||||
|
)
|
||||||
|
|
||||||
|
file(REMOVE "${OUTPUT_FILE}")
|
||||||
|
|
||||||
|
execute_process(
|
||||||
|
COMMAND "${CMAKE_COMMAND}" -E tar
|
||||||
|
cf "${OUTPUT_FILE}"
|
||||||
|
--format=zip
|
||||||
|
${PACKAGE_FILES}
|
||||||
|
WORKING_DIRECTORY "${BIN_DIR}"
|
||||||
|
RESULT_VARIABLE TAR_RESULT
|
||||||
|
)
|
||||||
|
|
||||||
|
if(NOT TAR_RESULT STREQUAL "0")
|
||||||
|
message(FATAL_ERROR "Failed to create ZIP package: ${TAR_RESULT}")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
message(STATUS "Package created: ${OUTPUT_FILE}")
|
||||||
@@ -69,6 +69,11 @@ namespace JuiceAgent {
|
|||||||
spdlog::error("[JuiceAgent] Agent not loaded! Please call preload() first!");
|
spdlog::error("[JuiceAgent] Agent not loaded! Please call preload() first!");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if(initialized_) {
|
||||||
|
spdlog::warn("[JuiceAgent] Agent already initialized! Skipping...");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
initialized_ = true;
|
||||||
|
|
||||||
// Add Abilities
|
// Add Abilities
|
||||||
jvmtiCapabilities caps{};
|
jvmtiCapabilities caps{};
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ namespace JuiceAgent {
|
|||||||
JuiceAgent::Config::Config config;
|
JuiceAgent::Config::Config config;
|
||||||
|
|
||||||
bool loaded = false;
|
bool loaded = false;
|
||||||
|
bool initialized_ = false;
|
||||||
public:
|
public:
|
||||||
// Singleton
|
// Singleton
|
||||||
static Agent& getInstance();
|
static Agent& getInstance();
|
||||||
|
|||||||
@@ -131,99 +131,5 @@ private:
|
|||||||
std::unordered_map<std::string, PendingPatch> pendingPatches_;
|
std::unordered_map<std::string, PendingPatch> pendingPatches_;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Remapper {
|
|
||||||
public:
|
|
||||||
static Remapper& getInstance() {
|
|
||||||
static Remapper instance;
|
|
||||||
return instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
Remapper(const Remapper&) = delete;
|
|
||||||
Remapper& operator=(const Remapper&) = delete;
|
|
||||||
|
|
||||||
bool isInit() const { return initialized_; }
|
|
||||||
void setInit(bool init) { initialized_ = init; }
|
|
||||||
|
|
||||||
// Cached JNI static declarations for the Java remap class.
|
|
||||||
jclass getRemapClass() const { return remapClass_; }
|
|
||||||
jmethodID getRemapMethodId() const { return remapMethodId_; }
|
|
||||||
void setRemapClass(jclass clazz) { remapClass_ = clazz; }
|
|
||||||
void setRemapMethodId(jmethodID methodId) { remapMethodId_ = methodId; }
|
|
||||||
|
|
||||||
// ---- Include/Exclude filters ----
|
|
||||||
|
|
||||||
void addInclude(std::string name) {
|
|
||||||
std::lock_guard<std::mutex> lock(filterMutex_);
|
|
||||||
includes_.insert(std::move(name));
|
|
||||||
}
|
|
||||||
|
|
||||||
void removeInclude(const std::string& name) {
|
|
||||||
std::lock_guard<std::mutex> lock(filterMutex_);
|
|
||||||
includes_.erase(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
void clearIncludes() {
|
|
||||||
std::lock_guard<std::mutex> lock(filterMutex_);
|
|
||||||
includes_.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
void addExclude(std::string name) {
|
|
||||||
std::lock_guard<std::mutex> lock(filterMutex_);
|
|
||||||
excludes_.insert(std::move(name));
|
|
||||||
}
|
|
||||||
|
|
||||||
void removeExclude(const std::string& name) {
|
|
||||||
std::lock_guard<std::mutex> lock(filterMutex_);
|
|
||||||
excludes_.erase(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
void clearExcludes() {
|
|
||||||
std::lock_guard<std::mutex> 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<std::mutex> 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<std::string> includes_;
|
|
||||||
std::unordered_set<std::string> excludes_;
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -10,9 +10,6 @@
|
|||||||
// Generated by JuiceAgent-API
|
// Generated by JuiceAgent-API
|
||||||
#include <JuiceAgent/cn_xiaozhou233_juiceagent_api_JuiceAgent.h>
|
#include <JuiceAgent/cn_xiaozhou233_juiceagent_api_JuiceAgent.h>
|
||||||
|
|
||||||
// Generated by JuiceRemapper
|
|
||||||
#include <JuiceRemapper/cn_xiaozhou233_juiceremapper_JuiceRemapper.h>
|
|
||||||
|
|
||||||
// Event callbacks
|
// Event callbacks
|
||||||
// See https://docs.oracle.com/javase/8/docs/platform/jvmti/jvmti.html#ClassFileLoadHook
|
// See https://docs.oracle.com/javase/8/docs/platform/jvmti/jvmti.html#ClassFileLoadHook
|
||||||
void JNICALL ClassFileLoadHook(
|
void JNICALL ClassFileLoadHook(
|
||||||
|
|||||||
@@ -108,7 +108,7 @@ JNIEXPORT jbyteArray JNICALL Java_cn_xiaozhou233_juiceagent_api_JuiceAgent_getCl
|
|||||||
std::string internalName = dottedToInternal(utf);
|
std::string internalName = dottedToInternal(utf);
|
||||||
env->ReleaseStringUTFChars(name, utf);
|
env->ReleaseStringUTFChars(name, utf);
|
||||||
|
|
||||||
jclass clazz = env->FindClass(internalName.c_str());
|
jclass clazz = findLoadedClass(agent.getJVMTI(), env, internalName);
|
||||||
if (!clazz) {
|
if (!clazz) {
|
||||||
if (env->ExceptionCheck()) env->ExceptionClear();
|
if (env->ExceptionCheck()) env->ExceptionClear();
|
||||||
spdlog::error("[bytecode] Class not found: {}", internalName);
|
spdlog::error("[bytecode] Class not found: {}", internalName);
|
||||||
|
|||||||
@@ -1,140 +0,0 @@
|
|||||||
#include <jni_common.hpp>
|
|
||||||
#include <global.hpp>
|
|
||||||
|
|
||||||
static const char* remap_class = "cn/xiaozhou233/juiceremapper/remapper/Remap";
|
|
||||||
static const char* remap_method = "remap";
|
|
||||||
static const char* remap_sig = "(Ljava/lang/String;[B)[B";
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
|
|
||||||
// Resolve and cache the static class/method declarations.
|
|
||||||
// Returns true on success. On failure, caches are left null so the
|
|
||||||
// next call retries the lookup.
|
|
||||||
bool cacheRemapDeclarations(JNIEnv* env) {
|
|
||||||
auto& remapper = JuiceAgent::Services::Remapper::getInstance();
|
|
||||||
|
|
||||||
if (remapper.getRemapClass() && remapper.getRemapMethodId()) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
jclass localClass = env->FindClass(remap_class);
|
|
||||||
if (!localClass) {
|
|
||||||
if (env->ExceptionCheck()) env->ExceptionClear();
|
|
||||||
spdlog::error("[JuiceRemapper JNI] FindClass failed: {}", remap_class);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
jclass globalClass = static_cast<jclass>(env->NewGlobalRef(localClass));
|
|
||||||
env->DeleteLocalRef(localClass);
|
|
||||||
if (!globalClass) {
|
|
||||||
spdlog::error("[JuiceRemapper JNI] NewGlobalRef failed: {}", remap_class);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
jmethodID methodId = env->GetStaticMethodID(globalClass, remap_method, remap_sig);
|
|
||||||
if (!methodId) {
|
|
||||||
if (env->ExceptionCheck()) env->ExceptionClear();
|
|
||||||
env->DeleteGlobalRef(globalClass);
|
|
||||||
spdlog::error("[JuiceRemapper JNI] GetStaticMethodID failed: {}.{}",
|
|
||||||
remap_class, remap_method);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
remapper.setRemapClass(globalClass);
|
|
||||||
remapper.setRemapMethodId(methodId);
|
|
||||||
spdlog::debug("[JuiceRemapper JNI] Cached static declarations: {}.{}",
|
|
||||||
remap_class, remap_method);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace
|
|
||||||
|
|
||||||
JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceremapper_JuiceRemapper_initNative
|
|
||||||
(JNIEnv *env, jclass) {
|
|
||||||
// TODO: Init
|
|
||||||
|
|
||||||
auto& remapper = JuiceAgent::Services::Remapper::getInstance();
|
|
||||||
if (remapper.isInit()) {
|
|
||||||
spdlog::warn("JuiceRemapper::initNative: Remapper is already initialized");
|
|
||||||
return JNI_FALSE;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!cacheRemapDeclarations(env)) {
|
|
||||||
spdlog::error("JuiceRemapper::initNative: failed to cache static declarations");
|
|
||||||
return JNI_FALSE;
|
|
||||||
}
|
|
||||||
|
|
||||||
remapper.setInit(true);
|
|
||||||
|
|
||||||
spdlog::info("[JuiceRemapper JNI] JuiceRemapper is initialized.");
|
|
||||||
|
|
||||||
return JNI_TRUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
JNIEXPORT jboolean JNICALL Java_cn_xiaozhou233_juiceremapper_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 *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 *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 *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) {
|
|
||||||
JuiceAgent::Services::Remapper::getInstance().clearIncludes();
|
|
||||||
spdlog::debug("[JuiceRemapper JNI] clearIncludes");
|
|
||||||
}
|
|
||||||
|
|
||||||
JNIEXPORT void JNICALL Java_cn_xiaozhou233_juiceremapper_JuiceRemapper_clearExcludes
|
|
||||||
(JNIEnv *, jclass) {
|
|
||||||
JuiceAgent::Services::Remapper::getInstance().clearExcludes();
|
|
||||||
spdlog::debug("[JuiceRemapper JNI] clearExcludes");
|
|
||||||
}
|
|
||||||
@@ -1,151 +0,0 @@
|
|||||||
#include <services/base.hpp>
|
|
||||||
#include <services/service_manager.hpp>
|
|
||||||
#include <event/event_type.hpp>
|
|
||||||
#include <JuiceAgent/Logger.hpp>
|
|
||||||
#include <JuiceAgent/Utils.hpp>
|
|
||||||
#include <global.hpp>
|
|
||||||
|
|
||||||
#include <string_view>
|
|
||||||
#include <cstring>
|
|
||||||
|
|
||||||
namespace JuiceAgent {
|
|
||||||
namespace Services {
|
|
||||||
|
|
||||||
class JuiceRemapperService : public IService {
|
|
||||||
public:
|
|
||||||
const char* name() const override { return "JuiceRemapperService"; }
|
|
||||||
|
|
||||||
bool onInitialize() override {
|
|
||||||
listen<JuiceRemapperService, EventLoaded>(
|
|
||||||
EventId::Loaded,
|
|
||||||
&JuiceRemapperService::onLoaded
|
|
||||||
);
|
|
||||||
|
|
||||||
listen<JuiceRemapperService, EventClassFileLoadHook>(
|
|
||||||
EventId::ClassFileLoadHook,
|
|
||||||
&JuiceRemapperService::onClassFileLoadHook
|
|
||||||
);
|
|
||||||
|
|
||||||
spdlog::debug("[JuiceRemapperService] Initialized");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void onShutdown() override {
|
|
||||||
unlistenAll();
|
|
||||||
spdlog::debug("[JuiceRemapperService] Shutdown");
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
void onLoaded(const EventLoaded& /*event*/) {
|
|
||||||
auto& agent = Agent::getInstance();
|
|
||||||
auto& cfg = agent.getConfig();
|
|
||||||
|
|
||||||
const bool enabled = cfg.get<bool>(
|
|
||||||
"JuiceAgent.Modules.JuiceRemapper.Enabled", false);
|
|
||||||
|
|
||||||
if (!enabled) {
|
|
||||||
spdlog::debug("[JuiceRemapperService] Disabled by config");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
spdlog::debug("[JuiceRemapperService] Enabled. Waiting for addInclude to start remapping");
|
|
||||||
}
|
|
||||||
|
|
||||||
void onClassFileLoadHook(const EventClassFileLoadHook& e) {
|
|
||||||
if (!e.name || !e.jni_env || !e.jvmti_env ||
|
|
||||||
!e.classbytes || e.class_data_len <= 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto& remapper = JuiceAgent::Services::Remapper::getInstance();
|
|
||||||
if (!remapper.isInit())
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (std::string_view(e.name).starts_with("cn/xiaozhou233/juiceremapper/"))
|
|
||||||
return;
|
|
||||||
|
|
||||||
// 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();
|
|
||||||
if (!remapClass || !remapMethod) {
|
|
||||||
spdlog::error("[JuiceRemapperService] Remap declarations not cached");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
JNIEnv* env = e.jni_env;
|
|
||||||
|
|
||||||
// className: const char* -> jstring
|
|
||||||
jstring jClassName = env->NewStringUTF(e.name);
|
|
||||||
if (!jClassName) {
|
|
||||||
if (env->ExceptionCheck()) env->ExceptionClear();
|
|
||||||
spdlog::error("[JuiceRemapperService] Failed to create className jstring");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// classBytes: const unsigned char* -> jbyteArray
|
|
||||||
jbyteArray jClassBytes = env->NewByteArray(e.class_data_len);
|
|
||||||
if (!jClassBytes) {
|
|
||||||
if (env->ExceptionCheck()) env->ExceptionClear();
|
|
||||||
env->DeleteLocalRef(jClassName);
|
|
||||||
spdlog::error("[JuiceRemapperService] Failed to create classBytes jbyteArray");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
env->SetByteArrayRegion(
|
|
||||||
jClassBytes, 0, e.class_data_len,
|
|
||||||
reinterpret_cast<const jbyte*>(e.classbytes));
|
|
||||||
if (env->ExceptionCheck()) {
|
|
||||||
env->ExceptionClear();
|
|
||||||
env->DeleteLocalRef(jClassName);
|
|
||||||
env->DeleteLocalRef(jClassBytes);
|
|
||||||
spdlog::error("[JuiceRemapperService] Failed to fill classBytes");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Call static byte[] Remap.remap(String, byte[])
|
|
||||||
jbyteArray jResult = static_cast<jbyteArray>(
|
|
||||||
env->CallStaticObjectMethod(remapClass, remapMethod, jClassName, jClassBytes));
|
|
||||||
if (env->ExceptionCheck() || !jResult) {
|
|
||||||
env->ExceptionClear();
|
|
||||||
env->DeleteLocalRef(jClassName);
|
|
||||||
env->DeleteLocalRef(jClassBytes);
|
|
||||||
spdlog::warn("[JuiceRemapperService] Remap returned null/exception for: {}", e.name);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const jsize newLen = env->GetArrayLength(jResult);
|
|
||||||
jbyte* newBytes = env->GetByteArrayElements(jResult, nullptr);
|
|
||||||
if (!newBytes) {
|
|
||||||
env->DeleteLocalRef(jClassName);
|
|
||||||
env->DeleteLocalRef(jClassBytes);
|
|
||||||
env->DeleteLocalRef(jResult);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write remapped bytes back to the JVMTI callback
|
|
||||||
unsigned char* newBuf = nullptr;
|
|
||||||
if (e.jvmti_env->Allocate(newLen, &newBuf) == JVMTI_ERROR_NONE) {
|
|
||||||
std::memcpy(newBuf, newBytes, newLen);
|
|
||||||
*e.new_class_data_len = newLen;
|
|
||||||
*e.new_classbytes = newBuf;
|
|
||||||
spdlog::info("[JuiceRemapperService] Remapped: {} ({} -> {} bytes)",
|
|
||||||
e.name, e.class_data_len, newLen);
|
|
||||||
} else {
|
|
||||||
spdlog::error("[JuiceRemapperService] Failed to allocate buffer for: {}", e.name);
|
|
||||||
}
|
|
||||||
|
|
||||||
env->ReleaseByteArrayElements(jResult, newBytes, JNI_ABORT);
|
|
||||||
env->DeleteLocalRef(jClassName);
|
|
||||||
env->DeleteLocalRef(jClassBytes);
|
|
||||||
env->DeleteLocalRef(jResult);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
JUICEAGENT_REGISTER_SERVICE(::JuiceAgent::Services::JuiceRemapperService)
|
|
||||||
Reference in new issue
Block a user