From f513e1a3740ad8e268826db08798d39d7b96b475 Mon Sep 17 00:00:00 2001 From: Maschell Date: Fri, 11 Feb 2022 20:36:37 +0100 Subject: [PATCH] Round up the size of the allocated memory to avoid heap corruption --- source/memory_mapping.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/source/memory_mapping.cpp b/source/memory_mapping.cpp index 7028675..e24dfb7 100644 --- a/source/memory_mapping.cpp +++ b/source/memory_mapping.cpp @@ -395,6 +395,9 @@ void MemoryMapping_setupMemoryMapping() { OSInitMutex(&allocMutex); } +#define ROUNDDOWN(val, align) ((val) & ~(align - 1)) +#define ROUNDUP(val, align) ROUNDDOWN(((val) + (align - 1)), align) + void *MemoryMapping_allocEx(uint32_t size, int32_t align, bool videoOnly) { OSLockMutex(&allocMutex); void *res = nullptr; @@ -411,7 +414,10 @@ void *MemoryMapping_allocEx(uint32_t size, int32_t align, bool videoOnly) { continue; } - res = MEMAllocFromExpHeapEx(heapHandle, size, align); + // We round up the size to avoid heap corruption. + // FSReadFile expects the buffer size to be a multiple of 0x40 + // This can remove once all modules/plugins have been updated :) + res = MEMAllocFromExpHeapEx(heapHandle, ROUNDUP(size, 0x40), align); if (res != nullptr) { break; }