CPU/CodeCache: Fix crash on Apple Silicon

This commit is contained in:
Stenzek
2023-10-24 18:23:55 +10:00
parent 06c4dc5e1b
commit f786138175
5 changed files with 72 additions and 46 deletions

View File

@ -18,6 +18,11 @@
#include <unistd.h>
#endif
#if defined(__APPLE__) && defined(__aarch64__)
// pthread_jit_write_protect_np()
#include <pthread.h>
#endif
Log_SetChannel(MemoryArena);
#ifdef _WIN32
@ -398,3 +403,31 @@ bool SharedMemoryMappingArea::Unmap(void* map_base, size_t map_size)
}
#endif
#if defined(__APPLE__) && defined(__aarch64__)
static thread_local int s_code_write_depth = 0;
void MemMap::BeginCodeWrite()
{
// Log_DebugFmt("BeginCodeWrite(): {}", s_code_write_depth);
if ((s_code_write_depth++) == 0)
{
// Log_DebugPrint(" pthread_jit_write_protect_np(0)");
pthread_jit_write_protect_np(0);
}
}
void MemMap::EndCodeWrite()
{
// Log_DebugFmt("EndCodeWrite(): {}", s_code_write_depth);
DebugAssert(s_code_write_depth > 0);
if ((--s_code_write_depth) == 0)
{
// Log_DebugPrint(" pthread_jit_write_protect_np(1)");
pthread_jit_write_protect_np(1);
}
}
#endif

View File

@ -41,6 +41,17 @@ void DestroySharedMemory(void* ptr);
void* MapSharedMemory(void* handle, size_t offset, void* baseaddr, size_t size, PageProtect mode);
void UnmapSharedMemory(void* baseaddr, size_t size);
bool MemProtect(void* baseaddr, size_t size, PageProtect mode);
/// JIT write protect for Apple Silicon. Needs to be called prior to writing to any RWX pages.
#if !defined(__APPLE__) || !defined(__aarch64__)
// clang-format off
ALWAYS_INLINE static void BeginCodeWrite() { }
ALWAYS_INLINE static void EndCodeWrite() { }
// clang-format on
#else
void BeginCodeWrite();
void EndCodeWrite();
#endif
} // namespace MemMap
class SharedMemoryMappingArea