From 5b4a3828568af2c68bfd25d1fce72d35e643c8df Mon Sep 17 00:00:00 2001 From: Maschell Date: Sun, 4 Aug 2024 14:12:52 +0200 Subject: [PATCH] Make PluginLinkInformation not optional anymore in PluginContainer, add stub link data if missing --- source/PluginManagement.cpp | 20 ++++++--------- source/hooks.cpp | 8 +----- source/main.cpp | 4 +-- source/patcher/hooks_patcher_static.cpp | 12 ++++----- source/plugin/PluginContainer.cpp | 25 ++++++++----------- source/plugin/PluginContainer.h | 12 ++++----- source/plugin/PluginLinkInformation.cpp | 8 ++++++ source/plugin/PluginLinkInformation.h | 4 +++ .../plugin/PluginLinkInformationFactory.cpp | 2 +- source/utils/config/ConfigUtils.cpp | 4 +-- source/utils/exports.cpp | 18 ++++++++----- 11 files changed, 59 insertions(+), 58 deletions(-) diff --git a/source/PluginManagement.cpp b/source/PluginManagement.cpp index c54aa83..6633638 100644 --- a/source/PluginManagement.cpp +++ b/source/PluginManagement.cpp @@ -44,10 +44,10 @@ PluginManagement::loadPlugins(const std::set> &plugi DisplayErrorNotificationMessage(errMsg, 15.0f); continue; } - plugins.emplace_back(std::move(*metaInfo), std::move(linkInfo), pluginData); + plugins.emplace_back(std::move(*metaInfo), std::move(*linkInfo), pluginData); } else { DEBUG_FUNCTION_LINE_INFO("We want to skip %s by %s", metaInfo->getName().c_str(), metaInfo->getAuthor().c_str()); - plugins.emplace_back(std::move(*metaInfo), std::nullopt, pluginData); + plugins.emplace_back(std::move(*metaInfo), PluginLinkInformation::CreateStub(), pluginData); } } else { auto errMsg = string_format("Failed to load plugin: %s", pluginData->getSource().c_str()); @@ -156,13 +156,13 @@ bool PluginManagement::doRelocations(const std::vector &plugins OSDynLoad_SetAllocator(CustomDynLoadAlloc, CustomDynLoadFree); for (const auto &pluginContainer : plugins) { - if (!pluginContainer.isPluginLinkedAndLoaded()) { + if (pluginContainer.isLinkedAndLoaded()) { continue; } DEBUG_FUNCTION_LINE_VERBOSE("Doing relocations for plugin: %s", pluginContainer.getMetaInformation().getName().c_str()); - if (!PluginManagement::doRelocation(pluginContainer.getPluginLinkInformation()->getRelocationDataList(), + if (!PluginManagement::doRelocation(pluginContainer.getPluginLinkInformation().getRelocationDataList(), trampData, - pluginContainer.getPluginLinkInformation()->getTrampolineId(), + pluginContainer.getPluginLinkInformation().getTrampolineId(), usedRPls)) { return false; } @@ -175,10 +175,7 @@ bool PluginManagement::doRelocations(const std::vector &plugins bool PluginManagement::RestoreFunctionPatches(std::vector &plugins) { for (auto &cur : std::ranges::reverse_view(plugins)) { - if (!cur.isPluginLinkedAndLoaded()) { - continue; - } - for (auto &curFunction : std::ranges::reverse_view(cur.getPluginLinkInformation()->getFunctionDataList())) { + for (auto &curFunction : std::ranges::reverse_view(cur.getPluginLinkInformation().getFunctionDataList())) { if (!curFunction.RemovePatch()) { return false; } @@ -189,10 +186,7 @@ bool PluginManagement::RestoreFunctionPatches(std::vector &plug bool PluginManagement::DoFunctionPatches(std::vector &plugins) { for (auto &cur : plugins) { - if (!cur.isPluginLinkedAndLoaded()) { - continue; - } - for (auto &curFunction : cur.getPluginLinkInformation()->getFunctionDataList()) { + for (auto &curFunction : cur.getPluginLinkInformation().getFunctionDataList()) { if (!curFunction.AddPatch()) { DEBUG_FUNCTION_LINE_ERR("Failed to add function patch for: plugin %s", cur.getMetaInformation().getName().c_str()); return false; diff --git a/source/hooks.cpp b/source/hooks.cpp index 0e8cbe8..b8bb48d 100644 --- a/source/hooks.cpp +++ b/source/hooks.cpp @@ -38,18 +38,12 @@ static const char **hook_names = (const char *[]){ void CallHook(const std::vector &plugins, wups_loader_hook_type_t hook_type) { DEBUG_FUNCTION_LINE_VERBOSE("Calling hook of type %s [%d]", hook_names[hook_type], hook_type); for (const auto &plugin : plugins) { - if (!plugin.isPluginLinkedAndLoaded()) { - return; - } CallHook(plugin, hook_type); } } void CallHook(const PluginContainer &plugin, wups_loader_hook_type_t hook_type) { - if (!plugin.isPluginLinkedAndLoaded()) { - return; - } - for (const auto &hook : plugin.getPluginLinkInformation()->getHookDataList()) { + for (const auto &hook : plugin.getPluginLinkInformation().getHookDataList()) { if (hook.getType() == hook_type) { DEBUG_FUNCTION_LINE_VERBOSE("Calling hook of type %s for plugin %s [%d]", hook_names[hook.getType()], plugin.getMetaInformation().getName().c_str(), hook_type); void *func_ptr = hook.getFunctionPointer(); diff --git a/source/main.cpp b/source/main.cpp index fb941f6..69a2860 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -191,10 +191,10 @@ WUMS_APPLICATION_STARTS() { void CheckCleanupCallbackUsage(const std::vector &plugins) { auto *curThread = OSGetCurrentThread(); for (const auto &cur : plugins) { - if (!cur.isPluginLinkedAndLoaded()) { + if (!cur.isLinkedAndLoaded()) { continue; } - auto textSection = cur.getPluginLinkInformation()->getSectionInfo(".text"); + auto textSection = cur.getPluginLinkInformation().getSectionInfo(".text"); if (!textSection) { continue; } diff --git a/source/patcher/hooks_patcher_static.cpp b/source/patcher/hooks_patcher_static.cpp index e1ec617..c7a8b5a 100644 --- a/source/patcher/hooks_patcher_static.cpp +++ b/source/patcher/hooks_patcher_static.cpp @@ -146,10 +146,10 @@ DECL_FUNCTION(uint32_t, SC17_FindClosestSymbol, char *moduleNameBuffer, uint32_t moduleNameBufferLength) { for (const auto &plugin : gLoadedPlugins) { - if (!plugin.isPluginLinkedAndLoaded()) { + if (!plugin.isLinkedAndLoaded()) { continue; } - const auto sectionInfo = plugin.getPluginLinkInformation()->getSectionInfo(".text"); + const auto sectionInfo = plugin.getPluginLinkInformation().getSectionInfo(".text"); if (!sectionInfo) { continue; } @@ -159,7 +159,7 @@ DECL_FUNCTION(uint32_t, SC17_FindClosestSymbol, } strncpy(moduleNameBuffer, plugin.getMetaInformation().getName().c_str(), moduleNameBufferLength - 1); - auto functionSymbolData = plugin.getPluginLinkInformation()->getNearestFunctionSymbolData(addr); + auto functionSymbolData = plugin.getPluginLinkInformation().getNearestFunctionSymbolData(addr); if (functionSymbolData) { strncpy(symbolNameBuffer, functionSymbolData->getName().c_str(), moduleNameBufferLength - 1); if (outDistance) { @@ -182,10 +182,10 @@ DECL_FUNCTION(uint32_t, SC17_FindClosestSymbol, DECL_FUNCTION(uint32_t, KiGetAppSymbolName, uint32_t addr, char *buffer, int32_t bufSize) { for (const auto &plugin : gLoadedPlugins) { - if (!plugin.isPluginLinkedAndLoaded()) { + if (!plugin.isLinkedAndLoaded()) { continue; } - const auto sectionInfo = plugin.getPluginLinkInformation()->getSectionInfo(".text"); + const auto sectionInfo = plugin.getPluginLinkInformation().getSectionInfo(".text"); if (!sectionInfo) { continue; } @@ -201,7 +201,7 @@ DECL_FUNCTION(uint32_t, KiGetAppSymbolName, uint32_t addr, char *buffer, int32_t } strncpy(buffer, plugin.getMetaInformation().getName().c_str(), bufSize - 1); - const auto functionSymbolData = plugin.getPluginLinkInformation()->getNearestFunctionSymbolData(addr); + const auto functionSymbolData = plugin.getPluginLinkInformation().getNearestFunctionSymbolData(addr); if (functionSymbolData) { buffer[pluginNameLen] = '|'; buffer[pluginNameLen + 1] = '\0'; diff --git a/source/plugin/PluginContainer.cpp b/source/plugin/PluginContainer.cpp index 439aafe..5f975b8 100644 --- a/source/plugin/PluginContainer.cpp +++ b/source/plugin/PluginContainer.cpp @@ -1,6 +1,6 @@ #include "PluginContainer.h" -PluginContainer::PluginContainer(PluginMetaInformation metaInformation, std::optional pluginLinkInformation, std::shared_ptr pluginData) +PluginContainer::PluginContainer(PluginMetaInformation metaInformation, PluginLinkInformation pluginLinkInformation, std::shared_ptr pluginData) : mMetaInformation(std::move(metaInformation)), mPluginLinkInformation(std::move(pluginLinkInformation)), mPluginData(std::move(pluginData)) { @@ -33,22 +33,13 @@ const PluginMetaInformation &PluginContainer::getMetaInformation() const { return this->mMetaInformation; } -bool PluginContainer::isPluginLinkedAndLoaded() const { - return this->mPluginLinkInformation.has_value(); + +const PluginLinkInformation &PluginContainer::getPluginLinkInformation() const { + return this->mPluginLinkInformation; } -const PluginLinkInformation *PluginContainer::getPluginLinkInformation() const { - if (this->mPluginLinkInformation.has_value()) { - return this->mPluginLinkInformation.operator->(); - } - return nullptr; -} - -PluginLinkInformation *PluginContainer::getPluginLinkInformation() { - if (this->mPluginLinkInformation.has_value()) { - return this->mPluginLinkInformation.operator->(); - } - return nullptr; +PluginLinkInformation &PluginContainer::getPluginLinkInformation() { + return this->mPluginLinkInformation; } std::shared_ptr PluginContainer::getPluginDataCopy() const { @@ -67,6 +58,10 @@ void PluginContainer::setConfigData(const PluginConfigData &pluginConfigData) { mPluginConfigData = pluginConfigData; } +bool PluginContainer::isLinkedAndLoaded() const { + return mPluginLinkInformation.hasValidData(); +} + WUPSStorageError PluginContainer::OpenStorage() { if (getMetaInformation().getWUPSVersion() < WUPSVersion(0, 8, 0)) { return WUPS_STORAGE_ERROR_SUCCESS; diff --git a/source/plugin/PluginContainer.h b/source/plugin/PluginContainer.h index 62d670f..8209efe 100644 --- a/source/plugin/PluginContainer.h +++ b/source/plugin/PluginContainer.h @@ -28,7 +28,7 @@ class PluginContainer { public: - PluginContainer(PluginMetaInformation metaInformation, std::optional pluginLinkInformation, std::shared_ptr pluginData); + PluginContainer(PluginMetaInformation metaInformation, PluginLinkInformation pluginLinkInformation, std::shared_ptr pluginData); PluginContainer(const PluginContainer &) = delete; @@ -38,18 +38,18 @@ public: [[nodiscard]] const PluginMetaInformation &getMetaInformation() const; - [[nodiscard]] const PluginLinkInformation *getPluginLinkInformation() const; + [[nodiscard]] const PluginLinkInformation &getPluginLinkInformation() const; - [[nodiscard]] PluginLinkInformation *getPluginLinkInformation(); + [[nodiscard]] PluginLinkInformation &getPluginLinkInformation(); [[nodiscard]] std::shared_ptr getPluginDataCopy() const; - [[nodiscard]] bool isPluginLinkedAndLoaded() const; - [[nodiscard]] uint32_t getHandle() const; [[nodiscard]] const std::optional &getConfigData() const; + [[nodiscard]] bool isLinkedAndLoaded() const; + void setConfigData(const PluginConfigData &pluginConfigData); WUPSStorageError OpenStorage(); @@ -62,7 +62,7 @@ public: private: PluginMetaInformation mMetaInformation; - std::optional mPluginLinkInformation; + PluginLinkInformation mPluginLinkInformation; std::shared_ptr mPluginData; std::optional mPluginConfigData; diff --git a/source/plugin/PluginLinkInformation.cpp b/source/plugin/PluginLinkInformation.cpp index e900ade..3ed8952 100644 --- a/source/plugin/PluginLinkInformation.cpp +++ b/source/plugin/PluginLinkInformation.cpp @@ -110,3 +110,11 @@ const HeapMemoryFixedSize &PluginLinkInformation::getTextMemory() const { const HeapMemoryFixedSize &PluginLinkInformation::getDataMemory() const { return mAllocatedDataMemoryAddress; } + +PluginLinkInformation PluginLinkInformation::CreateStub() { + return {}; +} + +bool PluginLinkInformation::hasValidData() const { + return mAllocatedDataMemoryAddress.size() > 0 && mAllocatedTextMemoryAddress.size() > 0; +} diff --git a/source/plugin/PluginLinkInformation.h b/source/plugin/PluginLinkInformation.h index 00d6889..9144038 100644 --- a/source/plugin/PluginLinkInformation.h +++ b/source/plugin/PluginLinkInformation.h @@ -42,6 +42,8 @@ struct FunctionSymbolDataComparator { class PluginLinkInformation { public: + static PluginLinkInformation CreateStub(); + PluginLinkInformation(const PluginLinkInformation &) = delete; PluginLinkInformation(PluginLinkInformation &&src); @@ -68,6 +70,8 @@ public: [[nodiscard]] const HeapMemoryFixedSize &getDataMemory() const; + [[nodiscard]] bool hasValidData() const; + private: PluginLinkInformation() = default; diff --git a/source/plugin/PluginLinkInformationFactory.cpp b/source/plugin/PluginLinkInformationFactory.cpp index a5ca88d..9fd1861 100644 --- a/source/plugin/PluginLinkInformationFactory.cpp +++ b/source/plugin/PluginLinkInformationFactory.cpp @@ -337,7 +337,7 @@ bool PluginLinkInformationFactory::addImportRelocationData(PluginLinkInformation } bool PluginLinkInformationFactory::linkSection(const elfio &reader, uint32_t section_index, uint32_t destination, uint32_t base_text, uint32_t base_data, - std::vector &trampolineData, uint8_t trampolineId) { + std::vector &trampolineData, uint8_t trampolineId) { uint32_t sec_num = reader.sections.size(); for (uint32_t i = 0; i < sec_num; ++i) { diff --git a/source/utils/config/ConfigUtils.cpp b/source/utils/config/ConfigUtils.cpp index f7313a6..6ce44b5 100644 --- a/source/utils/config/ConfigUtils.cpp +++ b/source/utils/config/ConfigUtils.cpp @@ -76,7 +76,7 @@ void ConfigUtils::displayMenu() { std::vector configs; for (const auto &plugin : gLoadedPlugins) { - if (!plugin.isPluginLinkedAndLoaded()) { + if (!plugin.isLinkedAndLoaded()) { continue; } GeneralConfigInformation info; @@ -103,7 +103,7 @@ void ConfigUtils::displayMenu() { DEBUG_FUNCTION_LINE_ERR("Failed to create config for plugin: \"%s\"", info.name.c_str()); } } else { - for (const auto &hook : plugin.getPluginLinkInformation()->getHookDataList()) { + for (const auto &hook : plugin.getPluginLinkInformation().getHookDataList()) { if (hook.getType() == WUPS_LOADER_HOOK_GET_CONFIG_DEPRECATED) { if (hook.getFunctionPointer() == nullptr) { DEBUG_FUNCTION_LINE_ERR("Hook had invalid ptr"); diff --git a/source/utils/exports.cpp b/source/utils/exports.cpp index f096a6c..1ae4949 100644 --- a/source/utils/exports.cpp +++ b/source/utils/exports.cpp @@ -260,7 +260,7 @@ extern "C" PluginBackendApiErrorType WUPSGetNumberOfLoadedPlugins(uint32_t *outC if (outCount == nullptr) { return PLUGIN_BACKEND_API_ERROR_INVALID_ARG; } - *outCount = gLoadedPlugins.size(); + *outCount = std::count_if(gLoadedPlugins.begin(), gLoadedPlugins.end(), [](const auto &cur) { return cur.isLinkedAndLoaded(); }); return PLUGIN_BACKEND_API_ERROR_NONE; } @@ -272,9 +272,12 @@ extern "C" PluginBackendApiErrorType WUPSGetSectionInformationForPlugin(const wu if (handle != 0 && plugin_section_list != nullptr && buffer_size != 0) { bool found = false; for (const auto &curContainer : gLoadedPlugins) { - if (curContainer.isPluginLinkedAndLoaded() && curContainer.getHandle() == handle) { + if (!curContainer.isLinkedAndLoaded()) { + continue; + } + if (curContainer.getHandle() == handle) { found = true; - const auto §ionInfoList = curContainer.getPluginLinkInformation()->getSectionInfoList(); + const auto §ionInfoList = curContainer.getPluginLinkInformation().getSectionInfoList(); uint32_t offset = 0; for (auto const &[key, sectionInfo] : sectionInfoList) { @@ -316,9 +319,12 @@ extern "C" PluginBackendApiErrorType WUPSGetSectionMemoryAddresses(wups_backend_ return PLUGIN_BACKEND_API_ERROR_INVALID_ARG; } for (const auto &curContainer : gLoadedPlugins) { - if (curContainer.isPluginLinkedAndLoaded() && curContainer.getHandle() == handle) { - *textAddress = (void *) curContainer.getPluginLinkInformation()->getTextMemory().data(); - *dataAddress = (void *) curContainer.getPluginLinkInformation()->getDataMemory().data(); + if (!curContainer.isLinkedAndLoaded()) { + continue; + } + if (curContainer.getHandle() == handle) { + *textAddress = (void *) curContainer.getPluginLinkInformation().getTextMemory().data(); + *dataAddress = (void *) curContainer.getPluginLinkInformation().getDataMemory().data(); return PLUGIN_BACKEND_API_ERROR_NONE; } }