ExecutableMemory: Use VirtualAlloc() instead of `new` on windows VirtualAlloc() allocates pages. Use this instead of over-allocating and aligning to pages with `new`. This is more efficient with memory, and avoids a nasty ASAN issue where pages can have their executable bit removed. Bug: chromium:1466124 Change-Id: Ic31a9a84a837363b6f38b5ba0db3c833375b3e32 Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/71988 Reviewed-by: Antonio Maiorano <amaiorano@google.com> Tested-by: Ben Clayton <bclayton@google.com> Presubmit-Ready: Ben Clayton <bclayton@google.com> Commit-Queue: Ben Clayton <bclayton@google.com> Kokoro-Result: Ben Clayton <bclayton@google.com>
diff --git a/src/Reactor/ExecutableMemory.cpp b/src/Reactor/ExecutableMemory.cpp index d35f3d5..db5145b 100644 --- a/src/Reactor/ExecutableMemory.cpp +++ b/src/Reactor/ExecutableMemory.cpp
@@ -276,7 +276,10 @@ size_t length = roundUp(bytes, pageSize); void *mapping = nullptr; -#if defined(__linux__) && defined(REACTOR_ANONYMOUS_MMAP_NAME) +#if defined(_WIN32) + return VirtualAlloc(nullptr, length, MEM_COMMIT | MEM_RESERVE, + permissionsToProtectMode(permissions)); +#elif defined(__linux__) && defined(REACTOR_ANONYMOUS_MMAP_NAME) int flags = MAP_PRIVATE; // Try to name the memory region for the executable code, @@ -392,10 +395,11 @@ { #if defined(_WIN32) unsigned long oldProtection; - BOOL result = - VirtualProtect(memory, bytes, PAGE_READWRITE, &oldProtection); + BOOL result; + result = VirtualProtect(memory, bytes, PAGE_READWRITE, &oldProtection); ASSERT(result); - deallocate(memory); + result = VirtualFree(memory, 0, MEM_RELEASE); + ASSERT(result); #elif defined(__APPLE__) || (defined(__linux__) && defined(REACTOR_ANONYMOUS_MMAP_NAME)) size_t pageSize = memoryPageSize(); size_t length = (bytes + pageSize - 1) & ~(pageSize - 1);