[Pcre-svn] [1757] code/trunk/sljit: JIT compiler update.

Top Page
Delete this message
Author: Subversion repository
Date:  
To: pcre-svn
Subject: [Pcre-svn] [1757] code/trunk/sljit: JIT compiler update.
Revision: 1757
          http://vcs.pcre.org/viewvc?view=rev&revision=1757
Author:   zherczeg
Date:     2019-11-19 12:54:59 +0000 (Tue, 19 Nov 2019)
Log Message:
-----------
JIT compiler update.


Modified Paths:
--------------
    code/trunk/sljit/sljitConfigInternal.h
    code/trunk/sljit/sljitExecAllocator.c
    code/trunk/sljit/sljitNativeX86_64.c
    code/trunk/sljit/sljitNativeX86_common.c
    code/trunk/sljit/sljitUtils.c


Modified: code/trunk/sljit/sljitConfigInternal.h
===================================================================
--- code/trunk/sljit/sljitConfigInternal.h    2019-07-22 11:12:47 UTC (rev 1756)
+++ code/trunk/sljit/sljitConfigInternal.h    2019-11-19 12:54:59 UTC (rev 1757)
@@ -214,6 +214,10 @@
 #define SLJIT_MEMCPY(dest, src, len) memcpy(dest, src, len)
 #endif


+#ifndef SLJIT_MEMMOVE
+#define SLJIT_MEMMOVE(dest, src, len) memmove(dest, src, len)
+#endif
+
#ifndef SLJIT_ZEROMEM
#define SLJIT_ZEROMEM(dest, len) memset(dest, 0, len)
#endif

Modified: code/trunk/sljit/sljitExecAllocator.c
===================================================================
--- code/trunk/sljit/sljitExecAllocator.c    2019-07-22 11:12:47 UTC (rev 1756)
+++ code/trunk/sljit/sljitExecAllocator.c    2019-11-19 12:54:59 UTC (rev 1757)
@@ -118,10 +118,20 @@
     if (map_jit_flag == -1) {
         struct utsname name;


+        map_jit_flag = 0;
         uname(&name);


         /* Kernel version for 10.14.0 (Mojave) */
-        map_jit_flag = (atoi(name.release) >= 18) ? MAP_JIT : 0;
+        if (atoi(name.release) >= 18) {
+            /* Only use MAP_JIT if a hardened runtime is used, because MAP_JIT is incompatible with fork(). */
+            void *ptr = mmap(NULL, getpagesize(), PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
+
+            if (ptr == MAP_FAILED) {
+                map_jit_flag = MAP_JIT;
+            } else {
+                munmap(ptr, getpagesize());
+            }
+        }
     }


     return map_jit_flag;
@@ -137,6 +147,7 @@
 static SLJIT_INLINE void* alloc_chunk(sljit_uw size)
 {
     void *retval;
+    const int prot = PROT_READ | PROT_WRITE | PROT_EXEC;


#ifdef MAP_ANON

@@ -146,16 +157,25 @@
     flags |= get_map_jit_flag();
 #endif


-    retval = mmap(NULL, size, PROT_READ | PROT_WRITE | PROT_EXEC, flags, -1, 0);
+    retval = mmap(NULL, size, prot, flags, -1, 0);
 #else /* !MAP_ANON */
     if (dev_zero < 0) {
         if (open_dev_zero())
             return NULL;
     }
-    retval = mmap(NULL, size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, dev_zero, 0);
+    retval = mmap(NULL, size, prot, MAP_PRIVATE, dev_zero, 0);
 #endif /* MAP_ANON */


-    return (retval != MAP_FAILED) ? retval : NULL;
+    if (retval == MAP_FAILED)
+        retval = NULL;
+    else {
+        if (mprotect(retval, size, prot) < 0) {
+            munmap(retval, size);
+            retval = NULL;
+        }
+    }
+
+    return retval;
 }


static SLJIT_INLINE void free_chunk(void *chunk, sljit_uw size)

Modified: code/trunk/sljit/sljitNativeX86_64.c
===================================================================
--- code/trunk/sljit/sljitNativeX86_64.c    2019-07-22 11:12:47 UTC (rev 1756)
+++ code/trunk/sljit/sljitNativeX86_64.c    2019-11-19 12:54:59 UTC (rev 1757)
@@ -103,7 +103,7 @@
     }


     code_ptr -= put_label->flags + (2 + sizeof(sljit_uw));
-    SLJIT_MEMCPY(code_ptr, code_ptr + (2 + sizeof(sljit_uw)), put_label->flags);
+    SLJIT_MEMMOVE(code_ptr, code_ptr + (2 + sizeof(sljit_uw)), put_label->flags);


     SLJIT_ASSERT((code_ptr[0] & 0xf8) == REX_W);



Modified: code/trunk/sljit/sljitNativeX86_common.c
===================================================================
--- code/trunk/sljit/sljitNativeX86_common.c    2019-07-22 11:12:47 UTC (rev 1756)
+++ code/trunk/sljit/sljitNativeX86_common.c    2019-11-19 12:54:59 UTC (rev 1757)
@@ -2515,6 +2515,7 @@
     jump = (struct sljit_jump*)ensure_abuf(compiler, sizeof(struct sljit_jump));
     PTR_FAIL_IF_NULL(jump);
     set_jump(jump, compiler, (type & SLJIT_REWRITABLE_JUMP) | ((type & 0xff) << TYPE_SHIFT));
+    type &= 0xff;


     /* Worst case size. */
 #if (defined SLJIT_CONFIG_X86_32 && SLJIT_CONFIG_X86_32)


Modified: code/trunk/sljit/sljitUtils.c
===================================================================
--- code/trunk/sljit/sljitUtils.c    2019-07-22 11:12:47 UTC (rev 1756)
+++ code/trunk/sljit/sljitUtils.c    2019-11-19 12:54:59 UTC (rev 1757)
@@ -154,7 +154,13 @@
 #include "windows.h"
 #else
 /* Provides mmap function. */
+#include <sys/types.h>
 #include <sys/mman.h>
+#ifndef MAP_ANON
+#ifdef MAP_ANONYMOUS
+#define MAP_ANON MAP_ANONYMOUS
+#endif
+#endif
 /* For detecting the page size. */
 #include <unistd.h>