From 1f7d853d655215a667ad696847aaf06ab77369c7 Mon Sep 17 00:00:00 2001 From: Maschell Date: Sun, 26 Mar 2023 15:55:36 +0200 Subject: [PATCH] Memset every buffer allocated on the default heap before freeing --- wumsloader/src/fs/FileUtils.cpp | 5 ++--- wumsloader/src/module/ModuleDataFactory.cpp | 7 ++++++- wumsloader/src/utils/utils.h | 2 ++ 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/wumsloader/src/fs/FileUtils.cpp b/wumsloader/src/fs/FileUtils.cpp index 9bb10fa..5746b49 100644 --- a/wumsloader/src/fs/FileUtils.cpp +++ b/wumsloader/src/fs/FileUtils.cpp @@ -1,4 +1,5 @@ #include "utils/logger.h" +#include "utils/utils.h" #include #include #include @@ -6,9 +7,6 @@ #include #include -#define ROUNDDOWN(val, align) ((val) & ~(align - 1)) -#define ROUNDUP(val, align) ROUNDDOWN(((val) + (align - 1)), align) - int32_t LoadFileToMem(const std::string &filepath, uint8_t **inbuffer, uint32_t *size) { //! always initialze input *inbuffer = nullptr; @@ -47,6 +45,7 @@ int32_t LoadFileToMem(const std::string &filepath, uint8_t **inbuffer, uint32_t ::close(iFd); if (done != filesize) { + memset(buffer, 0, ROUNDUP(filesize, 0x40)); free(buffer); buffer = nullptr; return -3; diff --git a/wumsloader/src/module/ModuleDataFactory.cpp b/wumsloader/src/module/ModuleDataFactory.cpp index 723e7a4..62e2d1d 100644 --- a/wumsloader/src/module/ModuleDataFactory.cpp +++ b/wumsloader/src/module/ModuleDataFactory.cpp @@ -45,7 +45,12 @@ std::optional> ModuleDataFactory::load(const std::st return {}; } - auto cleanupBuffer = onLeavingScope([buffer]() { MEMFreeToDefaultHeap(buffer); }); + auto cleanupBuffer = onLeavingScope([buffer, fsize]() { + // Some games (e.g. Minecraft) expect the default heap to be empty. + // Make sure to clean up the memory after using it + memset(buffer, 0, ROUNDUP(fsize, 0x40)); + MEMFreeToDefaultHeap(buffer); + }); // Load ELF data if (!reader.load(reinterpret_cast(buffer), fsize)) { diff --git a/wumsloader/src/utils/utils.h b/wumsloader/src/utils/utils.h index e7357b8..f25451e 100644 --- a/wumsloader/src/utils/utils.h +++ b/wumsloader/src/utils/utils.h @@ -2,6 +2,8 @@ #include #include +#define ROUNDDOWN(val, align) ((val) & ~(align - 1)) +#define ROUNDUP(val, align) ROUNDDOWN(((val) + (align - 1)), align) template std::unique_ptr make_unique_nothrow(Args &&...args) noexcept(noexcept(T(std::forward(args)...))) {