Use proper alignmend buffer for reading/writing

This commit is contained in:
Maschell 2022-02-14 20:23:27 +01:00
parent e57a7eedb0
commit a6ced98864
7 changed files with 48 additions and 39 deletions

View File

@ -1,9 +1,9 @@
FROM wiiuenv/devkitppc:20220212
FROM wiiuenv/devkitppc:20220213
COPY --from=wiiuenv/wiiumodulesystem:20220127 /artifacts $DEVKITPRO
COPY --from=wiiuenv/wiiupluginsystem:20220123 /artifacts $DEVKITPRO
COPY --from=wiiuenv/libfunctionpatcher:20210924 /artifacts $DEVKITPRO
COPY --from=wiiuenv/libmappedmemory:20210924 /artifacts $DEVKITPRO
COPY --from=wiiuenv/libwupsbackend:20211001 /artifacts $DEVKITPRO
COPY --from=wiiuenv/libwupsbackend:20220213 /artifacts $DEVKITPRO
WORKDIR project

View File

@ -1,4 +1,5 @@
#include "utils/utils.h"
#include <cstdarg>
#include <cstdio>
#include <fs/CFile.hpp>
@ -156,7 +157,7 @@ int32_t CFile::seek(long int offset, int32_t origin) {
}
int32_t CFile::fwrite(const char *format, ...) {
char tmp[512];
ALIGN_DATA_0x40 char tmp[512];
tmp[0] = 0;
int32_t result = -1;

View File

@ -1,32 +1,35 @@
#include "fs/FSUtils.h"
#include "fs/CFile.hpp"
#include "utils/logger.h"
#include "utils/utils.h"
#include <cstdio>
#include <cstring>
#include <fcntl.h>
#include <malloc.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int32_t FSUtils::LoadFileToMem(const char *filepath, uint8_t **inbuffer, uint32_t *size) {
//! always initialze input
*inbuffer = nullptr;
if (size)
*inbuffer = NULL;
if (size) {
*size = 0;
}
int32_t iFd = open(filepath, O_RDONLY);
if (iFd < 0)
if (iFd < 0) {
return -1;
}
uint32_t filesize = lseek(iFd, 0, SEEK_END);
lseek(iFd, 0, SEEK_SET);
uint8_t *buffer = (uint8_t *) malloc(filesize);
auto *buffer = (uint8_t *) memalign(0x40, ROUNDUP(filesize, 0x40));
if (buffer == nullptr) {
close(iFd);
return -2;
}
uint32_t blocksize = 0x4000;
uint32_t blocksize = 0x20000;
uint32_t done = 0;
int32_t readBytes = 0;
@ -40,7 +43,7 @@ int32_t FSUtils::LoadFileToMem(const char *filepath, uint8_t **inbuffer, uint32_
done += readBytes;
}
close(iFd);
::close(iFd);
if (done != filesize) {
free(buffer);

View File

@ -15,14 +15,12 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#include "PluginDataFactory.h"
#include "../fs/FSUtils.h"
#include "../utils/StringTools.h"
#include "../utils/logger.h"
#include <dirent.h>
#include <fcntl.h>
#include <memory>
#include <sys/stat.h>
std::vector<std::shared_ptr<PluginData>> PluginDataFactory::loadDir(const std::string &path, MEMHeapHandle heapHandle) {
std::vector<std::shared_ptr<PluginData>> result;
struct dirent *dp;
@ -65,28 +63,17 @@ std::vector<std::shared_ptr<PluginData>> PluginDataFactory::loadDir(const std::s
}
std::optional<std::shared_ptr<PluginData>> PluginDataFactory::load(const std::string &filename, MEMHeapHandle heapHandle) {
// Not going to explicitly check these.
// The use of gcount() below will compensate for a failure here.
std::ifstream is(filename, std::ios::binary);
is.seekg(0, std::ios::end);
std::streampos length = is.tellg();
is.seekg(0, std::ios::beg);
// reading into a 0x40 aligned buffer increases reading speed.
char *data = (char *) memalign(0x40, length);
if (!data) {
is.close();
DEBUG_FUNCTION_LINE("Failed to alloc memory for holding the plugin");
uint8_t *buffer = nullptr;
uint32_t fsize = 0;
if (FSUtils::LoadFileToMem(filename.c_str(), &buffer, &fsize) < 0) {
DEBUG_FUNCTION_LINE("Failed to load file");
return {};
}
is.read(data, length);
std::vector<uint8_t> result;
result.resize(length);
memcpy(&result[0], data, length);
free(data);
is.close();
result.resize(fsize);
memcpy(&result[0], buffer, fsize);
free(buffer);
DEBUG_FUNCTION_LINE_VERBOSE("Loaded file!");

View File

@ -16,6 +16,7 @@
****************************************************************************/
#include "PluginMetaInformationFactory.h"
#include "../fs/FSUtils.h"
#include "../utils/StringTools.h"
#include <dirent.h>
#include <memory>
@ -32,18 +33,28 @@ std::optional<std::shared_ptr<PluginMetaInformation>> PluginMetaInformationFacto
elfio reader;
if (!reader.load((char *) pluginData->buffer, pluginData->length)) {
DEBUG_FUNCTION_LINE("Can't process PluginData in elfio");
return std::nullopt;
return {};
}
return loadPlugin(reader);
}
std::optional<std::shared_ptr<PluginMetaInformation>> PluginMetaInformationFactory::loadPlugin(std::string &filePath) {
elfio reader;
if (!reader.load(filePath)) {
DEBUG_FUNCTION_LINE("Can't find or process ELF file");
return std::nullopt;
uint8_t *buffer = nullptr;
uint32_t length = 0;
if (FSUtils::LoadFileToMem(filePath.c_str(), &buffer, &length) < 0) {
DEBUG_FUNCTION_LINE("Failed to load file to memory");
return {};
}
return loadPlugin(reader);
if (!reader.load((char *) buffer, length)) {
DEBUG_FUNCTION_LINE("Can't process PluginData in elfio");
return {};
}
auto res = loadPlugin(reader);
free(buffer);
return res;
}
std::optional<std::shared_ptr<PluginMetaInformation>> PluginMetaInformationFactory::loadPlugin(char *buffer, size_t size) {

View File

@ -3,6 +3,7 @@
#include "fs/CFile.hpp"
#include "fs/FSUtils.h"
#include "utils.h"
#include "utils/json.hpp"
#include "utils/logger.h"
@ -58,7 +59,7 @@ int StorageUtils::OpenStorage(const char *plugin_id, wups_storage_item_t *items)
nlohmann::json j;
CFile file(filePath, CFile::ReadOnly);
if (file.isOpen() && file.size() > 0) {
uint8_t *json_data = new uint8_t[file.size() + 1];
auto *json_data = (uint8_t *) memalign(0x40, ROUNDUP(file.size() + 1, 0x40));
json_data[file.size()] = '\0';
file.read(json_data, file.size());
@ -127,7 +128,10 @@ int StorageUtils::CloseStorage(const char *plugin_id, wups_storage_item_t *items
j["storageitems"] = processItems(items);
std::string jsonString = j.dump(4);
file.write((const uint8_t *) jsonString.c_str(), jsonString.size());
auto writeSize = jsonString.size();
auto *data = (uint8_t *) memalign(0x40, ROUNDUP(writeSize, 0x40));
memcpy(data, jsonString.c_str(), writeSize);
file.write(data, writeSize);
file.close();
return WUPS_STORAGE_ERROR_SUCCESS;
}

View File

@ -22,6 +22,9 @@ extern "C" {
#define ALIGN4(x) (((x) + 3) & ~3)
#define ALIGN32(x) (((x) + 31) & ~31)
#define ALIGN_DATA(align) __attribute__((aligned(align)))
#define ALIGN_DATA_0x40 ALIGN_DATA(0x40)
// those work only in powers of 2
#define ROUNDDOWN(val, align) ((val) & ~(align - 1))
#define ROUNDUP(val, align) ROUNDDOWN(((val) + (align - 1)), align)