From a06f639393d0b5fd577befab63e5026ed173b6dd Mon Sep 17 00:00:00 2001 From: faumaray Date: Sat, 11 Jul 2026 23:42:31 +0300 Subject: [PATCH] Plugin: copy_on_load option; unique hotswap temp names MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - copy_on_load: dlopen a fresh-inode temp copy on first load (reloads already did), so deployments that rewrite the plugin file in place — e.g. a build writing its output — cannot corrupt the mapped image. The registry keeps addressing the original path for watch/reload. - hotswap_copy now generates a unique temp name per copy. glibc dedupes dlopen by pathname, so the previous fixed ".void_hotswap" name made every second reload through the same path silently return the already-mapped OLD image instead of the new bytes. - hot_reload_under_concurrent_load now asserts the true downgrade semantics (v2 -> v1 is rejected transactionally, v2 stays); it only ever passed the old all-succeed expectation via the dedup bug, since no flip after the first actually took effect. Co-Authored-By: Claude Fable 5 Claude-Session: https://claude.ai/code/session_018yj92cCihyaYtf4gKCT3nv --- include/Void/Plugin/Manager.hpp | 6 ++++ src/Plugin/Manager.cpp | 29 ++++++++++++++++--- .../standalone/Plugin/HotReloadUnderLoad.cpp | 29 ++++++++++++++----- tests/standalone/Plugin/Manager.cpp | 28 ++++++++++++++++++ 4 files changed, 80 insertions(+), 12 deletions(-) diff --git a/include/Void/Plugin/Manager.hpp b/include/Void/Plugin/Manager.hpp index e132f7a..06d6b09 100644 --- a/include/Void/Plugin/Manager.hpp +++ b/include/Void/Plugin/Manager.hpp @@ -74,6 +74,12 @@ namespace Void::Plugin // executing would crash. Pins already prevent the instance from // being destroyed; this guards the code pages. bool dlclose_on_swap = false; + // dlopen a fresh-inode temp copy instead of the original file on + // the first load (reloads already do). Turn on when deployments + // overwrite the plugin file in place (e.g. a build writing its + // output) — mapping the original would then corrupt the running + // image (SIGBUS). The registry keeps tracking the original path. + bool copy_on_load = false; // Per-plugin configuration source. When set, its result is passed // to the plugin's optional void_plugin_configure entry point after // create and after each reload swap, before on_load(). The plugin diff --git a/src/Plugin/Manager.cpp b/src/Plugin/Manager.cpp index f7bcb9d..c97799a 100644 --- a/src/Plugin/Manager.cpp +++ b/src/Plugin/Manager.cpp @@ -8,7 +8,9 @@ #endif #include +#include #include +#include #include #include @@ -170,12 +172,18 @@ namespace Void::Plugin } // Copy to a fresh-inode temp path so dlopen does not return the - // already-mapped image when the file is replaced in place. + // already-mapped image when the file is replaced in place. The name + // must be unique per copy: glibc dedupes dlopen by pathname, so + // reusing a temp name would hand back the previously mapped (old) + // image no matter what bytes the file now holds. std::expected hotswap_copy(const std::filesystem::path& path) { - const auto tmp = path.parent_path() / - (path.filename().string() + ".void_hotswap"); + static std::atomic counter{0}; + const auto tmp = + path.parent_path() / + (path.filename().string() + ".void_hotswap." + + std::to_string(counter.fetch_add(1, std::memory_order_relaxed))); std::error_code ec; std::filesystem::copy_file( path, tmp, std::filesystem::copy_options::overwrite_existing, @@ -211,7 +219,20 @@ namespace Void::Plugin if (!std::filesystem::exists(path, ec)) { return std::unexpected{LoadError::FileNotFound}; } - auto inst = open_instance(path, m_options.dlclose_on_swap); + std::filesystem::path load_path = path; + if (m_options.copy_on_load) { + auto tmp = hotswap_copy(path); + if (!tmp.has_value()) { + return std::unexpected{tmp.error()}; + } + load_path = *tmp; + } + auto inst = open_instance(load_path, m_options.dlclose_on_swap); + if (m_options.copy_on_load) { + // The mapping keeps the unlinked inode alive; the registry + // keeps addressing the original path for watch/reload. + std::filesystem::remove(load_path, ec); + } if (!inst.has_value()) { Void::Log::warn("Plugin.Manager", "load '{}' failed: {}", path.string(), to_string(inst.error())); diff --git a/tests/standalone/Plugin/HotReloadUnderLoad.cpp b/tests/standalone/Plugin/HotReloadUnderLoad.cpp index 7807d32..4b45f03 100644 --- a/tests/standalone/Plugin/HotReloadUnderLoad.cpp +++ b/tests/standalone/Plugin/HotReloadUnderLoad.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -78,13 +79,23 @@ Void::Testing::Test hot_reload_under_concurrent_load() }); } - // Main thread flips the file v1<->v2 and reloads repeatedly. + // Main thread flips the file v1<->v2 and reloads repeatedly. The first + // v2 reload migrates v1 state forward; after that, installing v1 is a + // DOWNGRADE — v1 cannot deserialize v2 state, so the transactional + // reload must fail with StateRejected and keep the v2 instance. (An + // earlier version of this test expected every reload to succeed; that + // only passed because a fixed hotswap temp name made dlopen return the + // already-mapped image, so no flip after the first ever took effect.) for (int i = 0; i < 24; ++i) { - const char* src = - (i % 2 == 0) ? VOID_STATEFUL_V2_PATH : VOID_STATEFUL_V1_PATH; - install(src, so); + const bool to_v2 = (i % 2 == 0); + install(to_v2 ? VOID_STATEFUL_V2_PATH : VOID_STATEFUL_V1_PATH, so); const auto r = mgr.reload("StatefulPlugin"); - Void::Testing::expect(r.has_value()); + if (to_v2) { + Void::Testing::expect(r.has_value()); + } else { + Void::Testing::expect(!r.has_value()); + Void::Testing::expect(r.error() == LoadError::StateRejected); + } std::this_thread::sleep_for(std::chrono::milliseconds{2}); } @@ -97,12 +108,14 @@ Void::Testing::Test hot_reload_under_concurrent_load() Void::Testing::expect_eq(crashes.load(), 0); Void::Testing::expect(ops.load() > 0); - // State survived every migration: the final instance's counter is at - // least the number of reloads' worth of preserved bumps (monotonic, - // never reset to zero by a swap). + // State survived every migration: the counter is monotonic, never + // reset by a swap, and the surviving instance is the v2 build (every + // attempted downgrade was rejected transactionally). auto final = mgr.acquire("StatefulPlugin"); Void::Testing::expect(static_cast(final)); Void::Testing::expect(VoidTest::as_stateful(final.get())->counter() > 0); + Void::Testing::expect_eq( + VoidTest::as_stateful(final.get())->schema_version(), 2); } #endif // stateful plugins available diff --git a/tests/standalone/Plugin/Manager.cpp b/tests/standalone/Plugin/Manager.cpp index 7e929a0..fa7c0c0 100644 --- a/tests/standalone/Plugin/Manager.cpp +++ b/tests/standalone/Plugin/Manager.cpp @@ -215,6 +215,34 @@ Void::Testing::Test manager_unload_drops_entry_but_pinned_handle_survives() Void::Testing::expect_eq(stateful(pinned)->counter(), 1); } +Void::Testing::Test manager_copy_on_load_survives_in_place_overwrite() +{ + Void::FileSystem::TempDir tmp{"void_mgr"}; + const auto so = tmp.path() / "plugin.so"; + std::filesystem::copy_file(VOID_STATEFUL_V1_PATH, so); + + PluginManager mgr{{.copy_on_load = true}}; + Void::Testing::expect(mgr.load(so).has_value()); + + auto h = mgr.acquire("StatefulPlugin"); + stateful(h)->bump(); + + // Overwrite the original file IN PLACE (what a build does). With + // copy_on_load the mapped image is a private unlinked copy, so the + // running instance is unaffected. + std::filesystem::copy_file( + VOID_STATEFUL_V2_PATH, so, + std::filesystem::copy_options::overwrite_existing); + Void::Testing::expect_eq(stateful(h)->schema_version(), 1); + Void::Testing::expect_eq(stateful(h)->counter(), 1); + + // Reload still tracks the original path and picks up the new bytes. + Void::Testing::expect(mgr.reload("StatefulPlugin").has_value()); + auto fresh = mgr.acquire("StatefulPlugin"); + Void::Testing::expect_eq(stateful(fresh)->schema_version(), 2); + Void::Testing::expect_eq(stateful(fresh)->counter(), 1); +} + Void::Testing::Test manager_state_snapshot_round_trip() { PluginManager mgr;