Make PluginLinkInformation not optional anymore in PluginContainer, add stub link data if missing

This commit is contained in:
Maschell 2024-08-04 14:12:52 +02:00
parent 050a078b08
commit 5b4a382856
11 changed files with 59 additions and 58 deletions

View File

@ -44,10 +44,10 @@ PluginManagement::loadPlugins(const std::set<std::shared_ptr<PluginData>> &plugi
DisplayErrorNotificationMessage(errMsg, 15.0f); DisplayErrorNotificationMessage(errMsg, 15.0f);
continue; continue;
} }
plugins.emplace_back(std::move(*metaInfo), std::move(linkInfo), pluginData); plugins.emplace_back(std::move(*metaInfo), std::move(*linkInfo), pluginData);
} else { } else {
DEBUG_FUNCTION_LINE_INFO("We want to skip %s by %s", metaInfo->getName().c_str(), metaInfo->getAuthor().c_str()); 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 { } else {
auto errMsg = string_format("Failed to load plugin: %s", pluginData->getSource().c_str()); auto errMsg = string_format("Failed to load plugin: %s", pluginData->getSource().c_str());
@ -156,13 +156,13 @@ bool PluginManagement::doRelocations(const std::vector<PluginContainer> &plugins
OSDynLoad_SetAllocator(CustomDynLoadAlloc, CustomDynLoadFree); OSDynLoad_SetAllocator(CustomDynLoadAlloc, CustomDynLoadFree);
for (const auto &pluginContainer : plugins) { for (const auto &pluginContainer : plugins) {
if (!pluginContainer.isPluginLinkedAndLoaded()) { if (pluginContainer.isLinkedAndLoaded()) {
continue; continue;
} }
DEBUG_FUNCTION_LINE_VERBOSE("Doing relocations for plugin: %s", pluginContainer.getMetaInformation().getName().c_str()); 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, trampData,
pluginContainer.getPluginLinkInformation()->getTrampolineId(), pluginContainer.getPluginLinkInformation().getTrampolineId(),
usedRPls)) { usedRPls)) {
return false; return false;
} }
@ -175,10 +175,7 @@ bool PluginManagement::doRelocations(const std::vector<PluginContainer> &plugins
bool PluginManagement::RestoreFunctionPatches(std::vector<PluginContainer> &plugins) { bool PluginManagement::RestoreFunctionPatches(std::vector<PluginContainer> &plugins) {
for (auto &cur : std::ranges::reverse_view(plugins)) { for (auto &cur : std::ranges::reverse_view(plugins)) {
if (!cur.isPluginLinkedAndLoaded()) { for (auto &curFunction : std::ranges::reverse_view(cur.getPluginLinkInformation().getFunctionDataList())) {
continue;
}
for (auto &curFunction : std::ranges::reverse_view(cur.getPluginLinkInformation()->getFunctionDataList())) {
if (!curFunction.RemovePatch()) { if (!curFunction.RemovePatch()) {
return false; return false;
} }
@ -189,10 +186,7 @@ bool PluginManagement::RestoreFunctionPatches(std::vector<PluginContainer> &plug
bool PluginManagement::DoFunctionPatches(std::vector<PluginContainer> &plugins) { bool PluginManagement::DoFunctionPatches(std::vector<PluginContainer> &plugins) {
for (auto &cur : plugins) { for (auto &cur : plugins) {
if (!cur.isPluginLinkedAndLoaded()) { for (auto &curFunction : cur.getPluginLinkInformation().getFunctionDataList()) {
continue;
}
for (auto &curFunction : cur.getPluginLinkInformation()->getFunctionDataList()) {
if (!curFunction.AddPatch()) { if (!curFunction.AddPatch()) {
DEBUG_FUNCTION_LINE_ERR("Failed to add function patch for: plugin %s", cur.getMetaInformation().getName().c_str()); DEBUG_FUNCTION_LINE_ERR("Failed to add function patch for: plugin %s", cur.getMetaInformation().getName().c_str());
return false; return false;

View File

@ -38,18 +38,12 @@ static const char **hook_names = (const char *[]){
void CallHook(const std::vector<PluginContainer> &plugins, wups_loader_hook_type_t hook_type) { void CallHook(const std::vector<PluginContainer> &plugins, wups_loader_hook_type_t hook_type) {
DEBUG_FUNCTION_LINE_VERBOSE("Calling hook of type %s [%d]", hook_names[hook_type], hook_type); DEBUG_FUNCTION_LINE_VERBOSE("Calling hook of type %s [%d]", hook_names[hook_type], hook_type);
for (const auto &plugin : plugins) { for (const auto &plugin : plugins) {
if (!plugin.isPluginLinkedAndLoaded()) {
return;
}
CallHook(plugin, hook_type); CallHook(plugin, hook_type);
} }
} }
void CallHook(const PluginContainer &plugin, wups_loader_hook_type_t hook_type) { void CallHook(const PluginContainer &plugin, wups_loader_hook_type_t hook_type) {
if (!plugin.isPluginLinkedAndLoaded()) { for (const auto &hook : plugin.getPluginLinkInformation().getHookDataList()) {
return;
}
for (const auto &hook : plugin.getPluginLinkInformation()->getHookDataList()) {
if (hook.getType() == hook_type) { 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); 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(); void *func_ptr = hook.getFunctionPointer();

View File

@ -191,10 +191,10 @@ WUMS_APPLICATION_STARTS() {
void CheckCleanupCallbackUsage(const std::vector<PluginContainer> &plugins) { void CheckCleanupCallbackUsage(const std::vector<PluginContainer> &plugins) {
auto *curThread = OSGetCurrentThread(); auto *curThread = OSGetCurrentThread();
for (const auto &cur : plugins) { for (const auto &cur : plugins) {
if (!cur.isPluginLinkedAndLoaded()) { if (!cur.isLinkedAndLoaded()) {
continue; continue;
} }
auto textSection = cur.getPluginLinkInformation()->getSectionInfo(".text"); auto textSection = cur.getPluginLinkInformation().getSectionInfo(".text");
if (!textSection) { if (!textSection) {
continue; continue;
} }

View File

@ -146,10 +146,10 @@ DECL_FUNCTION(uint32_t, SC17_FindClosestSymbol,
char *moduleNameBuffer, char *moduleNameBuffer,
uint32_t moduleNameBufferLength) { uint32_t moduleNameBufferLength) {
for (const auto &plugin : gLoadedPlugins) { for (const auto &plugin : gLoadedPlugins) {
if (!plugin.isPluginLinkedAndLoaded()) { if (!plugin.isLinkedAndLoaded()) {
continue; continue;
} }
const auto sectionInfo = plugin.getPluginLinkInformation()->getSectionInfo(".text"); const auto sectionInfo = plugin.getPluginLinkInformation().getSectionInfo(".text");
if (!sectionInfo) { if (!sectionInfo) {
continue; continue;
} }
@ -159,7 +159,7 @@ DECL_FUNCTION(uint32_t, SC17_FindClosestSymbol,
} }
strncpy(moduleNameBuffer, plugin.getMetaInformation().getName().c_str(), moduleNameBufferLength - 1); strncpy(moduleNameBuffer, plugin.getMetaInformation().getName().c_str(), moduleNameBufferLength - 1);
auto functionSymbolData = plugin.getPluginLinkInformation()->getNearestFunctionSymbolData(addr); auto functionSymbolData = plugin.getPluginLinkInformation().getNearestFunctionSymbolData(addr);
if (functionSymbolData) { if (functionSymbolData) {
strncpy(symbolNameBuffer, functionSymbolData->getName().c_str(), moduleNameBufferLength - 1); strncpy(symbolNameBuffer, functionSymbolData->getName().c_str(), moduleNameBufferLength - 1);
if (outDistance) { 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) { DECL_FUNCTION(uint32_t, KiGetAppSymbolName, uint32_t addr, char *buffer, int32_t bufSize) {
for (const auto &plugin : gLoadedPlugins) { for (const auto &plugin : gLoadedPlugins) {
if (!plugin.isPluginLinkedAndLoaded()) { if (!plugin.isLinkedAndLoaded()) {
continue; continue;
} }
const auto sectionInfo = plugin.getPluginLinkInformation()->getSectionInfo(".text"); const auto sectionInfo = plugin.getPluginLinkInformation().getSectionInfo(".text");
if (!sectionInfo) { if (!sectionInfo) {
continue; 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); 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) { if (functionSymbolData) {
buffer[pluginNameLen] = '|'; buffer[pluginNameLen] = '|';
buffer[pluginNameLen + 1] = '\0'; buffer[pluginNameLen + 1] = '\0';

View File

@ -1,6 +1,6 @@
#include "PluginContainer.h" #include "PluginContainer.h"
PluginContainer::PluginContainer(PluginMetaInformation metaInformation, std::optional<PluginLinkInformation> pluginLinkInformation, std::shared_ptr<PluginData> pluginData) PluginContainer::PluginContainer(PluginMetaInformation metaInformation, PluginLinkInformation pluginLinkInformation, std::shared_ptr<PluginData> pluginData)
: mMetaInformation(std::move(metaInformation)), : mMetaInformation(std::move(metaInformation)),
mPluginLinkInformation(std::move(pluginLinkInformation)), mPluginLinkInformation(std::move(pluginLinkInformation)),
mPluginData(std::move(pluginData)) { mPluginData(std::move(pluginData)) {
@ -33,22 +33,13 @@ const PluginMetaInformation &PluginContainer::getMetaInformation() const {
return this->mMetaInformation; 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 { PluginLinkInformation &PluginContainer::getPluginLinkInformation() {
if (this->mPluginLinkInformation.has_value()) { return this->mPluginLinkInformation;
return this->mPluginLinkInformation.operator->();
}
return nullptr;
}
PluginLinkInformation *PluginContainer::getPluginLinkInformation() {
if (this->mPluginLinkInformation.has_value()) {
return this->mPluginLinkInformation.operator->();
}
return nullptr;
} }
std::shared_ptr<PluginData> PluginContainer::getPluginDataCopy() const { std::shared_ptr<PluginData> PluginContainer::getPluginDataCopy() const {
@ -67,6 +58,10 @@ void PluginContainer::setConfigData(const PluginConfigData &pluginConfigData) {
mPluginConfigData = pluginConfigData; mPluginConfigData = pluginConfigData;
} }
bool PluginContainer::isLinkedAndLoaded() const {
return mPluginLinkInformation.hasValidData();
}
WUPSStorageError PluginContainer::OpenStorage() { WUPSStorageError PluginContainer::OpenStorage() {
if (getMetaInformation().getWUPSVersion() < WUPSVersion(0, 8, 0)) { if (getMetaInformation().getWUPSVersion() < WUPSVersion(0, 8, 0)) {
return WUPS_STORAGE_ERROR_SUCCESS; return WUPS_STORAGE_ERROR_SUCCESS;

View File

@ -28,7 +28,7 @@
class PluginContainer { class PluginContainer {
public: public:
PluginContainer(PluginMetaInformation metaInformation, std::optional<PluginLinkInformation> pluginLinkInformation, std::shared_ptr<PluginData> pluginData); PluginContainer(PluginMetaInformation metaInformation, PluginLinkInformation pluginLinkInformation, std::shared_ptr<PluginData> pluginData);
PluginContainer(const PluginContainer &) = delete; PluginContainer(const PluginContainer &) = delete;
@ -38,18 +38,18 @@ public:
[[nodiscard]] const PluginMetaInformation &getMetaInformation() const; [[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<PluginData> getPluginDataCopy() const; [[nodiscard]] std::shared_ptr<PluginData> getPluginDataCopy() const;
[[nodiscard]] bool isPluginLinkedAndLoaded() const;
[[nodiscard]] uint32_t getHandle() const; [[nodiscard]] uint32_t getHandle() const;
[[nodiscard]] const std::optional<PluginConfigData> &getConfigData() const; [[nodiscard]] const std::optional<PluginConfigData> &getConfigData() const;
[[nodiscard]] bool isLinkedAndLoaded() const;
void setConfigData(const PluginConfigData &pluginConfigData); void setConfigData(const PluginConfigData &pluginConfigData);
WUPSStorageError OpenStorage(); WUPSStorageError OpenStorage();
@ -62,7 +62,7 @@ public:
private: private:
PluginMetaInformation mMetaInformation; PluginMetaInformation mMetaInformation;
std::optional<PluginLinkInformation> mPluginLinkInformation; PluginLinkInformation mPluginLinkInformation;
std::shared_ptr<PluginData> mPluginData; std::shared_ptr<PluginData> mPluginData;
std::optional<PluginConfigData> mPluginConfigData; std::optional<PluginConfigData> mPluginConfigData;

View File

@ -110,3 +110,11 @@ const HeapMemoryFixedSize &PluginLinkInformation::getTextMemory() const {
const HeapMemoryFixedSize &PluginLinkInformation::getDataMemory() const { const HeapMemoryFixedSize &PluginLinkInformation::getDataMemory() const {
return mAllocatedDataMemoryAddress; return mAllocatedDataMemoryAddress;
} }
PluginLinkInformation PluginLinkInformation::CreateStub() {
return {};
}
bool PluginLinkInformation::hasValidData() const {
return mAllocatedDataMemoryAddress.size() > 0 && mAllocatedTextMemoryAddress.size() > 0;
}

View File

@ -42,6 +42,8 @@ struct FunctionSymbolDataComparator {
class PluginLinkInformation { class PluginLinkInformation {
public: public:
static PluginLinkInformation CreateStub();
PluginLinkInformation(const PluginLinkInformation &) = delete; PluginLinkInformation(const PluginLinkInformation &) = delete;
PluginLinkInformation(PluginLinkInformation &&src); PluginLinkInformation(PluginLinkInformation &&src);
@ -68,6 +70,8 @@ public:
[[nodiscard]] const HeapMemoryFixedSize &getDataMemory() const; [[nodiscard]] const HeapMemoryFixedSize &getDataMemory() const;
[[nodiscard]] bool hasValidData() const;
private: private:
PluginLinkInformation() = default; PluginLinkInformation() = default;

View File

@ -76,7 +76,7 @@ void ConfigUtils::displayMenu() {
std::vector<ConfigDisplayItem> configs; std::vector<ConfigDisplayItem> configs;
for (const auto &plugin : gLoadedPlugins) { for (const auto &plugin : gLoadedPlugins) {
if (!plugin.isPluginLinkedAndLoaded()) { if (!plugin.isLinkedAndLoaded()) {
continue; continue;
} }
GeneralConfigInformation info; GeneralConfigInformation info;
@ -103,7 +103,7 @@ void ConfigUtils::displayMenu() {
DEBUG_FUNCTION_LINE_ERR("Failed to create config for plugin: \"%s\"", info.name.c_str()); DEBUG_FUNCTION_LINE_ERR("Failed to create config for plugin: \"%s\"", info.name.c_str());
} }
} else { } 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.getType() == WUPS_LOADER_HOOK_GET_CONFIG_DEPRECATED) {
if (hook.getFunctionPointer() == nullptr) { if (hook.getFunctionPointer() == nullptr) {
DEBUG_FUNCTION_LINE_ERR("Hook had invalid ptr"); DEBUG_FUNCTION_LINE_ERR("Hook had invalid ptr");

View File

@ -260,7 +260,7 @@ extern "C" PluginBackendApiErrorType WUPSGetNumberOfLoadedPlugins(uint32_t *outC
if (outCount == nullptr) { if (outCount == nullptr) {
return PLUGIN_BACKEND_API_ERROR_INVALID_ARG; 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; 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) { if (handle != 0 && plugin_section_list != nullptr && buffer_size != 0) {
bool found = false; bool found = false;
for (const auto &curContainer : gLoadedPlugins) { for (const auto &curContainer : gLoadedPlugins) {
if (curContainer.isPluginLinkedAndLoaded() && curContainer.getHandle() == handle) { if (!curContainer.isLinkedAndLoaded()) {
continue;
}
if (curContainer.getHandle() == handle) {
found = true; found = true;
const auto &sectionInfoList = curContainer.getPluginLinkInformation()->getSectionInfoList(); const auto &sectionInfoList = curContainer.getPluginLinkInformation().getSectionInfoList();
uint32_t offset = 0; uint32_t offset = 0;
for (auto const &[key, sectionInfo] : sectionInfoList) { for (auto const &[key, sectionInfo] : sectionInfoList) {
@ -316,9 +319,12 @@ extern "C" PluginBackendApiErrorType WUPSGetSectionMemoryAddresses(wups_backend_
return PLUGIN_BACKEND_API_ERROR_INVALID_ARG; return PLUGIN_BACKEND_API_ERROR_INVALID_ARG;
} }
for (const auto &curContainer : gLoadedPlugins) { for (const auto &curContainer : gLoadedPlugins) {
if (curContainer.isPluginLinkedAndLoaded() && curContainer.getHandle() == handle) { if (!curContainer.isLinkedAndLoaded()) {
*textAddress = (void *) curContainer.getPluginLinkInformation()->getTextMemory().data(); continue;
*dataAddress = (void *) curContainer.getPluginLinkInformation()->getDataMemory().data(); }
if (curContainer.getHandle() == handle) {
*textAddress = (void *) curContainer.getPluginLinkInformation().getTextMemory().data();
*dataAddress = (void *) curContainer.getPluginLinkInformation().getDataMemory().data();
return PLUGIN_BACKEND_API_ERROR_NONE; return PLUGIN_BACKEND_API_ERROR_NONE;
} }
} }