Loading mods

Signed-off-by: Faumaray <faumaray@gmail.com>
This commit is contained in:
faumaray committed 2025-10-20 22:04:25 +03:00
1 parent de940dab27
commit 6ea34bf00b
10 files changed
+169 -10

No files matched your search

-1
View File
@@ -43,7 +43,6 @@ FetchContent_Declare(
GIT_TAG main # release-1.10.0
)
FetchContent_MakeAvailable(VoidLoaderApi)
# ============================================================
+6
View File
@@ -0,0 +1,6 @@
#pragma once
#if defined(_WIN32)
# include <Mods/LoadLibraryWin.hpp>
#else
# include <Mods/LoadLibraryUnix.hpp>
#endif
+8
View File
@@ -0,0 +1,8 @@
#pragma once
#include <string>
void* LoadLibrary(const std::string& path, bool isGlobal);
bool CloseLibrary(void* handle);
void* LoadSymbol(void* handle, const std::string& name);
+9
View File
@@ -0,0 +1,9 @@
#pragma once
#if defined(_WIN32)
#include <string>
void* LoadLibrary(const std::string& path, bool isGlobal);
bool CloseLibrary(void* handle);
void* LoadSymbol(void* handle, const std::string& name);
#endif
+1 -1
View File
@@ -13,7 +13,7 @@ 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);
std::size_t loadModule(const std::string& path, const Context& ctx, bool isGlobal);
// Start/stop all currently loaded instances.
void startAll();
+3 -2
View File
@@ -1,3 +1,4 @@
#include <Mods/LoadLibrary.hpp>
#include <Logging/Level.hpp>
#include <Logging/Logger.hpp>
#include <Logging/LoggerManager.hpp>
@@ -36,7 +37,7 @@ namespace vl::Agent {
return;
}
dlopen(mods.at("VoidLoaderApi.vlmod").c_str(), RTLD_NOW | RTLD_GLOBAL);
LoadLibrary(mods.at("VoidLoaderApi.vlmod").c_str(), true);
mods.erase("VoidLoaderApi.vlmod");
mods.erase("VoidLoader.vlmod");
@@ -134,7 +135,7 @@ extern "C" JNIEXPORT jint JNICALL Java_nativeAgentStart(JNIEnv* env, jclass claz
for (auto& [name, path] : instance.m_mods)
{
instance.m_logger->trace("Loading mod: {}", name);
loaded += instance.m_modloader->loadModule(path, instance.m_context);
loaded += instance.m_modloader->loadModule(path, instance.m_context, false);
}
instance.m_logger->info("Mods loaded: {}, Starting", loaded);
+20
View File
@@ -0,0 +1,20 @@
#if defined(unix)
#include <Mods/LoadLibraryUnix.hpp>
#include <dlfcn.h>
void* LoadLibrary(const std::string& path, bool isGlobal)
{
dlerror();
return dlopen(path.c_str(), isGlobal ? RTLD_NOW | RTLD_GLOBAL : RTLD_NOW);
}
bool CloseLibrary(void* handle)
{
return dlclose(handle) == 0;
}
void* LoadSymbol(void* handle, const std::string& name)
{
dlerror();
return dlsym(handle, name.c_str());
}
#endif
+114
View File
@@ -0,0 +1,114 @@
#if defined(_WIN32)
#include <Mods/LoadLibraryWin.hpp>
#include <assert.h>
#include <cstdint>
#include <windows.h>
static int32_t __wtf8_decode1(const char** input) {
uint32_t code_point;
uint8_t b1;
uint8_t b2;
uint8_t b3;
uint8_t b4;
b1 = **input;
if (b1 <= 0x7F)
return b1; /* ASCII code point */
if (b1 < 0xC2)
return -1; /* invalid: continuation byte */
code_point = b1;
b2 = *++*input;
if ((b2 & 0xC0) != 0x80)
return -1; /* invalid: not a continuation byte */
code_point = (code_point << 6) | (b2 & 0x3F);
if (b1 <= 0xDF)
return 0x7FF & code_point; /* two-byte character */
b3 = *++*input;
if ((b3 & 0xC0) != 0x80)
return -1; /* invalid: not a continuation byte */
code_point = (code_point << 6) | (b3 & 0x3F);
if (b1 <= 0xEF)
return 0xFFFF & code_point; /* three-byte character */
b4 = *++*input;
if ((b4 & 0xC0) != 0x80)
return -1; /* invalid: not a continuation byte */
code_point = (code_point << 6) | (b4 & 0x3F);
if (b1 <= 0xF4) {
code_point &= 0x1FFFFF;
if (code_point <= 0x10FFFF)
return code_point; /* four-byte character */
}
/* code point too large */
return -1;
}
ssize_t wtf8_length_as_utf16(const char* source_ptr) {
size_t w_target_len = 0;
int32_t code_point;
do {
code_point = __wtf8_decode1(&source_ptr);
if (code_point < 0)
return -1;
if (code_point > 0xFFFF)
w_target_len++;
w_target_len++;
} while (*source_ptr++);
return w_target_len;
}
void wtf8_to_utf16(const char* source_ptr,
uint16_t* w_target,
size_t w_target_len) {
int32_t code_point;
do {
code_point = __wtf8_decode1(&source_ptr);
/* uv_wtf8_length_as_utf16 should have been called and checked first. */
assert(code_point >= 0);
if (code_point > 0xFFFF) {
assert(code_point < 0x10FFFF);
*w_target++ = (((code_point - 0x10000) >> 10) + 0xD800);
*w_target++ = ((code_point - 0x10000) & 0x3FF) + 0xDC00;
w_target_len -= 2;
} else {
*w_target++ = code_point;
w_target_len -= 1;
}
} while (*source_ptr++);
(void)w_target_len;
assert(w_target_len == 0);
}
void* LoadLibrary(const std::string& path, bool isGlobal)
{
WCHAR filename_w[32768];
ssize_t r;
void* handle = nullptr;
r = wtf8_length_as_utf16(path.c_str());
if (r < 0)
return nullptr;
if ((size_t) r > ARRAY_SIZE(filename_w))
return nullptr;
_wtf8_to_utf16(path.c_str(), filename_w, r);
return LoadLibraryExW(filename_w, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
}
bool CloseLibrary(void* handle)
{
return FreeLibrary(handle) != 0;
}
void* LoadSymbol(void* handle, const std::string& name)
{
return (void*)(uintptr_t) GetProcAddress(handle, name.c_str());
}
#endif
+5 -3
View File
@@ -1,4 +1,6 @@
#include <Mods/LoadLibrary.hpp>
#include <Mods/Loader.hpp>
#include <Mods/LoadLibrary.hpp>
#include <Agent.hpp>
#include <dlfcn.h>
#include <iostream>
@@ -6,8 +8,8 @@
namespace vl::mod {
std::size_t ModLoader::loadModule(const std::string& path, const Context& ctx) {
void* handle = dlopen(path.c_str(), RTLD_NOW | RTLD_GLOBAL);
std::size_t ModLoader::loadModule(const std::string& path, const Context& ctx, bool isGlobal) {
void* handle = LoadLibrary(path, isGlobal);
if (!handle) throw std::runtime_error(std::string("dlopen failed: ") + dlerror());
handles_.push_back(handle);
@@ -48,7 +50,7 @@ void ModLoader::stopAll() {
void ModLoader::unloadAll() {
stopAll();
instances_.clear();
for (auto* h : handles_) if (h) dlclose(h);
for (auto* h : handles_) if (h) CloseLibrary(h);
handles_.clear();
}
+3 -3
View File
@@ -1,5 +1,5 @@
#include <Mods/Registry.hpp>
#include <dlfcn.h> // dlopen/dlsym (ELF)
#include <Mods/LoadLibrary.hpp>
#include <iostream>
#include <stdexcept>
@@ -21,8 +21,8 @@ std::vector<Registry::Entry>& Registry::mutableAll() {
std::size_t Registry::addFromModule(void* soHandle) {
#if defined(__ELF__)
using BoundPtr = const Registration*;
auto symStart = dlsym(soHandle, "__start_vlmods");
auto symStop = dlsym(soHandle, "__stop_vlmods");
auto symStart = LoadSymbol(soHandle, "__start_vlmods");
auto symStop = LoadSymbol(soHandle, "__stop_vlmods");
if ((symStart == nullptr) || (symStop == nullptr))
{
return 0;