From 5199b69cd692bbc01c8e0ce1f07000ff73ae6ec7 Mon Sep 17 00:00:00 2001 From: Philipp Schuster Date: Mon, 26 Aug 2024 15:57:46 +0200 Subject: [PATCH] plugin-interface: use `extern "C"` for plugin init function Currently, smartvmi loads plugins dynamically by loading their shared object. From that shared object, the main entry function is searched. The problem with that is that the current approach depends on the hard-coded C++-mangled symbol name. This however depends on the toolchain (compiler version etc.). Specifically, when compiled on NixOS, the name didn't match the upstream source code version. As this symbol name is only relevant when loading a shared object into memory as part of the shared object's metadata analysis, this name doesn't have to be unique across plugins. Therefore, to get rid of the hardcoded symbol name, we use "extern C" to make things easier and more portable. The function was renamed from "init" to "vmicore_plugin_init" to have a more expressive name. The disadvantage is that the caller can no longer implicitly verify the function signature used when the plugin was compiled. However, with a few more assertions, we can mitigate this mostly. --- plugins/apitracing/src/lib/ApiTracing.cpp | 8 ++++---- plugins/inmemoryscanner/src/lib/InMemory.cpp | 8 ++++---- plugins/template/src/lib/Template.cpp | 11 ++++++----- vmicore/src/include/vmicore/plugins/IPlugin.h | 4 ++-- vmicore/src/lib/plugins/PluginSystem.cpp | 6 ++---- vmicore/src/lib/plugins/PluginSystem.h | 2 ++ 6 files changed, 20 insertions(+), 19 deletions(-) mode change 100755 => 100644 plugins/template/src/lib/Template.cpp diff --git a/plugins/apitracing/src/lib/ApiTracing.cpp b/plugins/apitracing/src/lib/ApiTracing.cpp index 5cfa0dcb..85be9b27 100644 --- a/plugins/apitracing/src/lib/ApiTracing.cpp +++ b/plugins/apitracing/src/lib/ApiTracing.cpp @@ -99,10 +99,10 @@ namespace ApiTracing } } -std::unique_ptr -VmiCore::Plugin::init(PluginInterface* pluginInterface, - std::shared_ptr config, // NOLINT(performance-unnecessary-value-param) - std::vector args) +extern "C" std::unique_ptr VmiCore::Plugin::vmicore_plugin_init( + PluginInterface* pluginInterface, + std::shared_ptr config, // NOLINT(performance-unnecessary-value-param) + std::vector args) { return std::make_unique(pluginInterface, *config, args); } diff --git a/plugins/inmemoryscanner/src/lib/InMemory.cpp b/plugins/inmemoryscanner/src/lib/InMemory.cpp index f68c626f..f4b9d20a 100644 --- a/plugins/inmemoryscanner/src/lib/InMemory.cpp +++ b/plugins/inmemoryscanner/src/lib/InMemory.cpp @@ -66,10 +66,10 @@ namespace InMemoryScanner } } -std::unique_ptr -VmiCore::Plugin::init(PluginInterface* pluginInterface, - std::shared_ptr config, // NOLINT(performance-unnecessary-value-param) - std::vector args) +extern "C" std::unique_ptr VmiCore::Plugin::vmicore_plugin_init( + PluginInterface* pluginInterface, + std::shared_ptr config, // NOLINT(performance-unnecessary-value-param) + std::vector args) { return std::make_unique(pluginInterface, *config, args); } diff --git a/plugins/template/src/lib/Template.cpp b/plugins/template/src/lib/Template.cpp old mode 100755 new mode 100644 index c2a10465..c9d22181 --- a/plugins/template/src/lib/Template.cpp +++ b/plugins/template/src/lib/Template.cpp @@ -51,12 +51,13 @@ namespace Template } } -// This is the init function. It is called by VmiCore and is responsible for creating an instance of a plugin. +// This is the init function. It is linked and called dynamically at runtime by +// VmiCore and is responsible for creating an instance of a plugin. // All plugins have to inherit from IPlugin. -std::unique_ptr -VmiCore::Plugin::init(PluginInterface* pluginInterface, - std::shared_ptr config, // NOLINT(performance-unnecessary-value-param) - std::vector args) +extern "C" std::unique_ptr VmiCore::Plugin::vmicore_plugin_init( + PluginInterface* pluginInterface, + std::shared_ptr config, // NOLINT(performance-unnecessary-value-param) + std::vector args) { return std::make_unique(pluginInterface, *config, args); } diff --git a/vmicore/src/include/vmicore/plugins/IPlugin.h b/vmicore/src/include/vmicore/plugins/IPlugin.h index 9b6aef11..d598e8f8 100644 --- a/vmicore/src/include/vmicore/plugins/IPlugin.h +++ b/vmicore/src/include/vmicore/plugins/IPlugin.h @@ -44,8 +44,8 @@ namespace VmiCore::Plugin * @param args Commandline arguments passed to this specific plugin. Elements are whitespace separated. * @return An instance of the plugin. Has to implement the IPlugin interface. Lifetime will be managed by VMICore. */ - extern std::unique_ptr - init(PluginInterface* pluginInterface, std::shared_ptr config, std::vector args); + extern "C" std::unique_ptr + vmicore_plugin_init(PluginInterface* pluginInterface, std::shared_ptr config, std::vector args); } #endif // APITRACING_IPLUGIN_H diff --git a/vmicore/src/lib/plugins/PluginSystem.cpp b/vmicore/src/lib/plugins/PluginSystem.cpp index cba1bed3..c0400120 100644 --- a/vmicore/src/lib/plugins/PluginSystem.cpp +++ b/vmicore/src/lib/plugins/PluginSystem.cpp @@ -161,10 +161,8 @@ namespace VmiCore Plugin::PluginInterface::API_VERSION)); } - auto pluginInitFunction = std::bit_cast( - dlsym(libraryHandle, - "_ZN7VmiCore6Plugin4initEPNS0_15PluginInterfaceENSt3__110shared_ptrINS0_13IPluginConfigEEENS3_" - "6vectorINS3_12basic_stringIcNS3_11char_traitsIcEENS3_9allocatorIcEEEENSB_ISD_EEEE")); + auto pluginInitFunction = + std::bit_cast(dlsym(libraryHandle, PLUGIN_INIT_FUNCTION.data())); dlErrorMessage = dlerror(); if (dlErrorMessage != nullptr) { diff --git a/vmicore/src/lib/plugins/PluginSystem.h b/vmicore/src/lib/plugins/PluginSystem.h index c8be60c0..113bea51 100644 --- a/vmicore/src/lib/plugins/PluginSystem.h +++ b/vmicore/src/lib/plugins/PluginSystem.h @@ -17,6 +17,8 @@ namespace VmiCore { + constexpr std::string_view PLUGIN_INIT_FUNCTION = "vmicore_plugin_init"; + class IPluginSystem : public Plugin::PluginInterface { public: