Commit 0e5694756fc for php

commit 0e5694756fcd26c1ba91264e96651a8bf9c560e6
Merge: bba8d1f7716 3afaf7ed982
Author: Arnaud Le Blanc <arnaud.lb@gmail.com>
Date:   Mon Nov 16 15:19:43 2026 +0100

    Merge branch 'PHP-8.5' into PHP-8.6

    * PHP-8.5:
      Fix GH-8739: OPcache restart corrupts SHM in threaded SAPIs (#24276)

diff --cc NEWS
index f367a11a3f0,1d5dc70a89b..17be7f4a920
--- a/NEWS
+++ b/NEWS
@@@ -95,16 -73,11 +95,18 @@@ PH
    . Fixed use-after-free in JIT when the error handler releases a string while
      an "Undefined variable" warning is raised for its offset in ?? or nested
      isset()/empty(). (Jorg Sowa)
 -  . Fixed bug GH-24622 (Tracing JIT skips spilling inherited register values
 -    before property helpers). (ndossche, Alessio Attilio)
+   . Fixed bug GH-8739 (OPcache restart corrupts SHM in threaded SAPIs).
+     (pavimus)

 +- OpenSSL:
 +  . Fixed bug GH-25172 (TLS session reuse shares request-lifetime callback
 +    structures with persistent streams). (cxxz16, David Carlier)
 +  . Fixed an exception thrown by the session_get_cb stream context callback
 +    being replaced with a TypeError. (Ilia Alshanetsky)
 +  . Fixed use-after-free when a stream is closed by the callback for
 +    `early_data_cb`, `psk_client_cb`, `psk_server_cb`, `session_get_cb`, or
 +    `session_new_cb`. (DanielEScherzer)
 +
  - PDO_Firebird:
    . Fixed stack buffer overflow with long connection user names, passwords,
      charsets, or roles. (Ilia Alshanetsky)
diff --cc ext/opcache/ZendAccelerator.c
index a5fa14a5fc7,39ecd57d046..daa5e07e892
--- a/ext/opcache/ZendAccelerator.c
+++ b/ext/opcache/ZendAccelerator.c
@@@ -337,9 -337,65 +337,65 @@@ static inline bool accel_restart_is_act
  		return LOCKVAL(restart_in) != 0;
  #endif
  	}
 -	return 0;
 +	return false;
  }

