Skip to content

Commit

Permalink
Merge pull request tevador#226 from jtgrassie/fix-non-arm-osx
Browse files Browse the repository at this point in the history
Fix W^X policy for apple portability
  • Loading branch information
hyc authored Nov 19, 2021
2 parents f9ae3f2 + 4d8ef87 commit b0d4807
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions src/virtual_memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifdef __APPLE__
#include <mach/vm_statistics.h>
#include <TargetConditionals.h>
# ifdef TARGET_OS_OSX
# define USE_PTHREAD_JIT_WP 1
# include <pthread.h>
#include <AvailabilityMacros.h>
# if TARGET_OS_OSX
# define USE_PTHREAD_JIT_WP 1
# include <pthread.h>
# endif
#endif
#include <sys/types.h>
Expand Down Expand Up @@ -114,8 +115,11 @@ void* allocMemoryPages(std::size_t bytes) {
mem = mmap(nullptr, bytes, PAGE_READWRITE | RESERVED_FLAGS | PEXTRA, MAP_ANONYMOUS | MAP_PRIVATE | MEXTRA, -1, 0);
if (mem == MAP_FAILED)
throw std::runtime_error("allocMemoryPages - mmap failed");
#ifdef USE_PTHREAD_JIT_WP
pthread_jit_write_protect_np(false);
#if defined(USE_PTHREAD_JIT_WP) && defined(MAC_OS_VERSION_11_0) \
&& MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_VERSION_11_0
if (__builtin_available(macOS 11.0, *)) {
pthread_jit_write_protect_np(false);
}
#endif
#endif
return mem;
Expand All @@ -134,16 +138,26 @@ static inline void pageProtect(void* ptr, std::size_t bytes, int rules) {
}

void setPagesRW(void* ptr, std::size_t bytes) {
#ifdef USE_PTHREAD_JIT_WP
pthread_jit_write_protect_np(false);
#if defined(USE_PTHREAD_JIT_WP) && defined(MAC_OS_VERSION_11_0) \
&& MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_VERSION_11_0
if (__builtin_available(macOS 11.0, *)) {
pthread_jit_write_protect_np(false);
} else {
pageProtect(ptr, bytes, PAGE_READWRITE);
}
#else
pageProtect(ptr, bytes, PAGE_READWRITE);
#endif
}

void setPagesRX(void* ptr, std::size_t bytes) {
#ifdef USE_PTHREAD_JIT_WP
pthread_jit_write_protect_np(true);
#if defined(USE_PTHREAD_JIT_WP) && defined(MAC_OS_VERSION_11_0) \
&& MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_VERSION_11_0
if (__builtin_available(macOS 11.0, *)) {
pthread_jit_write_protect_np(true);
} else {
pageProtect(ptr, bytes, PAGE_EXECUTE_READ);
}
#else
pageProtect(ptr, bytes, PAGE_EXECUTE_READ);
#endif
Expand Down

0 comments on commit b0d4807

Please sign in to comment.