diff options
author | Jan Vorlicek <jan.vorlicek@volny.cz> | 2021-06-12 10:54:58 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-12 10:54:58 +0300 |
commit | cc6d3147ff1f10d2ee2b0751cf799fc4cc1abbae (patch) | |
tree | 6f152bfc4ba7880faff9b2189e25d6b85743771a /src/coreclr/pal | |
parent | 2a011f802b83051d1381f7800131ca5734b4c59e (diff) |
Unify macOS ARM64 write protection holders with W^X ones (#54067)
* Unify macOS ARM64 write protection holders with W^X ones
This change removes the original holders that were added for changing
memory protection for executable code and moves the actual switching to
the recently added W^X holders.
The unixexports files don't support target specific symbols. So I needed
to export a dummy version of the PAL_JitWriteProtect for macOS x64.
Diffstat (limited to 'src/coreclr/pal')
-rw-r--r-- | src/coreclr/pal/inc/pal.h | 30 | ||||
-rw-r--r-- | src/coreclr/pal/src/map/virtual.cpp | 31 |
2 files changed, 25 insertions, 36 deletions
diff --git a/src/coreclr/pal/inc/pal.h b/src/coreclr/pal/inc/pal.h index d22c9ba8c8d..69d90522a0d 100644 --- a/src/coreclr/pal/inc/pal.h +++ b/src/coreclr/pal/inc/pal.h @@ -2760,32 +2760,14 @@ VirtualFree( IN SIZE_T dwSize, IN DWORD dwFreeType); -#if defined(HOST_OSX) && defined(HOST_ARM64) -#ifdef __cplusplus -extern "C++" { -struct PAL_JITWriteEnableHolder -{ -public: - PAL_JITWriteEnableHolder(bool jitWriteEnable) - { - m_jitWriteEnableRestore = JITWriteEnable(jitWriteEnable); - }; - ~PAL_JITWriteEnableHolder() - { - JITWriteEnable(m_jitWriteEnableRestore); - } +#if defined(HOST_OSX) -private: - bool JITWriteEnable(bool enable); - bool m_jitWriteEnableRestore; -}; +PALIMPORT +VOID +PALAPI +PAL_JitWriteProtect(bool writeEnable); -inline -PAL_JITWriteEnableHolder -PAL_JITWriteEnable(IN bool enable) { return PAL_JITWriteEnableHolder(enable); } -} -#endif // __cplusplus -#endif // defined(HOST_OSX) && defined(HOST_ARM64) +#endif // defined(HOST_OSX) PALIMPORT BOOL diff --git a/src/coreclr/pal/src/map/virtual.cpp b/src/coreclr/pal/src/map/virtual.cpp index cea55e86e25..9267f4fdf1c 100644 --- a/src/coreclr/pal/src/map/virtual.cpp +++ b/src/coreclr/pal/src/map/virtual.cpp @@ -1760,22 +1760,29 @@ ExitVirtualProtect: return bRetVal; } -#if defined(HOST_OSX) && defined(HOST_ARM64) -bool -PAL_JITWriteEnableHolder::JITWriteEnable(bool writeEnable) +#if defined(HOST_OSX) +PALAPI VOID PAL_JitWriteProtect(bool writeEnable) { - // Use a thread local to track per thread JIT Write enable state - // Per Apple, new threads start with MAP_JIT pages readable and executable (R-X) by default. - thread_local bool enabled = false; - bool result = enabled; - if (enabled != writeEnable) +#if defined(HOST_ARM64) + thread_local int enabledCount = 0; + if (writeEnable) { - pthread_jit_write_protect_np(writeEnable ? 0 : 1); - enabled = writeEnable; + if (enabledCount++ == 0) + { + pthread_jit_write_protect_np(0); + } + } + else + { + if (--enabledCount == 0) + { + pthread_jit_write_protect_np(1); + } + _ASSERTE(enabledCount >= 0); } - return result; +#endif // HOST_ARM64 } -#endif +#endif // HOST_OSX #if HAVE_VM_ALLOCATE //--------------------------------------------------------------------------------------- |