Skip to content

Commit

Permalink
vm: change strategy for marking executable pages on macos aarch64
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjbq7 committed Nov 15, 2023
1 parent cb51398 commit 6235c8c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
3 changes: 3 additions & 0 deletions vm/main-unix.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include "master.hpp"

int main(int argc, char** argv) {
#if defined(__APPLE__) && defined(FACTOR_ARM64)
pthread_jit_write_protect_np(0);
#endif
factor::init_mvm();
factor::start_standalone_factor(argc, argv);
return 0;
Expand Down
21 changes: 14 additions & 7 deletions vm/os-unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,27 +79,34 @@ segment::segment(cell size_, bool executable_p) {

int pagesize = getpagesize();

#if defined(__APPLE__) && defined(FACTOR_ARM64)
int prot = PROT_READ | PROT_WRITE;
int flags = MAP_ANON | MAP_PRIVATE | MAP_JIT;
#else
int prot;
if (executable_p)
prot = PROT_READ | PROT_WRITE | PROT_EXEC;
else
prot = PROT_READ | PROT_WRITE;
int flags = MAP_ANON | MAP_PRIVATE;
#endif

cell alloc_size = 2 * pagesize + size;
#if defined(__APPLE__) && defined(FACTOR_ARM64) // FIXME: could be in header file
char* array = (char*)mmap(NULL, alloc_size, prot,
MAP_ANON | MAP_PRIVATE | MAP_JIT, -1, 0);
#else
char* array = (char*)mmap(NULL, alloc_size, prot,
MAP_ANON | MAP_PRIVATE, -1, 0);
#endif
char* array = (char*)mmap(NULL, alloc_size, prot, flags, -1, 0);

if (array == (char*)-1)
fatal_error("Out of memory in mmap", alloc_size);

start = (cell)(array + pagesize);
end = start + size;

#if defined(__APPLE__) && defined(FACTOR_ARM64)
if (executable_p) {
if (mprotect((char*)start, size, prot | PROT_EXEC) == -1)
fatal_error("mprotect executable page failed", 0);
}
#endif

set_border_locked(true);
}

Expand Down

0 comments on commit 6235c8c

Please sign in to comment.