Memset every buffer allocated on the default heap before freeing

This commit is contained in:
Maschell 2023-03-26 15:55:36 +02:00
parent f7f8b007fa
commit 1f7d853d65
3 changed files with 10 additions and 4 deletions

View File

@ -1,4 +1,5 @@
#include "utils/logger.h" #include "utils/logger.h"
#include "utils/utils.h"
#include <coreinit/memdefaultheap.h> #include <coreinit/memdefaultheap.h>
#include <cstdint> #include <cstdint>
#include <fcntl.h> #include <fcntl.h>
@ -6,9 +7,6 @@
#include <string> #include <string>
#include <unistd.h> #include <unistd.h>
#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) { int32_t LoadFileToMem(const std::string &filepath, uint8_t **inbuffer, uint32_t *size) {
//! always initialze input //! always initialze input
*inbuffer = nullptr; *inbuffer = nullptr;
@ -47,6 +45,7 @@ int32_t LoadFileToMem(const std::string &filepath, uint8_t **inbuffer, uint32_t
::close(iFd); ::close(iFd);
if (done != filesize) { if (done != filesize) {
memset(buffer, 0, ROUNDUP(filesize, 0x40));
free(buffer); free(buffer);
buffer = nullptr; buffer = nullptr;
return -3; return -3;

View File

@ -45,7 +45,12 @@ std::optional<std::shared_ptr<ModuleData>> ModuleDataFactory::load(const std::st
return {}; 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 // Load ELF data
if (!reader.load(reinterpret_cast<char *>(buffer), fsize)) { if (!reader.load(reinterpret_cast<char *>(buffer), fsize)) {

View File

@ -2,6 +2,8 @@
#include <memory> #include <memory>
#include <vector> #include <vector>
#define ROUNDDOWN(val, align) ((val) & ~(align - 1))
#define ROUNDUP(val, align) ROUNDDOWN(((val) + (align - 1)), align)
template<class T, class... Args> template<class T, class... Args>
std::unique_ptr<T> make_unique_nothrow(Args &&...args) noexcept(noexcept(T(std::forward<Args>(args)...))) { std::unique_ptr<T> make_unique_nothrow(Args &&...args) noexcept(noexcept(T(std::forward<Args>(args)...))) {