+ #if defined(ZTS) && !defined(ZEND_WIN32)
+ /* fcntl() locks belong to the process, so under ZTS they cannot tell which threads
+  * use SHM (GH-8739). The threads of this process that use SHM are counted here, and
+  * the process holds the reader lock (byte 1) whenever the count is not zero, so other
+  * processes still see it. A restart needs a zero count and a write lock on byte 1.
+  * The 0 <-> 1 transitions and the restart are serialized by accel_zts_readers_mutex,
+  * other changes of the count are lock-free. */
+ static MUTEX_T accel_zts_readers_mutex = NULL;
+ static zend_atomic_int accel_zts_readers;
+ static time_t accel_zts_next_warning;
+
+ static bool accel_zts_lock_reader_byte(int cmd, int type)
+ {
+ 	struct flock lock;
+
+ 	lock.l_type = type;
+ 	lock.l_whence = SEEK_SET;
+ 	lock.l_start = 1;
+ 	lock.l_len = 1;
+
+ 	return fcntl(lock_file, cmd, &lock) != -1;
+ }
+
+ /* Adds (+1, only from RINIT) or releases (-1) a reference of the calling thread.
+  * Does not use ZCG() as it is also called from accel_globals_dtor(). */
+ static bool accel_zts_readers_add(int delta)
+ {
+ 	int count;
+
+ #ifndef HAVE_NO_ATOMICS
+ 	count = zend_atomic_int_load_ex(&accel_zts_readers);
+ 	while (count > 0 && count + delta > 0) {
+ 		if (zend_atomic_int_compare_exchange_ex(&accel_zts_readers, &count, count + delta)) {
+ 			return true;
+ 		}
+ 	}
+ #endif
+
+ 	tsrm_mutex_lock(accel_zts_readers_mutex);
+ 	count = zend_atomic_int_load_ex(&accel_zts_readers);
+ 	/* waits while another process restarts the cache (it holds the write lock) */
+ 	if (count == 0 && !accel_zts_lock_reader_byte(F_SETLKW, F_RDLCK)) {
+ 		zend_accel_error(ACCEL_LOG_DEBUG, "UpdateC(+1):  %s (%d)", strerror(errno), errno);
+ 		tsrm_mutex_unlock(accel_zts_readers_mutex);
+ 		return false;
+ 	}
+ 	while (!zend_atomic_int_compare_exchange_ex(&accel_zts_readers, &count, count + delta));
+ 	if (count + delta == 0) {
+ 		/* the last reader of this process; unlock before another thread may relock */
+ 		accel_zts_lock_reader_byte(F_SETLK, F_UNLCK);
+ 	}
+ 	tsrm_mutex_unlock(accel_zts_readers_mutex);
+ 	return true;
+ }
+ #endif
+
  /* Creates a read lock for SHM access */
  static inline zend_result accel_activate_add(void)
  {
@@@ -985,7 -1033,49 +1053,49 @@@ static inline bool accel_is_inactive(vo
  	return false;
  }

+ #if defined(ZTS) && !defined(ZEND_WIN32)
+ /* Replaces accel_is_inactive() under ZTS; called under zend_shared_alloc_lock().
+  * On success the readers mutex and the write lock on byte 1 are held until
+  * accel_zts_restart_end(): no thread of this process can become a reader (the count is
+  * zero and may only grow from zero under the mutex), and other processes can neither
+  * hold the reader lock nor take it. */
+ static bool accel_zts_restart_begin(void)
+ {
+ 	int readers;
+ 	time_t now;
+
+ 	tsrm_mutex_lock(accel_zts_readers_mutex);
+ 	readers = zend_atomic_int_load_ex(&accel_zts_readers);
+ 	if (readers == 0 && accel_zts_lock_reader_byte(F_SETLK, F_WRLCK)) {
+ 		return true;
+ 	}
+ 	tsrm_mutex_unlock(accel_zts_readers_mutex);
+
+ 	now = time(NULL);
+ 	if (readers == 0) {
+ 		/* readers in other processes: as without ZTS, force_restart_timeout kills them,
+ 		 * here with all their threads */
+ 		(void) accel_is_inactive();
+ 	} else if (ZCG(accel_directives).force_restart_timeout && ZCSG(force_restart_time)
+ 			&& now >= ZCSG(force_restart_time) && now >= accel_zts_next_warning) {
+ 		/* Threads cannot be killed and this process must not kill itself: the restart
+ 		 * waits for the running requests, new ones run uncached meanwhile */
+ 		zend_accel_error(ACCEL_LOG_WARNING, "Restart delayed for %ld seconds by %d threads of process %d still using the cache",
+ 			(long)(now - ZCSG(force_restart_time) + ZCG(accel_directives).force_restart_timeout), readers, getpid());
+ 		accel_zts_next_warning = now + ZCG(accel_directives).force_restart_timeout;
+ 	}
+ 	return false;
+ }
+
+ static void accel_zts_restart_end(void)
+ {
+ 	accel_zts_lock_reader_byte(F_SETLK, F_UNLCK);
+ 	accel_zts_next_warning = 0;
+ 	tsrm_mutex_unlock(accel_zts_readers_mutex);
+ }
+ #endif
+
 -static int zend_get_stream_timestamp(const char *filename, zend_stat_t *statbuf)
 +static zend_result zend_get_stream_timestamp(const char *filename, zend_stat_t *statbuf)
  {
  	php_stream_wrapper *wrapper;
  	php_stream_statbuf stream_statbuf;