From d842f8d79f718175bfb2f35dba7c33359fad6525 Mon Sep 17 00:00:00 2001 From: Pratik Bhujel Date: Thu, 19 Nov 2026 16:57:08 +0545 Subject: [PATCH] Fix GH-25555: Set memory_consumption when loading file_cache script into SHM When a script is written to opcache.file_cache without being stored in shared memory (because SHM was full or opcache.file_cache_only=1 was active), script->dynamic_members.memory_consumption in the serialized .bin file is 0. When zend_file_cache_script_load() later loads that .bin into shared memory (cache_it == true), initialize script->dynamic_members.memory_consumption to ZEND_ALIGNED_SIZE(script->size) so opcache_get_status() reports the allocated SHM size and invalidation accounts for it in wasted_memory. Closes GH-25555. --- ext/opcache/tests/gh25555.phpt | 72 ++++++++++++++++++++++++++++++++++ ext/opcache/zend_file_cache.c | 1 + 2 files changed, 73 insertions(+) create mode 100644 ext/opcache/tests/gh25555.phpt diff --git a/ext/opcache/tests/gh25555.phpt b/ext/opcache/tests/gh25555.phpt new file mode 100644 index 000000000000..38d06bb2865e --- /dev/null +++ b/ext/opcache/tests/gh25555.phpt @@ -0,0 +1,72 @@ +--TEST-- +GH-25555: Scripts loaded from file_cache into SHM report memory_consumption 0 when stored without SHM +--EXTENSIONS-- +opcache +--FILE-- + 0, $w1 - $w0 === $mem);' + . ' }', + var_export($script, true), + $file_cache_only ? 'true' : 'false', + var_export($script, true), + var_export($script, true), + ); + array_push($cmd, + '-d', 'opcache.enable=1', + '-d', 'opcache.enable_cli=1', + '-d', 'opcache.jit=0', + '-d', 'opcache.file_cache="' . $cache . '"', + '-d', 'opcache.file_cache_only=' . ($file_cache_only ? '1' : '0'), + '-d', 'opcache.file_update_protection=0', + '-r', $code, + ); + $proc = proc_open($cmd, [1 => ['pipe', 'w'], 2 => ['redirect', 1]], $pipes); + echo stream_get_contents($pipes[1]); + proc_close($proc); +} + +run_child($cache, $script, true); +run_child($cache, $script, false); +?> +--CLEAN-- +isDir() ? rmdir($file->getPathname()) : unlink($file->getPathname()); + } + rmdir($cache); +} +?> +--EXPECT-- +bool(true) +bool(true) diff --git a/ext/opcache/zend_file_cache.c b/ext/opcache/zend_file_cache.c index a8463e4252e6..fc64ba5640d8 100644 --- a/ext/opcache/zend_file_cache.c +++ b/ext/opcache/zend_file_cache.c @@ -1987,6 +1987,7 @@ zend_persistent_script *zend_file_cache_script_load(zend_file_handle *file_handl ZCSG(map_ptr_last) = CG(map_ptr_last); zend_accel_pending_ce_cache_commit(); script->dynamic_members.last_used = ZCG(request_time); + script->dynamic_members.memory_consumption = ZEND_ALIGNED_SIZE(script->size); zend_accel_hash_update(&ZCSG(hash), script->script.filename, 0, script);