From f94366104e2be8d8ed51de1dce60fb4492463bd9 Mon Sep 17 00:00:00 2001 From: faumaray Date: Sun, 12 Jul 2026 02:37:49 +0300 Subject: [PATCH] Update Signed-off-by: faumaray --- cmake/Module.cmake | 16 +++++++++------- include/Void/Plugin/Loader.hpp | 2 ++ include/Void/Plugin/Plugin.hpp | 8 ++++++++ src/Plugin/Loader.cpp | 27 +++++++++++++++++++++++---- src/Plugin/Manager.cpp | 25 +++++++++++++++++++++++-- tests/plugins/ConfigurablePlugin.cpp | 10 ++++++++++ tests/standalone/Plugin/Configure.cpp | 25 +++++++++++++++++++++++++ 7 files changed, 100 insertions(+), 13 deletions(-) diff --git a/cmake/Module.cmake b/cmake/Module.cmake index ab1c96e..0a8081a 100644 --- a/cmake/Module.cmake +++ b/cmake/Module.cmake @@ -23,13 +23,15 @@ function(void_add_module MODULE_NAME) add_library(${TGT} INTERFACE) target_compile_features(${TGT} INTERFACE cxx_std_26) else() - set(LIB_TYPE "SHARED") - set(SCOPE "PUBLIC") - if (MODULE_NAME STREQUAL "Testing") - add_library(${TGT} STATIC) - else() - add_library(${TGT} SHARED) - endif() + set(SCOPE "PUBLIC") + if (MODULE_NAME STREQUAL "Testing") + add_library(${TGT} STATIC) + else() + # Honour the standard BUILD_SHARED_LIBS policy. Consumers that ship a + # single validated executable must be able to embed Void modules, + # while Void's standalone default remains shared. + add_library(${TGT}) + endif() target_sources(${TGT} PRIVATE ${SRC}) target_compile_features(${TGT} PUBLIC cxx_std_26) endif() diff --git a/include/Void/Plugin/Loader.hpp b/include/Void/Plugin/Loader.hpp index 4d0d894..77e4806 100644 --- a/include/Void/Plugin/Loader.hpp +++ b/include/Void/Plugin/Loader.hpp @@ -17,6 +17,7 @@ enum class LoadError { CreateFailed, NotSupported, // platform does not support dlopen StateRejected, // new instance refused the serialized state + ActivationFailed, // on_load threw or readiness validation failed UnknownPlugin, // PluginManager: no plugin registered under that name }; @@ -72,6 +73,7 @@ private: IPlugin* plugin_ {}; destroy_fn destroy_ {}; transfer_fn transfer_ {}; + ready_fn ready_ {}; state_version_fn state_version_{}; serialize_fn serialize_ {}; free_string_fn free_string_ {}; diff --git a/include/Void/Plugin/Plugin.hpp b/include/Void/Plugin/Plugin.hpp index b74aa54..f401848 100644 --- a/include/Void/Plugin/Plugin.hpp +++ b/include/Void/Plugin/Plugin.hpp @@ -55,6 +55,12 @@ public: // JSON) after create and after each hot-reload swap, before // on_load(). The string is only valid for the duration of the call. // +// Optional (transactional activation check): +// int void_plugin_ready(void* instance) +// — called after on_load() and state restoration, immediately before +// the instance becomes visible. Return 0 when the plugin is ready. +// Any other value aborts load/reload and keeps the old instance. +// // Optional v2 state protocol (preferred over transfer_state when both // sides provide it; survives layout changes between plugin versions): // @@ -74,6 +80,7 @@ using destroy_fn = void (*)(void*); using info_fn = PluginInfo (*)(void); using transfer_fn = void (*)(void* old_instance, void* new_instance); using configure_fn = void (*)(void* instance, const char* config); +using ready_fn = int (*)(void* instance); using state_version_fn = std::uint32_t (*)(void); using serialize_fn = const char* (*)(void* instance); using free_string_fn = void (*)(const char*); @@ -85,6 +92,7 @@ inline constexpr const char* kDestroySymbol = "void_plugin_destroy"; inline constexpr const char* kInfoSymbol = "void_plugin_info"; inline constexpr const char* kTransferSymbol = "void_plugin_transfer_state"; inline constexpr const char* kConfigureSymbol = "void_plugin_configure"; +inline constexpr const char* kReadySymbol = "void_plugin_ready"; inline constexpr const char* kStateVersionSymbol = "void_plugin_state_version"; inline constexpr const char* kSerializeSymbol = "void_plugin_serialize_state"; inline constexpr const char* kFreeStringSymbol = "void_plugin_free_string"; diff --git a/src/Plugin/Loader.cpp b/src/Plugin/Loader.cpp index ad6c851..063e57e 100644 --- a/src/Plugin/Loader.cpp +++ b/src/Plugin/Loader.cpp @@ -23,6 +23,7 @@ std::string_view to_string(LoadError e) noexcept 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"; @@ -44,6 +45,7 @@ void PluginLoader::destroy_current() noexcept } destroy_ = nullptr; transfer_ = nullptr; + ready_ = nullptr; state_version_ = nullptr; serialize_ = nullptr; free_string_ = nullptr; @@ -121,6 +123,7 @@ PluginLoader::load_from(const std::filesystem::path& path) plugin_ = static_cast(raw); destroy_ = sym_destroy; transfer_ = reinterpret_cast(dlsym(h, kTransferSymbol)); + ready_ = reinterpret_cast(dlsym(h, kReadySymbol)); state_version_ = reinterpret_cast( dlsym(h, kStateVersionSymbol)); serialize_ = reinterpret_cast(dlsym(h, kSerializeSymbol)); @@ -153,7 +156,14 @@ PluginLoader::load(std::filesystem::path path) return std::unexpected{r.error()}; } - ldr.plugin_->on_load(); + 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; @@ -166,6 +176,7 @@ PluginLoader::PluginLoader(PluginLoader&& o) noexcept , 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)} @@ -182,6 +193,7 @@ PluginLoader& PluginLoader::operator=(PluginLoader&& o) noexcept 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); @@ -228,11 +240,18 @@ std::expected PluginLoader::reload() return std::unexpected{LoadError::StateRejected}; } - // Destroy old, install new. + try { + next.plugin_->on_load(); + } catch (...) { + return std::unexpected{LoadError::ActivationFailed}; + } + if (next.ready_ && next.ready_(next.plugin_) != 0) { + return std::unexpected{LoadError::ActivationFailed}; + } + + // Activation succeeded; only now destroy old and install new. destroy_current(); *this = std::move(next); - - plugin_->on_load(); Void::Log::info("Plugin", "reloaded '{}' ({} v{})", path_.string(), info_.name, info_.plugin_version); diff --git a/src/Plugin/Manager.cpp b/src/Plugin/Manager.cpp index c97799a..52da33d 100644 --- a/src/Plugin/Manager.cpp +++ b/src/Plugin/Manager.cpp @@ -25,6 +25,7 @@ namespace Void::Plugin destroy_fn destroy{nullptr}; transfer_fn transfer{nullptr}; configure_fn configure{nullptr}; + ready_fn ready{nullptr}; state_version_fn state_version{nullptr}; serialize_fn serialize{nullptr}; free_string_fn free_string{nullptr}; @@ -121,6 +122,8 @@ namespace Void::Plugin reinterpret_cast(dlsym(h, kTransferSymbol)); inst->configure = reinterpret_cast(dlsym(h, kConfigureSymbol)); + inst->ready = + reinterpret_cast(dlsym(h, kReadySymbol)); inst->state_version = reinterpret_cast( dlsym(h, kStateVersionSymbol)); inst->serialize = @@ -243,7 +246,15 @@ namespace Void::Plugin const std::string config = m_options.config_provider(name); (*inst)->configure((*inst)->plugin, config.c_str()); } - (*inst)->plugin->on_load(); + try { + (*inst)->plugin->on_load(); + } catch (...) { + return std::unexpected{LoadError::ActivationFailed}; + } + if ((*inst)->ready != nullptr && + (*inst)->ready((*inst)->plugin) != 0) { + return std::unexpected{LoadError::ActivationFailed}; + } std::lock_guard lock(m_mutex); auto entry = std::make_unique(); @@ -372,7 +383,17 @@ namespace Void::Plugin } } - (*next)->plugin->on_load(); + try { + (*next)->plugin->on_load(); + } catch (...) { + return std::unexpected{LoadError::ActivationFailed}; + } + if ((*next)->ready != nullptr && + (*next)->ready((*next)->plugin) != 0) { + Void::Log::warn("Plugin.Manager", + "reload '{}' aborted: activation 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/plugins/ConfigurablePlugin.cpp b/tests/plugins/ConfigurablePlugin.cpp index 8140130..ea997e8 100644 --- a/tests/plugins/ConfigurablePlugin.cpp +++ b/tests/plugins/ConfigurablePlugin.cpp @@ -44,6 +44,11 @@ namespace } } + [[nodiscard]] bool ready() const noexcept + { + return config_ != "reject"; + } + private: std::string config_; @@ -75,4 +80,9 @@ void void_plugin_configure(void* instance, const char* config) { static_cast(instance)->apply_config(config); } + +int void_plugin_ready(void* instance) +{ + return static_cast(instance)->ready() ? 0 : -1; +} } diff --git a/tests/standalone/Plugin/Configure.cpp b/tests/standalone/Plugin/Configure.cpp index 9aacaaf..1196e6b 100644 --- a/tests/standalone/Plugin/Configure.cpp +++ b/tests/standalone/Plugin/Configure.cpp @@ -67,4 +67,29 @@ Void::Testing::Test configure_skipped_without_provider() Void::Testing::expect_eq(configurable(h)->configure_calls(), 0); } +Void::Testing::Test activation_failure_keeps_current_instance() +{ + Void::FileSystem::TempDir tmp{"void_cfg_ready"}; + const auto so = tmp.path() / "plugin.so"; + std::filesystem::copy_file(VOID_CONFIGURABLE_PATH, so); + + std::string payload{"accepted"}; + PluginManager mgr{{.config_provider = [&payload](std::string_view) { + return payload; + }}}; + Void::Testing::expect(mgr.load(so).has_value()); + auto before = mgr.acquire("ConfigurablePlugin"); + Void::Testing::expect_eq(configurable(before)->last_config(), + std::string_view{"accepted"}); + + payload = "reject"; + 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()); + Void::Testing::expect_eq(configurable(after)->last_config(), + std::string_view{"accepted"}); +} + #endif // configurable plugin available