diff --git a/Common/CodeBlock.h b/Common/CodeBlock.h index 5712a1eaba..1f6a0fa376 100644 --- a/Common/CodeBlock.h +++ b/Common/CodeBlock.h @@ -76,7 +76,7 @@ public: // Cannot currently be undone. Will write protect the entire code region. // Start over if you need to change the code (call FreeCodeSpace(), AllocCodeSpace()). void WriteProtect() { - ProtectMemory(region, region_size, MEM_PROT_READ | MEM_PROT_EXEC); + ProtectMemoryPages(region, region_size, MEM_PROT_READ | MEM_PROT_EXEC); } void SetCodePtr(u8 *ptr) override { diff --git a/Common/MemoryUtil.cpp b/Common/MemoryUtil.cpp index ef647662b1..74bc8bdc4b 100644 --- a/Common/MemoryUtil.cpp +++ b/Common/MemoryUtil.cpp @@ -55,13 +55,44 @@ static RHeap* g_code_heap = NULL; static u8* g_next_ptr = NULL; static u8* g_orig_ptr = NULL; -void ResetExecutableMemory(void* ptr) -{ +void ResetExecutableMemory(void* ptr) { // Just reset the ptr to the base g_next_ptr = g_orig_ptr; } #endif +#ifdef _WIN32 +// Win32 flags are odd... +uint32_t ConvertProtFlagsWin32(uint32_t flags) { + uint32_t protect = 0; + switch (flags) { + case 0: protect = PAGE_NOACCESS; break; + case MEM_PROT_READ: protect = PAGE_READONLY; break; + case MEM_PROT_WRITE: protect = PAGE_READWRITE; break; // Can't set write-only + case MEM_PROT_EXEC: protect = PAGE_EXECUTE; break; + case MEM_PROT_READ | MEM_PROT_EXEC: protect = PAGE_EXECUTE_READ; break; + case MEM_PROT_WRITE | MEM_PROT_EXEC: protect = PAGE_EXECUTE_READWRITE; break; // Can't set write-only + case MEM_PROT_READ | MEM_PROT_WRITE: protect = PAGE_READWRITE; break; + case MEM_PROT_READ | MEM_PROT_WRITE | MEM_PROT_EXEC: protect = PAGE_EXECUTE_READWRITE; break; + } + return protect; +} + +#else + +uint32_t ConvertProtFlagsUnix(uint32_t flags) { + uint32_t protect = 0; + if (flags & MEM_PROT_READ) + protect |= PROT_READ; + if (flags & MEM_PROT_WRITE) + protect |= PROT_WRITE; + if (flags & MEM_PROT_EXEC) + protect |= PROT_EXEC; + return protect; +} + +#endif + #if defined(_WIN32) && defined(_M_X64) static uintptr_t last_executable_addr; static void *SearchForFreeMem(size_t size) { @@ -96,11 +127,11 @@ static void *SearchForFreeMem(size_t size) { // This is purposely not a full wrapper for virtualalloc/mmap, but it // provides exactly the primitive operations that PPSSPP needs. -void *AllocateExecutableMemory(size_t size, bool exec) { +void *AllocateExecutableMemory(size_t size) { #if defined(_WIN32) void *ptr; #if defined(_M_X64) - if (exec && (uintptr_t)&hint_location > 0xFFFFFFFFULL) { + if ((uintptr_t)&hint_location > 0xFFFFFFFFULL) { if (sys_info.dwPageSize == 0) GetSystemInfo(&sys_info); @@ -186,21 +217,22 @@ void *AllocateExecutableMemory(size_t size, bool exec) { return ptr; } -void *AllocateMemoryPages(size_t size) { +void *AllocateMemoryPages(size_t size, uint32_t memProtFlags) { size = (size + 4095) & (~4095); #ifdef _WIN32 - void* ptr = VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE); + uint32_t protect = ConvertProtFlagsWin32(memProtFlags); + void* ptr = VirtualAlloc(0, size, MEM_COMMIT, protect); #elif defined(__SYMBIAN32__) void* ptr = malloc(size); #else - void* ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); + uint32_t protect = ConvertProtFlagsUnix(memProtFlags); + void* ptr = mmap(0, size, protect, MAP_ANON | MAP_PRIVATE, -1, 0); #endif // printf("Mapped memory at %p (size %ld)\n", ptr, // (unsigned long)size); if (ptr == NULL) PanicAlert("Failed to allocate raw memory"); - return ptr; } @@ -215,7 +247,7 @@ void *AllocateAlignedMemory(size_t size, size_t alignment) { // On Symbian, alignment won't matter as NEON isn't supported. ptr = malloc(size); #else - if(posix_memalign(&ptr, alignment, size) != 0) + if (posix_memalign(&ptr, alignment, size) != 0) ptr = NULL; #endif #endif @@ -258,36 +290,20 @@ bool PlatformIsWXExclusive() { return false; } -void ProtectMemory(void* ptr, size_t size, uint32_t flags) { +void ProtectMemoryPages(void* ptr, size_t size, uint32_t memProtFlags) { #ifdef _WIN32 - uint32_t protect = 0; - // Win32 flags are odd... - switch (flags) { - case 0: protect = PAGE_NOACCESS; break; - case MEM_PROT_READ: protect = PAGE_READONLY; break; - case MEM_PROT_WRITE: protect = PAGE_READWRITE; break; // Can't set write-only - case MEM_PROT_EXEC: protect = PAGE_EXECUTE; break; - case MEM_PROT_READ | MEM_PROT_EXEC: protect = PAGE_EXECUTE_READ; break; - case MEM_PROT_WRITE | MEM_PROT_EXEC: protect = PAGE_EXECUTE_READWRITE; break; // Can't set write-only - case MEM_PROT_READ | MEM_PROT_WRITE: protect = PAGE_READWRITE; break; - case MEM_PROT_READ | MEM_PROT_WRITE | MEM_PROT_EXEC: protect = PAGE_EXECUTE_READWRITE; break; - } - + uint32_t protect = ConvertProtFlagsWin32(memProtFlags); DWORD oldValue; if (!VirtualProtect(ptr, size, protect, &oldValue)) PanicAlert("WriteProtectMemory failed!\n%s", GetLastErrorMsg()); +#elif defined(__SYMBIAN32__) + // Do nothing #else + uint32_t protect = ConvertProtFlagsUnix(memProtFlags); if (PlatformIsWXExclusive()) { if ((flags & (MEM_PROT_WRITE | MEM_PROT_EXEC)) == (MEM_PROT_WRITE | MEM_PROT_EXEC)) PanicAlert("Bad memory protect : W^X is in effect, can't both write and exec"); } - uint32_t protect = 0; - if (flags & MEM_PROT_READ) - protect |= PROT_READ; - if (flags & MEM_PROT_WRITE) - protect |= PROT_WRITE; - if (flags & MEM_PROT_EXEC) - protect |= PROT_EXEC; mprotect(ptr, size, protect); #endif } \ No newline at end of file diff --git a/Common/MemoryUtil.h b/Common/MemoryUtil.h index 21e4f0278e..6d63590adc 100644 --- a/Common/MemoryUtil.h +++ b/Common/MemoryUtil.h @@ -29,24 +29,36 @@ using std::size_t; // Returns true if we need to avoid setting both writable and executable at the same time (W^X) bool PlatformIsWXExclusive(); -void* AllocateExecutableMemory(size_t size, bool exec = true); -void* AllocateMemoryPages(size_t size); -void FreeMemoryPages(void* ptr, size_t size); -void* AllocateAlignedMemory(size_t size,size_t alignment); -void FreeAlignedMemory(void* ptr); - -// Note that on platforms returning PlatformIsWXExclusive, you cannot set a page to be both readable and writable at the same time. #define MEM_PROT_READ 1 #define MEM_PROT_WRITE 2 #define MEM_PROT_EXEC 4 -void ProtectMemory(void* ptr, size_t size, uint32_t flags); + +// Note that some platforms go through special contortions to allocate executable memory. So for memory +// that's intended for execution, allocate it first using AllocateExecutableMemory, then modify protection as desired. +// AllocateMemoryPages is simpler and more generic. +void* AllocateExecutableMemory(size_t size); +void* AllocateMemoryPages(size_t size, uint32_t memProtFlags); +// Note that on platforms returning PlatformIsWXExclusive, you cannot set a page to be both readable and writable at the same time. +void ProtectMemoryPages(void* ptr, size_t size, uint32_t memProtFlags); +void FreeMemoryPages(void* ptr, size_t size); + +// Regular aligned memory. Don't try to apply memory protection willy-nilly to memory allocated this way as in-page alignment is unknown (though could be checked). +void* AllocateAlignedMemory(size_t size, size_t alignment); +void FreeAlignedMemory(void* ptr); #ifdef __SYMBIAN32__ void ResetExecutableMemory(void* ptr); #endif -inline int GetPageSize() { return 4096; } +inline int GetMemoryProtectPageSize() { +#ifdef _WIN32 + // For memory protection purposes, Windows is still stuck at 64k except in WSL (linux on windows). + return 65536; +#else + return PAGE_SIZE; +#endif +} template class SimpleBuf { @@ -74,7 +86,7 @@ public: if (buf_ != 0) { FreeMemoryPages(buf_, size_ * sizeof(T)); } - buf_ = (T *)AllocateMemoryPages(size * sizeof(T)); + buf_ = (T *)AllocateMemoryPages(size * sizeof(T), MEM_PROT_READ | MEM_PROT_WRITE); size_ = size; } } diff --git a/Common/MipsEmitter.cpp b/Common/MipsEmitter.cpp index b54bafcaae..a43a6c00c0 100644 --- a/Common/MipsEmitter.cpp +++ b/Common/MipsEmitter.cpp @@ -497,11 +497,11 @@ void MIPSCodeBlock::FreeCodeSpace() { } void MIPSCodeBlock::WriteProtect() { - ProtectMemory(region, region_size, MEM_PROT_READ | MEM_PROT_EXEC); + ProtectMemoryPages(region, region_size, MEM_PROT_READ | MEM_PROT_EXEC); } void MIPSCodeBlock::UnWriteProtect() { - ProtectMemory(region, region_size, MEM_PROT_READ | MEM_PROT_EXEC | MEM_PROT_WRITE); + ProtectMemoryPages(region, region_size, MEM_PROT_READ | MEM_PROT_EXEC | MEM_PROT_WRITE); } } diff --git a/GPU/Directx9/DrawEngineDX9.cpp b/GPU/Directx9/DrawEngineDX9.cpp index d3b6a57cf0..03459e3aa0 100644 --- a/GPU/Directx9/DrawEngineDX9.cpp +++ b/GPU/Directx9/DrawEngineDX9.cpp @@ -102,11 +102,11 @@ DrawEngineDX9::DrawEngineDX9() // Allocate nicely aligned memory. Maybe graphics drivers will // appreciate it. // All this is a LOT of memory, need to see if we can cut down somehow. - decoded = (u8 *)AllocateMemoryPages(DECODED_VERTEX_BUFFER_SIZE); - decIndex = (u16 *)AllocateMemoryPages(DECODED_INDEX_BUFFER_SIZE); - splineBuffer = (u8 *)AllocateMemoryPages(SPLINE_BUFFER_SIZE); - transformed = (TransformedVertex *)AllocateMemoryPages(TRANSFORMED_VERTEX_BUFFER_SIZE); - transformedExpanded = (TransformedVertex *)AllocateMemoryPages(3 * TRANSFORMED_VERTEX_BUFFER_SIZE); + decoded = (u8 *)AllocateMemoryPages(DECODED_VERTEX_BUFFER_SIZE, MEM_PROT_READ | MEM_PROT_WRITE); + decIndex = (u16 *)AllocateMemoryPages(DECODED_INDEX_BUFFER_SIZE, MEM_PROT_READ | MEM_PROT_WRITE); + splineBuffer = (u8 *)AllocateMemoryPages(SPLINE_BUFFER_SIZE, MEM_PROT_READ | MEM_PROT_WRITE); + transformed = (TransformedVertex *)AllocateMemoryPages(TRANSFORMED_VERTEX_BUFFER_SIZE, MEM_PROT_READ | MEM_PROT_WRITE); + transformedExpanded = (TransformedVertex *)AllocateMemoryPages(3 * TRANSFORMED_VERTEX_BUFFER_SIZE, MEM_PROT_READ | MEM_PROT_WRITE); if (g_Config.bPrescaleUV) { uvScale = new UVScale[MAX_DEFERRED_DRAW_CALLS]; diff --git a/GPU/GLES/DrawEngineGLES.cpp b/GPU/GLES/DrawEngineGLES.cpp index 0663d2d013..f80ed13baf 100644 --- a/GPU/GLES/DrawEngineGLES.cpp +++ b/GPU/GLES/DrawEngineGLES.cpp @@ -136,11 +136,11 @@ DrawEngineGLES::DrawEngineGLES() // Allocate nicely aligned memory. Maybe graphics drivers will // appreciate it. // All this is a LOT of memory, need to see if we can cut down somehow. - decoded = (u8 *)AllocateMemoryPages(DECODED_VERTEX_BUFFER_SIZE); - decIndex = (u16 *)AllocateMemoryPages(DECODED_INDEX_BUFFER_SIZE); - splineBuffer = (u8 *)AllocateMemoryPages(SPLINE_BUFFER_SIZE); - transformed = (TransformedVertex *)AllocateMemoryPages(TRANSFORMED_VERTEX_BUFFER_SIZE); - transformedExpanded = (TransformedVertex *)AllocateMemoryPages(3 * TRANSFORMED_VERTEX_BUFFER_SIZE); + decoded = (u8 *)AllocateMemoryPages(DECODED_VERTEX_BUFFER_SIZE, MEM_PROT_READ | MEM_PROT_WRITE); + decIndex = (u16 *)AllocateMemoryPages(DECODED_INDEX_BUFFER_SIZE, MEM_PROT_READ | MEM_PROT_WRITE); + splineBuffer = (u8 *)AllocateMemoryPages(SPLINE_BUFFER_SIZE, MEM_PROT_READ | MEM_PROT_WRITE); + transformed = (TransformedVertex *)AllocateMemoryPages(TRANSFORMED_VERTEX_BUFFER_SIZE, MEM_PROT_READ | MEM_PROT_WRITE); + transformedExpanded = (TransformedVertex *)AllocateMemoryPages(3 * TRANSFORMED_VERTEX_BUFFER_SIZE, MEM_PROT_READ | MEM_PROT_WRITE); if (g_Config.bPrescaleUV) { uvScale = new UVScale[MAX_DEFERRED_DRAW_CALLS]; diff --git a/GPU/Vulkan/DrawEngineVulkan.cpp b/GPU/Vulkan/DrawEngineVulkan.cpp index e035101577..bf2d175c4a 100644 --- a/GPU/Vulkan/DrawEngineVulkan.cpp +++ b/GPU/Vulkan/DrawEngineVulkan.cpp @@ -87,11 +87,11 @@ DrawEngineVulkan::DrawEngineVulkan(VulkanContext *vulkan) // Allocate nicely aligned memory. Maybe graphics drivers will // appreciate it. // All this is a LOT of memory, need to see if we can cut down somehow. - decoded = (u8 *)AllocateMemoryPages(DECODED_VERTEX_BUFFER_SIZE); - decIndex = (u16 *)AllocateMemoryPages(DECODED_INDEX_BUFFER_SIZE); - splineBuffer = (u8 *)AllocateMemoryPages(SPLINE_BUFFER_SIZE); - transformed = (TransformedVertex *)AllocateMemoryPages(TRANSFORMED_VERTEX_BUFFER_SIZE); - transformedExpanded = (TransformedVertex *)AllocateMemoryPages(3 * TRANSFORMED_VERTEX_BUFFER_SIZE); + decoded = (u8 *)AllocateMemoryPages(DECODED_VERTEX_BUFFER_SIZE, MEM_PROT_READ | MEM_PROT_WRITE); + decIndex = (u16 *)AllocateMemoryPages(DECODED_INDEX_BUFFER_SIZE, MEM_PROT_READ | MEM_PROT_WRITE); + splineBuffer = (u8 *)AllocateMemoryPages(SPLINE_BUFFER_SIZE, MEM_PROT_READ | MEM_PROT_WRITE); + transformed = (TransformedVertex *)AllocateMemoryPages(TRANSFORMED_VERTEX_BUFFER_SIZE, MEM_PROT_READ | MEM_PROT_WRITE); + transformedExpanded = (TransformedVertex *)AllocateMemoryPages(3 * TRANSFORMED_VERTEX_BUFFER_SIZE, MEM_PROT_READ | MEM_PROT_WRITE); indexGen.Setup(decIndex);