Agent rewrite

Signed-off-by: Faumaray <faumaray@gmail.com>
This commit is contained in:
faumaray committed 2025-10-20 21:24:52 +03:00
1 parent 65b91ff8b2
commit de940dab27
17 files changed
+451 -421

No files matched your search

+82
View File
@@ -0,0 +1,82 @@
#pragma once
#include <class_transformer.h>
#include <Logging/Logger.hpp>
#include <Mod/Context.hpp>
#include <Mods/Loader.hpp>
#include <atomic>
#include <filesystem>
#include <jvmti.h>
#include <jni.h>
#include <memory>
#include <mutex>
#include <string>
#include <unordered_map>
extern "C" JNIEXPORT jint JNICALL Java_nativeAgentInit(JNIEnv* env, jclass clazz, jobjectArray args, jobject instrumentation);
extern "C" JNIEXPORT jint JNICALL Java_nativeAgentStart(JNIEnv* env, jclass clazz);
extern "C" JNIEXPORT jint JNICALL Java_ru_faumaray_VoidLoaderAgent_nativeAgentStart(JNIEnv* env, jclass clazz);
extern "C" JNIEXPORT jint JNICALL Java_ru_faumaray_VoidLoaderAgent_nativeAgentInit(JNIEnv* env, jclass jclass, jobject instrumentation, jobjectArray args);
extern "C" JNIEXPORT void JNICALL Java_ru_faumaray_VoidLoaderAgent_nativeCleanup(JNIEnv* env, jclass clazz);
namespace vl::Agent {
class Agent {
public:
static void JNICALL vmInit(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread);
static void JNICALL vmDeath(jvmtiEnv* jvmti, JNIEnv* jni);
static Agent& getInstance(const std::unordered_map<std::string, std::string>& args) {
std::call_once(initFlag_, [&] {
instance_.reset(new Agent(args));
});
return *instance_;
}
// Accessor that just retrieves the existing instance
static Agent& getInstance() {
if (!instance_) {
throw std::runtime_error("Agent not initialized — call getInstance(args) first.");
}
return *instance_;
}
explicit Agent(std::unordered_map<std::string, std::string> args);
Agent(const Agent&) = delete;
Agent& operator=(const Agent&) = delete;
Agent(Agent&&) = delete;
Agent& operator=(Agent&&) = delete;
bool isValid() const noexcept
{
return m_isValid;
}
private:
std::unordered_map<std::string, std::filesystem::path> findMods(const std::string& dir);
friend int JNICALL ::Java_nativeAgentInit(JNIEnv* env, jclass clazz, jobjectArray args, jobject instrumentation);
friend int JNICALL ::Java_nativeAgentInit(JNIEnv* env, jclass clazz, jobjectArray args, jobject instrumentation);
friend int JNICALL ::Java_nativeAgentStart(JNIEnv* env, jclass clazz);
friend int JNICALL ::Java_ru_faumaray_VoidLoaderAgent_nativeAgentStart(JNIEnv* env, jclass clazz);
friend int JNICALL ::Java_ru_faumaray_VoidLoaderAgent_nativeAgentInit(JNIEnv* env, jclass jclass, jobject instrumentation, jobjectArray args);
friend void JNICALL ::Java_ru_faumaray_VoidLoaderAgent_nativeCleanup(JNIEnv* env, jclass clazz);
friend class ClassTransformer;
friend class vl::mod::ModLoader;
private:
std::unordered_map<std::string, std::filesystem::path> m_mods;
jvmtiEnv* m_jmti = nullptr;
JavaVM* m_jvm = nullptr;
JNIEnv* m_jni = nullptr;
std::unique_ptr<vl::mod::ModLoader> m_modloader;
std::unique_ptr<ClassTransformer> m_transformer;
vl::mod::Context m_context;
std::atomic<bool> m_isValid;
std::shared_ptr<vl::logging::Logger> m_logger;
static std::unique_ptr<Agent> instance_;
static std::once_flag initFlag_;
};
inline std::unique_ptr<Agent> Agent::instance_ = nullptr;
inline std::once_flag Agent::initFlag_;
}
+2
View File
@@ -14,6 +14,8 @@ public:
std::string_view prefix() const noexcept override { return prefix_; }
void setLevel(Level l) noexcept override { level_ = l; }
//FIXME: make possible to override rotation
void setMaxBytes(std::uintmax_t bytes) noexcept override { };
Level level() const noexcept override { return level_; }
protected:
+29
View File
@@ -0,0 +1,29 @@
#pragma once
#include <Mod/Base.hpp>
#include <memory>
#include <string>
namespace vl::mod {
class LoadedMod {
public:
LoadedMod(std::string name, std::string version,
std::unique_ptr<Base> instance, void* handle)
: name_(std::move(name)), version_(std::move(version)),
instance_(std::move(instance)), handle_(handle) {}
Base& mod() { return *instance_; }
const Base& mod() const { return *instance_; }
const std::string& name() const noexcept { return name_; }
const std::string& version() const noexcept { return version_; }
void* handle() const noexcept { return handle_; }
private:
std::string name_;
std::string version_;
std::unique_ptr<Base> instance_;
void* handle_{};
};
} // namespace vl::log
+32
View File
@@ -0,0 +1,32 @@
#pragma once
#include <Mod/Context.hpp>
#include <Mods/Loaded.hpp>
#include <Mods/Registry.hpp>
#include <memory>
#include <string>
#include <vector>
namespace vl::mod {
// Handles dlopen/dlsym, discovers mod registrations, constructs mods (C2)
class ModLoader {
public:
// Load a module (.so), discover CRTP-registered mod types, instantiate each with Context.
// Returns the number of instances created from this module.
std::size_t loadModule(const std::string& path, const Context& ctx);
// Start/stop all currently loaded instances.
void startAll();
void stopAll();
// Unload everything (calls stopAll first if not stopped).
void unloadAll();
const std::vector<LoadedMod>& instances() const noexcept { return instances_; }
private:
std::vector<void*> handles_;
std::vector<LoadedMod> instances_;
};
} // namespace vl::log
+32
View File
@@ -0,0 +1,32 @@
#pragma once
#include <Mod/Registration.hpp>
#include <functional>
#include <memory>
#include <string>
#include <string_view>
#include <vector>
namespace vl::mod {
class Registry {
public:
struct Entry {
std::string typeName;
std::string name; // from T::Name()
std::string version; // from T::Version()
std::function<std::unique_ptr<Base>(const Context&)> factory;
};
// Read registration entries from a loaded module (ELF .so handle)
// Returns number of entries found.
static std::size_t addFromModule(void* soHandle);
// Enumerate all known entries from all modules loaded so far
static const std::vector<Entry>& all();
static std::vector<Entry>& mutableAll();
private:
static std::vector<Entry>& storage_();
};
} // namespace vl::log
-23
View File
@@ -1,23 +0,0 @@
#pragma once
#include <jvmti.h>
#include <jni.h>
#include <iostream>
class JvmtiHandler {
public:
static void JNICALL vmInit(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread);
static void JNICALL vmDeath(jvmtiEnv* jvmti, JNIEnv* jni);
// Accessor methods for global variables
static jvmtiEnv* getJvmti();
static JavaVM* getJavaVM();
static bool isAgentInitialized();
// Setter methods
static void setJvmti(jvmtiEnv* jvmti);
static void setJavaVM(JavaVM* jvm);
static void setAgentInitialized(bool initialized);
private:
// No global variable declarations here - they'll be in agent.cpp
};
-55
View File
@@ -1,55 +0,0 @@
#pragma once
#include <string>
#include <vector>
#include <filesystem>
#include <jvmti.h>
enum class ModType {
JAR, // Java/bytecode mod
NATIVE // Native C++ mod
};
struct ModInfo {
std::string name;
std::string version;
std::string file_path;
ModType type;
bool enabled;
// Mod metadata (from fabric.mod.json or similar)
std::string author;
std::string description;
std::vector<std::string> dependencies;
};
class ModManager {
public:
ModManager();
~ModManager();
// Discover and load mods from directory
void discoverMods(const std::string& mods_dir);
// Load a single mod
void loadMod(const std::filesystem::path& mod_path);
// Apply transformations from all mods to a class
void applyTransformations(
const std::string& class_name,
const unsigned char* class_data,
jint class_data_len
);
// Get list of loaded mods
const std::vector<ModInfo>& getLoadedMods() const;
// Enable/disable a mod
void setModEnabled(const std::string& mod_name, bool enabled);
private:
std::vector<ModInfo> loaded_mods;
// Parse mod metadata
void parseJarMod(const std::filesystem::path& jar_path, ModInfo& mod);
void parseNativeMod(const std::filesystem::path& native_path, ModInfo& mod);
};