Commit 46ac97c265b for php
commit 46ac97c265bc83aa57fb0f5ad09c863a6978a2da
Author: Arnaud Le Blanc <365207+arnaud-lb@users.noreply.github.com>
Date: Thu Oct 22 11:43:05 2026 +0200
JIT: Fix TLS access with SHM reattachment (8.6 follow-up) (#24456)
Co-Authored-By: ndossche <7771979+ndossche@users.noreply.github.com>
diff --git a/ext/opcache/jit/zend_jit_ir.c b/ext/opcache/jit/zend_jit_ir.c
index bde6f855803..ca7bd838333 100644
--- a/ext/opcache/jit/zend_jit_ir.c
+++ b/ext/opcache/jit/zend_jit_ir.c
@@ -360,6 +360,8 @@ static int zend_jit_assign_to_variable(zend_jit_ctx *jit,
static ir_ref jit_CONST_FUNC(zend_jit_ctx *jit, uintptr_t addr, uint16_t flags);
+static ir_ref jit_ADD_OFFSET(zend_jit_ctx *jit, ir_ref addr, uintptr_t offset);
+
static void zend_jit_preserve_parent_regs(zend_jit_ctx *jit,
zend_ssa *ssa,
zend_jit_trace_info *parent,
@@ -522,9 +524,28 @@ static ir_ref jit_TLS(zend_jit_ctx *jit)
if (tsrm_ls_cache_tcb_offset == 0 && tsrm_tls_index == -1) {
jit->tls = ir_CALL(IR_ADDR, ir_CONST_FC_FUNC(zend_jit_get_tsrm_ls_cache));
} else {
+# ifdef ZEND_OPCACHE_SHM_REATTACHMENT
+ /* Different processes may load TLS variables in a different order, so
+ * they may have a different module index. Fetch the module index at
+ * runtime from tsrm_tls_index. Its address is stable as it's in the same
+ * module as execute_ex (whose address is checked before SHM reattachment). */
+
+# ifdef _WIN64
+ const int offset = 0x58;
+# else
+ const int offset = 0x2c;
+# endif
+
+ /* NtCurrentTeb()->ThreadLocalStoragePointer */
+ ir_ref tls_array = ir_TLS(-1, offset);
+
+ ir_ref index = ir_LOAD_A(ir_CONST_ADDR(&tsrm_tls_index));
+ jit->tls = ir_LOAD_A(ir_ADD_OFFSET(ir_LOAD_A(ir_ADD_A(tls_array, index)), tsrm_tls_offset));
+# else
jit->tls = ir_TLS(
tsrm_ls_cache_tcb_offset ? tsrm_ls_cache_tcb_offset : tsrm_tls_index,
tsrm_ls_cache_tcb_offset ? IR_NULL : tsrm_tls_offset);
+# endif
}
return jit->tls;
@@ -3444,10 +3465,6 @@ static void zend_jit_setup(bool reattached)
#endif
#ifdef ZTS
-# ifdef ZEND_OPCACHE_SHM_REATTACHMENT
- /* Different processes may load TLS variables in a different order, so we
- * can not rely on offsets. We will fallback to runtime resolution. */
-# else
zend_result result = zend_jit_resolve_tsrm_ls_cache_offsets(
&tsrm_ls_cache_tcb_offset,
&tsrm_tls_index,
@@ -3457,7 +3474,6 @@ static void zend_jit_setup(bool reattached)
zend_accel_error(ACCEL_LOG_INFO,
"Could not get _tsrm_ls_cache offsets, will fallback to runtime resolution");
}
-# endif
#endif
#if !defined(ZEND_WIN32) && !defined(IR_TARGET_AARCH64)