Skip to content

Commit

Permalink
Improve error message when parsing a plugin failed
Browse files Browse the repository at this point in the history
  • Loading branch information
Maschell committed Aug 16, 2023
1 parent 07de996 commit 1e7c77b
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 23 deletions.
15 changes: 12 additions & 3 deletions source/PluginManagement.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "PluginManagement.h"
#include "NotificationsUtils.h"
#include "hooks.h"
#include "patcher/hooks_patcher_static.h"
#include "plugin/PluginContainer.h"
#include "plugin/PluginInformationFactory.h"
#include "plugin/PluginMetaInformationFactory.h"
#include "utils/ElfUtils.h"
#include "utils/StringTools.h"
#include "utils/utils.h"
#include <coreinit/cache.h>
#include <coreinit/dynload.h>
Expand Down Expand Up @@ -152,8 +154,10 @@ PluginManagement::loadPlugins(const std::forward_list<std::shared_ptr<PluginData

uint32_t trampolineID = 0;
for (auto &pluginData : pluginList) {
auto metaInfo = PluginMetaInformationFactory::loadPlugin(pluginData);
if (metaInfo) {
PluginParseErrors error = PLUGIN_PARSE_ERROR_UNKNOWN;

auto metaInfo = PluginMetaInformationFactory::loadPlugin(pluginData, error);
if (metaInfo && error == PLUGIN_PARSE_ERROR_NONE) {
auto info = PluginInformationFactory::load(pluginData, trampoline_data, trampoline_data_length, trampolineID++);
if (!info) {
auto errMsg = string_format("Failed to load plugin: %s", metaInfo.value()->getName().c_str());
Expand All @@ -168,7 +172,12 @@ PluginManagement::loadPlugins(const std::forward_list<std::shared_ptr<PluginData
}
plugins.push_back(std::move(container));
} else {
DEBUG_FUNCTION_LINE_ERR("Failed to get meta information");
auto errMsg = string_format("Failed to load plugin: %s", pluginData->mSource.c_str());
if (error == PLUGIN_PARSE_ERROR_INCOMPATIBLE_VERSION) {
errMsg += ". Incompatible version.";
}
DEBUG_FUNCTION_LINE_ERR("%s", errMsg.c_str());
DisplayErrorNotificationMessage(errMsg, 15.0f);
}
}

Expand Down
2 changes: 1 addition & 1 deletion source/plugin/PluginData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "utils/logger.h"
#include "utils/utils.h"

PluginData::PluginData(const std::vector<uint8_t> &input) : length(input.size()) {
PluginData::PluginData(const std::vector<uint8_t> &input, std::string source) : length(input.size()), mSource(std::move(source)) {
auto data_copy = make_unique_nothrow<uint8_t[]>(length);
if (!data_copy) {
DEBUG_FUNCTION_LINE_ERR("Failed to allocate space on default heap");
Expand Down
3 changes: 2 additions & 1 deletion source/plugin/PluginData.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@

class PluginData {
public:
explicit PluginData(const std::vector<uint8_t> &buffer);
explicit PluginData(const std::vector<uint8_t> &buffer, std::string source);

uint32_t getHandle() {
return (uint32_t) this;
}

size_t length = 0;
std::unique_ptr<uint8_t[]> buffer;
std::string mSource;
};
6 changes: 3 additions & 3 deletions source/plugin/PluginDataFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ std::optional<std::unique_ptr<PluginData>> PluginDataFactory::load(const std::st

DEBUG_FUNCTION_LINE_VERBOSE("Loaded file!");

return load(result);
return load(result, filename);
}

std::optional<std::unique_ptr<PluginData>> PluginDataFactory::load(const std::vector<uint8_t> &buffer) {
std::optional<std::unique_ptr<PluginData>> PluginDataFactory::load(const std::vector<uint8_t> &buffer, const std::string &source) {
if (buffer.empty()) {
return {};
}

auto res = make_unique_nothrow<PluginData>(buffer);
auto res = make_unique_nothrow<PluginData>(buffer, source);
if (!res) {
return {};
}
Expand Down
2 changes: 1 addition & 1 deletion source/plugin/PluginDataFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ class PluginDataFactory {

static std::optional<std::unique_ptr<PluginData>> load(const std::string &path);

static std::optional<std::unique_ptr<PluginData>> load(const std::vector<uint8_t> &buffer);
static std::optional<std::unique_ptr<PluginData>> load(const std::vector<uint8_t> &buffer, const std::string &source);
};
22 changes: 15 additions & 7 deletions source/plugin/PluginMetaInformationFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,50 +22,55 @@
#include "utils/wiiu_zlib.hpp"
#include <memory>

std::optional<std::unique_ptr<PluginMetaInformation>> PluginMetaInformationFactory::loadPlugin(const std::shared_ptr<PluginData> &pluginData) {
std::optional<std::unique_ptr<PluginMetaInformation>> PluginMetaInformationFactory::loadPlugin(const std::shared_ptr<PluginData> &pluginData, PluginParseErrors &error) {
if (!pluginData->buffer) {
error = PLUGIN_PARSE_ERROR_BUFFER_EMPTY;
DEBUG_FUNCTION_LINE_ERR("Buffer is empty");
return {};
}
ELFIO::elfio reader(new wiiu_zlib);
if (!reader.load(reinterpret_cast<const char *>(pluginData->buffer.get()), pluginData->length)) {
error = PLUGIN_PARSE_ERROR_ELFIO_PARSE_FAILED;
DEBUG_FUNCTION_LINE_ERR("Can't process PluginData in elfio");
return {};
}
return loadPlugin(reader);
return loadPlugin(reader, error);
}

std::optional<std::unique_ptr<PluginMetaInformation>> PluginMetaInformationFactory::loadPlugin(const std::string &filePath) {
std::optional<std::unique_ptr<PluginMetaInformation>> PluginMetaInformationFactory::loadPlugin(const std::string &filePath, PluginParseErrors &error) {
ELFIO::elfio reader(new wiiu_zlib);

uint8_t *buffer = nullptr;
uint32_t length = 0;
if (FSUtils::LoadFileToMem(filePath.c_str(), &buffer, &length) < 0) {
DEBUG_FUNCTION_LINE_ERR("Failed to load file to memory");
error = PLUGIN_PARSE_ERROR_IO_ERROR;
return {};
}

if (!reader.load(reinterpret_cast<const char *>(buffer), length)) {
error = PLUGIN_PARSE_ERROR_ELFIO_PARSE_FAILED;
DEBUG_FUNCTION_LINE_ERR("Can't process PluginData in elfio");
return {};
}
auto res = loadPlugin(reader);
auto res = loadPlugin(reader, error);
free(buffer);
return res;
}

std::optional<std::unique_ptr<PluginMetaInformation>> PluginMetaInformationFactory::loadPlugin(char *buffer, size_t size) {
std::optional<std::unique_ptr<PluginMetaInformation>> PluginMetaInformationFactory::loadPlugin(char *buffer, size_t size, PluginParseErrors &error) {
ELFIO::elfio reader(new wiiu_zlib);

if (!reader.load(reinterpret_cast<const char *>(buffer), size)) {
error = PLUGIN_PARSE_ERROR_ELFIO_PARSE_FAILED;
DEBUG_FUNCTION_LINE_ERR("Can't find or process ELF file");
return std::nullopt;
}

return loadPlugin(reader);
return loadPlugin(reader, error);
}

std::optional<std::unique_ptr<PluginMetaInformation>> PluginMetaInformationFactory::loadPlugin(const ELFIO::elfio &reader) {
std::optional<std::unique_ptr<PluginMetaInformation>> PluginMetaInformationFactory::loadPlugin(const ELFIO::elfio &reader, PluginParseErrors &error) {
size_t pluginSize = 0;

auto pluginInfo = std::unique_ptr<PluginMetaInformation>(new PluginMetaInformation);
Expand Down Expand Up @@ -120,6 +125,7 @@ std::optional<std::unique_ptr<PluginMetaInformation>> PluginMetaInformationFacto
pluginInfo->setStorageId(value);
} else if (key == "wups") {
if (value != "0.7.1") {
error = PLUGIN_PARSE_ERROR_INCOMPATIBLE_VERSION;
DEBUG_FUNCTION_LINE_ERR("Warning: Ignoring plugin - Unsupported WUPS version: %s.", value.c_str());
return std::nullopt;
}
Expand All @@ -132,5 +138,7 @@ std::optional<std::unique_ptr<PluginMetaInformation>> PluginMetaInformationFacto

pluginInfo->setSize(pluginSize);

error = PLUGIN_PARSE_ERROR_NONE;

return pluginInfo;
}
17 changes: 13 additions & 4 deletions source/plugin/PluginMetaInformationFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,22 @@
#include <string>
#include <vector>

enum PluginParseErrors {
PLUGIN_PARSE_ERROR_NONE,
PLUGIN_PARSE_ERROR_UNKNOWN,
PLUGIN_PARSE_ERROR_INCOMPATIBLE_VERSION,
PLUGIN_PARSE_ERROR_BUFFER_EMPTY,
PLUGIN_PARSE_ERROR_ELFIO_PARSE_FAILED,
PLUGIN_PARSE_ERROR_IO_ERROR,
};

class PluginMetaInformationFactory {
public:
static std::optional<std::unique_ptr<PluginMetaInformation>> loadPlugin(const std::shared_ptr<PluginData> &pluginData);
static std::optional<std::unique_ptr<PluginMetaInformation>> loadPlugin(const std::shared_ptr<PluginData> &pluginData, PluginParseErrors &error);

static std::optional<std::unique_ptr<PluginMetaInformation>> loadPlugin(const std::string &filePath);
static std::optional<std::unique_ptr<PluginMetaInformation>> loadPlugin(const std::string &filePath, PluginParseErrors &error);

static std::optional<std::unique_ptr<PluginMetaInformation>> loadPlugin(char *buffer, size_t size);
static std::optional<std::unique_ptr<PluginMetaInformation>> loadPlugin(char *buffer, size_t size, PluginParseErrors &error);

static std::optional<std::unique_ptr<PluginMetaInformation>> loadPlugin(const ELFIO::elfio &reader);
static std::optional<std::unique_ptr<PluginMetaInformation>> loadPlugin(const ELFIO::elfio &reader, PluginParseErrors &error);
};
7 changes: 4 additions & 3 deletions source/utils/exports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ extern "C" PluginBackendApiErrorType WUPSLoadPluginAsData(GetPluginInformationIn
} else if (inputType == PLUGIN_INFORMATION_INPUT_TYPE_BUFFER && buffer != nullptr && size > 0) {
std::vector<uint8_t> data(size);
memcpy(&data[0], buffer, size);
pluginData = PluginDataFactory::load(data);
pluginData = PluginDataFactory::load(data, "<UNKNOWN>");
} else {
return PLUGIN_BACKEND_API_ERROR_INVALID_ARG;
}
Expand Down Expand Up @@ -95,11 +95,12 @@ extern "C" PluginBackendApiErrorType WUPSLoadPluginAsDataByBuffer(plugin_data_ha

extern "C" PluginBackendApiErrorType WUPSGetPluginMetaInformation(GetPluginInformationInputType inputType, const char *path, char *buffer, size_t size, plugin_information *output) {
std::optional<std::unique_ptr<PluginMetaInformation>> pluginInfo;
PluginParseErrors error = PLUGIN_PARSE_ERROR_UNKNOWN;
if (inputType == PLUGIN_INFORMATION_INPUT_TYPE_PATH && path != nullptr) {
std::string pathStr(path);
pluginInfo = PluginMetaInformationFactory::loadPlugin(pathStr);
pluginInfo = PluginMetaInformationFactory::loadPlugin(pathStr, error);
} else if (inputType == PLUGIN_INFORMATION_INPUT_TYPE_BUFFER && buffer != nullptr && size > 0) {
pluginInfo = PluginMetaInformationFactory::loadPlugin(buffer, size);
pluginInfo = PluginMetaInformationFactory::loadPlugin(buffer, size, error);
} else {
DEBUG_FUNCTION_LINE_ERR("PLUGIN_BACKEND_API_ERROR_INVALID_ARG");
return PLUGIN_BACKEND_API_ERROR_INVALID_ARG;
Expand Down

0 comments on commit 1e7c77b

Please sign in to comment.