feat(plugin): add CAbi.h v1, CModuleHost quiesce/reload/pins, and unlocked PluginManager staging
This commit is contained in:
1 parent
1474a69ab1
commit
a24e086e47
16 files changed
+3961
-164
No files matched your search
@@ -252,4 +252,56 @@ if(TARGET VoidTests AND TARGET Void::Plugin)
|
||||
)
|
||||
add_dependencies(VoidTests VoidServicePluginV1 VoidServicePluginV2)
|
||||
endif()
|
||||
|
||||
enable_language(C)
|
||||
function(_void_add_cabi_fixture target_name source_file)
|
||||
add_library(${target_name} SHARED "${CMAKE_CURRENT_SOURCE_DIR}/plugins/${source_file}")
|
||||
target_include_directories(${target_name} PRIVATE "${CMAKE_SOURCE_DIR}/include")
|
||||
set_target_properties(${target_name} PROPERTIES
|
||||
C_STANDARD 11
|
||||
C_STANDARD_REQUIRED ON
|
||||
C_EXTENSIONS OFF
|
||||
VOID_TESTING_NO_AUTO_SCAN TRUE)
|
||||
if(VOID_WARNINGS_AS_ERRORS)
|
||||
target_compile_options(${target_name} PRIVATE -Wall -Wextra -Wpedantic -Werror)
|
||||
endif()
|
||||
if(VOID_ENABLE_ASAN)
|
||||
target_compile_options(${target_name} PRIVATE -fsanitize=address -fno-omit-frame-pointer)
|
||||
target_link_options(${target_name} PRIVATE -fsanitize=address)
|
||||
endif()
|
||||
if(VOID_ENABLE_UBSAN)
|
||||
target_compile_options(${target_name} PRIVATE -fsanitize=undefined)
|
||||
target_link_options(${target_name} PRIVATE -fsanitize=undefined)
|
||||
endif()
|
||||
if(VOID_ENABLE_TSAN)
|
||||
target_compile_options(${target_name} PRIVATE -fsanitize=thread)
|
||||
target_link_options(${target_name} PRIVATE -fsanitize=thread)
|
||||
endif()
|
||||
foreach(_def IN LISTS ARGN)
|
||||
target_compile_definitions(${target_name} PRIVATE "${_def}")
|
||||
endforeach()
|
||||
add_dependencies(VoidTests ${target_name})
|
||||
endfunction()
|
||||
|
||||
_void_add_cabi_fixture(VoidCAbiPluginV1 CAbiPluginV1.c)
|
||||
_void_add_cabi_fixture(VoidCAbiPluginV2 CAbiPluginV2.c)
|
||||
_void_add_cabi_fixture(VoidCAbiBadTrunc CAbiBadPlugin.c "BAD_MODE=1")
|
||||
_void_add_cabi_fixture(VoidCAbiBadMajor CAbiBadPlugin.c "BAD_MODE=2")
|
||||
_void_add_cabi_fixture(VoidCAbiBadReserved CAbiBadPlugin.c "BAD_MODE=3")
|
||||
_void_add_cabi_fixture(VoidCAbiDepA CAbiDepPlugin.c "DEP_MODE=1")
|
||||
_void_add_cabi_fixture(VoidCAbiDepB CAbiDepPlugin.c "DEP_MODE=2")
|
||||
_void_add_cabi_fixture(VoidCAbiCycA CAbiDepPlugin.c "DEP_MODE=3")
|
||||
_void_add_cabi_fixture(VoidCAbiCycB CAbiDepPlugin.c "DEP_MODE=4")
|
||||
|
||||
target_compile_definitions(VoidTests PRIVATE
|
||||
"VOID_CABI_V1_PATH=\"$<TARGET_FILE:VoidCAbiPluginV1>\""
|
||||
"VOID_CABI_V2_PATH=\"$<TARGET_FILE:VoidCAbiPluginV2>\""
|
||||
"VOID_CABI_BAD_TRUNC_PATH=\"$<TARGET_FILE:VoidCAbiBadTrunc>\""
|
||||
"VOID_CABI_BAD_MAJOR_PATH=\"$<TARGET_FILE:VoidCAbiBadMajor>\""
|
||||
"VOID_CABI_BAD_RESERVED_PATH=\"$<TARGET_FILE:VoidCAbiBadReserved>\""
|
||||
"VOID_CABI_DEP_A_PATH=\"$<TARGET_FILE:VoidCAbiDepA>\""
|
||||
"VOID_CABI_DEP_B_PATH=\"$<TARGET_FILE:VoidCAbiDepB>\""
|
||||
"VOID_CABI_CYC_A_PATH=\"$<TARGET_FILE:VoidCAbiCycA>\""
|
||||
"VOID_CABI_CYC_B_PATH=\"$<TARGET_FILE:VoidCAbiCycB>\""
|
||||
)
|
||||
endif()
|
||||
@@ -252,6 +252,16 @@ if(_has_service_plugin)
|
||||
-DCMAKE_DISABLE_FIND_PACKAGE_SQLite3=TRUE)
|
||||
endif()
|
||||
|
||||
has_modules(_has_plugin Plugin Core Functional System Data Time Async LogState Text Service)
|
||||
if(_has_plugin)
|
||||
run_consumer(PluginOnly "${install_prefix}" "PluginOnly"
|
||||
-DCMAKE_DISABLE_FIND_PACKAGE_Boost=TRUE
|
||||
-DCMAKE_DISABLE_FIND_PACKAGE_LibArchive=TRUE
|
||||
-DCMAKE_DISABLE_FIND_PACKAGE_OpenSSL=TRUE
|
||||
-DCMAKE_DISABLE_FIND_PACKAGE_PkgConfig=TRUE
|
||||
-DCMAKE_DISABLE_FIND_PACKAGE_SQLite3=TRUE)
|
||||
endif()
|
||||
|
||||
# Foundation consumer with Core-formatting bridge and zero external packages.
|
||||
has_modules(_has_foundation Math Text Time Functional Binary FunctionalCoreFormatting Memory Core)
|
||||
if(_has_foundation)
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
cmake_minimum_required(VERSION 4.0)
|
||||
project(VoidPluginOnlyConsumer LANGUAGES C CXX)
|
||||
|
||||
find_package(Void CONFIG REQUIRED COMPONENTS Plugin)
|
||||
|
||||
add_executable(void_plugin_only_consumer main.cpp)
|
||||
target_link_libraries(void_plugin_only_consumer PRIVATE Void::Plugin)
|
||||
target_compile_features(void_plugin_only_consumer PRIVATE cxx_std_26)
|
||||
|
||||
enable_testing()
|
||||
add_test(NAME PluginOnlyConsumer COMMAND void_plugin_only_consumer)
|
||||
@@ -0,0 +1,18 @@
|
||||
#include <Void/Plugin/CAbi.h>
|
||||
#include <Void/Plugin/CAbiAdapter.hpp>
|
||||
#include <Void/Plugin/Host.hpp>
|
||||
#include <Void/Plugin/Manager.hpp>
|
||||
|
||||
#include <cstring>
|
||||
|
||||
int main()
|
||||
{
|
||||
Void::Plugin::PluginHostLimits limits{};
|
||||
Void::Plugin::CModuleHost host{limits};
|
||||
const auto loaded = host.acquire("missing");
|
||||
void_module_api_v1 api{};
|
||||
api.struct_size = sizeof(api);
|
||||
api.abi_major = VOID_CABI_MAJOR_V1;
|
||||
api.abi_minor = VOID_CABI_MINOR_V1;
|
||||
return !loaded && api.abi_major == 1u ? 0 : 1;
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
#include <Void/Plugin/CAbi.h>
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifndef BAD_MODE
|
||||
#define BAD_MODE 1
|
||||
#endif
|
||||
|
||||
static int32_t stub_get_info(void_module_info_v1* out)
|
||||
{
|
||||
if (out == NULL) return VOID_STATUS_INVALID_ARGUMENT;
|
||||
memset(out, 0, sizeof(*out));
|
||||
out->struct_size = (uint32_t)sizeof(void_module_info_v1);
|
||||
out->module_id = "cabi.bad";
|
||||
out->version = "1.0.0";
|
||||
return VOID_STATUS_OK;
|
||||
}
|
||||
|
||||
static int32_t stub_create(const void_host_api_v1* h, void_module_handle** out)
|
||||
{
|
||||
(void)h;
|
||||
if (out != NULL) *out = NULL;
|
||||
return VOID_STATUS_INTERNAL;
|
||||
}
|
||||
|
||||
static int32_t stub_activate(void_module_handle* h, void_bytes_v1 c, void_error_detail_v1* e)
|
||||
{
|
||||
(void)h; (void)c; (void)e;
|
||||
return VOID_STATUS_OK;
|
||||
}
|
||||
|
||||
static int32_t stub_readiness(void_module_handle* h, uint32_t* r, void_error_detail_v1* e)
|
||||
{
|
||||
(void)h; (void)r; (void)e;
|
||||
return VOID_STATUS_OK;
|
||||
}
|
||||
|
||||
static int32_t stub_stop(void_module_handle* h)
|
||||
{
|
||||
(void)h;
|
||||
return VOID_STATUS_OK;
|
||||
}
|
||||
|
||||
static void stub_destroy(void_module_handle* h) { (void)h; }
|
||||
|
||||
int32_t void_module_query_v1(uint32_t host_struct_size,
|
||||
void_module_api_v1* out_api)
|
||||
{
|
||||
(void)host_struct_size;
|
||||
if (out_api == NULL) {
|
||||
return VOID_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
memset(out_api, 0, sizeof(*out_api));
|
||||
out_api->struct_size = (uint32_t)sizeof(void_module_api_v1);
|
||||
out_api->abi_major = VOID_CABI_MAJOR_V1;
|
||||
out_api->abi_minor = VOID_CABI_MINOR_V1;
|
||||
out_api->reserved = 0;
|
||||
out_api->get_info = stub_get_info;
|
||||
out_api->create = stub_create;
|
||||
out_api->activate = stub_activate;
|
||||
out_api->readiness = stub_readiness;
|
||||
out_api->request_stop = stub_stop;
|
||||
out_api->deactivate = stub_stop;
|
||||
out_api->destroy = stub_destroy;
|
||||
|
||||
#if BAD_MODE == 1
|
||||
/* Truncated struct_size */
|
||||
out_api->struct_size = 16u;
|
||||
#elif BAD_MODE == 2
|
||||
/* Unsupported abi_major */
|
||||
out_api->abi_major = 99u;
|
||||
#elif BAD_MODE == 3
|
||||
/* Non-zero reserved */
|
||||
out_api->reserved = 1u;
|
||||
#endif
|
||||
return VOID_STATUS_OK;
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
#include <Void/Plugin/CAbi.h>
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifndef DEP_MODE
|
||||
#define DEP_MODE 1
|
||||
#endif
|
||||
|
||||
struct void_module_handle {
|
||||
int dummy;
|
||||
};
|
||||
|
||||
#if DEP_MODE == 2
|
||||
static const void_dependency_v1 kDeps[] = {
|
||||
{.module_id = "cabi.dep.a", .version_range = "^1.0.0", .optional_dep = 0},
|
||||
};
|
||||
#elif DEP_MODE == 3
|
||||
static const void_dependency_v1 kDeps[] = {
|
||||
{.module_id = "cabi.cyc.b", .version_range = "^1.0.0", .optional_dep = 0},
|
||||
};
|
||||
#elif DEP_MODE == 4
|
||||
static const void_dependency_v1 kDeps[] = {
|
||||
{.module_id = "cabi.cyc.a", .version_range = "^1.0.0", .optional_dep = 0},
|
||||
};
|
||||
#endif
|
||||
|
||||
static int32_t dep_get_info(void_module_info_v1* out)
|
||||
{
|
||||
if (out == NULL) return VOID_STATUS_INVALID_ARGUMENT;
|
||||
memset(out, 0, sizeof(*out));
|
||||
out->struct_size = (uint32_t)sizeof(void_module_info_v1);
|
||||
#if DEP_MODE == 1
|
||||
out->module_id = "cabi.dep.a";
|
||||
out->version = "1.0.0";
|
||||
out->activation_policy = VOID_POLICY_RESTART_REQUIRED;
|
||||
#elif DEP_MODE == 2
|
||||
out->module_id = "cabi.dep.b";
|
||||
out->version = "1.0.0";
|
||||
out->activation_policy = VOID_POLICY_RELOADABLE;
|
||||
out->dependencies = kDeps;
|
||||
out->dependency_count = 1u;
|
||||
#elif DEP_MODE == 3
|
||||
out->module_id = "cabi.cyc.a";
|
||||
out->version = "1.0.0";
|
||||
out->dependencies = kDeps;
|
||||
out->dependency_count = 1u;
|
||||
#elif DEP_MODE == 4
|
||||
out->module_id = "cabi.cyc.b";
|
||||
out->version = "1.0.0";
|
||||
out->dependencies = kDeps;
|
||||
out->dependency_count = 1u;
|
||||
#endif
|
||||
return VOID_STATUS_OK;
|
||||
}
|
||||
|
||||
static int32_t dep_create(const void_host_api_v1* h, void_module_handle** out)
|
||||
{
|
||||
(void)h;
|
||||
*out = (struct void_module_handle*)calloc(1, sizeof(struct void_module_handle));
|
||||
return *out ? VOID_STATUS_OK : VOID_STATUS_EXHAUSTED;
|
||||
}
|
||||
|
||||
static int32_t dep_activate(void_module_handle* h, void_bytes_v1 c, void_error_detail_v1* e)
|
||||
{
|
||||
(void)h; (void)c; (void)e;
|
||||
return VOID_STATUS_OK;
|
||||
}
|
||||
|
||||
static int32_t dep_readiness(void_module_handle* h, uint32_t* r, void_error_detail_v1* e)
|
||||
{
|
||||
(void)h; (void)e;
|
||||
*r = 1u;
|
||||
return VOID_STATUS_OK;
|
||||
}
|
||||
|
||||
static int32_t dep_stop(void_module_handle* h)
|
||||
{
|
||||
(void)h;
|
||||
return VOID_STATUS_OK;
|
||||
}
|
||||
|
||||
static void dep_destroy(void_module_handle* h) { free(h); }
|
||||
|
||||
int32_t void_module_query_v1(uint32_t host_struct_size,
|
||||
void_module_api_v1* out_api)
|
||||
{
|
||||
if (out_api == NULL || host_struct_size < VOID_MODULE_API_V1_MIN_SIZE) {
|
||||
return VOID_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
memset(out_api, 0, sizeof(*out_api));
|
||||
out_api->struct_size = (uint32_t)sizeof(void_module_api_v1);
|
||||
out_api->abi_major = VOID_CABI_MAJOR_V1;
|
||||
out_api->abi_minor = VOID_CABI_MINOR_V1;
|
||||
out_api->get_info = dep_get_info;
|
||||
out_api->create = dep_create;
|
||||
out_api->activate = dep_activate;
|
||||
out_api->readiness = dep_readiness;
|
||||
out_api->request_stop = dep_stop;
|
||||
out_api->deactivate = dep_stop;
|
||||
out_api->destroy = dep_destroy;
|
||||
return VOID_STATUS_OK;
|
||||
}
|
||||
@@ -0,0 +1,488 @@
|
||||
#include <Void/Plugin/CAbi.h>
|
||||
|
||||
#include <stdatomic.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <threads.h>
|
||||
|
||||
typedef struct cabi_calc_v1 {
|
||||
uint32_t (*version)(void_module_handle* h);
|
||||
int32_t (*accumulate)(void_module_handle* h, int32_t delta);
|
||||
} cabi_calc_v1;
|
||||
|
||||
struct void_operation_handle {
|
||||
void_module_handle* owner;
|
||||
thrd_t worker;
|
||||
int worker_started;
|
||||
atomic_int cancelled;
|
||||
uint32_t op_kind;
|
||||
int32_t delta;
|
||||
void_async_complete_fn cb;
|
||||
void* cb_ctx;
|
||||
};
|
||||
|
||||
struct void_module_handle {
|
||||
void_host_api_v1 host;
|
||||
atomic_int total;
|
||||
atomic_int quiescing;
|
||||
int fail_quiesce;
|
||||
int fail_resume;
|
||||
int ckpt_fail;
|
||||
int ckpt_oversized;
|
||||
int ready;
|
||||
mtx_t ops_mu;
|
||||
struct void_operation_handle* ops[64];
|
||||
size_t ops_count;
|
||||
};
|
||||
|
||||
static const void_interface_id_v1 kCalcIfaceId = {
|
||||
{0x76, 0x6f, 0x69, 0x64, 0x63, 0x61, 0x6c, 0x63, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x01}};
|
||||
|
||||
static void free_c_str(void* ctx, const char* msg)
|
||||
{
|
||||
(void)ctx;
|
||||
free((void*)msg);
|
||||
}
|
||||
|
||||
static void free_c_bytes(void* ctx, const uint8_t* data, uint64_t size)
|
||||
{
|
||||
(void)ctx;
|
||||
(void)size;
|
||||
free((void*)data);
|
||||
}
|
||||
|
||||
static void set_err(void_error_detail_v1* out_err,
|
||||
int32_t status,
|
||||
const char* text)
|
||||
{
|
||||
if (out_err == NULL || out_err->struct_size < sizeof(void_error_detail_v1)) {
|
||||
return;
|
||||
}
|
||||
out_err->status = status;
|
||||
if (text != NULL) {
|
||||
size_t n = strlen(text);
|
||||
char* copy = (char*)malloc(n + 1);
|
||||
if (copy != NULL) {
|
||||
memcpy(copy, text, n + 1);
|
||||
out_err->message = copy;
|
||||
out_err->release = free_c_str;
|
||||
out_err->release_ctx = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int cfg_has(void_bytes_v1 cfg, const char* needle)
|
||||
{
|
||||
if (cfg.data == NULL || cfg.size == 0) {
|
||||
return 0;
|
||||
}
|
||||
size_t nlen = strlen(needle);
|
||||
if (cfg.size < nlen) {
|
||||
return 0;
|
||||
}
|
||||
for (uint64_t i = 0; i + nlen <= cfg.size; ++i) {
|
||||
if (memcmp(cfg.data + i, needle, nlen) == 0) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int32_t cabi_get_info(void_module_info_v1* out_info)
|
||||
{
|
||||
if (out_info == NULL) {
|
||||
return VOID_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
memset(out_info, 0, sizeof(*out_info));
|
||||
out_info->struct_size = (uint32_t)sizeof(void_module_info_v1);
|
||||
out_info->module_id = "cabi.calc";
|
||||
out_info->version = "1.0.0";
|
||||
out_info->activation_policy = VOID_POLICY_RELOADABLE;
|
||||
out_info->state_schema_version = 1;
|
||||
out_info->dependencies = NULL;
|
||||
out_info->dependency_count = 0;
|
||||
return VOID_STATUS_OK;
|
||||
}
|
||||
|
||||
static int32_t cabi_create(const void_host_api_v1* host_api,
|
||||
void_module_handle** out_handle)
|
||||
{
|
||||
if (out_handle == NULL) {
|
||||
return VOID_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
*out_handle = NULL;
|
||||
if (host_api == NULL || host_api->struct_size < sizeof(void_host_api_v1)) {
|
||||
return VOID_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
struct void_module_handle* h =
|
||||
(struct void_module_handle*)calloc(1, sizeof(*h));
|
||||
if (h == NULL) {
|
||||
return VOID_STATUS_EXHAUSTED;
|
||||
}
|
||||
h->host = *host_api;
|
||||
atomic_init(&h->total, 0);
|
||||
atomic_init(&h->quiescing, 0);
|
||||
h->ready = 1;
|
||||
mtx_init(&h->ops_mu, mtx_plain);
|
||||
*out_handle = h;
|
||||
return VOID_STATUS_OK;
|
||||
}
|
||||
|
||||
static int32_t cabi_activate(void_module_handle* h,
|
||||
void_bytes_v1 config,
|
||||
void_error_detail_v1* out_err)
|
||||
{
|
||||
if (h == NULL) {
|
||||
return VOID_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
if (cfg_has(config, "fail_activate")) {
|
||||
set_err(out_err, VOID_STATUS_INTERNAL, "V1 activation rejected");
|
||||
return VOID_STATUS_INTERNAL;
|
||||
}
|
||||
h->ready = cfg_has(config, "not_ready") ? 0 : 1;
|
||||
h->fail_quiesce = cfg_has(config, "quiesce_fail");
|
||||
h->fail_resume = cfg_has(config, "quiesce_fail_resume_fail");
|
||||
if (h->fail_resume) {
|
||||
h->fail_quiesce = 1;
|
||||
}
|
||||
h->ckpt_fail = cfg_has(config, "ckpt_fail");
|
||||
h->ckpt_oversized = cfg_has(config, "ckpt_oversized");
|
||||
return VOID_STATUS_OK;
|
||||
}
|
||||
|
||||
static int32_t cabi_readiness(void_module_handle* h,
|
||||
uint32_t* out_ready,
|
||||
void_error_detail_v1* out_err)
|
||||
{
|
||||
if (h == NULL || out_ready == NULL) {
|
||||
return VOID_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
*out_ready = h->ready ? 1u : 0u;
|
||||
if (!h->ready) {
|
||||
set_err(out_err, VOID_STATUS_NOT_READY, "V1 not ready");
|
||||
}
|
||||
return VOID_STATUS_OK;
|
||||
}
|
||||
|
||||
static int32_t cabi_quiesce(void_module_handle* h,
|
||||
const void_op_options_v1* opts,
|
||||
void_error_detail_v1* out_err)
|
||||
{
|
||||
(void)opts;
|
||||
if (h == NULL) {
|
||||
return VOID_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
atomic_store(&h->quiescing, 1);
|
||||
if (h->fail_quiesce) {
|
||||
set_err(out_err, VOID_STATUS_QUIESCING, "V1 quiesce refused");
|
||||
return VOID_STATUS_QUIESCING;
|
||||
}
|
||||
return VOID_STATUS_OK;
|
||||
}
|
||||
|
||||
static int32_t cabi_resume(void_module_handle* h, void_error_detail_v1* out_err)
|
||||
{
|
||||
if (h == NULL) {
|
||||
return VOID_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
if (h->fail_resume) {
|
||||
set_err(out_err, VOID_STATUS_INTERNAL, "V1 resume failed");
|
||||
return VOID_STATUS_INTERNAL;
|
||||
}
|
||||
atomic_store(&h->quiescing, 0);
|
||||
return VOID_STATUS_OK;
|
||||
}
|
||||
|
||||
static int32_t cabi_checkpoint(void_module_handle* h,
|
||||
const void_op_options_v1* opts,
|
||||
void_owned_bytes_v1* out_state,
|
||||
void_error_detail_v1* out_err)
|
||||
{
|
||||
(void)opts;
|
||||
if (h == NULL || out_state == NULL) {
|
||||
return VOID_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
memset(out_state, 0, sizeof(*out_state));
|
||||
if (h->ckpt_fail) {
|
||||
set_err(out_err, VOID_STATUS_INTERNAL, "V1 checkpoint error");
|
||||
return VOID_STATUS_INTERNAL;
|
||||
}
|
||||
if (h->ckpt_oversized) {
|
||||
uint8_t* dummy = (uint8_t*)malloc(16);
|
||||
out_state->data = dummy;
|
||||
out_state->size = UINT64_C(8) * UINT64_C(1024) * UINT64_C(1024);
|
||||
out_state->release = free_c_bytes;
|
||||
out_state->release_ctx = NULL;
|
||||
return VOID_STATUS_OK;
|
||||
}
|
||||
char buf[64];
|
||||
int n = snprintf(buf, sizeof(buf), "v1:%d", atomic_load(&h->total));
|
||||
if (n <= 0) {
|
||||
return VOID_STATUS_INTERNAL;
|
||||
}
|
||||
uint8_t* copy = (uint8_t*)malloc((size_t)n);
|
||||
if (copy == NULL) {
|
||||
return VOID_STATUS_EXHAUSTED;
|
||||
}
|
||||
memcpy(copy, buf, (size_t)n);
|
||||
out_state->data = copy;
|
||||
out_state->size = (uint64_t)n;
|
||||
out_state->release = free_c_bytes;
|
||||
out_state->release_ctx = NULL;
|
||||
return VOID_STATUS_OK;
|
||||
}
|
||||
|
||||
static int32_t cabi_restore(void_module_handle* h,
|
||||
void_bytes_v1 state,
|
||||
void_error_detail_v1* out_err)
|
||||
{
|
||||
(void)h;
|
||||
(void)state;
|
||||
(void)out_err;
|
||||
return VOID_STATUS_OK;
|
||||
}
|
||||
|
||||
static uint32_t iface_get_version(void_module_handle* h)
|
||||
{
|
||||
(void)h;
|
||||
return 1u;
|
||||
}
|
||||
|
||||
static int32_t iface_accumulate(void_module_handle* h, int32_t delta)
|
||||
{
|
||||
if (h == NULL) {
|
||||
return -1;
|
||||
}
|
||||
if (atomic_load(&h->quiescing) != 0 && delta != 0) {
|
||||
return -1;
|
||||
}
|
||||
return atomic_fetch_add(&h->total, delta) + delta;
|
||||
}
|
||||
|
||||
static const cabi_calc_v1 kCalcTable = {iface_get_version, iface_accumulate};
|
||||
|
||||
static int32_t cabi_query_interface(void_module_handle* h,
|
||||
const void_interface_id_v1* iface_id,
|
||||
uint32_t min_major,
|
||||
uint32_t min_table_size,
|
||||
void_interface_view_v1* out_view)
|
||||
{
|
||||
if (h == NULL || iface_id == NULL || out_view == NULL) {
|
||||
return VOID_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
if (memcmp(iface_id->bytes, kCalcIfaceId.bytes, 16) != 0) {
|
||||
return VOID_STATUS_NOT_FOUND;
|
||||
}
|
||||
if (min_major != 1u || min_table_size > sizeof(cabi_calc_v1)) {
|
||||
return VOID_STATUS_UNSUPPORTED;
|
||||
}
|
||||
out_view->struct_size = (uint32_t)sizeof(void_interface_view_v1);
|
||||
out_view->major = 1u;
|
||||
out_view->minor = 0u;
|
||||
out_view->table_size = (uint32_t)sizeof(cabi_calc_v1);
|
||||
out_view->function_table = &kCalcTable;
|
||||
return VOID_STATUS_OK;
|
||||
}
|
||||
|
||||
static void cabi_release_interface(void_module_handle* h,
|
||||
const void_interface_id_v1* iface_id,
|
||||
const void_interface_view_v1* view)
|
||||
{
|
||||
(void)h;
|
||||
(void)iface_id;
|
||||
(void)view;
|
||||
}
|
||||
|
||||
static int async_worker(void* arg)
|
||||
{
|
||||
struct void_operation_handle* op = (struct void_operation_handle*)arg;
|
||||
int pinned = 0;
|
||||
if (op->op_kind == 3u && op->owner->host.acquire_activity_pin != NULL) {
|
||||
if (op->owner->host.acquire_activity_pin(
|
||||
op->owner->host.host_ctx, op->owner->host.generation) ==
|
||||
VOID_STATUS_OK) {
|
||||
pinned = 1;
|
||||
}
|
||||
}
|
||||
struct timespec ts = {.tv_sec = 0, .tv_nsec = 2000000};
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
if (atomic_load(&op->cancelled) != 0) {
|
||||
op->cb(op->cb_ctx, VOID_STATUS_CANCELLED, NULL);
|
||||
if (pinned) {
|
||||
op->owner->host.release_activity_pin(
|
||||
op->owner->host.host_ctx, op->owner->host.generation);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
thrd_sleep(&ts, NULL);
|
||||
}
|
||||
int32_t val = atomic_fetch_add(&op->owner->total, op->delta) + op->delta;
|
||||
uint8_t buf[4];
|
||||
memcpy(buf, &val, sizeof(val));
|
||||
void_bytes_v1 res = {.data = buf, .size = sizeof(buf)};
|
||||
op->cb(op->cb_ctx, VOID_STATUS_OK, &res);
|
||||
if (pinned) {
|
||||
op->owner->host.release_activity_pin(op->owner->host.host_ctx,
|
||||
op->owner->host.generation);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int32_t cabi_submit_async(void_module_handle* h,
|
||||
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)
|
||||
{
|
||||
(void)out_err;
|
||||
if (h == NULL || cb == NULL || out_op == NULL) {
|
||||
return VOID_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
*out_op = NULL;
|
||||
if (opts != NULL && opts->stop_requested) {
|
||||
return VOID_STATUS_CANCELLED;
|
||||
}
|
||||
int32_t delta = 0;
|
||||
if (payload.data != NULL && payload.size >= 4) {
|
||||
memcpy(&delta, payload.data, 4);
|
||||
}
|
||||
struct void_operation_handle* op =
|
||||
(struct void_operation_handle*)calloc(1, sizeof(*op));
|
||||
if (op == NULL) {
|
||||
return VOID_STATUS_EXHAUSTED;
|
||||
}
|
||||
op->owner = h;
|
||||
op->op_kind = op_kind;
|
||||
op->delta = delta;
|
||||
op->cb = cb;
|
||||
op->cb_ctx = cb_ctx;
|
||||
atomic_init(&op->cancelled, 0);
|
||||
|
||||
if (op_kind == 2u) {
|
||||
int32_t val = atomic_fetch_add(&h->total, delta) + delta;
|
||||
uint8_t buf[4];
|
||||
memcpy(buf, &val, sizeof(val));
|
||||
void_bytes_v1 res = {.data = buf, .size = sizeof(buf)};
|
||||
*out_op = op;
|
||||
cb(cb_ctx, VOID_STATUS_OK, &res);
|
||||
return VOID_STATUS_OK;
|
||||
}
|
||||
|
||||
mtx_lock(&h->ops_mu);
|
||||
if (h->ops_count < 64) {
|
||||
h->ops[h->ops_count++] = op;
|
||||
}
|
||||
mtx_unlock(&h->ops_mu);
|
||||
|
||||
op->worker_started = 1;
|
||||
*out_op = op;
|
||||
if (thrd_create(&op->worker, async_worker, op) != thrd_success) {
|
||||
op->worker_started = 0;
|
||||
*out_op = NULL;
|
||||
free(op);
|
||||
return VOID_STATUS_INTERNAL;
|
||||
}
|
||||
return VOID_STATUS_OK;
|
||||
}
|
||||
|
||||
static int32_t cabi_cancel_async(void_module_handle* h, void_operation_handle* op)
|
||||
{
|
||||
(void)h;
|
||||
if (op == NULL) {
|
||||
return VOID_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
atomic_store(&op->cancelled, 1);
|
||||
return VOID_STATUS_OK;
|
||||
}
|
||||
|
||||
static void cabi_release_async(void_module_handle* h, void_operation_handle* op)
|
||||
{
|
||||
if (op == NULL) {
|
||||
return;
|
||||
}
|
||||
if (op->worker_started) {
|
||||
thrd_join(op->worker, NULL);
|
||||
op->worker_started = 0;
|
||||
}
|
||||
if (h != NULL) {
|
||||
mtx_lock(&h->ops_mu);
|
||||
for (size_t i = 0; i < h->ops_count; ++i) {
|
||||
if (h->ops[i] == op) {
|
||||
h->ops[i] = h->ops[h->ops_count - 1];
|
||||
--h->ops_count;
|
||||
break;
|
||||
}
|
||||
}
|
||||
mtx_unlock(&h->ops_mu);
|
||||
}
|
||||
free(op);
|
||||
}
|
||||
|
||||
static int32_t cabi_request_stop(void_module_handle* h)
|
||||
{
|
||||
if (h == NULL) {
|
||||
return VOID_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
atomic_store(&h->quiescing, 1);
|
||||
mtx_lock(&h->ops_mu);
|
||||
for (size_t i = 0; i < h->ops_count; ++i) {
|
||||
if (h->ops[i] != NULL) {
|
||||
atomic_store(&h->ops[i]->cancelled, 1);
|
||||
}
|
||||
}
|
||||
mtx_unlock(&h->ops_mu);
|
||||
return VOID_STATUS_OK;
|
||||
}
|
||||
|
||||
static int32_t cabi_deactivate(void_module_handle* h)
|
||||
{
|
||||
return cabi_request_stop(h);
|
||||
}
|
||||
|
||||
static void cabi_destroy(void_module_handle* h)
|
||||
{
|
||||
if (h == NULL) {
|
||||
return;
|
||||
}
|
||||
mtx_destroy(&h->ops_mu);
|
||||
free(h);
|
||||
}
|
||||
|
||||
int32_t void_module_query_v1(uint32_t host_struct_size,
|
||||
void_module_api_v1* out_api)
|
||||
{
|
||||
if (out_api == NULL || host_struct_size < VOID_MODULE_API_V1_MIN_SIZE) {
|
||||
return VOID_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
memset(out_api, 0, sizeof(*out_api));
|
||||
out_api->struct_size = (uint32_t)sizeof(void_module_api_v1);
|
||||
out_api->abi_major = VOID_CABI_MAJOR_V1;
|
||||
out_api->abi_minor = VOID_CABI_MINOR_V1;
|
||||
out_api->reserved = 0;
|
||||
out_api->capabilities = VOID_CAP_STATELESS_RELOAD |
|
||||
VOID_CAP_STATEFUL_CHECKPOINT | VOID_CAP_ASYNC_OPS |
|
||||
VOID_CAP_SERVICE_INTERFACES;
|
||||
out_api->get_info = cabi_get_info;
|
||||
out_api->create = cabi_create;
|
||||
out_api->activate = cabi_activate;
|
||||
out_api->readiness = cabi_readiness;
|
||||
out_api->quiesce = cabi_quiesce;
|
||||
out_api->resume = cabi_resume;
|
||||
out_api->checkpoint = cabi_checkpoint;
|
||||
out_api->restore = cabi_restore;
|
||||
out_api->query_interface = cabi_query_interface;
|
||||
out_api->release_interface = cabi_release_interface;
|
||||
out_api->submit_async = cabi_submit_async;
|
||||
out_api->cancel_async = cabi_cancel_async;
|
||||
out_api->release_async = cabi_release_async;
|
||||
out_api->request_stop = cabi_request_stop;
|
||||
out_api->deactivate = cabi_deactivate;
|
||||
out_api->destroy = cabi_destroy;
|
||||
return VOID_STATUS_OK;
|
||||
}
|
||||
@@ -0,0 +1,357 @@
|
||||
#include <Void/Plugin/CAbi.h>
|
||||
|
||||
#include <stdatomic.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef struct cabi_calc_v1 {
|
||||
uint32_t (*version)(void_module_handle* h);
|
||||
int32_t (*accumulate)(void_module_handle* h, int32_t delta);
|
||||
} cabi_calc_v1;
|
||||
|
||||
struct void_module_handle {
|
||||
void_host_api_v1 host;
|
||||
atomic_int total;
|
||||
int ready;
|
||||
};
|
||||
|
||||
static const void_interface_id_v1 kCalcIfaceId = {
|
||||
{0x76, 0x6f, 0x69, 0x64, 0x63, 0x61, 0x6c, 0x63, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x01}};
|
||||
|
||||
static void free_c_str(void* ctx, const char* msg)
|
||||
{
|
||||
(void)ctx;
|
||||
free((void*)msg);
|
||||
}
|
||||
|
||||
static void free_c_bytes(void* ctx, const uint8_t* data, uint64_t size)
|
||||
{
|
||||
(void)ctx;
|
||||
(void)size;
|
||||
free((void*)data);
|
||||
}
|
||||
|
||||
static void set_err(void_error_detail_v1* out_err,
|
||||
int32_t status,
|
||||
const char* text)
|
||||
{
|
||||
if (out_err == NULL || out_err->struct_size < sizeof(void_error_detail_v1)) {
|
||||
return;
|
||||
}
|
||||
out_err->status = status;
|
||||
if (text != NULL) {
|
||||
size_t n = strlen(text);
|
||||
char* copy = (char*)malloc(n + 1);
|
||||
if (copy != NULL) {
|
||||
memcpy(copy, text, n + 1);
|
||||
out_err->message = copy;
|
||||
out_err->release = free_c_str;
|
||||
out_err->release_ctx = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int cfg_has(void_bytes_v1 cfg, const char* needle)
|
||||
{
|
||||
if (cfg.data == NULL || cfg.size == 0) {
|
||||
return 0;
|
||||
}
|
||||
size_t nlen = strlen(needle);
|
||||
if (cfg.size < nlen) {
|
||||
return 0;
|
||||
}
|
||||
for (uint64_t i = 0; i + nlen <= cfg.size; ++i) {
|
||||
if (memcmp(cfg.data + i, needle, nlen) == 0) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int32_t cabi_v2_get_info(void_module_info_v1* out_info)
|
||||
{
|
||||
if (out_info == NULL) {
|
||||
return VOID_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
memset(out_info, 0, sizeof(*out_info));
|
||||
out_info->struct_size = (uint32_t)sizeof(void_module_info_v1);
|
||||
out_info->module_id = "cabi.calc";
|
||||
out_info->version = "2.0.0";
|
||||
out_info->activation_policy = VOID_POLICY_RELOADABLE;
|
||||
out_info->state_schema_version = 2;
|
||||
return VOID_STATUS_OK;
|
||||
}
|
||||
|
||||
static int32_t cabi_v2_create(const void_host_api_v1* host_api,
|
||||
void_module_handle** out_handle)
|
||||
{
|
||||
if (out_handle == NULL) {
|
||||
return VOID_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
*out_handle = NULL;
|
||||
if (host_api == NULL || host_api->struct_size < sizeof(void_host_api_v1)) {
|
||||
return VOID_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
struct void_module_handle* h =
|
||||
(struct void_module_handle*)calloc(1, sizeof(*h));
|
||||
if (h == NULL) {
|
||||
return VOID_STATUS_EXHAUSTED;
|
||||
}
|
||||
h->host = *host_api;
|
||||
atomic_init(&h->total, 0);
|
||||
h->ready = 1;
|
||||
*out_handle = h;
|
||||
return VOID_STATUS_OK;
|
||||
}
|
||||
|
||||
static int32_t cabi_v2_activate(void_module_handle* h,
|
||||
void_bytes_v1 config,
|
||||
void_error_detail_v1* out_err)
|
||||
{
|
||||
if (h == NULL) {
|
||||
return VOID_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
if (cfg_has(config, "fail_activate")) {
|
||||
set_err(out_err, VOID_STATUS_INTERNAL, "V2 activation failed");
|
||||
return VOID_STATUS_INTERNAL;
|
||||
}
|
||||
h->ready = cfg_has(config, "not_ready") ? 0 : 1;
|
||||
return VOID_STATUS_OK;
|
||||
}
|
||||
|
||||
static int32_t cabi_v2_readiness(void_module_handle* h,
|
||||
uint32_t* out_ready,
|
||||
void_error_detail_v1* out_err)
|
||||
{
|
||||
if (h == NULL || out_ready == NULL) {
|
||||
return VOID_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
*out_ready = h->ready ? 1u : 0u;
|
||||
if (!h->ready) {
|
||||
set_err(out_err, VOID_STATUS_NOT_READY, "V2 not ready");
|
||||
}
|
||||
return VOID_STATUS_OK;
|
||||
}
|
||||
|
||||
static int32_t cabi_v2_quiesce(void_module_handle* h,
|
||||
const void_op_options_v1* opts,
|
||||
void_error_detail_v1* out_err)
|
||||
{
|
||||
(void)h;
|
||||
(void)opts;
|
||||
(void)out_err;
|
||||
return VOID_STATUS_OK;
|
||||
}
|
||||
|
||||
static int32_t cabi_v2_resume(void_module_handle* h,
|
||||
void_error_detail_v1* out_err)
|
||||
{
|
||||
(void)h;
|
||||
(void)out_err;
|
||||
return VOID_STATUS_OK;
|
||||
}
|
||||
|
||||
static int32_t cabi_v2_checkpoint(void_module_handle* h,
|
||||
const void_op_options_v1* opts,
|
||||
void_owned_bytes_v1* out_state,
|
||||
void_error_detail_v1* out_err)
|
||||
{
|
||||
(void)opts;
|
||||
(void)out_err;
|
||||
if (h == NULL || out_state == NULL) {
|
||||
return VOID_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
char buf[64];
|
||||
int val = atomic_load(&h->total);
|
||||
int len = 0;
|
||||
if (val == 0) {
|
||||
memcpy(buf, "v2:0", 4);
|
||||
len = 4;
|
||||
} else {
|
||||
/* Decimal conversion without stdio overhead. */
|
||||
char rev[32];
|
||||
int rlen = 0;
|
||||
int neg = val < 0;
|
||||
unsigned u = neg ? (unsigned)(-val) : (unsigned)val;
|
||||
while (u > 0) {
|
||||
rev[rlen++] = (char)('0' + (u % 10u));
|
||||
u /= 10u;
|
||||
}
|
||||
buf[len++] = 'v';
|
||||
buf[len++] = '2';
|
||||
buf[len++] = ':';
|
||||
if (neg) {
|
||||
buf[len++] = '-';
|
||||
}
|
||||
while (rlen > 0) {
|
||||
buf[len++] = rev[--rlen];
|
||||
}
|
||||
}
|
||||
uint8_t* copy = (uint8_t*)malloc((size_t)len);
|
||||
if (copy == NULL) {
|
||||
return VOID_STATUS_EXHAUSTED;
|
||||
}
|
||||
memcpy(copy, buf, (size_t)len);
|
||||
out_state->data = copy;
|
||||
out_state->size = (uint64_t)len;
|
||||
out_state->release = free_c_bytes;
|
||||
out_state->release_ctx = NULL;
|
||||
return VOID_STATUS_OK;
|
||||
}
|
||||
|
||||
static int32_t cabi_v2_restore(void_module_handle* h,
|
||||
void_bytes_v1 state,
|
||||
void_error_detail_v1* out_err)
|
||||
{
|
||||
if (h == NULL || state.data == NULL || state.size < 4) {
|
||||
set_err(out_err, VOID_STATUS_STATE_REJECTED, "V2 invalid checkpoint");
|
||||
return VOID_STATUS_STATE_REJECTED;
|
||||
}
|
||||
if (memcmp(state.data, "v1:", 3) != 0 &&
|
||||
memcmp(state.data, "v2:", 3) != 0) {
|
||||
set_err(out_err, VOID_STATUS_STATE_REJECTED, "V2 unknown checkpoint header");
|
||||
return VOID_STATUS_STATE_REJECTED;
|
||||
}
|
||||
char buf[64];
|
||||
size_t n = (size_t)(state.size - 3);
|
||||
if (n >= sizeof(buf)) {
|
||||
return VOID_STATUS_STATE_REJECTED;
|
||||
}
|
||||
memcpy(buf, state.data + 3, n);
|
||||
buf[n] = '\0';
|
||||
int val = atoi(buf);
|
||||
atomic_store(&h->total, val);
|
||||
return VOID_STATUS_OK;
|
||||
}
|
||||
|
||||
static uint32_t iface_v2_version(void_module_handle* h)
|
||||
{
|
||||
(void)h;
|
||||
return 2u;
|
||||
}
|
||||
|
||||
static int32_t iface_v2_accumulate(void_module_handle* h, int32_t delta)
|
||||
{
|
||||
if (h == NULL) {
|
||||
return -1;
|
||||
}
|
||||
return atomic_fetch_add(&h->total, delta) + delta;
|
||||
}
|
||||
|
||||
static const cabi_calc_v1 kCalcV2 = {iface_v2_version, iface_v2_accumulate};
|
||||
|
||||
static int32_t cabi_v2_query_interface(void_module_handle* h,
|
||||
const void_interface_id_v1* iface_id,
|
||||
uint32_t min_major,
|
||||
uint32_t min_table_size,
|
||||
void_interface_view_v1* out_view)
|
||||
{
|
||||
if (h == NULL || iface_id == NULL || out_view == NULL) {
|
||||
return VOID_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
if (memcmp(iface_id->bytes, kCalcIfaceId.bytes, 16) != 0) {
|
||||
return VOID_STATUS_NOT_FOUND;
|
||||
}
|
||||
if (min_major != 1u || min_table_size > sizeof(cabi_calc_v1)) {
|
||||
return VOID_STATUS_UNSUPPORTED;
|
||||
}
|
||||
out_view->struct_size = (uint32_t)sizeof(void_interface_view_v1);
|
||||
out_view->major = 1u;
|
||||
out_view->minor = 1u;
|
||||
out_view->table_size = (uint32_t)sizeof(cabi_calc_v1);
|
||||
out_view->function_table = &kCalcV2;
|
||||
return VOID_STATUS_OK;
|
||||
}
|
||||
|
||||
static void cabi_v2_release_interface(void_module_handle* h,
|
||||
const void_interface_id_v1* iface_id,
|
||||
const void_interface_view_v1* view)
|
||||
{
|
||||
(void)h;
|
||||
(void)iface_id;
|
||||
(void)view;
|
||||
}
|
||||
|
||||
static int32_t cabi_v2_submit_async(void_module_handle* h,
|
||||
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)
|
||||
{
|
||||
(void)h;
|
||||
(void)op_kind;
|
||||
(void)payload;
|
||||
(void)opts;
|
||||
(void)cb;
|
||||
(void)cb_ctx;
|
||||
(void)out_op;
|
||||
(void)out_err;
|
||||
return VOID_STATUS_UNSUPPORTED;
|
||||
}
|
||||
|
||||
static int32_t cabi_v2_cancel_async(void_module_handle* h,
|
||||
void_operation_handle* op)
|
||||
{
|
||||
(void)h;
|
||||
(void)op;
|
||||
return VOID_STATUS_UNSUPPORTED;
|
||||
}
|
||||
|
||||
static void cabi_v2_release_async(void_module_handle* h,
|
||||
void_operation_handle* op)
|
||||
{
|
||||
(void)h;
|
||||
(void)op;
|
||||
}
|
||||
|
||||
static int32_t cabi_v2_request_stop(void_module_handle* h)
|
||||
{
|
||||
(void)h;
|
||||
return VOID_STATUS_OK;
|
||||
}
|
||||
|
||||
static int32_t cabi_v2_deactivate(void_module_handle* h)
|
||||
{
|
||||
(void)h;
|
||||
return VOID_STATUS_OK;
|
||||
}
|
||||
|
||||
static void cabi_v2_destroy(void_module_handle* h) { free(h); }
|
||||
|
||||
int32_t void_module_query_v1(uint32_t host_struct_size,
|
||||
void_module_api_v1* out_api)
|
||||
{
|
||||
if (out_api == NULL || host_struct_size < VOID_MODULE_API_V1_MIN_SIZE) {
|
||||
return VOID_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
memset(out_api, 0, sizeof(*out_api));
|
||||
out_api->struct_size = (uint32_t)sizeof(void_module_api_v1);
|
||||
out_api->abi_major = VOID_CABI_MAJOR_V1;
|
||||
out_api->abi_minor = VOID_CABI_MINOR_V1;
|
||||
out_api->reserved = 0;
|
||||
out_api->capabilities = VOID_CAP_STATELESS_RELOAD |
|
||||
VOID_CAP_STATEFUL_CHECKPOINT |
|
||||
VOID_CAP_SERVICE_INTERFACES;
|
||||
out_api->get_info = cabi_v2_get_info;
|
||||
out_api->create = cabi_v2_create;
|
||||
out_api->activate = cabi_v2_activate;
|
||||
out_api->readiness = cabi_v2_readiness;
|
||||
out_api->quiesce = cabi_v2_quiesce;
|
||||
out_api->resume = cabi_v2_resume;
|
||||
out_api->checkpoint = cabi_v2_checkpoint;
|
||||
out_api->restore = cabi_v2_restore;
|
||||
out_api->query_interface = cabi_v2_query_interface;
|
||||
out_api->release_interface = cabi_v2_release_interface;
|
||||
out_api->submit_async = cabi_v2_submit_async;
|
||||
out_api->cancel_async = cabi_v2_cancel_async;
|
||||
out_api->release_async = cabi_v2_release_async;
|
||||
out_api->request_stop = cabi_v2_request_stop;
|
||||
out_api->deactivate = cabi_v2_deactivate;
|
||||
out_api->destroy = cabi_v2_destroy;
|
||||
return VOID_STATUS_OK;
|
||||
}
|
||||
@@ -0,0 +1,315 @@
|
||||
#include <Void/Plugin/CAbi.h>
|
||||
#include <Void/Plugin/CAbiAdapter.hpp>
|
||||
#include <Void/Plugin/Host.hpp>
|
||||
#include <Void/Plugin/Manager.hpp>
|
||||
#include <Void/System/TempDir.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <stop_token>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
using namespace Void::Plugin;
|
||||
|
||||
struct CabiCalcTable {
|
||||
uint32_t (*version)(void_module_handle* h);
|
||||
int32_t (*accumulate)(void_module_handle* h, int32_t delta);
|
||||
};
|
||||
|
||||
const void_interface_id_v1 kCalcIfaceId{
|
||||
{0x76, 0x6f, 0x69, 0x64, 0x63, 0x61, 0x6c, 0x63, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x01}};
|
||||
|
||||
std::vector<std::uint8_t> encode_i32_le(std::int32_t v)
|
||||
{
|
||||
std::vector<std::uint8_t> out(4);
|
||||
std::memcpy(out.data(), &v, 4);
|
||||
return out;
|
||||
}
|
||||
|
||||
std::int32_t decode_i32_le(const std::vector<std::uint8_t>& b)
|
||||
{
|
||||
std::int32_t v = 0;
|
||||
if (b.size() >= 4) {
|
||||
std::memcpy(&v, b.data(), 4);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
#if defined(VOID_CABI_V1_PATH) && defined(VOID_CABI_V2_PATH) && \
|
||||
__has_include(<dlfcn.h>)
|
||||
|
||||
Void::Testing::Test plugin_cabi_v1_to_v2_stateful_quiesce_and_reload()
|
||||
{
|
||||
PluginHostLimits limits{};
|
||||
limits.allow_safe_dlclose = true;
|
||||
CModuleHost host{limits};
|
||||
|
||||
auto id = host.load(VOID_CABI_V1_PATH);
|
||||
Void::Testing::require(id.has_value());
|
||||
Void::Testing::expect_eq(*id, std::string{"cabi.calc"});
|
||||
|
||||
auto g1 = host.acquire("cabi.calc");
|
||||
Void::Testing::require(static_cast<bool>(g1));
|
||||
const auto gen1 = g1.generation();
|
||||
|
||||
auto iface1 = g1.query_interface(kCalcIfaceId, 1, sizeof(CabiCalcTable));
|
||||
Void::Testing::require(iface1.has_value());
|
||||
const auto* t1 = iface1->table<CabiCalcTable>();
|
||||
Void::Testing::require(t1 != nullptr);
|
||||
Void::Testing::expect_eq(t1->version(nullptr), std::uint32_t{1});
|
||||
|
||||
// Run async op on V1 (kind=3 pins activity via host API, kind=2 sync).
|
||||
auto op_sync = g1.run_async(2u, encode_i32_le(15));
|
||||
Void::Testing::require(op_sync.has_value());
|
||||
Void::Testing::expect_eq(decode_i32_le(*op_sync), 15);
|
||||
|
||||
auto op_async = g1.run_async(3u, encode_i32_le(25));
|
||||
Void::Testing::require(op_async.has_value());
|
||||
Void::Testing::expect_eq(decode_i32_le(*op_async), 40);
|
||||
|
||||
// Reload V1 -> V2 while g1 + iface1 are still pinned!
|
||||
auto gen2_res =
|
||||
host.replace("cabi.calc", VOID_CABI_V2_PATH, {}, gen1);
|
||||
Void::Testing::require(gen2_res.has_value());
|
||||
Void::Testing::expect(*gen2_res > gen1);
|
||||
|
||||
// Old generation stays mapped & live until both leases drop.
|
||||
auto retained_mid = host.retained_generations("cabi.calc");
|
||||
Void::Testing::expect_eq(retained_mid.size(), std::size_t{2});
|
||||
|
||||
// Acquire V2 interface and confirm checkpoint migrated 40 -> V2.
|
||||
auto g2 = host.acquire("cabi.calc");
|
||||
Void::Testing::require(static_cast<bool>(g2));
|
||||
auto iface2 = g2.query_interface(kCalcIfaceId, 1, sizeof(CabiCalcTable));
|
||||
Void::Testing::require(iface2.has_value());
|
||||
const auto* t2 = iface2->table<CabiCalcTable>();
|
||||
Void::Testing::expect_eq(t2->version(nullptr), std::uint32_t{2});
|
||||
|
||||
auto op2 = g2.run_async(1u, encode_i32_le(5));
|
||||
// V2 does not implement async ops -> UNSUPPORTED mapped to abi_mismatch.
|
||||
Void::Testing::expect(!op2.has_value());
|
||||
|
||||
// Old iface1/g1 still pinned; V1 sees quiesced mutation return -1.
|
||||
Void::Testing::expect_eq(t1->version(nullptr), std::uint32_t{1});
|
||||
|
||||
// Drop all pins to gen1 and verify host retirement thread dlcloses gen1.
|
||||
iface1 = InterfaceLease{};
|
||||
g1 = GenerationLease{};
|
||||
for (int i = 0; i < 100; ++i) {
|
||||
if (host.retained_generations("cabi.calc").size() == 1) {
|
||||
break;
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds{5});
|
||||
}
|
||||
Void::Testing::expect_eq(host.retained_generations("cabi.calc").size(),
|
||||
std::size_t{1});
|
||||
}
|
||||
|
||||
Void::Testing::Test plugin_cabi_async_cancel_and_safe_dlclose_wait()
|
||||
{
|
||||
PluginHostLimits limits{};
|
||||
limits.allow_safe_dlclose = true;
|
||||
CModuleHost host{limits};
|
||||
|
||||
Void::Testing::require(host.load(VOID_CABI_V1_PATH).has_value());
|
||||
auto lease = host.acquire("cabi.calc");
|
||||
ActivityPin extra_pin = lease.acquire_activity_pin();
|
||||
|
||||
std::stop_source ss;
|
||||
std::thread canceller([&] {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds{2});
|
||||
ss.request_stop();
|
||||
});
|
||||
Void::Async::OperationOptions opts;
|
||||
opts.stop = ss.get_token();
|
||||
auto res = lease.run_async(1u, encode_i32_le(1), opts);
|
||||
canceller.join();
|
||||
Void::Testing::expect(!res.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
res.error().code().value,
|
||||
static_cast<std::int32_t>(PluginErrorCode::cancelled));
|
||||
|
||||
// Unload while extra_pin + lease still exist -> cannot destroy/dlclose yet.
|
||||
Void::Testing::require(host.unload("cabi.calc").has_value());
|
||||
lease = GenerationLease{};
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds{10});
|
||||
extra_pin = ActivityPin{};
|
||||
host.close_and_join();
|
||||
}
|
||||
|
||||
Void::Testing::Test plugin_cabi_bad_abi_oversized_ckpt_and_resume_degraded()
|
||||
{
|
||||
CModuleHost host{PluginHostLimits{.allow_safe_dlclose = true}};
|
||||
|
||||
for (const char* bad_path :
|
||||
{VOID_CABI_BAD_TRUNC_PATH, VOID_CABI_BAD_MAJOR_PATH,
|
||||
VOID_CABI_BAD_RESERVED_PATH}) {
|
||||
auto r = host.load(bad_path);
|
||||
Void::Testing::expect(!r.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
r.error().code().value,
|
||||
static_cast<std::int32_t>(PluginErrorCode::abi_mismatch));
|
||||
}
|
||||
|
||||
// Prepublication V2 readiness failure resumes V1 cleanly.
|
||||
Void::Testing::require(host.load(VOID_CABI_V1_PATH).has_value());
|
||||
auto fail_v2 =
|
||||
host.replace("cabi.calc", VOID_CABI_V2_PATH, "not_ready");
|
||||
Void::Testing::expect(!fail_v2.has_value());
|
||||
auto after = host.acquire("cabi.calc");
|
||||
Void::Testing::expect(after.health() == GenerationHealth::Healthy);
|
||||
Void::Testing::expect_eq(after.version(), std::string_view{"1.0.0"});
|
||||
|
||||
// Reload into V1 with quiesce_fail_resume_fail -> failed reload leaves old
|
||||
// generation marked Degraded.
|
||||
Void::Testing::require(
|
||||
host.replace("cabi.calc", VOID_CABI_V1_PATH,
|
||||
"quiesce_fail_resume_fail")
|
||||
.has_value());
|
||||
auto deg_attempt = host.replace("cabi.calc", VOID_CABI_V2_PATH);
|
||||
Void::Testing::expect(!deg_attempt.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
deg_attempt.error().code().value,
|
||||
static_cast<std::int32_t>(PluginErrorCode::resume_degraded));
|
||||
Void::Testing::expect(host.acquire("cabi.calc").health() ==
|
||||
GenerationHealth::Degraded);
|
||||
Void::Testing::require(host.unload("cabi.calc").has_value());
|
||||
|
||||
// Oversized checkpoint rejected without crashing or leaking.
|
||||
Void::Testing::require(
|
||||
host.load(VOID_CABI_V1_PATH, "ckpt_oversized").has_value());
|
||||
auto ov = host.replace("cabi.calc", VOID_CABI_V2_PATH);
|
||||
Void::Testing::expect(!ov.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
ov.error().code().value,
|
||||
static_cast<std::int32_t>(PluginErrorCode::budget_exceeded));
|
||||
}
|
||||
|
||||
Void::Testing::Test plugin_cabi_retention_byte_budget_and_dep_batch()
|
||||
{
|
||||
// 4-generation retention cap + byte budget.
|
||||
PluginHostLimits limits{};
|
||||
limits.allow_safe_dlclose = true;
|
||||
limits.max_retained_generations = 4;
|
||||
CModuleHost host{limits};
|
||||
|
||||
Void::Testing::require(host.load(VOID_CABI_V1_PATH).has_value());
|
||||
std::vector<GenerationLease> pins;
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
pins.push_back(host.acquire("cabi.calc"));
|
||||
auto next = host.replace("cabi.calc", VOID_CABI_V1_PATH);
|
||||
Void::Testing::require(next.has_value());
|
||||
}
|
||||
pins.push_back(host.acquire("cabi.calc"));
|
||||
// Now 4 distinct generations are pinned -> 5th replace fails retention_limit.
|
||||
auto cap_fail = host.replace("cabi.calc", VOID_CABI_V2_PATH);
|
||||
Void::Testing::expect(!cap_fail.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
cap_fail.error().code().value,
|
||||
static_cast<std::int32_t>(PluginErrorCode::retention_limit));
|
||||
Void::Testing::expect(host.mapped_bytes("cabi.calc") > 0);
|
||||
pins.clear();
|
||||
|
||||
// Byte budget rejection.
|
||||
PluginHostLimits tiny_mem{};
|
||||
tiny_mem.max_mapped_bytes_per_module = 1024;
|
||||
CModuleHost tight{tiny_mem};
|
||||
auto too_big = tight.load(VOID_CABI_V1_PATH);
|
||||
Void::Testing::expect(!too_big.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
too_big.error().code().value,
|
||||
static_cast<std::int32_t>(PluginErrorCode::budget_exceeded));
|
||||
|
||||
// Dependency batch: B depends on A (supplied out of order) -> A sorted first.
|
||||
CModuleHost batch_host;
|
||||
auto batch_ok = batch_host.load_batch(
|
||||
{{.path = VOID_CABI_DEP_B_PATH}, {.path = VOID_CABI_DEP_A_PATH}});
|
||||
Void::Testing::require(batch_ok.has_value());
|
||||
Void::Testing::expect_eq(batch_ok->size(), std::size_t{2});
|
||||
Void::Testing::expect_eq((*batch_ok)[0], std::string{"cabi.dep.a"});
|
||||
Void::Testing::expect_eq((*batch_ok)[1], std::string{"cabi.dep.b"});
|
||||
|
||||
// RestartRequired policy refuses live upgrade.
|
||||
auto rr = batch_host.replace("cabi.dep.a", VOID_CABI_DEP_A_PATH);
|
||||
Void::Testing::expect(!rr.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
rr.error().code().value,
|
||||
static_cast<std::int32_t>(PluginErrorCode::restart_required));
|
||||
|
||||
// Cycle in batch rejected before activating anything.
|
||||
CModuleHost cyc_host;
|
||||
auto cyc_res = cyc_host.load_batch(
|
||||
{{.path = VOID_CABI_CYC_A_PATH}, {.path = VOID_CABI_CYC_B_PATH}});
|
||||
Void::Testing::expect(!cyc_res.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
cyc_res.error().code().value,
|
||||
static_cast<std::int32_t>(PluginErrorCode::cycle_detected));
|
||||
}
|
||||
|
||||
Void::Testing::Test plugin_cpp_adapter_contains_exceptions_and_manager_unlocks()
|
||||
{
|
||||
struct ThrowingModule final : Void::Plugin::IModule {
|
||||
std::string_view name() const noexcept override { return "throw"; }
|
||||
Void::Result::Result<void> activate(Void::Plugin::ModuleHost&) override
|
||||
{
|
||||
throw std::runtime_error("boom");
|
||||
}
|
||||
Void::Json::Value checkpoint() const override
|
||||
{
|
||||
throw std::runtime_error("ckpt-boom");
|
||||
}
|
||||
};
|
||||
Void::Plugin::CModuleAdapterBox box{.module = std::make_unique<ThrowingModule>()};
|
||||
void_error_detail_v1 err{};
|
||||
err.struct_size = sizeof(err);
|
||||
Void::Testing::expect_eq(Void::Plugin::adapt_activate(&box, &err),
|
||||
static_cast<std::int32_t>(VOID_STATUS_INTERNAL));
|
||||
if (err.release != nullptr && err.message != nullptr) {
|
||||
Void::Testing::expect_eq(std::string_view{err.message},
|
||||
std::string_view{"boom"});
|
||||
err.release(err.release_ctx, err.message);
|
||||
}
|
||||
void_owned_bytes_v1 ck{};
|
||||
void_error_detail_v1 err2{};
|
||||
err2.struct_size = sizeof(err2);
|
||||
Void::Testing::expect_eq(Void::Plugin::adapt_checkpoint(&box, &ck, &err2),
|
||||
static_cast<std::int32_t>(VOID_STATUS_INTERNAL));
|
||||
if (err2.release != nullptr && err2.message != nullptr) {
|
||||
err2.release(err2.release_ctx, err2.message);
|
||||
}
|
||||
|
||||
// Reentrant candidate/instance validator in PluginManager must NOT deadlock
|
||||
// on m_mutex.
|
||||
#if defined(VOID_STATEFUL_V1_PATH) && defined(VOID_STATEFUL_V2_PATH)
|
||||
PluginManager* mgr_ptr = nullptr;
|
||||
PluginManager mgr{{
|
||||
.instance_validator = [&](IPlugin&, const PluginInfo&) {
|
||||
auto current = mgr_ptr->acquire("StatefulPlugin");
|
||||
auto all_names = mgr_ptr->names();
|
||||
return all_names.empty() || static_cast<bool>(current);
|
||||
},
|
||||
.candidate_validator = [&](const PluginHandle&) {
|
||||
auto current = mgr_ptr->acquire("StatefulPlugin");
|
||||
return static_cast<bool>(current) || mgr_ptr->names().empty();
|
||||
},
|
||||
}};
|
||||
mgr_ptr = &mgr;
|
||||
Void::Testing::require(mgr.load(VOID_STATEFUL_V1_PATH).has_value());
|
||||
Void::Testing::require(
|
||||
mgr.replace("StatefulPlugin", VOID_STATEFUL_V2_PATH).has_value());
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif // C ABI fixtures available
|
||||
Reference in new issue
Block a user