diff --git a/Core/HLE/ReplaceTables.cpp b/Core/HLE/ReplaceTables.cpp index f9ddbf7de7..f09e59006c 100644 --- a/Core/HLE/ReplaceTables.cpp +++ b/Core/HLE/ReplaceTables.cpp @@ -138,8 +138,8 @@ static int Replace_memcpy() { } } if (!skip && bytes != 0) { - u8 *dst = Memory::GetPointerWrite(destPtr); - const u8 *src = Memory::GetPointer(srcPtr); + u8 *dst = Memory::GetPointerWriteRange(destPtr, bytes); + const u8 *src = Memory::GetPointerRange(srcPtr, bytes); if (!dst || !src) { // Already logged. @@ -190,8 +190,8 @@ static int Replace_memcpy_jak() { } } if (!skip && bytes != 0) { - u8 *dst = Memory::GetPointerWrite(destPtr); - const u8 *src = Memory::GetPointer(srcPtr); + u8 *dst = Memory::GetPointerWriteRange(destPtr, bytes); + const u8 *src = Memory::GetPointerRange(srcPtr, bytes); if (!dst || !src) { } else { @@ -241,8 +241,8 @@ static int Replace_memcpy16() { } } if (!skip && bytes != 0) { - u8 *dst = Memory::GetPointerWrite(destPtr); - const u8 *src = Memory::GetPointer(srcPtr); + u8 *dst = Memory::GetPointerWriteRange(destPtr, bytes); + const u8 *src = Memory::GetPointerRange(srcPtr, bytes); if (dst && src) { memmove(dst, src, bytes); } @@ -313,8 +313,8 @@ static int Replace_memmove() { } } if (!skip && bytes != 0) { - u8 *dst = Memory::GetPointerWrite(destPtr); - const u8 *src = Memory::GetPointer(srcPtr); + u8 *dst = Memory::GetPointerWriteRange(destPtr, bytes); + const u8 *src = Memory::GetPointerRange(srcPtr, bytes); if (dst && src) { memmove(dst, src, bytes); } @@ -339,7 +339,7 @@ static int Replace_memset() { skip = gpu->PerformMemorySet(destPtr, value, bytes); } if (!skip && bytes != 0) { - u8 *dst = Memory::GetPointerWrite(destPtr); + u8 *dst = Memory::GetPointerWriteRange(destPtr, bytes); if (dst) { memset(dst, value, bytes); } @@ -366,7 +366,7 @@ static int Replace_memset_jak() { skip = gpu->PerformMemorySet(destPtr, value, bytes); } if (!skip && bytes != 0) { - u8 *dst = Memory::GetPointerWrite(destPtr); + u8 *dst = Memory::GetPointerWriteRange(destPtr, bytes); if (dst) { memset(dst, value, bytes); } diff --git a/Core/MemMap.h b/Core/MemMap.h index b123af4f3a..79949f06ed 100644 --- a/Core/MemMap.h +++ b/Core/MemMap.h @@ -246,6 +246,10 @@ inline void Write_Float(float f, u32 address) u8* GetPointerWrite(const u32 address); const u8* GetPointer(const u32 address); + +u8 *GetPointerWriteRange(const u32 address, const u32 size); +const u8 *GetPointerRange(const u32 address, const u32 size); + bool IsRAMAddress(const u32 address); inline bool IsVRAMAddress(const u32 address) { return ((address & 0x3F800000) == 0x04000000); diff --git a/Core/MemMapFunctions.cpp b/Core/MemMapFunctions.cpp index fc379a411f..af82475016 100644 --- a/Core/MemMapFunctions.cpp +++ b/Core/MemMapFunctions.cpp @@ -62,6 +62,38 @@ const u8 *GetPointer(const u32 address) { } } +u8 *GetPointerWriteRange(const u32 address, const u32 size) { + u8 *ptr = GetPointerWrite(address); + if (ptr) { + if (ValidSize(address, size) != size) { + // That's a memory exception! TODO: Adjust reported address to the end of the range? + Core_MemoryException(address, currentMIPS->pc, MemoryExceptionType::WRITE_BLOCK); + return nullptr; + } else { + return ptr; + } + } else { + // Error was reported in GetPointerWrite already, if we're not ignoring errors. + return nullptr; + } +} + +const u8 *GetPointerRange(const u32 address, const u32 size) { + const u8 *ptr = GetPointer(address); + if (ptr) { + if (ValidSize(address, size) != size) { + // That's a memory exception! TODO: Adjust reported address to the end of the range? + Core_MemoryException(address, currentMIPS->pc, MemoryExceptionType::READ_BLOCK); + return nullptr; + } else { + return ptr; + } + } else { + // Error was reported in GetPointer already, if we're not ignoring errors. + return nullptr; + } +} + template inline void ReadFromHardware(T &var, const u32 address) { // TODO: Figure out the fastest order of tests for both read and write (they are probably different).