JIT compiler update.

This commit is contained in:
Zoltán Herczeg 2021-04-26 05:56:04 +00:00
parent 8c1df186ab
commit 38dbea6200
3 changed files with 24 additions and 11 deletions

View File

@ -166,23 +166,28 @@ static SLJIT_INLINE void apple_update_wx_flags(sljit_s32 enable_exec)
static SLJIT_INLINE void* alloc_chunk(sljit_uw size) static SLJIT_INLINE void* alloc_chunk(sljit_uw size)
{ {
void *retval; void *retval;
const int prot = PROT_READ | PROT_WRITE | PROT_EXEC; int prot = PROT_READ | PROT_WRITE | PROT_EXEC;
int flags = MAP_PRIVATE;
int fd = -1;
#ifdef PROT_MAX
prot |= PROT_MAX(prot);
#endif
#ifdef MAP_ANON #ifdef MAP_ANON
int flags = MAP_PRIVATE | MAP_ANON | SLJIT_MAP_JIT; flags |= MAP_ANON | SLJIT_MAP_JIT;
retval = mmap(NULL, size, prot, flags, -1, 0);
#else /* !MAP_ANON */ #else /* !MAP_ANON */
if (SLJIT_UNLIKELY((dev_zero < 0) && open_dev_zero())) if (SLJIT_UNLIKELY((dev_zero < 0) && open_dev_zero()))
return NULL; return NULL;
retval = mmap(NULL, size, prot, MAP_PRIVATE, dev_zero, 0); fd = dev_zero;
#endif /* MAP_ANON */ #endif /* MAP_ANON */
retval = mmap(NULL, size, prot, flags, fd, 0);
if (retval == MAP_FAILED) if (retval == MAP_FAILED)
return NULL; return NULL;
if (mprotect(retval, size, prot) < 0) { if (mprotect(retval, size, PROT_READ | PROT_WRITE | PROT_EXEC) < 0) {
munmap(retval, size); munmap(retval, size);
return NULL; return NULL;
} }

View File

@ -48,7 +48,7 @@ static HANDLE allocator_lock;
static SLJIT_INLINE void allocator_grab_lock(void) static SLJIT_INLINE void allocator_grab_lock(void)
{ {
HANDLE lock; HANDLE lock;
if (SLJIT_UNLIKELY(!allocator_lock)) { if (SLJIT_UNLIKELY(!InterlockedCompareExchangePointer(&allocator_lock, NULL, NULL))) {
lock = CreateMutex(NULL, FALSE, NULL); lock = CreateMutex(NULL, FALSE, NULL);
if (InterlockedCompareExchangePointer(&allocator_lock, lock, NULL)) if (InterlockedCompareExchangePointer(&allocator_lock, lock, NULL))
CloseHandle(lock); CloseHandle(lock);
@ -146,9 +146,13 @@ static SLJIT_INLINE sljit_sw get_page_alignment(void) {
#include <unistd.h> #include <unistd.h>
static SLJIT_INLINE sljit_sw get_page_alignment(void) { static SLJIT_INLINE sljit_sw get_page_alignment(void) {
static sljit_sw sljit_page_align; static sljit_sw sljit_page_align = -1;
if (!sljit_page_align) { if (sljit_page_align < 0) {
#ifdef _SC_PAGESIZE
sljit_page_align = sysconf(_SC_PAGESIZE); sljit_page_align = sysconf(_SC_PAGESIZE);
#else
sljit_page_align = getpagesize();
#endif
/* Should never happen. */ /* Should never happen. */
if (sljit_page_align < 0) if (sljit_page_align < 0)
sljit_page_align = 4096; sljit_page_align = 4096;

View File

@ -121,14 +121,18 @@ SLJIT_API_FUNC_ATTRIBUTE void* sljit_malloc_exec(sljit_uw size)
static pthread_mutex_t se_lock = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t se_lock = PTHREAD_MUTEX_INITIALIZER;
#endif #endif
static int se_protected = !SLJIT_PROT_WX; static int se_protected = !SLJIT_PROT_WX;
int prot = PROT_READ | PROT_WRITE | SLJIT_PROT_WX;
sljit_uw* ptr; sljit_uw* ptr;
if (SLJIT_UNLIKELY(se_protected < 0)) if (SLJIT_UNLIKELY(se_protected < 0))
return NULL; return NULL;
#ifdef PROT_MAX
prot |= PROT_MAX(PROT_READ | PROT_WRITE | PROT_EXEC);
#endif
size += sizeof(sljit_uw); size += sizeof(sljit_uw);
ptr = (sljit_uw*)mmap(NULL, size, PROT_READ | PROT_WRITE | SLJIT_PROT_WX, ptr = (sljit_uw*)mmap(NULL, size, prot, MAP_PRIVATE | MAP_ANON, -1, 0);
MAP_PRIVATE | MAP_ANON, -1, 0);
if (ptr == MAP_FAILED) if (ptr == MAP_FAILED)
return NULL; return NULL;