1 parent
8583a479dd
commit
3851aeb703
42 files changed
+6948
-759
No files matched your search
+247
-224
@@ -1,9 +1,9 @@
|
||||
#include <Void/Log/Log.hpp>
|
||||
#include <Void/Plugin/Loader.hpp>
|
||||
#include <Void/Log/Log.hpp>
|
||||
|
||||
#if defined(__unix__) || defined(__APPLE__)
|
||||
# include <dlfcn.h>
|
||||
# define VOID_HAS_DLOPEN 1
|
||||
#include <dlfcn.h>
|
||||
#define VOID_HAS_DLOPEN 1
|
||||
#endif
|
||||
|
||||
#include <filesystem>
|
||||
@@ -13,258 +13,281 @@
|
||||
namespace Void::Plugin
|
||||
{
|
||||
|
||||
std::string_view to_string(LoadError e) noexcept
|
||||
{
|
||||
switch (e) {
|
||||
case LoadError::FileNotFound: return "file not found";
|
||||
case LoadError::DlopenFailed: return "dlopen failed";
|
||||
case LoadError::SymbolMissing: return "required symbol missing";
|
||||
case LoadError::AbiMismatch: return "ABI version mismatch";
|
||||
case LoadError::CreateFailed: return "plugin create() returned null";
|
||||
case LoadError::NotSupported: return "platform does not support dlopen";
|
||||
case LoadError::StateRejected: return "new instance rejected state";
|
||||
case LoadError::ActivationFailed: return "plugin activation failed";
|
||||
case LoadError::UnknownPlugin: return "no plugin registered under name";
|
||||
}
|
||||
return "unknown";
|
||||
}
|
||||
std::string_view to_string(LoadError e) noexcept
|
||||
{
|
||||
switch (e) {
|
||||
case LoadError::FileNotFound:
|
||||
return "file not found";
|
||||
case LoadError::DlopenFailed:
|
||||
return "dlopen failed";
|
||||
case LoadError::SymbolMissing:
|
||||
return "required symbol missing";
|
||||
case LoadError::AbiMismatch:
|
||||
return "ABI version mismatch";
|
||||
case LoadError::CreateFailed:
|
||||
return "plugin create() returned null";
|
||||
case LoadError::NotSupported:
|
||||
return "platform does not support dlopen";
|
||||
case LoadError::StateRejected:
|
||||
return "new instance rejected state";
|
||||
case LoadError::ActivationFailed:
|
||||
return "plugin activation failed";
|
||||
case LoadError::UnknownPlugin:
|
||||
return "no plugin registered under name";
|
||||
case LoadError::NameMismatch:
|
||||
return "replacement plugin name mismatch";
|
||||
}
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
// ── internal helpers ──────────────────────────────────────────────────────────
|
||||
// ── internal helpers
|
||||
// ──────────────────────────────────────────────────────────
|
||||
|
||||
void PluginLoader::destroy_current() noexcept
|
||||
{
|
||||
void PluginLoader::destroy_current() noexcept
|
||||
{
|
||||
#ifdef VOID_HAS_DLOPEN
|
||||
if (plugin_) {
|
||||
plugin_->on_unload();
|
||||
if (destroy_) destroy_(plugin_);
|
||||
plugin_ = nullptr;
|
||||
}
|
||||
if (handle_) {
|
||||
dlclose(handle_);
|
||||
handle_ = nullptr;
|
||||
}
|
||||
destroy_ = nullptr;
|
||||
transfer_ = nullptr;
|
||||
ready_ = nullptr;
|
||||
state_version_ = nullptr;
|
||||
serialize_ = nullptr;
|
||||
free_string_ = nullptr;
|
||||
deserialize_ = nullptr;
|
||||
if (plugin_) {
|
||||
plugin_->on_unload();
|
||||
if (destroy_) {
|
||||
destroy_(plugin_);
|
||||
}
|
||||
plugin_ = nullptr;
|
||||
}
|
||||
if (handle_) {
|
||||
dlclose(handle_);
|
||||
handle_ = nullptr;
|
||||
}
|
||||
destroy_ = nullptr;
|
||||
transfer_ = nullptr;
|
||||
ready_ = nullptr;
|
||||
state_version_ = nullptr;
|
||||
serialize_ = nullptr;
|
||||
free_string_ = nullptr;
|
||||
deserialize_ = nullptr;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
// Move plugin state old -> new across a reload. Prefers the v2
|
||||
// serialize/deserialize protocol (survives layout changes between
|
||||
// versions); falls back to the raw transfer_state hook; else no-op.
|
||||
// Returns false only when the new instance actively rejects the state.
|
||||
bool transfer_plugin_state(const PluginLoader& from, PluginLoader& to)
|
||||
{
|
||||
if (!from.plugin_ || !to.plugin_) {
|
||||
return true;
|
||||
}
|
||||
if (from.serialize_ && to.deserialize_) {
|
||||
const std::uint32_t from_version =
|
||||
from.state_version_ ? from.state_version_() : 0;
|
||||
const char* json = from.serialize_(from.plugin_);
|
||||
if (json != nullptr) {
|
||||
const int rc = to.deserialize_(to.plugin_, json, from_version);
|
||||
if (from.free_string_) {
|
||||
from.free_string_(json);
|
||||
}
|
||||
if (rc != 0) {
|
||||
Void::Log::warn("Plugin",
|
||||
"deserialize_state rejected state "
|
||||
"(from v{}, rc={})",
|
||||
from_version, rc);
|
||||
return false;
|
||||
}
|
||||
Void::Log::debug("Plugin",
|
||||
"state transferred via v2 protocol (v{})",
|
||||
from_version);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (to.transfer_) {
|
||||
to.transfer_(from.plugin_, to.plugin_);
|
||||
Void::Log::debug("Plugin", "state transferred via transfer_state");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
// Move plugin state old -> new across a reload. Prefers the v2
|
||||
// serialize/deserialize protocol (survives layout changes between
|
||||
// versions); falls back to the raw transfer_state hook; else no-op.
|
||||
// Returns false only when the new instance actively rejects the state.
|
||||
bool transfer_plugin_state(const PluginLoader& from, PluginLoader& to)
|
||||
{
|
||||
if (!from.plugin_ || !to.plugin_) {
|
||||
return true;
|
||||
}
|
||||
if (from.serialize_ && to.deserialize_) {
|
||||
const std::uint32_t from_version =
|
||||
from.state_version_ ? from.state_version_() : 0;
|
||||
const char* json = from.serialize_(from.plugin_);
|
||||
if (json != nullptr) {
|
||||
const int rc = to.deserialize_(to.plugin_, json, from_version);
|
||||
if (from.free_string_) {
|
||||
from.free_string_(json);
|
||||
}
|
||||
if (rc != 0) {
|
||||
Void::Log::warn("Plugin",
|
||||
"deserialize_state rejected state "
|
||||
"(from v{}, rc={})",
|
||||
from_version, rc);
|
||||
return false;
|
||||
}
|
||||
Void::Log::debug("Plugin",
|
||||
"state transferred via v2 protocol (v{})",
|
||||
from_version);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (to.transfer_) {
|
||||
to.transfer_(from.plugin_, to.plugin_);
|
||||
Void::Log::debug("Plugin", "state transferred via transfer_state");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::expected<void, LoadError>
|
||||
PluginLoader::load_from(const std::filesystem::path& path)
|
||||
{
|
||||
std::expected<void, LoadError>
|
||||
PluginLoader::load_from(const std::filesystem::path& path)
|
||||
{
|
||||
#ifdef VOID_HAS_DLOPEN
|
||||
void* h = dlopen(path.c_str(), RTLD_NOW | RTLD_LOCAL);
|
||||
if (!h) return std::unexpected{LoadError::DlopenFailed};
|
||||
void* h = dlopen(path.c_str(), RTLD_NOW | RTLD_LOCAL);
|
||||
if (!h) {
|
||||
return std::unexpected{LoadError::DlopenFailed};
|
||||
}
|
||||
|
||||
const auto sym_info = reinterpret_cast<info_fn> (dlsym(h, kInfoSymbol));
|
||||
const auto sym_create = reinterpret_cast<create_fn> (dlsym(h, kCreateSymbol));
|
||||
const auto sym_destroy = reinterpret_cast<destroy_fn>(dlsym(h, kDestroySymbol));
|
||||
const auto sym_info = reinterpret_cast<info_fn>(dlsym(h, kInfoSymbol));
|
||||
const auto sym_create =
|
||||
reinterpret_cast<create_fn>(dlsym(h, kCreateSymbol));
|
||||
const auto sym_destroy =
|
||||
reinterpret_cast<destroy_fn>(dlsym(h, kDestroySymbol));
|
||||
|
||||
if (!sym_info || !sym_create || !sym_destroy) {
|
||||
dlclose(h);
|
||||
return std::unexpected{LoadError::SymbolMissing};
|
||||
}
|
||||
if (!sym_info || !sym_create || !sym_destroy) {
|
||||
dlclose(h);
|
||||
return std::unexpected{LoadError::SymbolMissing};
|
||||
}
|
||||
|
||||
const PluginInfo info = sym_info();
|
||||
if (info.abi_version != ABI_VERSION) {
|
||||
dlclose(h);
|
||||
return std::unexpected{LoadError::AbiMismatch};
|
||||
}
|
||||
const PluginInfo info = sym_info();
|
||||
if (info.abi_version != ABI_VERSION) {
|
||||
dlclose(h);
|
||||
return std::unexpected{LoadError::AbiMismatch};
|
||||
}
|
||||
|
||||
void* raw = sym_create();
|
||||
if (!raw) {
|
||||
dlclose(h);
|
||||
return std::unexpected{LoadError::CreateFailed};
|
||||
}
|
||||
void* raw = sym_create();
|
||||
if (!raw) {
|
||||
dlclose(h);
|
||||
return std::unexpected{LoadError::CreateFailed};
|
||||
}
|
||||
|
||||
handle_ = h;
|
||||
plugin_ = static_cast<IPlugin*>(raw);
|
||||
destroy_ = sym_destroy;
|
||||
transfer_ = reinterpret_cast<transfer_fn>(dlsym(h, kTransferSymbol));
|
||||
ready_ = reinterpret_cast<ready_fn>(dlsym(h, kReadySymbol));
|
||||
state_version_ = reinterpret_cast<state_version_fn>(
|
||||
dlsym(h, kStateVersionSymbol));
|
||||
serialize_ = reinterpret_cast<serialize_fn>(dlsym(h, kSerializeSymbol));
|
||||
free_string_ = reinterpret_cast<free_string_fn>(
|
||||
dlsym(h, kFreeStringSymbol));
|
||||
deserialize_ = reinterpret_cast<deserialize_fn>(
|
||||
dlsym(h, kDeserializeSymbol));
|
||||
info_ = info;
|
||||
return {};
|
||||
handle_ = h;
|
||||
plugin_ = static_cast<IPlugin*>(raw);
|
||||
destroy_ = sym_destroy;
|
||||
transfer_ = reinterpret_cast<transfer_fn>(dlsym(h, kTransferSymbol));
|
||||
ready_ = reinterpret_cast<ready_fn>(dlsym(h, kReadySymbol));
|
||||
state_version_ =
|
||||
reinterpret_cast<state_version_fn>(dlsym(h, kStateVersionSymbol));
|
||||
serialize_ = reinterpret_cast<serialize_fn>(dlsym(h, kSerializeSymbol));
|
||||
free_string_ =
|
||||
reinterpret_cast<free_string_fn>(dlsym(h, kFreeStringSymbol));
|
||||
deserialize_ =
|
||||
reinterpret_cast<deserialize_fn>(dlsym(h, kDeserializeSymbol));
|
||||
info_ = info;
|
||||
return {};
|
||||
#else
|
||||
(void)path;
|
||||
return std::unexpected{LoadError::NotSupported};
|
||||
(void)path;
|
||||
return std::unexpected{LoadError::NotSupported};
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
// ── public API ────────────────────────────────────────────────────────────────
|
||||
// ── public API
|
||||
// ────────────────────────────────────────────────────────────────
|
||||
|
||||
std::expected<PluginLoader, LoadError>
|
||||
PluginLoader::load(std::filesystem::path path)
|
||||
{
|
||||
if (!std::filesystem::exists(path))
|
||||
return std::unexpected{LoadError::FileNotFound};
|
||||
std::expected<PluginLoader, LoadError>
|
||||
PluginLoader::load(std::filesystem::path path)
|
||||
{
|
||||
if (!std::filesystem::exists(path)) {
|
||||
return std::unexpected{LoadError::FileNotFound};
|
||||
}
|
||||
|
||||
PluginLoader ldr;
|
||||
ldr.path_ = path;
|
||||
PluginLoader ldr;
|
||||
ldr.path_ = path;
|
||||
|
||||
if (auto r = ldr.load_from(path); !r) {
|
||||
Void::Log::warn("Plugin", "load '{}' failed: {}", path.string(),
|
||||
to_string(r.error()));
|
||||
return std::unexpected{r.error()};
|
||||
}
|
||||
if (auto r = ldr.load_from(path); !r) {
|
||||
Void::Log::warn("Plugin", "load '{}' failed: {}", path.string(),
|
||||
to_string(r.error()));
|
||||
return std::unexpected{r.error()};
|
||||
}
|
||||
|
||||
try {
|
||||
ldr.plugin_->on_load();
|
||||
} catch (...) {
|
||||
return std::unexpected{LoadError::ActivationFailed};
|
||||
}
|
||||
if (ldr.ready_ && ldr.ready_(ldr.plugin_) != 0) {
|
||||
return std::unexpected{LoadError::ActivationFailed};
|
||||
}
|
||||
Void::Log::info("Plugin", "loaded '{}' ({} v{})", path.string(),
|
||||
ldr.info_.name, ldr.info_.plugin_version);
|
||||
return ldr;
|
||||
}
|
||||
try {
|
||||
ldr.plugin_->on_load();
|
||||
} catch (...) {
|
||||
return std::unexpected{LoadError::ActivationFailed};
|
||||
}
|
||||
if (ldr.ready_ && ldr.ready_(ldr.plugin_) != 0) {
|
||||
return std::unexpected{LoadError::ActivationFailed};
|
||||
}
|
||||
Void::Log::info("Plugin", "loaded '{}' ({} v{})", path.string(),
|
||||
ldr.info_.name, ldr.info_.plugin_version);
|
||||
return ldr;
|
||||
}
|
||||
|
||||
PluginLoader::~PluginLoader() { destroy_current(); }
|
||||
PluginLoader::~PluginLoader() { destroy_current(); }
|
||||
|
||||
PluginLoader::PluginLoader(PluginLoader&& o) noexcept
|
||||
: handle_ {std::exchange(o.handle_, nullptr)}
|
||||
, plugin_ {std::exchange(o.plugin_, nullptr)}
|
||||
, destroy_ {std::exchange(o.destroy_, nullptr)}
|
||||
, transfer_ {std::exchange(o.transfer_, nullptr)}
|
||||
, ready_ {std::exchange(o.ready_, nullptr)}
|
||||
, state_version_{std::exchange(o.state_version_, nullptr)}
|
||||
, serialize_ {std::exchange(o.serialize_, nullptr)}
|
||||
, free_string_ {std::exchange(o.free_string_, nullptr)}
|
||||
, deserialize_ {std::exchange(o.deserialize_, nullptr)}
|
||||
, info_ {o.info_}
|
||||
, path_ {std::move(o.path_)}
|
||||
{}
|
||||
PluginLoader::PluginLoader(PluginLoader&& o) noexcept
|
||||
: handle_{std::exchange(o.handle_, nullptr)},
|
||||
plugin_{std::exchange(o.plugin_, nullptr)},
|
||||
destroy_{std::exchange(o.destroy_, nullptr)},
|
||||
transfer_{std::exchange(o.transfer_, nullptr)},
|
||||
ready_{std::exchange(o.ready_, nullptr)},
|
||||
state_version_{std::exchange(o.state_version_, nullptr)},
|
||||
serialize_{std::exchange(o.serialize_, nullptr)},
|
||||
free_string_{std::exchange(o.free_string_, nullptr)},
|
||||
deserialize_{std::exchange(o.deserialize_, nullptr)}, info_{o.info_},
|
||||
path_{std::move(o.path_)}
|
||||
{
|
||||
}
|
||||
|
||||
PluginLoader& PluginLoader::operator=(PluginLoader&& o) noexcept
|
||||
{
|
||||
if (this != &o) {
|
||||
destroy_current();
|
||||
handle_ = std::exchange(o.handle_, nullptr);
|
||||
plugin_ = std::exchange(o.plugin_, nullptr);
|
||||
destroy_ = std::exchange(o.destroy_, nullptr);
|
||||
transfer_ = std::exchange(o.transfer_, nullptr);
|
||||
ready_ = std::exchange(o.ready_, nullptr);
|
||||
state_version_ = std::exchange(o.state_version_, nullptr);
|
||||
serialize_ = std::exchange(o.serialize_, nullptr);
|
||||
free_string_ = std::exchange(o.free_string_, nullptr);
|
||||
deserialize_ = std::exchange(o.deserialize_, nullptr);
|
||||
info_ = o.info_;
|
||||
path_ = std::move(o.path_);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
PluginLoader& PluginLoader::operator=(PluginLoader&& o) noexcept
|
||||
{
|
||||
if (this != &o) {
|
||||
destroy_current();
|
||||
handle_ = std::exchange(o.handle_, nullptr);
|
||||
plugin_ = std::exchange(o.plugin_, nullptr);
|
||||
destroy_ = std::exchange(o.destroy_, nullptr);
|
||||
transfer_ = std::exchange(o.transfer_, nullptr);
|
||||
ready_ = std::exchange(o.ready_, nullptr);
|
||||
state_version_ = std::exchange(o.state_version_, nullptr);
|
||||
serialize_ = std::exchange(o.serialize_, nullptr);
|
||||
free_string_ = std::exchange(o.free_string_, nullptr);
|
||||
deserialize_ = std::exchange(o.deserialize_, nullptr);
|
||||
info_ = o.info_;
|
||||
path_ = std::move(o.path_);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::expected<void, LoadError> PluginLoader::reload()
|
||||
{
|
||||
std::expected<void, LoadError> PluginLoader::reload()
|
||||
{
|
||||
#ifdef VOID_HAS_DLOPEN
|
||||
// Copy to a unique temp path so dlopen gets a fresh inode (bypasses the
|
||||
// path-based cache) even if the file was replaced in-place.
|
||||
const auto tmp = path_.parent_path()
|
||||
/ (path_.filename().string() + ".void_hotswap");
|
||||
// Copy to a unique temp path so dlopen gets a fresh inode (bypasses the
|
||||
// path-based cache) even if the file was replaced in-place.
|
||||
const auto tmp =
|
||||
path_.parent_path() / (path_.filename().string() + ".void_hotswap");
|
||||
|
||||
{
|
||||
std::error_code ec;
|
||||
std::filesystem::copy_file(path_, tmp,
|
||||
std::filesystem::copy_options::overwrite_existing, ec);
|
||||
if (ec) return std::unexpected{LoadError::FileNotFound};
|
||||
}
|
||||
{
|
||||
std::error_code ec;
|
||||
std::filesystem::copy_file(
|
||||
path_, tmp, std::filesystem::copy_options::overwrite_existing,
|
||||
ec);
|
||||
if (ec) {
|
||||
return std::unexpected{LoadError::FileNotFound};
|
||||
}
|
||||
}
|
||||
|
||||
// Load new instance while old is still alive (for state transfer).
|
||||
PluginLoader next;
|
||||
next.path_ = path_;
|
||||
if (auto r = next.load_from(tmp); !r) {
|
||||
std::error_code ec;
|
||||
std::filesystem::remove(tmp, ec);
|
||||
Void::Log::warn("Plugin", "reload '{}' failed: {}; keeping old",
|
||||
path_.string(), to_string(r.error()));
|
||||
return r;
|
||||
}
|
||||
// Load new instance while old is still alive (for state transfer).
|
||||
PluginLoader next;
|
||||
next.path_ = path_;
|
||||
if (auto r = next.load_from(tmp); !r) {
|
||||
std::error_code ec;
|
||||
std::filesystem::remove(tmp, ec);
|
||||
Void::Log::warn("Plugin", "reload '{}' failed: {}; keeping old",
|
||||
path_.string(), to_string(r.error()));
|
||||
return r;
|
||||
}
|
||||
|
||||
// Transfer state with both instances alive. A rejection aborts the
|
||||
// reload and keeps the old instance.
|
||||
if (!transfer_plugin_state(*this, next)) {
|
||||
std::error_code ec;
|
||||
std::filesystem::remove(tmp, ec);
|
||||
Void::Log::warn("Plugin", "reload '{}' aborted: state rejected",
|
||||
path_.string());
|
||||
return std::unexpected{LoadError::StateRejected};
|
||||
}
|
||||
// Transfer state with both instances alive. A rejection aborts the
|
||||
// reload and keeps the old instance.
|
||||
if (!transfer_plugin_state(*this, next)) {
|
||||
std::error_code ec;
|
||||
std::filesystem::remove(tmp, ec);
|
||||
Void::Log::warn("Plugin", "reload '{}' aborted: state rejected",
|
||||
path_.string());
|
||||
return std::unexpected{LoadError::StateRejected};
|
||||
}
|
||||
|
||||
try {
|
||||
next.plugin_->on_load();
|
||||
} catch (...) {
|
||||
std::error_code ec;
|
||||
std::filesystem::remove(tmp, ec);
|
||||
return std::unexpected{LoadError::ActivationFailed};
|
||||
}
|
||||
if (next.ready_ && next.ready_(next.plugin_) != 0) {
|
||||
std::error_code ec;
|
||||
std::filesystem::remove(tmp, ec);
|
||||
return std::unexpected{LoadError::ActivationFailed};
|
||||
}
|
||||
try {
|
||||
next.plugin_->on_load();
|
||||
} catch (...) {
|
||||
std::error_code ec;
|
||||
std::filesystem::remove(tmp, ec);
|
||||
return std::unexpected{LoadError::ActivationFailed};
|
||||
}
|
||||
if (next.ready_ && next.ready_(next.plugin_) != 0) {
|
||||
std::error_code ec;
|
||||
std::filesystem::remove(tmp, ec);
|
||||
return std::unexpected{LoadError::ActivationFailed};
|
||||
}
|
||||
|
||||
// Activation succeeded; only now destroy old and install new.
|
||||
destroy_current();
|
||||
*this = std::move(next);
|
||||
Void::Log::info("Plugin", "reloaded '{}' ({} v{})", path_.string(),
|
||||
info_.name, info_.plugin_version);
|
||||
// Activation succeeded; only now destroy old and install new.
|
||||
destroy_current();
|
||||
*this = std::move(next);
|
||||
Void::Log::info("Plugin", "reloaded '{}' ({} v{})", path_.string(),
|
||||
info_.name, info_.plugin_version);
|
||||
|
||||
std::error_code ec;
|
||||
std::filesystem::remove(tmp, ec);
|
||||
return {};
|
||||
std::error_code ec;
|
||||
std::filesystem::remove(tmp, ec);
|
||||
return {};
|
||||
#else
|
||||
return std::unexpected{LoadError::NotSupported};
|
||||
return std::unexpected{LoadError::NotSupported};
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Void::Plugin
|
||||
Reference in new issue
Block a user