From fa300981294cfa14ac06a6127bcc11f6e2c434fb Mon Sep 17 00:00:00 2001 From: faumaray Date: Sun, 12 Jul 2026 03:00:38 +0300 Subject: [PATCH] Update Signed-off-by: faumaray --- include/Void/Log/LoggingAdapter.hpp | 21 +++++++++++++++++++++ include/Void/Plugin/Manager.hpp | 3 +++ src/Plugin/Loader.cpp | 4 ++++ src/Plugin/Manager.cpp | 10 ++++++++++ tests/standalone/Plugin/Configure.cpp | 21 +++++++++++++++++++++ 5 files changed, 59 insertions(+) diff --git a/include/Void/Log/LoggingAdapter.hpp b/include/Void/Log/LoggingAdapter.hpp index 2612a8f..a2d746c 100644 --- a/include/Void/Log/LoggingAdapter.hpp +++ b/include/Void/Log/LoggingAdapter.hpp @@ -3,6 +3,8 @@ #include #include +#include + // Bridge between Void::Log (the thin emission layer used by all modules) and // Void::Logging::Logger (the full sink implementation). // @@ -69,4 +71,23 @@ namespace Void::Log }; } + /// Owning variant for process-global sinks. The logger remains alive until + /// this sink is replaced or the logging state is destroyed. + [[nodiscard]] inline Sink + make_sink(std::shared_ptr logger) + { + return [logger = std::move(logger)]( + Level level, std::string_view module, std::string_view message, + const std::source_location& location) { + if (!logger) { + return; + } + auto full = std::string{"["}; + full += module; + full += "] "; + full += message; + logger->log(to_logging_level(level), full, location); + }; + } + } // namespace Void::Log diff --git a/include/Void/Plugin/Manager.hpp b/include/Void/Plugin/Manager.hpp index 06d6b09..cb9ac42 100644 --- a/include/Void/Plugin/Manager.hpp +++ b/include/Void/Plugin/Manager.hpp @@ -86,6 +86,9 @@ namespace Void::Plugin // name argument is the registry name (IPlugin::name()). std::function config_provider; + // Host-specific contract checked after plugin activation and before + // publication. A failed validation aborts load/reload transactionally. + std::function instance_validator; }; // Identifies an on_reloaded observer for remove_on_reloaded. diff --git a/src/Plugin/Loader.cpp b/src/Plugin/Loader.cpp index 063e57e..e2765bc 100644 --- a/src/Plugin/Loader.cpp +++ b/src/Plugin/Loader.cpp @@ -243,9 +243,13 @@ std::expected PluginLoader::reload() 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}; } diff --git a/src/Plugin/Manager.cpp b/src/Plugin/Manager.cpp index 52da33d..58fb5a3 100644 --- a/src/Plugin/Manager.cpp +++ b/src/Plugin/Manager.cpp @@ -255,6 +255,10 @@ namespace Void::Plugin (*inst)->ready((*inst)->plugin) != 0) { return std::unexpected{LoadError::ActivationFailed}; } + if (m_options.instance_validator && + !m_options.instance_validator(*(*inst)->plugin, (*inst)->info)) { + return std::unexpected{LoadError::ActivationFailed}; + } std::lock_guard lock(m_mutex); auto entry = std::make_unique(); @@ -394,6 +398,12 @@ namespace Void::Plugin "reload '{}' aborted: activation failed", name); return std::unexpected{LoadError::ActivationFailed}; } + if (m_options.instance_validator && + !m_options.instance_validator(*(*next)->plugin, (*next)->info)) { + Void::Log::warn("Plugin.Manager", + "reload '{}' aborted: host validation failed", name); + return std::unexpected{LoadError::ActivationFailed}; + } // Atomic swap. The old instance keeps living while any acquired // handle still pins it; it is destroyed when the last one drops. entry.current.store(std::move(*next), std::memory_order_release); diff --git a/tests/standalone/Plugin/Configure.cpp b/tests/standalone/Plugin/Configure.cpp index 1196e6b..1642dd1 100644 --- a/tests/standalone/Plugin/Configure.cpp +++ b/tests/standalone/Plugin/Configure.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -92,4 +93,24 @@ Void::Testing::Test activation_failure_keeps_current_instance() std::string_view{"accepted"}); } +Void::Testing::Test host_validation_failure_keeps_current_instance() +{ + Void::FileSystem::TempDir tmp{"void_cfg_validator"}; + const auto so = tmp.path() / "plugin.so"; + std::filesystem::copy_file(VOID_CONFIGURABLE_PATH, so); + + bool accepted = true; + PluginManager mgr{{.instance_validator = [&accepted](IPlugin&, + const PluginInfo&) { return accepted; }}}; + Void::Testing::expect(mgr.load(so).has_value()); + auto before = mgr.acquire("ConfigurablePlugin"); + + accepted = false; + const auto result = mgr.reload("ConfigurablePlugin"); + Void::Testing::expect(!result.has_value()); + Void::Testing::expect(result.error() == LoadError::ActivationFailed); + auto after = mgr.acquire("ConfigurablePlugin"); + Void::Testing::expect(after.get() == before.get()); +} + #endif // configurable plugin available