diff --git a/CMakeLists.txt b/CMakeLists.txt index f24aeab..e3d5e64 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -195,6 +195,8 @@ else() endif() if(BUILD_TESTS) + include("${Void_SOURCE_DIR}/cmake/VoidTesting.cmake") + add_executable(echoion_log_rotation_tests tests/test_log_rotation.cpp src/LogRotation.cpp @@ -313,6 +315,15 @@ if(BUILD_TESTS) target_link_libraries(echoion_core_url_shortener_tests PRIVATE EchoionCore) add_test(NAME echoion_core_url_shortener_tests COMMAND echoion_core_url_shortener_tests) + + add_executable(echoion_core_plugin_compatibility_tests + tests/test_plugin_compatibility.cpp) + target_include_directories(echoion_core_plugin_compatibility_tests PRIVATE + "${CMAKE_CURRENT_SOURCE_DIR}/include" + "${CMAKE_CURRENT_SOURCE_DIR}/src") + target_link_libraries(echoion_core_plugin_compatibility_tests PRIVATE + EchoionCore) + void_add_tests(echoion_core_plugin_compatibility_tests) endif() option(BUNDLE_FFMPEG "Bundle FFmpeg libraries with installation" OFF) diff --git a/include/echoion/core/PluginHost.hpp b/include/echoion/core/PluginHost.hpp index 8968124..d81dd2b 100644 --- a/include/echoion/core/PluginHost.hpp +++ b/include/echoion/core/PluginHost.hpp @@ -74,8 +74,8 @@ namespace Echoion::Core private: - void publish(std::string_view name); - void unpublish(std::string_view name); + void publish(std::string name); + void unpublish(std::string name); void notifyChanged() const; [[nodiscard]] std::string pluginConfiguration(std::string_view name) const; diff --git a/src/Module.cpp b/src/Module.cpp index 3442596..a4747c7 100644 --- a/src/Module.cpp +++ b/src/Module.cpp @@ -1,5 +1,7 @@ #include +#include "ModuleContracts.hpp" + #include #include #include @@ -36,9 +38,7 @@ namespace Echoion::CoreModule .pre_release = {}, .build = {}, }, - .abi_hash = "sha256:" - "9cb668e2a502dcfe02d145ed63635d63619732d77ff57496858343" - "82705e8d21", + .abi_hash = coreAbiHash, .abi_contract = Void::Json::object({ {"schemaVersion", 1}, {"contractId", "echoion.module.core"}, @@ -49,9 +49,7 @@ namespace Echoion::CoreModule { .module_id = "EchoionApi", .version_range = "^2.0.0", - .abi_hash = "sha256:" - "d50293d345a97e52f9844088bdd42277f566bc815a" - "21b7636d3e053f06c6c113", + .abi_hash = apiAbiHash, }, }, .activation_policy = Void::Plugin::ActivationPolicy::Reloadable, @@ -69,10 +67,7 @@ namespace Echoion::CoreModule .component_id = Api::Module::Detail::stringView("EchoionApi"), .version_range = Api::Module::Detail::stringView(">=2.0.0 <3.0.0"), - .abi_hash = Api::Module::Detail::stringView( - "sha256:" - "d50293d345a97e52f9844088bdd42277f566bc815a21b7636d3e053f06" - "c6c113"), + .abi_hash = Api::Module::Detail::stringView(apiAbiHash), .optional = 0, .reserved_bytes = {}, }, @@ -85,10 +80,7 @@ namespace Echoion::CoreModule .version_minor = ECHOION_COMPONENT_VERSION_MINOR, .version_patch = ECHOION_COMPONENT_VERSION_PATCH, .version_prerelease = {}, - .abi_hash = Api::Module::Detail::stringView( - "sha256:" - "9cb668e2a502dcfe02d145ed63635d63619732d77ff5749685834382705e8d" - "21"), + .abi_hash = Api::Module::Detail::stringView(coreAbiHash), .dependencies = dependencies, .dependency_count = std::size(dependencies), .activation_policy = ECHOION_ACTIVATION_RELOADABLE_V1, diff --git a/src/ModuleContracts.hpp b/src/ModuleContracts.hpp new file mode 100644 index 0000000..4b7cc87 --- /dev/null +++ b/src/ModuleContracts.hpp @@ -0,0 +1,11 @@ +#pragma once + +namespace Echoion::CoreModule +{ + inline constexpr auto apiAbiHash = + "sha256:" + "d50293d345a97e52f9844088bdd42277f566bc815a21b7636d3e053f06c6c113"; + inline constexpr auto coreAbiHash = + "sha256:" + "9cb668e2a502dcfe02d145ed63635d63619732d77ff5749685834382705e8d21"; +} // namespace Echoion::CoreModule diff --git a/src/PluginCompatibility.cpp b/src/PluginCompatibility.cpp new file mode 100644 index 0000000..3f962d0 --- /dev/null +++ b/src/PluginCompatibility.cpp @@ -0,0 +1,116 @@ +#include "PluginCompatibility.hpp" + +#include "ModuleContracts.hpp" + +#include +#include +#include +#include + +namespace Echoion::Core::Detail +{ + namespace + { + constexpr std::size_t maximumDescriptorText = 1024; + constexpr std::size_t maximumDependencies = 64; + + [[nodiscard]] PluginCompatibility reject(std::string reason) + { + return {.compatible = false, .reason = std::move(reason)}; + } + + [[nodiscard]] bool valid(const echoion_string_view_v1 value) noexcept + { + return value.size <= maximumDescriptorText && + (value.size == 0U || value.data != nullptr); + } + + [[nodiscard]] std::string_view + view(const echoion_string_view_v1 value) noexcept + { + return value.data == nullptr + ? std::string_view{} + : std::string_view{value.data, value.size}; + } + } // namespace + + PluginCompatibility + checkExtensionDescriptor(const echoion_module_descriptor_v1* descriptor) + { + if (descriptor == nullptr) { + return reject("stable module descriptor is null"); + } + if (descriptor->struct_size < sizeof(echoion_module_descriptor_v1) || + descriptor->abi_version != ECHOION_MODULE_ABI_VERSION) { + return reject("stable module descriptor version is incompatible"); + } + if (!valid(descriptor->component_id) || + view(descriptor->component_id).empty() || + descriptor->version_major != ECHOION_COMPONENT_VERSION_MAJOR) { + return reject( + "component identity or major version is incompatible"); + } + if (descriptor->api == nullptr || + descriptor->api->struct_size < sizeof(echoion_module_api_v1) || + descriptor->api->abi_version != ECHOION_MODULE_ABI_VERSION || + descriptor->api->create == nullptr || + descriptor->api->destroy == nullptr || + descriptor->api->activate == nullptr || + descriptor->api->quiesce == nullptr || + descriptor->api->checkpoint == nullptr || + descriptor->api->restore == nullptr || + descriptor->api->readiness == nullptr || + descriptor->api->deactivate == nullptr) { + return reject("stable module function table is incomplete"); + } + if (descriptor->dependency_count > maximumDependencies || + (descriptor->dependency_count != 0U && + descriptor->dependencies == nullptr)) { + return reject("stable module dependency table is invalid"); + } + + bool apiCompatible = false; + bool coreCompatible = false; + for (std::size_t index = 0; index < descriptor->dependency_count; + ++index) { + const auto& dependency = descriptor->dependencies[index]; + if (dependency.struct_size < sizeof(echoion_module_dependency_v1) || + !valid(dependency.component_id) || + !valid(dependency.abi_hash)) { + return reject("stable module dependency entry is invalid"); + } + const auto component = view(dependency.component_id); + const auto abiHash = view(dependency.abi_hash); + if (component == "EchoionApi" && dependency.optional == 0U) { + apiCompatible = abiHash == CoreModule::apiAbiHash; + } + if (component == "EchoionCore" && dependency.optional == 0U) { + coreCompatible = abiHash == CoreModule::coreAbiHash; + } + } + if (!apiCompatible || !coreCompatible) { + return reject("plugin was built for incompatible Echoion APIs"); + } + return {.compatible = true, .reason = {}}; + } + + PluginCompatibility + checkExtensionCandidate(const Void::Plugin::PluginHandle& candidate) + { + const auto symbol = + candidate.raw_symbol(ECHOION_MODULE_ABI_QUERY_SYMBOL); + if (symbol == nullptr) { + return reject("stable module query symbol is missing"); + } + const auto query = + reinterpret_cast(symbol); + try { + return checkExtensionDescriptor(query()); + } catch (const std::exception& error) { + return reject(std::string{"stable module query threw: "} + + error.what()); + } catch (...) { + return reject("stable module query threw"); + } + } +} // namespace Echoion::Core::Detail diff --git a/src/PluginCompatibility.hpp b/src/PluginCompatibility.hpp new file mode 100644 index 0000000..c9ccb53 --- /dev/null +++ b/src/PluginCompatibility.hpp @@ -0,0 +1,20 @@ +#pragma once + +#include + +#include + +#include + +namespace Echoion::Core::Detail +{ + struct PluginCompatibility { + bool compatible{}; + std::string reason; + }; + + [[nodiscard]] PluginCompatibility checkExtensionDescriptor( + const echoion_module_descriptor_v1* descriptor); + [[nodiscard]] PluginCompatibility + checkExtensionCandidate(const Void::Plugin::PluginHandle& candidate); +} // namespace Echoion::Core::Detail diff --git a/src/PluginHost.cpp b/src/PluginHost.cpp index 7ce596a..0104156 100644 --- a/src/PluginHost.cpp +++ b/src/PluginHost.cpp @@ -4,6 +4,8 @@ #include "echoion/core/MusicManager.hpp" #include "echoion/core/sources/TrackSource.hpp" +#include "PluginCompatibility.hpp" + #include #include #include @@ -282,11 +284,22 @@ namespace Echoion::Core const Void::Plugin::PluginInfo&) { return dynamic_cast(&plugin) != nullptr; + }, + .candidate_validator = + [](const Void::Plugin::PluginHandle& candidate) { + const auto compatibility = + Detail::checkExtensionCandidate(candidate); + if (!compatibility.compatible) { + Void::Log::warn( + "PluginHost", "Plugin '{}' rejected: {}", + candidate.info().name, compatibility.reason); + } + return compatibility.compatible; }}), configuration_(std::move(configuration)) { reloadCallback_ = plugins_.on_reloaded( - [this](std::string_view name) { publish(name); }); + [this](std::string_view name) { publish(std::string{name}); }); } PluginHost::~PluginHost() @@ -950,8 +963,9 @@ namespace Echoion::Core } } - void PluginHost::publish(std::string_view name) + void PluginHost::publish(std::string name) { + Void::Log::info("PluginHost", "Publishing Echoion plugin '{}'", name); unpublish(name); auto pin = plugins_.acquire(name); auto* extension = @@ -1006,7 +1020,7 @@ namespace Echoion::Core notifyChanged(); } - void PluginHost::unpublish(std::string_view name) + void PluginHost::unpublish(std::string name) { commands_.unregisterExternalCommands(name); } diff --git a/tests/test_plugin_compatibility.cpp b/tests/test_plugin_compatibility.cpp new file mode 100644 index 0000000..7780339 --- /dev/null +++ b/tests/test_plugin_compatibility.cpp @@ -0,0 +1,141 @@ +#include "PluginCompatibility.hpp" + +#include +#include +#include + +#include +#include +#include + +namespace +{ + echoion_string_view_v1 view(const std::string_view value) + { + return {.data = value.data(), .size = value.size()}; + } + + echoion_status_v1 create(const echoion_module_host_v1*, + echoion_module_instance_v1*) + { + return ECHOION_STATUS_OK_V1; + } + void destroy(echoion_module_instance_v1) {} + echoion_status_v1 activate(echoion_module_instance_v1) + { + return ECHOION_STATUS_OK_V1; + } + echoion_status_v1 quiesce(echoion_module_instance_v1, std::int64_t) + { + return ECHOION_STATUS_OK_V1; + } + echoion_status_v1 checkpoint(echoion_module_instance_v1, + echoion_write_bytes_fn_v1, void*) + { + return ECHOION_STATUS_OK_V1; + } + echoion_status_v1 restore(echoion_module_instance_v1, echoion_bytes_view_v1) + { + return ECHOION_STATUS_OK_V1; + } + echoion_status_v1 readiness(echoion_module_instance_v1, + echoion_module_readiness_v1*) + { + return ECHOION_STATUS_OK_V1; + } + echoion_status_v1 deactivate(echoion_module_instance_v1) + { + return ECHOION_STATUS_OK_V1; + } + + const echoion_module_api_v1 api{ + .struct_size = sizeof(echoion_module_api_v1), + .abi_version = ECHOION_MODULE_ABI_VERSION, + .create = create, + .destroy = destroy, + .activate = activate, + .quiesce = quiesce, + .checkpoint = checkpoint, + .restore = restore, + .readiness = readiness, + .deactivate = deactivate, + .reserved = {}, + }; + + std::array + dependencies(const std::string_view apiHash, + const std::string_view coreHash) + { + return {{ + { + .struct_size = sizeof(echoion_module_dependency_v1), + .component_id = view("EchoionApi"), + .version_range = view(">=2.0.0 <3.0.0"), + .abi_hash = view(apiHash), + .optional = 0, + .reserved_bytes = {}, + }, + { + .struct_size = sizeof(echoion_module_dependency_v1), + .component_id = view("EchoionCore"), + .version_range = view(">=2.0.0 <3.0.0"), + .abi_hash = view(coreHash), + .optional = 0, + .reserved_bytes = {}, + }, + }}; + } + + echoion_module_descriptor_v1 + descriptor(const std::array& values) + { + return { + .struct_size = sizeof(echoion_module_descriptor_v1), + .abi_version = ECHOION_MODULE_ABI_VERSION, + .component_id = view("EchoionPluginExample"), + .version_major = 2, + .version_minor = 0, + .version_patch = 0, + .version_prerelease = {}, + .abi_hash = view("sha256:example"), + .dependencies = values.data(), + .dependency_count = values.size(), + .activation_policy = ECHOION_ACTIVATION_RELOADABLE_V1, + .api = &api, + .reserved = {}, + }; + } +} // namespace + +Void::Testing::Test compatible_v2_extension_descriptor_is_accepted() +{ + const auto values = dependencies( + "sha256:" + "d50293d345a97e52f9844088bdd42277f566bc815a21b7636d3e053f06c6c113", + "sha256:" + "9cb668e2a502dcfe02d145ed63635d63619732d77ff5749685834382705e8d21"); + const auto candidate = descriptor(values); + const auto result = + Echoion::Core::Detail::checkExtensionDescriptor(&candidate); + Void::Testing::expect(result.compatible); +} + +Void::Testing::Test legacy_or_mismatched_extension_descriptor_is_rejected() +{ + const auto values = dependencies("sha256:old-api", "sha256:old-core"); + auto candidate = descriptor(values); + auto result = Echoion::Core::Detail::checkExtensionDescriptor(&candidate); + Void::Testing::expect(!result.compatible); + + candidate.version_major = 1; + result = Echoion::Core::Detail::checkExtensionDescriptor(&candidate); + Void::Testing::expect(!result.compatible); + + Void::Testing::expect( + !Echoion::Core::Detail::checkExtensionDescriptor(nullptr).compatible); +} + +int main(int argc, char** argv) +{ + return Void::Testing::Runner::instance().main(argc, argv); +}