Commit 0a94b958 by Nicolas Capens Committed by Nicolas Capens

Specify the MAP_JIT flag when allocating executable memory on macOS

On macOS 10.14 and higher, executables that are code signed with the "runtime" option cannot execute writable memory by default. They can opt into this capability by specifying the "com.apple.security.cs.allow-jit" code signing entitlement and allocating the region with the MAP_JIT flag. Note this change only affects use of the Subzero backend of Reactor. Bug: chromium:985816 Change-Id: I7ea213cc2b62b64c85c4c46a0d4f28e0bace5d21 Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/34509Tested-by: 's avatarNicolas Capens <nicolascapens@google.com> Kokoro-Presubmit: kokoro <noreply+kokoro@google.com> Reviewed-by: 's avatarAlexis Hétu <sugoi@google.com>
parent fa0175c0
...@@ -194,7 +194,7 @@ void *allocateExecutable(size_t bytes) ...@@ -194,7 +194,7 @@ void *allocateExecutable(size_t bytes)
{ {
size_t pageSize = memoryPageSize(); size_t pageSize = memoryPageSize();
size_t length = roundUp(bytes, pageSize); size_t length = roundUp(bytes, pageSize);
void *mapping; void *mapping = nullptr;
#if defined(LINUX_ENABLE_NAMED_MMAP) #if defined(LINUX_ENABLE_NAMED_MMAP)
// Try to name the memory region for the executable code, // Try to name the memory region for the executable code,
...@@ -237,6 +237,18 @@ void *allocateExecutable(size_t bytes) ...@@ -237,6 +237,18 @@ void *allocateExecutable(size_t bytes)
ASSERT(roundUp(reservation, pageSize) == reservation); ASSERT(roundUp(reservation, pageSize) == reservation);
mapping = reinterpret_cast<void*>(reservation); mapping = reinterpret_cast<void*>(reservation);
#elif defined(__APPLE__)
// On macOS 10.14 and higher, executables that are code signed with the
// "runtime" option cannot execute writable memory by default. They can opt
// into this capability by specifying the "com.apple.security.cs.allow-jit"
// code signing entitlement and allocating the region with the MAP_JIT flag.
mapping = mmap(nullptr, length, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_JIT, -1, 0);
if(mapping == MAP_FAILED)
{
mapping = nullptr;
}
#else #else
mapping = allocate(length, pageSize); mapping = allocate(length, pageSize);
#endif #endif
...@@ -267,7 +279,7 @@ void deallocateExecutable(void *memory, size_t bytes) ...@@ -267,7 +279,7 @@ void deallocateExecutable(void *memory, size_t bytes)
unsigned long oldProtection; unsigned long oldProtection;
VirtualProtect(memory, bytes, PAGE_READWRITE, &oldProtection); VirtualProtect(memory, bytes, PAGE_READWRITE, &oldProtection);
deallocate(memory); deallocate(memory);
#elif defined(LINUX_ENABLE_NAMED_MMAP) #elif defined(LINUX_ENABLE_NAMED_MMAP) || defined(__APPLE__)
size_t pageSize = memoryPageSize(); size_t pageSize = memoryPageSize();
size_t length = (bytes + pageSize - 1) & ~(pageSize - 1); size_t length = (bytes + pageSize - 1) & ~(pageSize - 1);
munmap(memory, length); munmap(memory, length);
......
...@@ -72,7 +72,8 @@ bool Driver::loadSwiftShader() ...@@ -72,7 +72,8 @@ bool Driver::loadSwiftShader()
#endif #endif
#elif OS_MAC #elif OS_MAC
return load("./build/Darwin/libvk_swiftshader.dylib") || return load("./build/Darwin/libvk_swiftshader.dylib") ||
load("swiftshader/libvulkan.dylib"); load("swiftshader/libvulkan.dylib") ||
load("libvk_swiftshader.dylib");
#elif OS_LINUX #elif OS_LINUX
return load("./build/Linux/libvk_swiftshader.so") || return load("./build/Linux/libvk_swiftshader.so") ||
load("swiftshader/libvulkan.so") || load("swiftshader/libvulkan.so") ||
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment