feat(plugin): add CAbi.h v1, CModuleHost quiesce/reload/pins, and unlocked PluginManager staging

This commit is contained in:
faumaray committed 2026-11-26 16:16:29 +03:00
1 parent 1474a69ab1
commit a24e086e47
16 files changed
+3961 -164

No files matched your search

+184
View File
@@ -0,0 +1,184 @@
#ifndef VOID_PLUGIN_CABI_H
#define VOID_PLUGIN_CABI_H
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
#define VOID_CABI_MAJOR_V1 UINT32_C(1)
#define VOID_CABI_MINOR_V1 UINT32_C(0)
#define VOID_MODULE_QUERY_SYMBOL_V1 "void_module_query_v1"
#define VOID_CAP_STATELESS_RELOAD (UINT64_C(1) << 0)
#define VOID_CAP_STATEFUL_CHECKPOINT (UINT64_C(1) << 1)
#define VOID_CAP_ASYNC_OPS (UINT64_C(1) << 2)
#define VOID_CAP_SERVICE_INTERFACES (UINT64_C(1) << 3)
enum void_status_v1 {
VOID_STATUS_OK = 0,
VOID_STATUS_INVALID_ARGUMENT = 1,
VOID_STATUS_NOT_FOUND = 2,
VOID_STATUS_CONFLICT = 3,
VOID_STATUS_CANCELLED = 4,
VOID_STATUS_TIMEOUT = 5,
VOID_STATUS_OVERLOADED = 6,
VOID_STATUS_UNSUPPORTED = 7,
VOID_STATUS_CORRUPT = 8,
VOID_STATUS_EXHAUSTED = 9,
VOID_STATUS_INTERNAL = 10,
VOID_STATUS_NOT_READY = 11,
VOID_STATUS_QUIESCING = 12,
VOID_STATUS_STATE_REJECTED = 13,
VOID_STATUS_RESTART_REQUIRED = 14
};
enum void_activation_policy_v1 {
VOID_POLICY_RELOADABLE = 0,
VOID_POLICY_RESTART_REQUIRED = 1,
VOID_POLICY_INSTALLER_REQUIRED = 2
};
typedef struct void_module_handle void_module_handle;
typedef struct void_operation_handle void_operation_handle;
typedef struct void_host_context void_host_context;
typedef struct void_bytes_v1 {
const uint8_t* data;
uint64_t size;
} void_bytes_v1;
typedef struct void_owned_bytes_v1 {
const uint8_t* data;
uint64_t size;
void (*release)(void* release_ctx, const uint8_t* data, uint64_t size);
void* release_ctx;
} void_owned_bytes_v1;
typedef struct void_interface_id_v1 {
uint8_t bytes[16];
} void_interface_id_v1;
typedef struct void_dependency_v1 {
const char* module_id;
const char* version_range;
uint8_t optional_dep;
uint8_t reserved[7];
} void_dependency_v1;
typedef struct void_module_info_v1 {
uint32_t struct_size;
uint32_t reserved;
const char* module_id;
const char* version;
uint32_t activation_policy;
uint32_t state_schema_version;
const void_dependency_v1* dependencies;
uint64_t dependency_count;
} void_module_info_v1;
typedef struct void_op_options_v1 {
uint32_t struct_size;
uint32_t flags;
uint64_t deadline_ns;
uint8_t stop_requested;
uint8_t reserved[7];
} void_op_options_v1;
typedef struct void_error_detail_v1 {
uint32_t struct_size;
int32_t status;
const char* message;
void (*release)(void* release_ctx, const char* message);
void* release_ctx;
} void_error_detail_v1;
typedef struct void_interface_view_v1 {
uint32_t struct_size;
uint32_t major;
uint32_t minor;
uint32_t table_size;
const void* function_table;
} void_interface_view_v1;
typedef struct void_host_api_v1 {
uint32_t struct_size;
uint32_t abi_major;
uint32_t abi_minor;
uint32_t reserved;
void_host_context* host_ctx;
uint64_t generation;
uint64_t (*monotonic_now_ns)(void_host_context* ctx);
int32_t (*acquire_activity_pin)(void_host_context* ctx, uint64_t generation);
void (*release_activity_pin)(void_host_context* ctx, uint64_t generation);
void (*log_message)(void_host_context* ctx, int32_t level, const char* msg);
} void_host_api_v1;
typedef void (*void_async_complete_fn)(void* user_ctx,
int32_t status,
const void_bytes_v1* result_bytes);
typedef struct void_module_api_v1 {
uint32_t struct_size;
uint32_t abi_major;
uint32_t abi_minor;
uint32_t reserved;
uint64_t capabilities;
int32_t (*get_info)(void_module_info_v1* out_info);
int32_t (*create)(const void_host_api_v1* host_api,
void_module_handle** out_handle);
int32_t (*activate)(void_module_handle* handle,
void_bytes_v1 config,
void_error_detail_v1* out_err);
int32_t (*readiness)(void_module_handle* handle,
uint32_t* out_ready,
void_error_detail_v1* out_err);
int32_t (*quiesce)(void_module_handle* handle,
const void_op_options_v1* opts,
void_error_detail_v1* out_err);
int32_t (*resume)(void_module_handle* handle, void_error_detail_v1* out_err);
int32_t (*checkpoint)(void_module_handle* handle,
const void_op_options_v1* opts,
void_owned_bytes_v1* out_state,
void_error_detail_v1* out_err);
int32_t (*restore)(void_module_handle* handle,
void_bytes_v1 state,
void_error_detail_v1* out_err);
int32_t (*query_interface)(void_module_handle* handle,
const void_interface_id_v1* iface_id,
uint32_t min_major,
uint32_t min_table_size,
void_interface_view_v1* out_view);
void (*release_interface)(void_module_handle* handle,
const void_interface_id_v1* iface_id,
const void_interface_view_v1* view);
int32_t (*submit_async)(void_module_handle* handle,
uint32_t op_kind,
void_bytes_v1 payload,
const void_op_options_v1* opts,
void_async_complete_fn cb,
void* cb_ctx,
void_operation_handle** out_op,
void_error_detail_v1* out_err);
int32_t (*cancel_async)(void_module_handle* handle,
void_operation_handle* op);
void (*release_async)(void_module_handle* handle,
void_operation_handle* op);
int32_t (*request_stop)(void_module_handle* handle);
int32_t (*deactivate)(void_module_handle* handle);
void (*destroy)(void_module_handle* handle);
} void_module_api_v1;
#define VOID_MODULE_API_V1_MIN_SIZE sizeof(void_module_api_v1)
typedef int32_t (*void_module_query_v1_fn)(uint32_t host_struct_size,
void_module_api_v1* out_api);
#ifdef __cplusplus
}
#endif
#endif /* VOID_PLUGIN_CABI_H */
+165
View File
@@ -0,0 +1,165 @@
#pragma once
#include <Void/Data/Json.hpp>
#include <Void/Plugin/CAbi.h>
#include <Void/Plugin/Module.hpp>
#include <Void/Time/Clock.hpp>
#include <cstdint>
#include <cstring>
#include <exception>
#include <memory>
#include <string>
#include <utility>
namespace Void::Plugin
{
namespace detail
{
inline void free_cabi_cstring(void* /*ctx*/, const char* msg)
{
delete[] msg;
}
inline void set_error_detail(void_error_detail_v1* out_err,
std::int32_t status,
const std::string& msg) noexcept
{
if (out_err == nullptr ||
out_err->struct_size < sizeof(void_error_detail_v1)) {
return;
}
out_err->status = status;
try {
char* buf = new char[msg.size() + 1];
std::memcpy(buf, msg.c_str(), msg.size() + 1);
out_err->message = buf;
out_err->release = &free_cabi_cstring;
out_err->release_ctx = nullptr;
} catch (...) {
out_err->message = nullptr;
out_err->release = nullptr;
out_err->release_ctx = nullptr;
}
}
inline void free_cabi_bytes(void* /*ctx*/, const std::uint8_t* data,
std::uint64_t /*size*/)
{
delete[] data;
}
} // namespace detail
/// Exception-safe bridge adapting a C++ IModule instance to the C11
/// void_module_api_v1 contract without letting C++ exceptions unwind across C.
struct CModuleAdapterBox {
std::unique_ptr<IModule> module;
Service::ServiceRegistry fallback_registry;
ModuleHost host{fallback_registry};
const Void::Time::MonotonicClock* clock{
&Void::Time::SystemMonotonicClock::instance()};
};
template <typename CallbackFn>
[[nodiscard]] inline std::int32_t
invoke_contained(void_error_detail_v1* out_err, CallbackFn&& fn) noexcept
{
try {
return fn();
} catch (const std::exception& ex) {
detail::set_error_detail(out_err, VOID_STATUS_INTERNAL, ex.what());
return VOID_STATUS_INTERNAL;
} catch (...) {
detail::set_error_detail(out_err, VOID_STATUS_INTERNAL,
"CAbiAdapter: unknown C++ exception");
return VOID_STATUS_INTERNAL;
}
}
[[nodiscard]] inline std::int32_t
adapt_checkpoint(CModuleAdapterBox* box,
void_owned_bytes_v1* out_state,
void_error_detail_v1* out_err) noexcept
{
if (box == nullptr || !box->module || out_state == nullptr) {
return VOID_STATUS_INVALID_ARGUMENT;
}
return invoke_contained(out_err, [&]() -> std::int32_t {
const auto json = Void::Json::serialize(box->module->checkpoint());
auto* buf = new std::uint8_t[json.size()];
if (!json.empty()) {
std::memcpy(buf, json.data(), json.size());
}
out_state->data = buf;
out_state->size = static_cast<std::uint64_t>(json.size());
out_state->release = &detail::free_cabi_bytes;
out_state->release_ctx = nullptr;
return VOID_STATUS_OK;
});
}
[[nodiscard]] inline std::int32_t
adapt_restore(CModuleAdapterBox* box, void_bytes_v1 state,
void_error_detail_v1* out_err) noexcept
{
if (box == nullptr || !box->module) {
return VOID_STATUS_INVALID_ARGUMENT;
}
return invoke_contained(out_err, [&]() -> std::int32_t {
const std::string_view sv{
reinterpret_cast<const char*>(state.data),
static_cast<std::size_t>(state.size)};
const auto parsed = Void::Json::parse(sv);
if (!parsed.has_value()) {
detail::set_error_detail(out_err, VOID_STATUS_CORRUPT,
parsed.error().message);
return VOID_STATUS_CORRUPT;
}
const auto res = box->module->restore(*parsed);
if (!res.has_value()) {
detail::set_error_detail(out_err, VOID_STATUS_STATE_REJECTED,
res.error().message());
return VOID_STATUS_STATE_REJECTED;
}
return VOID_STATUS_OK;
});
}
[[nodiscard]] inline std::int32_t
adapt_activate(CModuleAdapterBox* box,
void_error_detail_v1* out_err) noexcept
{
if (box == nullptr || !box->module) {
return VOID_STATUS_INVALID_ARGUMENT;
}
return invoke_contained(out_err, [&]() -> std::int32_t {
const auto r = box->module->activate(box->host);
if (!r.has_value()) {
detail::set_error_detail(out_err, VOID_STATUS_INTERNAL,
r.error().message());
return VOID_STATUS_INTERNAL;
}
return VOID_STATUS_OK;
});
}
[[nodiscard]] inline std::int32_t
adapt_readiness(CModuleAdapterBox* box, std::uint32_t* out_ready,
void_error_detail_v1* out_err) noexcept
{
if (box == nullptr || !box->module || out_ready == nullptr) {
return VOID_STATUS_INVALID_ARGUMENT;
}
*out_ready = 0;
return invoke_contained(out_err, [&]() -> std::int32_t {
const auto r = box->module->readiness();
*out_ready = r.ready ? 1u : 0u;
if (!r.ready && !r.detail.empty()) {
detail::set_error_detail(out_err, VOID_STATUS_NOT_READY,
r.detail);
}
return VOID_STATUS_OK;
});
}
} // namespace Void::Plugin
+350
View File
@@ -0,0 +1,350 @@
#pragma once
#include <Void/Async/Operation.hpp>
#include <Void/Functional/Error.hpp>
#include <Void/Functional/Result.hpp>
#include <Void/Plugin/CAbi.h>
#include <Void/Time/Clock.hpp>
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <filesystem>
#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
namespace Void::Plugin
{
enum class PluginErrorCode : std::int32_t {
none = 0,
invalid_argument = 1,
file_not_found = 2,
dlopen_failed = 3,
symbol_missing = 4,
abi_mismatch = 5,
activation_failed = 6,
not_ready = 7,
quiesce_failed = 8,
resume_degraded = 9,
checkpoint_failed = 10,
restore_failed = 11,
restart_required = 12,
cycle_detected = 13,
missing_dependency = 14,
retention_limit = 15,
budget_exceeded = 16,
conflict = 17,
cancelled = 18,
timeout = 19,
not_found = 20,
internal = 21,
};
[[nodiscard]] inline Void::Result::Error
make_plugin_error(PluginErrorCode code, std::string message = {})
{
using Void::Result::Error;
using Void::Result::ErrorCode;
using Void::Result::ErrorKind;
ErrorKind kind = ErrorKind::internal;
switch (code) {
case PluginErrorCode::none:
kind = ErrorKind::internal;
break;
case PluginErrorCode::invalid_argument:
case PluginErrorCode::abi_mismatch:
case PluginErrorCode::cycle_detected:
kind = ErrorKind::invalid_argument;
break;
case PluginErrorCode::file_not_found:
case PluginErrorCode::symbol_missing:
case PluginErrorCode::missing_dependency:
case PluginErrorCode::not_found:
kind = ErrorKind::not_found;
break;
case PluginErrorCode::dlopen_failed:
kind = ErrorKind::io;
break;
case PluginErrorCode::conflict:
case PluginErrorCode::restart_required:
kind = ErrorKind::conflict;
break;
case PluginErrorCode::cancelled:
kind = ErrorKind::cancelled;
break;
case PluginErrorCode::timeout:
kind = ErrorKind::timeout;
break;
case PluginErrorCode::retention_limit:
case PluginErrorCode::budget_exceeded:
kind = ErrorKind::exhausted;
break;
case PluginErrorCode::activation_failed:
case PluginErrorCode::not_ready:
case PluginErrorCode::quiesce_failed:
case PluginErrorCode::resume_degraded:
case PluginErrorCode::checkpoint_failed:
case PluginErrorCode::restore_failed:
case PluginErrorCode::internal:
kind = ErrorKind::internal;
break;
}
return Error{kind, std::move(message),
ErrorCode{Void::Result::kPluginErrorDomain,
static_cast<std::int32_t>(code)}};
}
struct PluginHostLimits {
std::size_t max_modules{64};
std::size_t max_interfaces_per_module{256};
std::size_t max_retained_generations{4};
std::size_t max_mapped_bytes_per_module{64 * 1024 * 1024};
std::size_t max_checkpoint_bytes{4 * 1024 * 1024};
std::size_t max_config_bytes{1 * 1024 * 1024};
std::size_t max_async_activities{1024};
std::size_t max_pending_callbacks{256};
std::chrono::milliseconds prepare_timeout{
std::chrono::seconds{5}};
std::chrono::milliseconds quiesce_timeout{
std::chrono::seconds{5}};
std::chrono::milliseconds readiness_timeout{
std::chrono::seconds{10}};
std::chrono::milliseconds total_reload_timeout{
std::chrono::seconds{30}};
bool allow_safe_dlclose{false};
};
enum class GenerationHealth : std::uint8_t {
Healthy = 0,
Quiescing,
Degraded,
Retiring,
Retired,
};
struct GenerationSummary {
std::uint64_t generation{0};
std::string module_id{};
std::string version{};
std::uint64_t image_bytes{0};
std::size_t external_refs{0};
std::size_t activity_pins{0};
GenerationHealth health{GenerationHealth::Healthy};
bool dlclosed{false};
};
struct ReloadEvent {
std::string module_id{};
std::uint64_t generation{0};
bool rollback{false};
std::string detail{};
};
struct BatchModuleSpec {
std::filesystem::path path{};
std::string config{};
};
namespace detail
{
struct GenerationRecord;
}
/// RAII pin holding an async activity count on a generation. Destroying
/// it may schedule asynchronous host retirement if no refs/pins remain.
class ActivityPin
{
public:
ActivityPin() noexcept = default;
~ActivityPin();
ActivityPin(ActivityPin&& other) noexcept;
ActivityPin& operator=(ActivityPin&& other) noexcept;
ActivityPin(const ActivityPin&) = delete;
ActivityPin& operator=(const ActivityPin&) = delete;
[[nodiscard]] explicit operator bool() const noexcept
{
return m_rec != nullptr;
}
[[nodiscard]] std::uint64_t generation() const noexcept;
private:
friend class CModuleHost;
friend class GenerationLease;
explicit ActivityPin(std::shared_ptr<detail::GenerationRecord> rec)
: m_rec(std::move(rec))
{
}
std::shared_ptr<detail::GenerationRecord> m_rec{};
};
/// Owning lease to a queried C interface function table from an exact
/// generation. Releases via release_interface before dropping generation ref.
class InterfaceLease
{
public:
InterfaceLease() noexcept = default;
~InterfaceLease();
InterfaceLease(InterfaceLease&& other) noexcept;
InterfaceLease& operator=(InterfaceLease&& other) noexcept;
InterfaceLease(const InterfaceLease&) = delete;
InterfaceLease& operator=(const InterfaceLease&) = delete;
[[nodiscard]] explicit operator bool() const noexcept
{
return m_view.function_table != nullptr;
}
template <typename TableT>
[[nodiscard]] const TableT* table() const noexcept
{
if (m_view.function_table == nullptr ||
m_view.table_size < sizeof(TableT)) {
return nullptr;
}
return static_cast<const TableT*>(m_view.function_table);
}
[[nodiscard]] const void_interface_view_v1& view() const noexcept
{
return m_view;
}
[[nodiscard]] std::uint64_t generation() const noexcept;
private:
friend class GenerationLease;
InterfaceLease(std::shared_ptr<detail::GenerationRecord> rec,
void_interface_id_v1 id,
void_interface_view_v1 view) noexcept
: m_rec(std::move(rec)), m_id(id), m_view(view)
{
}
std::shared_ptr<detail::GenerationRecord> m_rec{};
void_interface_id_v1 m_id{};
void_interface_view_v1 m_view{};
};
/// Owning generation reference acquired from CModuleHost. Prevents module
/// deactivation and dlclose while held, and supports activity pinning,
/// C interface querying, and bounded async operation submission.
class GenerationLease
{
public:
GenerationLease() noexcept = default;
~GenerationLease();
GenerationLease(const GenerationLease& other);
GenerationLease& operator=(const GenerationLease& other);
GenerationLease(GenerationLease&& other) noexcept;
GenerationLease& operator=(GenerationLease&& other) noexcept;
[[nodiscard]] explicit operator bool() const noexcept
{
return m_rec != nullptr;
}
[[nodiscard]] std::uint64_t generation() const noexcept;
[[nodiscard]] std::string_view module_id() const noexcept;
[[nodiscard]] std::string_view version() const noexcept;
[[nodiscard]] GenerationHealth health() const noexcept;
[[nodiscard]] ActivityPin acquire_activity_pin() const;
[[nodiscard]] Void::Result::Result<InterfaceLease>
query_interface(const void_interface_id_v1& id,
std::uint32_t min_major,
std::uint32_t min_table_size) const;
[[nodiscard]] Void::Result::Result<std::vector<std::uint8_t>>
run_async(std::uint32_t op_kind,
const std::vector<std::uint8_t>& payload,
Void::Async::OperationOptions options = {}) const;
private:
friend class CModuleHost;
struct AlreadyRefCountedTag {};
explicit GenerationLease(
std::shared_ptr<detail::GenerationRecord> rec);
GenerationLease(std::shared_ptr<detail::GenerationRecord> rec,
AlreadyRefCountedTag) noexcept
: m_rec(std::move(rec))
{
}
std::shared_ptr<detail::GenerationRecord> m_rec{};
};
/// Hardened C-ABI module host implementing size/version negotiation,
/// independent external_refs & activity_pins, host-thread retirement queue,
/// unlocked quiesce/checkpoint/restore/readiness/resume reload, and
/// dependency-ordered batch loading.
class CModuleHost
{
public:
struct Impl;
explicit CModuleHost(
PluginHostLimits limits = {},
const Void::Time::MonotonicClock& clock =
Void::Time::SystemMonotonicClock::instance());
~CModuleHost();
CModuleHost(const CModuleHost&) = delete;
CModuleHost& operator=(const CModuleHost&) = delete;
[[nodiscard]] Void::Result::Result<std::string>
load(const std::filesystem::path& path,
std::string_view config = {},
Void::Async::OperationOptions options = {});
[[nodiscard]] Void::Result::Result<std::vector<std::string>>
load_batch(const std::vector<BatchModuleSpec>& specs,
Void::Async::OperationOptions options = {});
[[nodiscard]] Void::Result::Result<std::uint64_t>
replace(std::string_view module_id,
const std::filesystem::path& new_path,
std::string_view config = {},
std::optional<std::uint64_t> expected_generation =
std::nullopt,
Void::Async::OperationOptions options = {},
std::function<Void::Result::Result<void>(
const GenerationLease&)>
on_publish_locked = {});
[[nodiscard]] Void::Result::Result<void>
unload(std::string_view module_id);
[[nodiscard]] GenerationLease
acquire(std::string_view module_id) const;
[[nodiscard]] std::vector<GenerationSummary>
retained_generations(std::string_view module_id) const;
[[nodiscard]] std::size_t
mapped_bytes(std::string_view module_id) const;
std::size_t add_observer(std::function<void(const ReloadEvent&)> cb);
void remove_observer(std::size_t id);
/// Drain retirement queue and shutdown background retirement worker.
void close_and_join();
private:
std::shared_ptr<Impl> m_impl;
};
} // namespace Void::Plugin
+7 -5
View File
@@ -215,18 +215,20 @@ namespace Void::Plugin
std::filesystem::path path;
std::atomic<std::shared_ptr<detail::Instance>> current;
bool watched{false};
std::mutex reload_mutex;
};
Entry* find(std::string_view name) const;
std::expected<void, LoadError>
reload_locked(Entry& entry, std::string_view name,
const std::filesystem::path& sourcePath);
std::shared_ptr<Entry> find_shared(std::string_view name) const;
std::expected<void, LoadError> reload_entry_unlocked(
const std::shared_ptr<Entry>& entry, std::string_view name,
const std::optional<std::filesystem::path>& newPath);
std::vector<std::function<void(std::string_view)>>
reload_callbacks_locked() const;
Options m_options;
mutable std::mutex m_mutex; // guards the registry map + reloads
std::unordered_map<std::string, std::unique_ptr<Entry>> m_plugins;
mutable std::mutex m_mutex; // guards the registry map & observer list
std::unordered_map<std::string, std::shared_ptr<Entry>> m_plugins;
std::vector<std::pair<ReloadCallbackId,
std::function<void(std::string_view)>>>
m_on_reloaded;