From be3428cf8e7698e4efe9c50cd6499516e7f36332 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 10 Aug 2014 04:50:58 -0400 Subject: [PATCH 1/2] Core: Fix case where a panic alert wouldn't be shown in MemoryUtil.cpp --- Source/Core/Common/MemoryUtil.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Source/Core/Common/MemoryUtil.cpp b/Source/Core/Common/MemoryUtil.cpp index 3a10d031cf..7a075692ce 100644 --- a/Source/Core/Common/MemoryUtil.cpp +++ b/Source/Core/Common/MemoryUtil.cpp @@ -91,10 +91,10 @@ void* AllocateMemoryPages(size_t size) #else void* ptr = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); -#endif - // printf("Mapped memory at %p (size %ld)\n", ptr, - // (unsigned long)size); + if (ptr == MAP_FAILED) + ptr = nullptr; +#endif if (ptr == nullptr) PanicAlert("Failed to allocate raw memory"); From 9a61cfc650c159b8e6b57059fc9f335dd29fe3dd Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 10 Aug 2014 05:28:00 -0400 Subject: [PATCH 2/2] Core: Actually show MemoryUtil.cpp allocation error messages on Linux --- Source/Core/Common/MemoryUtil.cpp | 38 ++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/Source/Core/Common/MemoryUtil.cpp b/Source/Core/Common/MemoryUtil.cpp index 7a075692ce..7d5eaa0b28 100644 --- a/Source/Core/Common/MemoryUtil.cpp +++ b/Source/Core/Common/MemoryUtil.cpp @@ -129,14 +129,20 @@ void FreeMemoryPages(void* ptr, size_t size) { if (ptr) { + bool error_occurred = false; + #ifdef _WIN32 if (!VirtualFree(ptr, 0, MEM_RELEASE)) - { - PanicAlert("FreeMemoryPages failed!\n%s", GetLastErrorMsg()); - } + error_occurred = true; #else - munmap(ptr, size); + int retval = munmap(ptr, size); + + if (retval != 0) + error_occurred = true; #endif + + if (error_occurred) + PanicAlert("FreeMemoryPages failed!\n%s", GetLastErrorMsg()); } } @@ -154,24 +160,40 @@ void FreeAlignedMemory(void* ptr) void WriteProtectMemory(void* ptr, size_t size, bool allowExecute) { + bool error_occurred = false; + #ifdef _WIN32 DWORD oldValue; if (!VirtualProtect(ptr, size, allowExecute ? PAGE_EXECUTE_READ : PAGE_READONLY, &oldValue)) - PanicAlert("WriteProtectMemory failed!\n%s", GetLastErrorMsg()); + error_occurred = true; #else - mprotect(ptr, size, allowExecute ? (PROT_READ | PROT_EXEC) : PROT_READ); + int retval = mprotect(ptr, size, allowExecute ? (PROT_READ | PROT_EXEC) : PROT_READ); + + if (retval != 0) + error_occurred = true; #endif + + if (error_occurred) + PanicAlert("WriteProtectMemory failed!\n%s", GetLastErrorMsg()); } void UnWriteProtectMemory(void* ptr, size_t size, bool allowExecute) { + bool error_occurred = false; + #ifdef _WIN32 DWORD oldValue; if (!VirtualProtect(ptr, size, allowExecute ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE, &oldValue)) - PanicAlert("UnWriteProtectMemory failed!\n%s", GetLastErrorMsg()); + error_occurred = true; #else - mprotect(ptr, size, allowExecute ? (PROT_READ | PROT_WRITE | PROT_EXEC) : PROT_WRITE | PROT_READ); + int retval = mprotect(ptr, size, allowExecute ? (PROT_READ | PROT_WRITE | PROT_EXEC) : PROT_WRITE | PROT_READ); + + if (retval != 0) + error_occurred = true; #endif + + if (error_occurred) + PanicAlert("UnWriteProtectMemory failed!\n%s", GetLastErrorMsg()); } std::string MemUsage()