Skip to content

Commit 1416961

Browse files
committed
opcache JIT support improvements attempts on macOs.
for cases when shared segments switch b/w R/W/X and R/X bits. Closes #8382.
1 parent 180557d commit 1416961

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ PHP NEWS
3636
for macOs Instrument. (David Carlier)
3737
. Fixed bug GH-8030 (Segfault with JIT and large match/switch statements).
3838
(Arnaud)
39+
. JIT support improvement for macOs for segments and executable permission
40+
bit handling. (David Carlier)
3941

4042
- PCRE:
4143
. Updated bundled libpcre to 10.40. (cmb)

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,7 @@ mmap \
597597
nice \
598598
nl_langinfo \
599599
poll \
600+
pthread_jit_write_protect_np \
600601
putenv \
601602
scandir \
602603
setitimer \

ext/opcache/jit/zend_jit.c

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@
4747

4848
#include "jit/zend_jit_internal.h"
4949

50+
#ifdef HAVE_PTHREAD_JIT_WRITE_PROTECT_NP
51+
#include <pthread.h>
52+
#endif
53+
5054
#ifdef ZTS
5155
int jit_globals_id;
5256
#else
@@ -106,6 +110,9 @@ int16_t zend_jit_hot_counters[ZEND_HOT_COUNTERS_COUNT];
106110

107111
const zend_op *zend_jit_halt_op = NULL;
108112
static int zend_jit_vm_kind = 0;
113+
#ifdef HAVE_PTHREAD_JIT_WRITE_PROTECT_NP
114+
static int zend_write_protect = 1;
115+
#endif
109116

110117
static void *dasm_buf = NULL;
111118
static void *dasm_end = NULL;
@@ -4601,12 +4608,13 @@ ZEND_EXT_API void zend_jit_unprotect(void)
46014608
if (!(JIT_G(debug) & (ZEND_JIT_DEBUG_GDB|ZEND_JIT_DEBUG_PERF_DUMP))) {
46024609
int opts = PROT_READ | PROT_WRITE;
46034610
#ifdef ZTS
4604-
/* TODO: EXEC+WRITE is not supported in macOS. Removing EXEC is still buggy as
4605-
* other threads, which are executing the JITed code, would crash anyway. */
4606-
# ifndef __APPLE__
4607-
/* Another thread may be executing JITed code. */
4611+
#ifdef HAVE_PTHREAD_JIT_WRITE_PROTECT_NP
4612+
if (zend_write_protect) {
4613+
pthread_jit_write_protect_np(0);
4614+
return;
4615+
}
4616+
#endif
46084617
opts |= PROT_EXEC;
4609-
# endif
46104618
#endif
46114619
if (mprotect(dasm_buf, dasm_size, opts) != 0) {
46124620
fprintf(stderr, "mprotect() failed [%d] %s\n", errno, strerror(errno));
@@ -4634,6 +4642,12 @@ ZEND_EXT_API void zend_jit_protect(void)
46344642
{
46354643
#ifdef HAVE_MPROTECT
46364644
if (!(JIT_G(debug) & (ZEND_JIT_DEBUG_GDB|ZEND_JIT_DEBUG_PERF_DUMP))) {
4645+
#ifdef HAVE_PTHREAD_JIT_WRITE_PROTECT_NP
4646+
if (zend_write_protect) {
4647+
pthread_jit_write_protect_np(1);
4648+
return;
4649+
}
4650+
#endif
46374651
if (mprotect(dasm_buf, dasm_size, PROT_READ | PROT_EXEC) != 0) {
46384652
fprintf(stderr, "mprotect() failed [%d] %s\n", errno, strerror(errno));
46394653
}
@@ -4889,11 +4903,19 @@ ZEND_EXT_API int zend_jit_startup(void *buf, size_t size, bool reattached)
48894903
}
48904904
}
48914905
#endif
4906+
#ifdef HAVE_PTHREAD_JIT_WRITE_PROTECT_NP
4907+
zend_write_protect = pthread_jit_write_protect_supported_np();
4908+
#endif
48924909

48934910
dasm_buf = buf;
48944911
dasm_size = size;
48954912

48964913
#ifdef HAVE_MPROTECT
4914+
#ifdef HAVE_PTHREAD_JIT_WRITE_PROTECT_NP
4915+
if (zend_write_protect) {
4916+
pthread_jit_write_protect_np(1);
4917+
}
4918+
#endif
48974919
if (JIT_G(debug) & (ZEND_JIT_DEBUG_GDB|ZEND_JIT_DEBUG_PERF_DUMP)) {
48984920
if (mprotect(dasm_buf, dasm_size, PROT_READ | PROT_WRITE | PROT_EXEC) != 0) {
48994921
fprintf(stderr, "mprotect() failed [%d] %s\n", errno, strerror(errno));

0 commit comments

Comments
 (0)